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

View File

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