export factions, too

give this a header and some flags so we can choose in detail what to
export.
This commit is contained in:
Enno Rehling 2014-03-07 08:03:53 -08:00
parent 804b8192ea
commit e9b9c9783a
3 changed files with 45 additions and 12 deletions

View File

@ -1,24 +1,43 @@
#include <util/bool.h>
#include <util/base36.h>
#include <platform.h> #include <platform.h>
#include "export.h"
#include <kernel/types.h> #include <kernel/types.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/faction.h>
#include <kernel/terrain.h> #include <kernel/terrain.h>
#include <stream.h> #include <stream.h>
#include "cJSON.h" #include "cJSON.h"
void export_json(stream * out) { void export_json(stream * out, unsigned int flags) {
region * r;
cJSON *json, *root = cJSON_CreateObject(); cJSON *json, *root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject()); if (flags & EXPORT_REGIONS) {
for (r=regions; r; r=r->next) { region * r;
char id[32]; cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject());
cJSON *data; for (r = regions; r; r = r->next) {
snprintf(id, sizeof(id), "%u", r->uid); char id[32];
cJSON_AddItemToObject(json, id, data = cJSON_CreateObject()); cJSON *data;
cJSON_AddNumberToObject(data, "x", r->x); snprintf(id, sizeof(id), "%u", r->uid);
cJSON_AddNumberToObject(data, "y", r->y); cJSON_AddItemToObject(json, id, data = cJSON_CreateObject());
cJSON_AddStringToObject(data, "type", r->terrain->_name); cJSON_AddNumberToObject(data, "x", r->x);
cJSON_AddNumberToObject(data, "y", r->y);
cJSON_AddStringToObject(data, "type", r->terrain->_name);
}
} }
if (out) { if (flags & EXPORT_FACTIONS) {
faction *f;
cJSON_AddItemToObject(root, "factions", json = cJSON_CreateObject());
for (f = factions; f; f = f->next) {
cJSON *data;
cJSON_AddItemToObject(json, itoa36(f->no), data = cJSON_CreateObject());
cJSON_AddStringToObject(data, "name", f->name);
cJSON_AddStringToObject(data, "email", f->email);
cJSON_AddNumberToObject(data, "score", f->score);
}
}
if (flags) {
char *tok, *output; char *tok, *output;
output = cJSON_Print(json); output = cJSON_Print(json);
tok = strtok(output, "\n\r"); tok = strtok(output, "\n\r");
@ -30,4 +49,5 @@ void export_json(stream * out) {
} }
free(output); free(output);
} }
cJSON_Delete(root);
} }

12
src/export.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#ifndef ERESSEA_EXPORT_H
#define ERESSEA_EXPORT_H
#define EXPORT_REGIONS 1<<0
#define EXPORT_FACTIONS 1<<1
#define EXPORT_UNITS 1<<2
struct stream;
void export_json(struct stream * out, unsigned int flags);
#endif

View File

@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
extern "C" { extern "C" {
#endif #endif
#include <stddef.h>
#include "types.h" #include "types.h"
/* FAST_CONNECT: regions are directly connected to neighbours, saves doing /* FAST_CONNECT: regions are directly connected to neighbours, saves doing