forked from github/server
read ships from config file (only very basic) and create them in a lua test.
This commit is contained in:
parent
d503937999
commit
6e56c56d39
9 changed files with 122 additions and 18 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -114,6 +114,7 @@
|
|||
<Include Dir="../critbit"/>
|
||||
<Include Dir="../iniparser"/>
|
||||
<Include Dir="../crypto"/>
|
||||
<Include Dir="../cJSON"/>
|
||||
</Includes>
|
||||
<Libs PreObjects="0">
|
||||
<Lib File="%bderessea.a"/>
|
||||
|
@ -238,6 +239,7 @@
|
|||
<Include Dir="../critbit"/>
|
||||
<Include Dir="../iniparser"/>
|
||||
<Include Dir="../crypto"/>
|
||||
<Include Dir="../cJSON"/>
|
||||
</Includes>
|
||||
<Libs PreObjects="0">
|
||||
<Lib File="%bderessea.a"/>
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include <platform.h>
|
||||
#include <kernel/types.h>
|
||||
#include <kernel/jsonconf.h>
|
||||
#include <util/log.h>
|
||||
#include <cJSON.h>
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ without prior permission by the authors of Eressea.
|
|||
#include <kernel/build.h>
|
||||
|
||||
#include <util/language.h>
|
||||
#include <util/log.h>
|
||||
|
||||
#include <tolua.h>
|
||||
#include <string.h>
|
||||
|
@ -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;
|
||||
|
|
|
@ -47,9 +47,37 @@ without prior permission by the authors of Eressea.
|
|||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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,16 +88,24 @@ 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) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void json_race(cJSON *json, race *rc) {
|
||||
cJSON *child;
|
||||
|
|
|
@ -2,12 +2,14 @@
|
|||
#include "types.h"
|
||||
#include "jsonconf.h"
|
||||
#include "race.h"
|
||||
#include "building.h"
|
||||
#include "ship.h"
|
||||
#include <CuTest.h>
|
||||
#include <cJSON.h>
|
||||
#include <tests.h>
|
||||
#include <stdio.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -63,6 +63,7 @@ void test_cleanup(void)
|
|||
default_locale = 0;
|
||||
free_locales();
|
||||
free_spells();
|
||||
free_shiptypes();
|
||||
free_races();
|
||||
free_spellbooks();
|
||||
free_gamedata();
|
||||
|
|
Loading…
Reference in a new issue