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
|
|
|
|
2023-10-31 00:17:10 +01:00
|
|
|
const int NO_OWNER = -1;
|
|
|
|
|
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;
|
|
|
|
|
2023-10-31 00:17:10 +01:00
|
|
|
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;
|
2022-11-01 22:09:32 +01:00
|
|
|
}
|
|
|
|
|
2023-10-31 00:17:10 +01:00
|
|
|
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",
|
2022-11-01 22:09:32 +01:00
|
|
|
expedition->id,
|
|
|
|
expedition->ship_count,
|
|
|
|
expedition->origin,
|
|
|
|
expedition->destination,
|
|
|
|
expedition->owner,
|
|
|
|
expedition->turns_remaining);
|
2023-10-31 00:17:10 +01:00
|
|
|
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;
|
2022-11-01 22:09:32 +01:00
|
|
|
}
|
|
|
|
|
2022-11-23 08:31:17 +01:00
|
|
|
void free_gamestate(Gamestate *this) {
|
2023-10-31 00:17:10 +01:00
|
|
|
fold_list(this->planets, NULL, free_planet);
|
2022-11-23 08:31:17 +01:00
|
|
|
free_list(this->planets);
|
2023-10-31 00:17:10 +01:00
|
|
|
fold_list(this->expeditions, NULL, free_expedition);
|
2022-11-23 08:31:17 +01:00
|
|
|
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;
|
2023-10-31 00:17:10 +01:00
|
|
|
Json *owner = (Json *) get_map(json->value.map, "owner");
|
|
|
|
char *name;
|
2022-11-23 08:31:17 +01:00
|
|
|
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;
|
2023-10-31 00:17:10 +01:00
|
|
|
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);
|
2022-11-23 08:31:17 +01:00
|
|
|
add_list(planets, planet);
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void * expedition_from_json(void *acc, int index, void *item) {
|
|
|
|
ArrayList *expeditions = (ArrayList *) acc;
|
|
|
|
Json *json = (Json *) item;
|
2023-10-31 00:17:10 +01:00
|
|
|
char *origin, *destination;
|
2022-11-23 08:31:17 +01:00
|
|
|
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;
|
2023-10-31 00:17:10 +01:00
|
|
|
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);
|
2022-11-23 08:31:17 +01:00
|
|
|
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();
|
2023-10-31 00:17:10 +01:00
|
|
|
print_gamestate(gamestate);
|
2022-11-23 08:31:17 +01:00
|
|
|
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;
|
|
|
|
}
|