From 6e56c56d3940cf76f69f8058228709b8ae6c4ad6 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 13 Jun 2014 08:04:06 -0700 Subject: [PATCH] read ships from config file (only very basic) and create them in a lua test. --- scripts/tests/config.lua | 21 +++++++++++++++- se/server.vpj | 2 ++ src/bind_config.c | 3 +++ src/bind_ship.c | 3 +++ src/kernel/jsonconf.c | 42 +++++++++++++++++++++++++++++--- src/kernel/jsonconf_test.c | 49 +++++++++++++++++++++++++++++++------- src/kernel/ship.c | 6 +++++ src/kernel/ship.h | 13 +++++----- src/tests.c | 1 + 9 files changed, 122 insertions(+), 18 deletions(-) diff --git a/scripts/tests/config.lua b/scripts/tests/config.lua index 9460d8bdd..9e0beae01 100644 --- a/scripts/tests/config.lua +++ b/scripts/tests/config.lua @@ -6,7 +6,7 @@ function setup() eressea.free_game() end -function test_read() +function test_read_race() local f f = faction.create("orc@example.com", "orc", "en") assert_equal(nil, f) @@ -16,3 +16,22 @@ function test_read() assert_not_nil(f) end +function test_read_ship() + local s + s = ship.create(nil, "boat") + assert_equal(nil, s) + assert_not_nil(eressea.config) + conf = [[{ + "ships": { + "boat" : { + "construction" : { + "maxsize" : 20 + } + } + } + }]] + eressea.config.parse(conf); + s = ship.create(nil, "boat") + assert_not_nil(s) +end + diff --git a/se/server.vpj b/se/server.vpj index aa3e0992c..d276c2f10 100644 --- a/se/server.vpj +++ b/se/server.vpj @@ -114,6 +114,7 @@ + @@ -238,6 +239,7 @@ + diff --git a/src/bind_config.c b/src/bind_config.c index 1dc34b82c..46fd9b973 100644 --- a/src/bind_config.c +++ b/src/bind_config.c @@ -3,6 +3,7 @@ #include #include #include +#include #include void config_parse(const char *json) @@ -11,6 +12,8 @@ void config_parse(const char *json) if (conf) { json_config(conf); cJSON_Delete(conf); + } else { + log_error("json parse error: %s\n", cJSON_GetErrorPtr()); } } diff --git a/src/bind_ship.c b/src/bind_ship.c index 50e803c31..820c437ac 100644 --- a/src/bind_ship.c +++ b/src/bind_ship.c @@ -21,6 +21,7 @@ without prior permission by the authors of Eressea. #include #include +#include #include #include @@ -129,6 +130,8 @@ static int tolua_ship_create(lua_State * L) sh->size = stype->construction->maxsize; tolua_pushusertype(L, (void *)sh, TOLUA_CAST "ship"); return 1; + } else { + log_error("Unkown ship type '%s'\n", sname); } } return 0; diff --git a/src/kernel/jsonconf.c b/src/kernel/jsonconf.c index e41c45b9d..a5ad197a8 100644 --- a/src/kernel/jsonconf.c +++ b/src/kernel/jsonconf.c @@ -47,9 +47,37 @@ without prior permission by the authors of Eressea. #include #include #include +#include #include -void json_building(cJSON *json, building_type *rc) { +void json_construction(cJSON *json, construction **consp) { + cJSON *child; + if (json->type!=cJSON_Object) { + log_error("building %s is not a json object: %d\n", json->string, json->type); + return; + } + construction * cons = (construction *)calloc(sizeof(construction), 1); + for (child=json->child;child;child=child->next) { + switch(child->type) { + case cJSON_Number: + if (strcmp(child->string, "maxsize")==0) { + cons->maxsize = child->valueint; + } + else if (strcmp(child->string, "reqsize")==0) { + cons->reqsize = child->valueint; + } + else if (strcmp(child->string, "minskill")==0) { + cons->minskill = child->valueint; + } + break; + default: + log_error("building %s contains unknown attribute %s\n", json->string, child->string); + } + } + *consp = cons; +} + +void json_building(cJSON *json, building_type *st) { cJSON *child; if (json->type!=cJSON_Object) { log_error("building %s is not a json object: %d\n", json->string, json->type); @@ -60,14 +88,22 @@ void json_building(cJSON *json, building_type *rc) { } } -void json_ship(cJSON *json, ship_type *rc) { +void json_ship(cJSON *json, ship_type *st) { cJSON *child; if (json->type!=cJSON_Object) { log_error("ship %s is not a json object: %d\n", json->string, json->type); return; } for (child=json->child;child;child=child->next) { - log_error("ship %s contains unknown attribute %s\n", json->string, child->string); + switch(child->type) { + case cJSON_Object: + if (strcmp(child->string, "construction")==0) { + json_construction(child, &st->construction); + } + break; + default: + log_error("ship %s contains unknown attribute %s\n", json->string, child->string); + } } } diff --git a/src/kernel/jsonconf_test.c b/src/kernel/jsonconf_test.c index 9056d3a24..6b5441106 100644 --- a/src/kernel/jsonconf_test.c +++ b/src/kernel/jsonconf_test.c @@ -2,12 +2,14 @@ #include "types.h" #include "jsonconf.h" #include "race.h" +#include "building.h" +#include "ship.h" #include #include #include #include -static void test_flag(CuTest *tc, const char *name, int flag) { +static void check_flag(CuTest *tc, const char *name, int flag) { char data[1024]; const struct race *rc; cJSON *json; @@ -22,13 +24,14 @@ static void test_flag(CuTest *tc, const char *name, int flag) { } static void test_flags(CuTest *tc) { - test_flag(tc, "playerrace", RCF_PLAYERRACE); - test_flag(tc, "scarepeasants", RCF_SCAREPEASANTS); - test_flag(tc, "cansteal", RCF_CANSTEAL); - test_flag(tc, "noheal", RCF_NOHEAL); - test_flag(tc, "undead", RCF_UNDEAD); - test_flag(tc, "dragon", RCF_DRAGON); - test_flag(tc, "fly", RCF_FLY); + check_flag(tc, "playerrace", RCF_PLAYERRACE); + check_flag(tc, "scarepeasants", RCF_SCAREPEASANTS); + check_flag(tc, "cansteal", RCF_CANSTEAL); + check_flag(tc, "noheal", RCF_NOHEAL); + check_flag(tc, "undead", RCF_UNDEAD); + check_flag(tc, "dragon", RCF_DRAGON); + check_flag(tc, "fly", RCF_FLY); + test_cleanup(); } static void test_races(CuTest * tc) @@ -48,8 +51,10 @@ static void test_races(CuTest * tc) "}}}"; cJSON *json = cJSON_Parse(data); const struct race *rc; + test_cleanup(); + CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, races); json_config(json); @@ -67,12 +72,40 @@ static void test_races(CuTest * tc) CuAssertIntEquals(tc, 4, rc->capacity); CuAssertIntEquals(tc, 5, rc->hitpoints); CuAssertIntEquals(tc, 6, rc->armor); + test_cleanup(); +} + +static void test_ships(CuTest * tc) +{ + const char * data = "{\"ships\": { \"boat\" : { " + "\"construction\" : { \"maxsize\" : 20, \"reqsize\" : 10, \"minskill\" : 1 }" + "}}}"; + + cJSON *json = cJSON_Parse(data); + const struct ship_type *st; + + test_cleanup(); + + CuAssertPtrNotNull(tc, json); + CuAssertPtrEquals(tc, 0, shiptypes); + json_config(json); + + CuAssertPtrNotNull(tc, shiptypes); + st = st_find("boat"); + CuAssertPtrNotNull(tc, st); + CuAssertPtrNotNull(tc, st->construction); + CuAssertIntEquals(tc, 10, st->construction->reqsize); + CuAssertIntEquals(tc, 20, st->construction->maxsize); + CuAssertIntEquals(tc, 1, st->construction->minskill); + test_cleanup(); } CuSuite *get_jsonconf_suite(void) { CuSuite *suite = CuSuiteNew(); + SUITE_ADD_TEST(suite, test_ships); SUITE_ADD_TEST(suite, test_races); SUITE_ADD_TEST(suite, test_flags); return suite; } + diff --git a/src/kernel/ship.c b/src/kernel/ship.c index 972d00025..8e2c00100 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -234,6 +234,12 @@ void free_ship(ship * s) free(s); } +void free_shiptypes(void) { + ql_foreach(shiptypes, free); + ql_free(shiptypes); + shiptypes = 0; +} + void free_ships(void) { while (deleted_ships) { diff --git a/src/kernel/ship.h b/src/kernel/ship.h index fd3d562b5..983d770a9 100644 --- a/src/kernel/ship.h +++ b/src/kernel/ship.h @@ -63,8 +63,9 @@ extern "C" { /* Alte Schiffstypen: */ - extern const ship_type *st_find(const char *name); - extern ship_type *st_get_or_create(const char *name); + const ship_type *st_find(const char *name); + ship_type *st_get_or_create(const char *name); + void free_shiptypes(void); #define NOSHIP NULL @@ -94,10 +95,10 @@ extern "C" { direction_t coast; } ship; - extern void damage_ship(struct ship * sh, double percent); - extern void ship_set_owner(struct unit * u); - extern struct unit *ship_owner(const struct ship *sh); - extern void ship_update_owner(struct ship * sh); + void damage_ship(struct ship * sh, double percent); + void ship_set_owner(struct unit * u); + struct unit *ship_owner(const struct ship *sh); + void ship_update_owner(struct ship * sh); extern const char *shipname(const struct ship *self); extern int shipcapacity(const struct ship *sh); diff --git a/src/tests.c b/src/tests.c index 7b9362535..17c3efa1d 100644 --- a/src/tests.c +++ b/src/tests.c @@ -63,6 +63,7 @@ void test_cleanup(void) default_locale = 0; free_locales(); free_spells(); + free_shiptypes(); free_races(); free_spellbooks(); free_gamedata();