fix allocation of planets and expeditions from json, print state

This commit is contained in:
Felix Van der Jeugt 2023-10-31 00:17:10 +01:00
parent cdd56b8e25
commit f416e614a7
No known key found for this signature in database
GPG Key ID: 58B209295023754D
1 changed files with 52 additions and 11 deletions

View File

@ -18,6 +18,8 @@ struct LIST {
} LIST = { list, add_list, get_list, free_listitems, free_list };
*/
const int NO_OWNER = -1;
typedef struct planet {
int ship_count;
float x;
@ -40,25 +42,54 @@ typedef struct gamestate {
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_planet(void *acc, int index, void *item) {
Planet *planet = item;
printf(" { \"ship_count\": %d , \"x\": %f , \"y\": %f , \"owner\": %d , \"name\": \"%s\" }\n",
planet->ship_count, planet->x, planet->y, planet->owner, planet->name);
return NULL;
}
void print_expedition(Expedition *expedition) {
printf("{ \"id\" : %d , \"ship_count\" : %d , \"origin\" : %s , \"destination\" : %s , \"owner\" : %d , \"turns_remaining\" : %d }\n",
void * print_expedition(void *acc, int index, void *item) {
Expedition *expedition = item;
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);
return NULL;
}
void print_gamestate(Gamestate *this) {
printf("\n");
printf("{ \"planets\": [\n");
fold_list(this->planets, NULL, print_planet);
printf("], \"expeditions\": [\n");
fold_list(this->expeditions, NULL, print_expedition);
printf("]}\n");
printf("\n");
}
void * free_planet(void *acc, int index, void *item) {
Planet *planet = item;
free(planet->name);
free(planet);
return NULL;
}
void * free_expedition(void *acc, int index, void *item) {
Expedition *expedition = item;
free(expedition->origin);
free(expedition->destination);
free(expedition);
return NULL;
}
void free_gamestate(Gamestate *this) {
fold_list(this->planets, NULL, free_listitem);
fold_list(this->planets, NULL, free_planet);
free_list(this->planets);
fold_list(this->expeditions, NULL, free_listitem);
fold_list(this->expeditions, NULL, free_expedition);
free_list(this->expeditions);
free(this);
}
@ -66,12 +97,16 @@ void free_gamestate(Gamestate *this) {
void * planet_from_json(void *acc, int index, void *item) {
ArrayList *planets = (ArrayList *) acc;
Json *json = (Json *) item;
Json *owner = (Json *) get_map(json->value.map, "owner");
char *name;
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;
planet->owner = owner->type == NIL ? NO_OWNER : owner->value.number;
name = ((Json *) get_map(json->value.map, "name"))->value.string;
planet->name = malloc(strlen(name) + 1);
strcpy(planet->name, name);
add_list(planets, planet);
return acc;
}
@ -79,11 +114,16 @@ void * planet_from_json(void *acc, int index, void *item) {
void * expedition_from_json(void *acc, int index, void *item) {
ArrayList *expeditions = (ArrayList *) acc;
Json *json = (Json *) item;
char *origin, *destination;
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;
origin = ((Json *) get_map(json->value.map, "origin"))->value.string;
expedition->origin = malloc(strlen(origin) + 1);
strcpy(expedition->origin, origin);
destination = ((Json *) get_map(json->value.map, "destination"))->value.string;
expedition->destination = malloc(strlen(destination) + 1);
strcpy(expedition->destination, destination);
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);
@ -108,6 +148,7 @@ Gamestate * parse_gamestate_json() {
int main(int argc, char **argv) {
Gamestate *gamestate = parse_gamestate_json();
print_gamestate(gamestate);
free_gamestate(gamestate);
/*Gamestate *gamestate;