begin configuring spells from JSON

This commit is contained in:
Enno Rehling 2014-07-02 21:15:22 -07:00
parent ee1d97df19
commit 91a776c0ac
2 changed files with 56 additions and 2 deletions

View File

@ -345,6 +345,35 @@ static void json_buildings(cJSON *json) {
} }
} }
static void json_spells(cJSON *json) {
cJSON *child;
if (json->type != cJSON_Object) {
log_error_n("spells is not a json object: %d", json->type);
return;
}
for (child = json->child; child; child = child->next) {
if (child->type == cJSON_Object) {
spell *sp;
cJSON * item = cJSON_GetObjectItem(child, "index");
sp = create_spell(child->string, item ? item->valueint : 0);
for (item = child->child; item; item = item->next) {
if (strcmp(item->string, "index") == 0) {
continue;
}
else if (strcmp(item->string, "cast") == 0) {
sp->cast = (spell_f)get_function(item->valuestring);
}
else if (strcmp(item->string, "fumble") == 0) {
sp->fumble = (fumble_f)get_function(item->valuestring);
}
else if (strcmp(item->string, "syntax") == 0) {
sp->syntax = _strdup(item->valuestring);
}
}
}
}
}
static 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) {
@ -563,6 +592,9 @@ void json_config(cJSON *json) {
else if (strcmp(child->string, "buildings") == 0) { else if (strcmp(child->string, "buildings") == 0) {
json_buildings(child); json_buildings(child);
} }
else if (strcmp(child->string, "spells") == 0) {
json_spells(child);
}
else if (strcmp(child->string, "terrains") == 0) { else if (strcmp(child->string, "terrains") == 0) {
json_terrains(child); json_terrains(child);
} else { } else {

View File

@ -8,6 +8,7 @@
#include "keyword.h" #include "keyword.h"
#include "race.h" #include "race.h"
#include "ship.h" #include "ship.h"
#include "spell.h"
#include "terrain.h" #include "terrain.h"
#include "util/language.h" #include "util/language.h"
#include <CuTest.h> #include <CuTest.h>
@ -177,6 +178,26 @@ static void test_castles(CuTest *tc) {
CuAssertPtrEquals(tc, 0, bt->construction->improvement->improvement); CuAssertPtrEquals(tc, 0, bt->construction->improvement->improvement);
} }
static void test_spells(CuTest * tc)
{
const char * data = "{\"spells\": { \"fireball\" : { \"syntax\" : \"u+\" } } }";
cJSON *json = cJSON_Parse(data);
const spell *sp;
test_cleanup();
CuAssertPtrNotNull(tc, json);
CuAssertPtrEquals(tc, 0, find_spell("fireball"));
json_config(json);
sp = find_spell("fireball");
CuAssertPtrNotNull(tc, sp);
CuAssertStrEquals(tc, "u+", sp->syntax);
test_cleanup();
CuAssertPtrEquals(tc, 0, find_spell("fireball"));
}
static void test_buildings(CuTest * tc) static void test_buildings(CuTest * tc)
{ {
const char * data = "{\"buildings\": { \"house\" : { " const char * data = "{\"buildings\": { \"house\" : { "
@ -331,6 +352,7 @@ CuSuite *get_jsonconf_suite(void)
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);
SUITE_ADD_TEST(suite, test_spells);
SUITE_ADD_TEST(suite, test_flags); SUITE_ADD_TEST(suite, test_flags);
return suite; return suite;
} }