2022-11-01 22:09:32 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "arraylist.h"
|
|
|
|
#include "sortedmap.h"
|
2022-11-02 00:05:01 +01:00
|
|
|
#include "json.h"
|
2022-11-01 22:09:32 +01:00
|
|
|
|
|
|
|
#define BUF 20
|
|
|
|
|
2022-11-02 00:05:01 +01:00
|
|
|
/*
|
2022-11-01 22:09:32 +01:00
|
|
|
struct LIST {
|
|
|
|
ArrayList * (*init)();
|
|
|
|
void (*add)(ArrayList *, void *);
|
|
|
|
void * (*get)(ArrayList *, int);
|
|
|
|
void (*freeitems)(ArrayList *);
|
|
|
|
void (*free)(ArrayList *);
|
|
|
|
} LIST = { list, add_list, get_list, free_listitems, free_list };
|
2022-11-02 00:05:01 +01:00
|
|
|
*/
|
2022-11-01 22:09:32 +01:00
|
|
|
|
|
|
|
typedef struct planet {
|
|
|
|
int ship_count;
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
int owner;
|
2022-11-23 08:31:17 +01:00
|
|
|
char *name;
|
2022-11-01 22:09:32 +01:00
|
|
|
} Planet;
|
|
|
|
|
|
|
|
typedef struct expedition {
|
|
|
|
int id;
|
|
|
|
int ship_count;
|
2022-11-23 08:31:17 +01:00
|
|
|
char *origin;
|
|
|
|
char *destination;
|
2022-11-01 22:09:32 +01:00
|
|
|
int owner;
|
|
|
|
int turns_remaining;
|
|
|
|
} Expedition;
|
|
|
|
|
|
|
|
typedef struct gamestate {
|
|
|
|
ArrayList *planets;
|
|
|
|
ArrayList *expeditions;
|
|
|
|
} Gamestate;
|
|
|
|
|
|
|
|
void print_planet(Planet *this) {
|
|
|
|
printf("{ \"ship_count\" : %d , \"x\" : %f , \"y\" : %f , \"owner\" : %d , \"name\" : %s }\n",
|
|
|
|
this->ship_count, this->x, this->y, this->owner, this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_expedition(Expedition *expedition) {
|
|
|
|
printf("{ \"id\" : %d , \"ship_count\" : %d , \"origin\" : %s , \"destination\" : %s , \"owner\" : %d , \"turns_remaining\" : %d }\n",
|
|
|
|
expedition->id,
|
|
|
|
expedition->ship_count,
|
|
|
|
expedition->origin,
|
|
|
|
expedition->destination,
|
|
|
|
expedition->owner,
|
|
|
|
expedition->turns_remaining);
|
|
|
|
}
|
|
|
|
|
2022-11-23 08:31:17 +01:00
|
|
|
void free_gamestate(Gamestate *this) {
|
|
|
|
fold_list(this->planets, NULL, free_listitem);
|
|
|
|
free_list(this->planets);
|
|
|
|
fold_list(this->expeditions, NULL, free_listitem);
|
|
|
|
free_list(this->expeditions);
|
|
|
|
free(this);
|
|
|
|
}
|
2022-11-01 22:09:32 +01:00
|
|
|
|
2022-11-23 08:31:17 +01:00
|
|
|
void * planet_from_json(void *acc, int index, void *item) {
|
|
|
|
ArrayList *planets = (ArrayList *) acc;
|
|
|
|
Json *json = (Json *) item;
|
|
|
|
Planet *planet = malloc(sizeof(Planet));
|
|
|
|
planet->ship_count = (int) ((Json *) get_map(json->value.map, "ship_count"))->value.number;
|
|
|
|
planet->x = ((Json *) get_map(json->value.map, "x"))->value.number;
|
|
|
|
planet->y = ((Json *) get_map(json->value.map, "y"))->value.number;
|
|
|
|
planet->owner = (int) ((Json *) get_map(json->value.map, "owner"))->value.number;
|
|
|
|
planet->name = ((Json *) get_map(json->value.map, "name"))->value.string;
|
|
|
|
add_list(planets, planet);
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void * expedition_from_json(void *acc, int index, void *item) {
|
|
|
|
ArrayList *expeditions = (ArrayList *) acc;
|
|
|
|
Json *json = (Json *) item;
|
|
|
|
Expedition *expedition = malloc(sizeof(Expedition));
|
|
|
|
expedition->id = (int) ((Json *) get_map(json->value.map, "id"))->value.number;
|
|
|
|
expedition->ship_count = (int) ((Json *) get_map(json->value.map, "ship_count"))->value.number;
|
|
|
|
expedition->origin = ((Json *) get_map(json->value.map, "origin"))->value.string;
|
|
|
|
expedition->destination = ((Json *) get_map(json->value.map, "destination"))->value.string;
|
|
|
|
expedition->owner = (int) ((Json *) get_map(json->value.map, "owner"))->value.number;
|
|
|
|
expedition->turns_remaining = (int) ((Json *) get_map(json->value.map, "turns_remaining"))->value.number;
|
2022-11-01 22:09:32 +01:00
|
|
|
add_list(expeditions, expedition);
|
2022-11-23 08:31:17 +01:00
|
|
|
return acc;
|
2022-11-01 22:09:32 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 08:31:17 +01:00
|
|
|
Gamestate * parse_gamestate_json() {
|
2022-11-01 22:09:32 +01:00
|
|
|
Gamestate *gamestate = malloc(sizeof(Gamestate));
|
2022-11-23 08:31:17 +01:00
|
|
|
Json *json = json_parse(stdin);
|
|
|
|
Json *planets = get_map(json->value.map, "planets");
|
|
|
|
Json *expeditions = get_map(json->value.map, "expeditions");
|
|
|
|
json_print(json, stderr);
|
|
|
|
json_print(planets, stderr);
|
|
|
|
json_print(expeditions, stderr);
|
2022-11-01 22:09:32 +01:00
|
|
|
gamestate->planets = list();
|
2022-11-23 08:31:17 +01:00
|
|
|
fold_list(planets->value.list, gamestate->planets, planet_from_json);
|
2022-11-01 22:09:32 +01:00
|
|
|
gamestate->expeditions = list();
|
2022-11-23 08:31:17 +01:00
|
|
|
fold_list(expeditions->value.list, gamestate->expeditions, expedition_from_json);
|
|
|
|
json_free(json);
|
2022-11-01 22:09:32 +01:00
|
|
|
return gamestate;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2022-11-23 08:31:17 +01:00
|
|
|
Gamestate *gamestate = parse_gamestate_json();
|
|
|
|
free_gamestate(gamestate);
|
|
|
|
|
2022-11-08 23:31:00 +01:00
|
|
|
/*Gamestate *gamestate;
|
2022-11-01 22:09:32 +01:00
|
|
|
SortedMap *test;
|
|
|
|
|
|
|
|
gamestate = read_gamestate();
|
|
|
|
free_gamestate(gamestate);
|
|
|
|
test = map();
|
|
|
|
test = put_map(test, "asdf", "value");
|
|
|
|
test = put_map(test, "asdf", "asdf");
|
|
|
|
test = put_map(test, "qwer", "asdf");
|
|
|
|
test = put_map(test, "qwer", "qwer");
|
|
|
|
test = put_map(test, "awer", "qwer");
|
|
|
|
printf("%s\n", (char *) get_map(test, "awer"));
|
|
|
|
print_map(test);
|
2022-11-08 23:31:00 +01:00
|
|
|
free_map(test); */
|
2022-11-01 22:09:32 +01:00
|
|
|
return 0;
|
|
|
|
}
|