From 26d6808ea1889868e5dcfec6ca37d64568b8d1d9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 4 Jul 2014 21:48:17 -0700 Subject: [PATCH] read building-maintenance requirements from JSON. --- src/kernel/jsonconf.c | 65 +++++++++++++++++++++++++++++++++++++- src/kernel/jsonconf.test.c | 30 ++++++++++++++++-- 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index f888ab88e..a73936afd 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -80,6 +80,63 @@ static void json_requirements(cJSON *json, requirement **matp) { *matp = mat; } +static void json_maintenance_i(cJSON *json, maintenance *mt) { + cJSON *child; + for (child = json->child; child; child = child->next) { + switch (child->type) { + case cJSON_Number: + if (strcmp(child->string, "amount") == 0) { + mt->number = child->valueint; + } + else { + log_error_n("maintenance contains unknown attribute %s", child->string); + } + break; + case cJSON_String: + if (strcmp(child->string, "type") == 0) { + mt->rtype = rt_get_or_create(child->valuestring); + } + else { + log_error_n("maintenance contains unknown attribute %s", child->string); + } + break; + case cJSON_Array: + if (strcmp(child->string, "flags") == 0) { + const char * flags[] = { "variable", "required", 0 }; + mt->flags = json_flags(child, flags); + } + else { + log_error_n("maintenance contains unknown attribute %s", child->string); + } + default: + log_error_n("maintenance contains unknown attribute %s", child->string); + } + } +} + +static void json_maintenance(cJSON *json, maintenance **mtp) { + cJSON *child; + maintenance *mt; + int i, size = 1; + + if (json->type == cJSON_Array) { + size = cJSON_GetArraySize(json); + } + else if (json->type != cJSON_Object) { + log_error_n("maintenance is not a json object or array (%d)", json->type); + return; + } + *mtp = mt = (struct maintenance *) calloc(sizeof(struct maintenance), size + 1); + if (json->type == cJSON_Array) { + for (i = 0, child = json->child; child; child = child->next, ++i) { + if (child->type == cJSON_Object) { + json_maintenance_i(child, mt+i); + } + } + } + json_maintenance_i(json, mt); +} + static void json_construction(cJSON *json, construction **consp) { cJSON *child; if (json->type==cJSON_Array) { @@ -159,14 +216,20 @@ static void json_building(cJSON *json, building_type *bt) { for (child=json->child;child;child=child->next) { switch(child->type) { case cJSON_Array: - if (strcmp(child->string, "construction")==0) { + if (strcmp(child->string, "construction") == 0) { json_construction(child, &bt->construction); } + else if (strcmp(child->string, "maintenance") == 0) { + json_maintenance(child, &bt->maintenance); + } break; case cJSON_Object: if (strcmp(child->string, "construction")==0) { json_construction(child, &bt->construction); } + else if (strcmp(child->string, "maintenance") == 0) { + json_maintenance(child, &bt->maintenance); + } break; case cJSON_String: if (strcmp(child->string, "name")==0) { diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index f128acf4a..932b88ac0 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -200,7 +200,11 @@ static void test_spells(CuTest * tc) static void test_buildings(CuTest * tc) { - const char * data = "{\"buildings\": { \"house\" : { " + const char * data = "{\"buildings\": { " + "\"house\" : { " + "\"maintenance\" : " + "{ \"type\" : \"iron\", \"amount\" : 1, \"flags\" : [ \"required\", \"variable\" ] }" + "," "\"construction\" : {" "\"maxsize\" : 20," "\"reqsize\" : 10," @@ -208,7 +212,13 @@ static void test_buildings(CuTest * tc) "\"materials\" : {" "\"stone\" : 2," "\"iron\" : 1" - "}}}}}"; + "}}}," + "\"shed\" : {" + "\"maintenance\" : [" + "{ \"type\" : \"iron\", \"amount\" : 1 }," + "{ \"type\" : \"stone\", \"amount\" : 2 }" + "]}" + "}}"; cJSON *json = cJSON_Parse(data); const building_type *bt; @@ -220,8 +230,24 @@ static void test_buildings(CuTest * tc) json_config(json); CuAssertPtrNotNull(tc, buildingtypes); + bt = bt_find("shed"); + CuAssertPtrNotNull(tc, bt); + CuAssertPtrNotNull(tc, bt->maintenance); + CuAssertPtrEquals(tc, (void *)get_resourcetype(R_IRON), (void *)bt->maintenance[0].rtype); + CuAssertPtrEquals(tc, (void *)get_resourcetype(R_STONE), (void *)bt->maintenance[1].rtype); + CuAssertIntEquals(tc, 1, bt->maintenance[0].number); + CuAssertIntEquals(tc, 2, bt->maintenance[1].number); + CuAssertIntEquals(tc, 0, bt->maintenance[2].number); + bt = bt_find("house"); CuAssertPtrNotNull(tc, bt); + + CuAssertPtrNotNull(tc, bt->maintenance); + CuAssertIntEquals(tc, 1, bt->maintenance[0].number); + CuAssertPtrEquals(tc, (void *)get_resourcetype(R_IRON), (void *)bt->maintenance[0].rtype); + CuAssertIntEquals(tc, MTF_VARIABLE|MTF_VITAL, bt->maintenance[0].flags); + CuAssertIntEquals(tc, 0, bt->maintenance[1].number); + CuAssertPtrNotNull(tc, bt->construction); CuAssertPtrNotNull(tc, bt->construction->materials); CuAssertIntEquals(tc, 2, bt->construction->materials[0].number);