use json parser in planarbot

This commit is contained in:
Felix Van der Jeugt 2022-11-23 08:31:17 +01:00
parent 025c8125fa
commit 35f48eb570
No known key found for this signature in database
GPG Key ID: 58B209295023754D

View File

@ -23,14 +23,14 @@ typedef struct planet {
float x;
float y;
int owner;
char name[BUF];
char *name;
} Planet;
typedef struct expedition {
int id;
int ship_count;
char origin[BUF];
char destination[BUF];
char *origin;
char *destination;
int owner;
int turns_remaining;
} Expedition;
@ -45,27 +45,6 @@ void print_planet(Planet *this) {
this->ship_count, this->x, this->y, this->owner, this->name);
}
int read_planet(ArrayList *planets) {
char owner[BUF] = {0};
Planet *planet = calloc(1, sizeof(Planet));
if (scanf(
" { \"ship_count\" : %d , \"x\" : %f , \"y\" : %f , \"owner\" : %19[0-9a-z] , \"name\" : \"%19[^\"]\" } ",
&planet->ship_count, &planet->x, &planet->y, owner, planet->name
) != 5) {
free(planet);
return 1;
}
scanf(" , ");
if (owner[0] == 'n') {
planet->owner = 0;
} else {
sscanf(owner, "%d", &planet->owner);
}
print_planet(planet);
add_list(planets, planet);
return 0;
}
void print_expedition(Expedition *expedition) {
printf("{ \"id\" : %d , \"ship_count\" : %d , \"origin\" : %s , \"destination\" : %s , \"owner\" : %d , \"turns_remaining\" : %d }\n",
expedition->id,
@ -76,33 +55,6 @@ void print_expedition(Expedition *expedition) {
expedition->turns_remaining);
}
int read_expedition(ArrayList *expeditions) {
Expedition *expedition = calloc(1, sizeof(Expedition));
if (scanf(
" { \"id\" : %d , \"ship_count\" : %d , \"origin\" : \"%19[^\"]\" , \"destination\" : \"%19[^\"]\" , \"owner\" : %d , \"turns_remaining\" : %d } ",
&expedition->id, &expedition->ship_count, expedition->origin, expedition->destination, &expedition->owner, &expedition->turns_remaining
) != 6) {
free(expedition);
return 1;
}
scanf(" , ");
print_expedition(expedition);
add_list(expeditions, expedition);
return 0;
}
Gamestate *read_gamestate() {
Gamestate *gamestate = malloc(sizeof(Gamestate));
gamestate->planets = list();
gamestate->expeditions = list();
scanf(" { \"planets\": [ ");
while (read_planet(gamestate->planets) == 0);
scanf(" ] , \"expeditions\" : [ ");
while (read_expedition(gamestate->expeditions) == 0);
return gamestate;
}
void free_gamestate(Gamestate *this) {
fold_list(this->planets, NULL, free_listitem);
free_list(this->planets);
@ -111,8 +63,53 @@ void free_gamestate(Gamestate *this) {
free(this);
}
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;
add_list(expeditions, expedition);
return acc;
}
Gamestate * parse_gamestate_json() {
Gamestate *gamestate = malloc(sizeof(Gamestate));
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);
gamestate->planets = list();
fold_list(planets->value.list, gamestate->planets, planet_from_json);
gamestate->expeditions = list();
fold_list(expeditions->value.list, gamestate->expeditions, expedition_from_json);
json_free(json);
return gamestate;
}
int main(int argc, char **argv) {
Json * json = json_parse(stdin);
Gamestate *gamestate = parse_gamestate_json();
free_gamestate(gamestate);
/*Gamestate *gamestate;
SortedMap *test;
@ -127,11 +124,5 @@ int main(int argc, char **argv) {
printf("%s\n", (char *) get_map(test, "awer"));
print_map(test);
free_map(test); */
json_print(json, stdout);
json_free(json);
json = json_parse(stdin);
json_print(json, stdout);
json_free(json);
return 0;
}