add JSON configuration to skills, and tests

This commit is contained in:
Enno Rehling 2014-06-21 08:53:14 -07:00
parent 46f6628a08
commit bfadc4914f
2 changed files with 70 additions and 0 deletions

View File

@ -314,6 +314,36 @@ void json_directions(cJSON *json) {
} }
} }
static void json_skill(cJSON *json, struct locale *lang) {
cJSON *child;
if (json->type!=cJSON_Object) {
log_error_n("skill for locale `%s` not a json object: %d", locale_name(lang), json->type);
return;
}
for (child=json->child;child;child=child->next) {
skill_t sk = findskill(child->string);
if (sk!=NOSKILL) {
if (child->type==cJSON_String) {
init_skill(lang, sk, child->valuestring);
locale_setstring(lang, mkname("skill", skillnames[sk]), child->valuestring);
}
else if (child->type==cJSON_Array) {
cJSON *entry;
for (entry=child->child;entry;entry=entry->next) {
init_skill(lang, sk, entry->valuestring);
if ((entry==child->child)) {
locale_setstring(lang, mkname("skill", skillnames[sk]), entry->valuestring);
}
}
} else {
log_error_n("invalid type %d for skill `%s`", child->type, child->string);
}
} else {
log_error_n("unknown skill `%s` for locale `%s`", child->string, locale_name(lang));
}
}
}
static void json_keyword(cJSON *json, struct locale *lang) { static void json_keyword(cJSON *json, struct locale *lang) {
cJSON *child; cJSON *child;
if (json->type!=cJSON_Object) { if (json->type!=cJSON_Object) {
@ -344,6 +374,18 @@ static void json_keyword(cJSON *json, struct locale *lang) {
} }
} }
void json_skills(cJSON *json) {
cJSON *child;
if (json->type!=cJSON_Object) {
log_error_n("skills is not a json object: %d", json->type);
return;
}
for (child=json->child;child;child=child->next) {
struct locale * lang = get_or_create_locale(child->string);
json_skill(child, lang);
}
}
void json_keywords(cJSON *json) { void json_keywords(cJSON *json) {
cJSON *child; cJSON *child;
if (json->type!=cJSON_Object) { if (json->type!=cJSON_Object) {
@ -386,6 +428,9 @@ void json_config(cJSON *json) {
else if (strcmp(child->string, "keywords")==0) { else if (strcmp(child->string, "keywords")==0) {
json_keywords(child); json_keywords(child);
} }
else if (strcmp(child->string, "skills")==0) {
json_skills(child);
}
else if (strcmp(child->string, "buildings")==0) { else if (strcmp(child->string, "buildings")==0) {
json_buildings(child); json_buildings(child);
} }

View File

@ -182,6 +182,30 @@ static void test_directions(CuTest * tc)
test_cleanup(); test_cleanup();
} }
static void test_skills(CuTest * tc)
{
const char * data = "{\"skills\": { \"de\" : { \"alchemy\" : \"ALCHEMIE\", \"crossbow\" : [ \"ARMBRUST\", \"KREUZBOGEN\" ] }}}";
const struct locale * lang;
cJSON *json = cJSON_Parse(data);
test_cleanup();
lang = get_or_create_locale("de");
CuAssertPtrNotNull(tc, json);
CuAssertIntEquals(tc, NOSKILL, get_skill("potato", lang));
json_config(json);
CuAssertIntEquals(tc, NOSKILL, get_skill("potato", lang));
CuAssertIntEquals(tc, SK_CROSSBOW, get_skill("armbrust", lang));
CuAssertIntEquals(tc, SK_CROSSBOW, get_skill("kreuz", lang));
CuAssertIntEquals(tc, SK_ALCHEMY, get_skill("alchemie", lang));
CuAssertStrEquals(tc, "ALCHEMIE", locale_string(lang, "skill::alchemy"));
CuAssertStrEquals(tc, "ARMBRUST", locale_string(lang, "skill::crossbow"));
test_cleanup();
}
static void test_keywords(CuTest * tc) static void test_keywords(CuTest * tc)
{ {
const char * data = "{\"keywords\": { \"de\" : { \"move\" : \"NACH\", \"study\" : [ \"LERNEN\", \"STUDIEREN\" ] }}}"; const char * data = "{\"keywords\": { \"de\" : { \"move\" : \"NACH\", \"study\" : [ \"LERNEN\", \"STUDIEREN\" ] }}}";
@ -208,6 +232,7 @@ CuSuite *get_jsonconf_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_keywords); SUITE_ADD_TEST(suite, test_keywords);
SUITE_ADD_TEST(suite, test_skills);
SUITE_ADD_TEST(suite, test_directions); SUITE_ADD_TEST(suite, test_directions);
SUITE_ADD_TEST(suite, test_ships); SUITE_ADD_TEST(suite, test_ships);
SUITE_ADD_TEST(suite, test_buildings); SUITE_ADD_TEST(suite, test_buildings);