forked from github/server
export factions, too
give this a header and some flags so we can choose in detail what to export.
This commit is contained in:
parent
804b8192ea
commit
e9b9c9783a
26
src/export.c
26
src/export.c
|
@ -1,13 +1,20 @@
|
|||
#include <util/bool.h>
|
||||
#include <util/base36.h>
|
||||
#include <platform.h>
|
||||
|
||||
#include "export.h"
|
||||
|
||||
#include <kernel/types.h>
|
||||
#include <kernel/region.h>
|
||||
#include <kernel/faction.h>
|
||||
#include <kernel/terrain.h>
|
||||
#include <stream.h>
|
||||
#include "cJSON.h"
|
||||
|
||||
void export_json(stream * out) {
|
||||
region * r;
|
||||
void export_json(stream * out, unsigned int flags) {
|
||||
cJSON *json, *root = cJSON_CreateObject();
|
||||
if (flags & EXPORT_REGIONS) {
|
||||
region * r;
|
||||
cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject());
|
||||
for (r = regions; r; r = r->next) {
|
||||
char id[32];
|
||||
|
@ -18,7 +25,19 @@ void export_json(stream * out) {
|
|||
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;
|
||||
output = cJSON_Print(json);
|
||||
tok = strtok(output, "\n\r");
|
||||
|
@ -30,4 +49,5 @@ void export_json(stream * out) {
|
|||
}
|
||||
free(output);
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
|
|
|
@ -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
|
|
@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include "types.h"
|
||||
|
||||
/* FAST_CONNECT: regions are directly connected to neighbours, saves doing
|
||||
|
|
Loading…
Reference in New Issue