forked from github/server
improve construction data in JSON.
test castle naming function
This commit is contained in:
parent
e39336e87a
commit
927ada92b6
4 changed files with 100 additions and 29 deletions
|
@ -52,7 +52,7 @@ without prior permission by the authors of Eressea.
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int json_flags(cJSON *json, const char *flags[]) {
|
static int json_flags(cJSON *json, const char *flags[]) {
|
||||||
cJSON *entry;
|
cJSON *entry;
|
||||||
int result = 0;
|
int result = 0;
|
||||||
assert(json->type==cJSON_Array);
|
assert(json->type==cJSON_Array);
|
||||||
|
@ -69,15 +69,45 @@ int json_flags(cJSON *json, const char *flags[]) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_construction(cJSON *json, construction **consp) {
|
static void json_requirements(cJSON *json, requirement **matp) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
int i;
|
||||||
|
requirement *mat = calloc(sizeof(requirement), 1+cJSON_GetArraySize(json));
|
||||||
|
for (i=0,child=json->child;child;child=child->next,++i) {
|
||||||
|
mat[i].number = child->valueint;
|
||||||
|
mat[i].rtype = rt_get_or_create(child->string);
|
||||||
|
}
|
||||||
|
*matp = mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void json_construction(cJSON *json, construction **consp) {
|
||||||
|
cJSON *child;
|
||||||
|
if (json->type==cJSON_Array) {
|
||||||
|
int size = 0;
|
||||||
|
for (child=json->child;child;child=child->next) {
|
||||||
|
construction *cons = 0;
|
||||||
|
json_construction(child, &cons);
|
||||||
|
if (cons) {
|
||||||
|
cons->maxsize -= size;
|
||||||
|
size += cons->maxsize + size;
|
||||||
|
*consp = cons;
|
||||||
|
consp = &cons->improvement;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (json->type != cJSON_Object) {
|
||||||
log_error_n("building %s is not a json object: %d", json->string, json->type);
|
log_error_n("building %s is not a json object: %d", json->string, json->type);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
construction * cons = (construction *)calloc(sizeof(construction), 1);
|
construction * cons = (construction *)calloc(sizeof(construction), 1);
|
||||||
for (child=json->child;child;child=child->next) {
|
for (child=json->child;child;child=child->next) {
|
||||||
switch(child->type) {
|
switch(child->type) {
|
||||||
|
case cJSON_Object:
|
||||||
|
if (strcmp(child->string, "materials")==0) {
|
||||||
|
json_requirements(child, &cons->materials);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case cJSON_Number:
|
case cJSON_Number:
|
||||||
if (strcmp(child->string, "maxsize")==0) {
|
if (strcmp(child->string, "maxsize")==0) {
|
||||||
cons->maxsize = child->valueint;
|
cons->maxsize = child->valueint;
|
||||||
|
@ -96,7 +126,7 @@ void json_construction(cJSON *json, construction **consp) {
|
||||||
*consp = cons;
|
*consp = cons;
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_terrain(cJSON *json, terrain_type *ter) {
|
static void json_terrain(cJSON *json, terrain_type *ter) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("terrain %s is not a json object: %d", json->string, json->type);
|
log_error_n("terrain %s is not a json object: %d", json->string, json->type);
|
||||||
|
@ -120,7 +150,7 @@ void json_terrain(cJSON *json, terrain_type *ter) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_building(cJSON *json, building_type *bt) {
|
static void json_building(cJSON *json, building_type *bt) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("building %s is not a json object: %d", json->string, json->type);
|
log_error_n("building %s is not a json object: %d", json->string, json->type);
|
||||||
|
@ -128,6 +158,11 @@ void json_building(cJSON *json, building_type *bt) {
|
||||||
}
|
}
|
||||||
for (child=json->child;child;child=child->next) {
|
for (child=json->child;child;child=child->next) {
|
||||||
switch(child->type) {
|
switch(child->type) {
|
||||||
|
case cJSON_Array:
|
||||||
|
if (strcmp(child->string, "construction")==0) {
|
||||||
|
json_construction(child, &bt->construction);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case cJSON_Object:
|
case cJSON_Object:
|
||||||
if (strcmp(child->string, "construction")==0) {
|
if (strcmp(child->string, "construction")==0) {
|
||||||
json_construction(child, &bt->construction);
|
json_construction(child, &bt->construction);
|
||||||
|
@ -147,7 +182,7 @@ void json_building(cJSON *json, building_type *bt) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_item(cJSON *json, item_type *itype) {
|
static void json_item(cJSON *json, item_type *itype) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
const char *flags[] = {
|
const char *flags[] = {
|
||||||
"herb", "cursed", "nodrop", "big", "animal", "vehicle", 0
|
"herb", "cursed", "nodrop", "big", "animal", "vehicle", 0
|
||||||
|
@ -182,7 +217,7 @@ void json_item(cJSON *json, item_type *itype) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_ship(cJSON *json, ship_type *st) {
|
static void json_ship(cJSON *json, ship_type *st) {
|
||||||
cJSON *child, *iter;
|
cJSON *child, *iter;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("ship %s is not a json object: %d", json->string, json->type);
|
log_error_n("ship %s is not a json object: %d", json->string, json->type);
|
||||||
|
@ -224,7 +259,7 @@ void json_ship(cJSON *json, ship_type *st) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_race(cJSON *json, race *rc) {
|
static void json_race(cJSON *json, race *rc) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
const char *flags[] = {
|
const char *flags[] = {
|
||||||
"playerrace", "killpeasants", "scarepeasants",
|
"playerrace", "killpeasants", "scarepeasants",
|
||||||
|
@ -288,7 +323,7 @@ void json_race(cJSON *json, race *rc) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_terrains(cJSON *json) {
|
static void json_terrains(cJSON *json) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("terrains is not a json object: %d", json->type);
|
log_error_n("terrains is not a json object: %d", json->type);
|
||||||
|
@ -299,7 +334,7 @@ void json_terrains(cJSON *json) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_buildings(cJSON *json) {
|
static void json_buildings(cJSON *json) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("buildings is not a json object: %d", json->type);
|
log_error_n("buildings is not a json object: %d", json->type);
|
||||||
|
@ -310,7 +345,7 @@ void json_buildings(cJSON *json) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_items(cJSON *json) {
|
static void json_items(cJSON *json) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("items is not a json object: %d", json->type);
|
log_error_n("items is not a json object: %d", json->type);
|
||||||
|
@ -326,7 +361,7 @@ void json_items(cJSON *json) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_ships(cJSON *json) {
|
static void json_ships(cJSON *json) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("ships is not a json object: %d", json->type);
|
log_error_n("ships is not a json object: %d", json->type);
|
||||||
|
@ -337,7 +372,7 @@ void json_ships(cJSON *json) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_locale(cJSON *json, struct locale *lang) {
|
static void json_locale(cJSON *json, struct locale *lang) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("strings is not a json object: %d", json->type);
|
log_error_n("strings is not a json object: %d", json->type);
|
||||||
|
@ -350,7 +385,7 @@ void json_locale(cJSON *json, struct locale *lang) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_strings(cJSON *json) {
|
static void json_strings(cJSON *json) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("strings is not a json object: %d", json->type);
|
log_error_n("strings is not a json object: %d", json->type);
|
||||||
|
@ -390,7 +425,7 @@ static void json_direction(cJSON *json, struct locale *lang) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_directions(cJSON *json) {
|
static void json_directions(cJSON *json) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("directions is not a json object: %d", json->type);
|
log_error_n("directions is not a json object: %d", json->type);
|
||||||
|
@ -462,7 +497,7 @@ static void json_keyword(cJSON *json, struct locale *lang) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_skills(cJSON *json) {
|
static void json_skills(cJSON *json) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("skills is not a json object: %d", json->type);
|
log_error_n("skills is not a json object: %d", json->type);
|
||||||
|
@ -474,7 +509,7 @@ void json_skills(cJSON *json) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_keywords(cJSON *json) {
|
static void json_keywords(cJSON *json) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("keywords is not a json object: %d", json->type);
|
log_error_n("keywords is not a json object: %d", json->type);
|
||||||
|
@ -486,7 +521,7 @@ void json_keywords(cJSON *json) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_races(cJSON *json) {
|
static void json_races(cJSON *json) {
|
||||||
cJSON *child;
|
cJSON *child;
|
||||||
if (json->type!=cJSON_Object) {
|
if (json->type!=cJSON_Object) {
|
||||||
log_error_n("races is not a json object: %d", json->type);
|
log_error_n("races is not a json object: %d", json->type);
|
||||||
|
|
|
@ -151,11 +151,43 @@ static void test_ships(CuTest * tc)
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_castles(CuTest *tc) {
|
||||||
|
const char * data = "{\"buildings\": { \"castle\" : { "
|
||||||
|
"\"construction\" : ["
|
||||||
|
"{ \"maxsize\" : 2 },"
|
||||||
|
"{ \"maxsize\" : 8 }"
|
||||||
|
"]}}}";
|
||||||
|
|
||||||
|
cJSON *json = cJSON_Parse(data);
|
||||||
|
const building_type *bt;
|
||||||
|
|
||||||
|
test_cleanup();
|
||||||
|
|
||||||
|
CuAssertPtrNotNull(tc, json);
|
||||||
|
CuAssertPtrEquals(tc, 0, buildingtypes);
|
||||||
|
json_config(json);
|
||||||
|
|
||||||
|
CuAssertPtrNotNull(tc, buildingtypes);
|
||||||
|
bt = bt_find("castle");
|
||||||
|
CuAssertPtrNotNull(tc, bt);
|
||||||
|
CuAssertPtrNotNull(tc, bt->construction);
|
||||||
|
CuAssertIntEquals(tc, 2, bt->construction->maxsize);
|
||||||
|
CuAssertPtrNotNull(tc, bt->construction->improvement);
|
||||||
|
CuAssertIntEquals(tc, 6, bt->construction->improvement->maxsize);
|
||||||
|
CuAssertPtrEquals(tc, 0, bt->construction->improvement->improvement);
|
||||||
|
}
|
||||||
|
|
||||||
static void test_buildings(CuTest * tc)
|
static void test_buildings(CuTest * tc)
|
||||||
{
|
{
|
||||||
const char * data = "{\"buildings\": { \"house\" : { "
|
const char * data = "{\"buildings\": { \"house\" : { "
|
||||||
"\"construction\" : { \"maxsize\" : 20, \"reqsize\" : 10, \"minskill\" : 1 }"
|
"\"construction\" : {"
|
||||||
"}}}";
|
"\"maxsize\" : 20,"
|
||||||
|
"\"reqsize\" : 10,"
|
||||||
|
"\"minskill\" : 1,"
|
||||||
|
"\"materials\" : {"
|
||||||
|
"\"stone\" : 2,"
|
||||||
|
"\"iron\" : 1"
|
||||||
|
"}}}}}";
|
||||||
|
|
||||||
cJSON *json = cJSON_Parse(data);
|
cJSON *json = cJSON_Parse(data);
|
||||||
const building_type *bt;
|
const building_type *bt;
|
||||||
|
@ -170,9 +202,16 @@ static void test_buildings(CuTest * tc)
|
||||||
bt = bt_find("house");
|
bt = bt_find("house");
|
||||||
CuAssertPtrNotNull(tc, bt);
|
CuAssertPtrNotNull(tc, bt);
|
||||||
CuAssertPtrNotNull(tc, bt->construction);
|
CuAssertPtrNotNull(tc, bt->construction);
|
||||||
|
CuAssertPtrNotNull(tc, bt->construction->materials);
|
||||||
|
CuAssertIntEquals(tc, 2, bt->construction->materials[0].number);
|
||||||
|
CuAssertPtrEquals(tc, (void *)get_resourcetype(R_STONE), (void *)bt->construction->materials[0].rtype);
|
||||||
|
CuAssertIntEquals(tc, 1, bt->construction->materials[1].number);
|
||||||
|
CuAssertPtrEquals(tc, (void *)get_resourcetype(R_IRON), (void *)bt->construction->materials[1].rtype);
|
||||||
|
CuAssertIntEquals(tc, 0, bt->construction->materials[2].number);
|
||||||
CuAssertIntEquals(tc, 10, bt->construction->reqsize);
|
CuAssertIntEquals(tc, 10, bt->construction->reqsize);
|
||||||
CuAssertIntEquals(tc, 20, bt->construction->maxsize);
|
CuAssertIntEquals(tc, 20, bt->construction->maxsize);
|
||||||
CuAssertIntEquals(tc, 1, bt->construction->minskill);
|
CuAssertIntEquals(tc, 1, bt->construction->minskill);
|
||||||
|
CuAssertPtrEquals(tc, 0, bt->construction->improvement);
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -288,6 +327,7 @@ CuSuite *get_jsonconf_suite(void)
|
||||||
SUITE_ADD_TEST(suite, test_items);
|
SUITE_ADD_TEST(suite, test_items);
|
||||||
SUITE_ADD_TEST(suite, test_ships);
|
SUITE_ADD_TEST(suite, test_ships);
|
||||||
SUITE_ADD_TEST(suite, test_buildings);
|
SUITE_ADD_TEST(suite, test_buildings);
|
||||||
|
SUITE_ADD_TEST(suite, test_castles);
|
||||||
SUITE_ADD_TEST(suite, test_terrains);
|
SUITE_ADD_TEST(suite, test_terrains);
|
||||||
SUITE_ADD_TEST(suite, test_races);
|
SUITE_ADD_TEST(suite, test_races);
|
||||||
SUITE_ADD_TEST(suite, test_strings);
|
SUITE_ADD_TEST(suite, test_strings);
|
||||||
|
|
|
@ -36,22 +36,18 @@ function setup()
|
||||||
assert(eressea.config.parse(conf)==0)
|
assert(eressea.config.parse(conf)==0)
|
||||||
end
|
end
|
||||||
|
|
||||||
function test_small_castles()
|
function test_castle_names()
|
||||||
local r = region.create(0, 0, "plain")
|
local r = region.create(0, 0, "plain")
|
||||||
local f1 = faction.create("test@eressea.de", "human", "de")
|
local f1 = faction.create("test@eressea.de", "human", "de")
|
||||||
local u1 = unit.create(f1, r, 1)
|
local u1 = unit.create(f1, r, 1)
|
||||||
local f2 = faction.create("test@eressea.de", "halfling", "de")
|
|
||||||
local u2 = unit.create(f2, r, 1)
|
|
||||||
u1:add_item("money", 10000)
|
u1:add_item("money", 10000)
|
||||||
|
|
||||||
local b = building.create(r, "castle")
|
local b = building.create(r, "castle")
|
||||||
u2.building = b
|
|
||||||
u1.building = b
|
u1.building = b
|
||||||
|
|
||||||
b.owner = u2
|
|
||||||
assert_equal("site", b:get_typename(7))
|
|
||||||
assert_equal("fortification", b:get_typename(8))
|
|
||||||
b.owner = u1
|
b.owner = u1
|
||||||
assert_equal("site", b:get_typename(9))
|
assert_equal("site", b:get_typename(1))
|
||||||
|
assert_equal("tradepost", b:get_typename(2))
|
||||||
|
assert_equal("tradepost", b:get_typename(9))
|
||||||
assert_equal("fortification", b:get_typename(10))
|
assert_equal("fortification", b:get_typename(10))
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,4 +6,4 @@ require "tests.regions"
|
||||||
require "tests.ships"
|
require "tests.ships"
|
||||||
require "tests.study"
|
require "tests.study"
|
||||||
require "tests.movement"
|
require "tests.movement"
|
||||||
-- require "tests.castles"
|
require "tests.castles"
|
||||||
|
|
Loading…
Reference in a new issue