Merge pull request #294 from ennorehling/feature/json-prefixes

JSON Prefixes
This commit is contained in:
Enno Rehling 2015-09-12 14:26:46 +02:00
commit c6fb869ae2
39 changed files with 264 additions and 123 deletions

View File

@ -1,6 +1,7 @@
{
"include": [
"keywords.json",
"prefixes.json",
"e2/terrains.json"
],
"settings": {

View File

@ -16,7 +16,6 @@
<xi:include href="config://core/common/potions.xml" />
<xi:include href="config://core/spoils.xml"/>
<xi:include href="config://game/races.xml"/>
<xi:include href="config://core/prefixes.xml"/>
<xi:include href="config://core/ships.xml"/>
<xi:include href="config://core/common/buildings.xml"/>
<xi:include href="config://game/familiars.xml"/>

View File

@ -1,6 +1,7 @@
{
"include": [
"keywords.json",
"prefixes.json",
"e3/terrains.json"
],
"settings": {

View File

@ -6,7 +6,6 @@
<xi:include href="config://core/common/items.xml"/>
<xi:include href="config://core/common/herbs.xml" />
<xi:include href="config://core/spoils.xml"/>
<xi:include href="config://core/prefixes.xml"/>
<xi:include href="config://core/common/buildings.xml"/>
<xi:include href="config://game/familiars.xml"/>

View File

@ -1,7 +1,8 @@
{
"include": [
"keywords.json",
"e3/terrains.xml"
"prefixes.json",
"e3/terrains.json"
],
"settings": {
"game.id": 4,

View File

@ -6,7 +6,6 @@
<xi:include href="config://core/common/items.xml"/>
<xi:include href="config://core/common/herbs.xml" />
<xi:include href="config://core/spoils.xml"/>
<xi:include href="config://core/prefixes.xml"/>
<xi:include href="config://core/common/buildings.xml"/>
<xi:include href="config://game/familiars.xml"/>

33
conf/prefixes.json Normal file
View File

@ -0,0 +1,33 @@
{
"prefixes": [
"Dunkel",
"Licht",
"Klein",
"Hoch",
"Huegel",
"Berg",
"Wald",
"Sumpf",
"Schnee",
"Sonnen",
"Mond",
"See",
"Tal",
"Schatten",
"Hoehlen",
"Blut",
"Wild",
"Chaos",
"Nacht",
"Nebel",
"Grau",
"Frost",
"Finster",
"Duester",
"flame",
"ice",
"star",
"black",
"arch"
]
}

View File

@ -6833,6 +6833,11 @@
<text locale="en">black </text>
</string>
<string name="arch">
<text locale="de">Erz</text>
<text locale="en">arch</text>
</string>
<string name="star">
<text locale="de">Sternen</text>
<text locale="en">star </text>

View File

@ -19,6 +19,7 @@ cd $ROOT
$ROOT/$BUILD/eressea/eressea -v0 scripts/run-tests.lua
$ROOT/$BUILD/eressea/eressea -v0 scripts/run-tests-e2.lua
$ROOT/$BUILD/eressea/eressea -v0 scripts/run-tests-e3.lua
$ROOT/$BUILD/eressea/eressea -v0 scripts/run-tests-e4.lua
rm -rf data reports orders.txt
cd $OLDWPD

View File

@ -15,6 +15,7 @@ require 'eressea'
require 'eressea.xmlconf'
require 'eressea.path'
require 'tests.e2'
require 'tests.xmas'
require 'lunit'
rules = require('eressea.' .. config.rules)

View File

@ -15,6 +15,7 @@ require 'eressea'
require 'eressea.path'
require 'eressea.xmlconf'
require 'tests.e3'
require 'tests.xmas'
require 'lunit'
eressea.settings.set("rules.alliances", "0")

23
scripts/run-tests-e4.lua Normal file
View File

@ -0,0 +1,23 @@
-- Tests that work in E3. With game config of E3.
-- Tests are under scripts/test/e3 and all files must be in scripts/test/e3/init.lua
path = 'scripts'
if config.install then
path = config.install .. '/' .. path
package.path = package.path .. ';' .. config.install .. '/lunit/?.lua'
--needed to find lunit if not run form eressea root. Needs right [lua] install setting in eressea.ini (point to eressea root from the start folder)
end
package.path = package.path .. ';' .. path .. '/?.lua;' .. path .. '/?/init.lua'
config.rules = 'e4'
require 'eressea'
require 'eressea.path'
require 'eressea.xmlconf'
require 'tests.e3'
require 'lunit'
eressea.settings.set("rules.alliances", "0")
rules = require('eressea.' .. config.rules)
result = lunit.main()
return result.errors + result.failed

View File

@ -1085,3 +1085,35 @@ function test_parser()
os.remove(filename)
assert_equal("Goldene Herde", u.name)
end
local function set_order(u, str)
u:clear_orders()
u:add_order(str)
end
function test_prefix()
local r0 = region.create(0, 0, "plain")
local f1 = faction.create("noreply@eressea.de", "human", "de")
local u1 = unit.create(f1, r0, 1)
set_order(u1, "PRAEFIX See")
process_orders()
assert_not_nil(u1:show():find("Seemensch"))
u1.race = "elf"
assert_not_nil(u1:show():find("Seeelf"))
set_order(u1, "PRAEFIX Mond")
process_orders()
assert_not_nil(u1:show():find("Mondelf"))
set_order(u1, "PRAEFIX")
process_orders()
assert_not_nil(u1:show():find("Elf"))
set_order(u1, "PRAEFIX Erz")
process_orders()
assert_not_nil(u1:show():find("Erzelf"))
u1.faction.locale = "en"
assert_not_nil(u1:show():find("archelf"))
end

View File

@ -199,27 +199,6 @@ function test_seecast()
assert_equal(8, u2.region.x)
end
local function use_tree(terrain)
local r = region.create(0,0, terrain)
local f = faction.create("noreply@eressea.de", "human", "de")
local u1 = unit.create(f, r, 5)
r:set_resource("tree", 0)
u1:add_item("xmastree", 1)
u1:clear_orders()
u1:add_order("BENUTZEN 1 Weihnachtsbaum")
process_orders()
return r
end
function test_xmastree()
local r
r = use_tree("ocean")
assert_equal(0, r:get_resource("tree"))
eressea.free_game()
r = use_tree("plain")
assert_equal(10, r:get_resource("tree"))
end
function test_fishing()
eressea.settings.set("rules.food.flags", "0")
local r = region.create(0,0, "ocean")

20
scripts/tests/xmas.lua Normal file
View File

@ -0,0 +1,20 @@
local function use_tree(terrain)
local r = region.create(0,0, terrain)
local f = faction.create("noreply@eressea.de", "human", "de")
local u1 = unit.create(f, r, 5)
r:set_resource("tree", 0)
u1:add_item("xmastree", 1)
u1:clear_orders()
u1:add_order("BENUTZEN 1 Weihnachtsbaum")
process_orders()
return r
end
function test_xmastree()
local r
r = use_tree("ocean")
assert_equal(0, r:get_resource("tree"))
eressea.free_game()
r = use_tree("plain")
assert_equal(10, r:get_resource("tree"))
end

View File

@ -77,6 +77,7 @@ TOLUA_BINDING(settings.pkg bind_settings.h)
ENDIF()
set (ERESSEA_SRC
calendar.c
move.c
spells.c
battle.c
@ -86,6 +87,7 @@ set (ERESSEA_SRC
names.c
lighthouse.c
reports.c
prefix.c
donations.c
seen.c
eressea.c
@ -197,6 +199,7 @@ set(TESTS_SRC
magic.test.c
market.test.c
move.test.c
prefix.test.c
skill.test.c
spells.test.c
spy.test.c

View File

@ -26,6 +26,7 @@ without prior permission by the authors of Eressea.
#include "console.h"
#include "reports.h"
#include "seen.h"
#include "calendar.h"
#include <kernel/config.h>
@ -33,7 +34,6 @@ without prior permission by the authors of Eressea.
#include <kernel/building.h>
#include <kernel/curse.h>
#include <kernel/equipment.h>
#include <kernel/calendar.h>
#include <kernel/unit.h>
#include <kernel/terrain.h>
#include <kernel/messages.h>

View File

@ -32,10 +32,10 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "monster.h"
#include "morale.h"
#include "reports.h"
#include "calendar.h"
/* kernel includes */
#include <kernel/building.h>
#include <kernel/calendar.h>
#include <kernel/curse.h>
#include <kernel/equipment.h>
#include <kernel/faction.h>

View File

@ -18,6 +18,7 @@
#include "console.h"
#include "listbox.h"
#include "wormhole.h"
#include "calendar.h"
#include <modules/xmas.h>
#include <modules/gmcmd.h>
@ -30,7 +31,6 @@
#include <modules/autoseed.h>
#include <kernel/building.h>
#include <kernel/calendar.h>
#include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/plane.h>

View File

@ -29,7 +29,6 @@ alliance.c
ally.c
build.c
building.c
calendar.c
command.c
config.c
connection.c

View File

@ -74,6 +74,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <util/xml.h>
#include "donations.h"
#include "prefix.h"
#ifdef USE_LIBXML2
/* libxml includes */

View File

@ -31,6 +31,9 @@ without prior permission by the authors of Eressea.
#include "spellbook.h"
#include "calendar.h"
/* game modules */
#include "prefix.h"
/* util includes */
#include <util/attrib.h>
#include <util/bsdstring.h>
@ -487,6 +490,17 @@ static void json_race(cJSON *json, race *rc) {
}
}
static void json_prefixes(cJSON *json) {
cJSON *child;
if (json->type != cJSON_Array) {
log_error("prefixes is not a json array: %d", json->type);
return;
}
for (child = json->child; child; child = child->next) {
add_raceprefix(child->valuestring);
}
}
static void json_terrains(cJSON *json) {
cJSON *child;
if (json->type != cJSON_Object) {
@ -837,6 +851,9 @@ void json_config(cJSON *json) {
else if (strcmp(child->string, "spells") == 0) {
json_spells(child);
}
else if (strcmp(child->string, "prefixes") == 0) {
json_prefixes(child);
}
else if (strcmp(child->string, "terrains") == 0) {
json_terrains(child);
init_terrains();

View File

@ -12,7 +12,11 @@
#include "spell.h"
#include "order.h"
#include "terrain.h"
#include "prefix.h"
#include "util/language.h"
#include <CuTest.h>
#include <cJSON.h>
#include <tests.h>
@ -77,6 +81,24 @@ static void test_settings(CuTest * tc)
test_cleanup();
}
static void test_prefixes(CuTest * tc)
{
const char * data = "{\"prefixes\": [ "
"\"snow\","
"\"sea\","
"\"dark\""
"]}";
cJSON *json = cJSON_Parse(data);
test_cleanup();
json_config(json);
CuAssertPtrNotNull(tc, race_prefixes);
CuAssertStrEquals(tc, "snow", race_prefixes[0]);
CuAssertStrEquals(tc, "dark", race_prefixes[2]);
CuAssertPtrEquals(tc, 0, race_prefixes[3]);
test_cleanup();
}
static void test_races(CuTest * tc)
{
const char * data = "{\"races\": { \"orc\" : { "
@ -553,6 +575,7 @@ CuSuite *get_jsonconf_suite(void)
SUITE_ADD_TEST(suite, test_spells);
SUITE_ADD_TEST(suite, test_flags);
SUITE_ADD_TEST(suite, test_settings);
SUITE_ADD_TEST(suite, test_prefixes);
SUITE_ADD_TEST(suite, test_infinitive_from_config);
return suite;
}

View File

@ -212,22 +212,6 @@ bool allowed_dragon(const region * src, const region * target)
return allowed_fly(src, target);
}
char **race_prefixes = NULL;
extern void add_raceprefix(const char *prefix)
{
static size_t size = 4;
static unsigned int next = 0;
if (race_prefixes == NULL)
race_prefixes = malloc(size * sizeof(char *));
if (next + 1 == size) {
size *= 2;
race_prefixes = realloc(race_prefixes, size * sizeof(char *));
}
race_prefixes[next++] = _strdup(prefix);
race_prefixes[next] = NULL;
}
bool r_insectstalled(const region * r)
{
return fval(r->terrain, ARCTIC_REGION);

View File

@ -249,9 +249,6 @@ extern "C" {
extern bool r_insectstalled(const struct region *r);
extern void add_raceprefix(const char *);
extern char **race_prefixes;
extern void write_race_reference(const struct race *rc,
struct storage *store);
extern variant read_race_reference(struct storage *store);

View File

@ -28,6 +28,7 @@ without prior permission by the authors of Eressea.
#include "spell.h"
#include "spellbook.h"
#include "calendar.h"
#include "prefix.h"
#include "vortex.h"
@ -2051,38 +2052,6 @@ static int parse_strings(xmlDocPtr doc)
return 0;
}
static void
xml_readprefixes(xmlXPathContextPtr xpath, xmlNodePtr * nodeTab, int nodeNr,
bool names)
{
int i;
for (i = 0; i != nodeNr; ++i) {
xmlNodePtr node = nodeTab[i];
xmlChar *propText = xmlNodeListGetString(node->doc, node->children, 1);
if (propText != NULL) {
add_raceprefix((const char *)propText);
xmlFree(propText);
}
}
}
static int parse_prefixes(xmlDocPtr doc)
{
xmlXPathContextPtr xpath = xmlXPathNewContext(doc);
xmlXPathObjectPtr strings;
/* reading eressea/strings/string */
strings = xmlXPathEvalExpression(BAD_CAST "/eressea/prefixes/prefix", xpath);
xml_readprefixes(xpath, strings->nodesetval->nodeTab,
strings->nodesetval->nodeNr, false);
xmlXPathFreeObject(strings);
xmlXPathFreeContext(xpath);
return 0;
}
static int parse_main(xmlDocPtr doc)
{
xmlXPathContextPtr xpath = xmlXPathNewContext(doc);
@ -2157,7 +2126,6 @@ void register_xmlreader(void)
xml_register_callback(parse_main);
xml_register_callback(parse_strings);
xml_register_callback(parse_prefixes);
xml_register_callback(parse_messages);
xml_register_callback(parse_resources);
xml_register_callback(parse_rules);

View File

@ -35,6 +35,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "spy.h"
#include "study.h"
#include "wormhole.h"
#include "prefix.h"
#include "calendar.h"
/* kernel includes */
#include <kernel/alliance.h>
@ -42,7 +44,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <kernel/connection.h>
#include <kernel/curse.h>
#include <kernel/building.h>
#include <kernel/calendar.h>
#include <kernel/faction.h>
#include <kernel/group.h>
#include <kernel/item.h>
@ -846,38 +847,6 @@ void demographics(void)
/* ------------------------------------------------------------- */
static int modify(int i)
{
int c;
c = i * 2 / 3;
if (c >= 1) {
return (c + rng_int() % c);
}
else {
return (i);
}
}
static void inactivefaction(faction * f)
{
FILE *inactiveFILE;
char zText[128];
sprintf(zText, "%s/%s", datapath(), "inactive");
inactiveFILE = fopen(zText, "a");
if (inactiveFILE) {
fprintf(inactiveFILE, "%s:%s:%d:%d\n",
factionid(f),
LOC(default_locale, rc_name_s(f->race, NAME_PLURAL)),
modify(count_all(f)), turn - f->lastorders);
fclose(inactiveFILE);
}
}
/* test if the unit can slip through a siege undetected.
* returns 0 if siege is successful, or 1 if the building is either
* not besieged or the unit can slip through the siege due to better stealth.
@ -1278,11 +1247,6 @@ static void remove_idle_players(void)
sprintf(info, "%d Einheiten, %d Personen, %d Silber",
f->no_units, f->num_total, f->money);
}
if (NMRTimeout() > 0 && turn - f->lastorders >= (NMRTimeout() - 1)) {
inactivefaction(f);
continue;
}
}
log_info(" - beseitige Spieler, die sich nach der Anmeldung nicht gemeldet haben...");
@ -1468,7 +1432,7 @@ static void init_prefixnames(void)
in->next = pnames;
in->lang = lang;
if (!exist) {
if (!exist && race_prefixes) {
int key;
for (key = 0; race_prefixes[key]; ++key) {
variant var;

View File

@ -30,7 +30,6 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <kernel/build.h>
#include <kernel/building.h>
#include <kernel/calendar.h>
#include <kernel/connection.h>
#include <kernel/curse.h>
#include <kernel/faction.h>
@ -49,6 +48,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <kernel/unit.h>
#include "direction.h"
#include "calendar.h"
#include "skill.h"
/* util includes */

38
src/prefix.c Normal file
View File

@ -0,0 +1,38 @@
#include <platform.h>
#include "prefix.h"
#include <assert.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
char **race_prefixes = NULL;
static size_t size = 4;
static unsigned int next = 0;
void add_raceprefix(const char *prefix)
{
assert(prefix);
if (race_prefixes == NULL) {
next = 0;
size = 4;
race_prefixes = malloc(size * sizeof(char *));
}
if (next + 1 == size) {
size *= 2;
race_prefixes = realloc(race_prefixes, size * sizeof(char *));
}
race_prefixes[next++] = _strdup(prefix);
race_prefixes[next] = NULL;
}
void free_prefixes(void) {
int i;
if (race_prefixes) {
for (i = 0; race_prefixes[i]; ++i) {
free(race_prefixes[i]);
}
free(race_prefixes);
race_prefixes = 0;
}
}

17
src/prefix.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#ifndef PREFIX_H
#define PREFIX_H
#ifdef __cplusplus
extern "C" {
#endif
void add_raceprefix(const char *);
char **race_prefixes; // zero-terminated array of valid prefixes
void free_prefixes(void);
#ifdef __cplusplus
}
#endif
#endif

29
src/prefix.test.c Normal file
View File

@ -0,0 +1,29 @@
#include "prefix.h"
#include <tests.h>
#include <stddef.h>
#include <CuTest.h>
static void test_add_prefix(CuTest *tc) {
test_cleanup();
CuAssertPtrEquals(tc, 0, race_prefixes);
add_raceprefix("sea");
CuAssertPtrNotNull(tc, race_prefixes);
CuAssertStrEquals(tc, "sea", race_prefixes[0]);
CuAssertPtrEquals(tc, 0, race_prefixes[1]);
add_raceprefix("moon");
CuAssertStrEquals(tc, "sea", race_prefixes[0]);
CuAssertStrEquals(tc, "moon", race_prefixes[1]);
CuAssertPtrEquals(tc, 0, race_prefixes[2]);
free_prefixes();
CuAssertPtrEquals(tc, 0, race_prefixes);
test_cleanup();
}
CuSuite *get_prefix_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_add_prefix);
return suite;
}

View File

@ -41,13 +41,13 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "move.h"
#include "upkeep.h"
#include "vortex.h"
#include "calendar.h"
/* kernel includes */
#include <kernel/ally.h>
#include <kernel/connection.h>
#include <kernel/build.h>
#include <kernel/building.h>
#include <kernel/calendar.h>
#include <kernel/curse.h>
#include <kernel/faction.h>
#include <kernel/group.h>

View File

@ -15,9 +15,9 @@
#include "summary.h"
#include "laws.h"
#include "monster.h"
#include "calendar.h"
#include <kernel/alliance.h>
#include <kernel/calendar.h>
#include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/race.h>

View File

@ -80,6 +80,7 @@ int RunAllTests(void)
RUN_TESTS(suite, ally);
RUN_TESTS(suite, messages);
/* gamecode */
RUN_TESTS(suite, prefix);
RUN_TESTS(suite, battle);
RUN_TESTS(suite, donations);
RUN_TESTS(suite, travelthru);

View File

@ -2,6 +2,7 @@
#include "tests.h"
#include "keyword.h"
#include "seen.h"
#include "prefix.h"
#include <kernel/config.h>
#include <kernel/region.h>
@ -84,6 +85,7 @@ void test_cleanup(void)
free_spellbooks();
free_gamedata();
free_seen();
free_prefixes();
mt_clear();
if (!mt_find("missing_message")) {
mt_register(mt_new_va("missing_message", "name:string", 0));

2
tests/data/inactive Normal file
View File

@ -0,0 +1,2 @@
c93c:Menschen:1:4
c93c:Menschen:1:4

View File

@ -5,5 +5,6 @@ SET SERVER=%BUILD%\eressea.exe
%SERVER% ..\scripts\run-tests.lua
%SERVER% ..\scripts\run-tests-e2.lua
%SERVER% ..\scripts\run-tests-e3.lua
%SERVER% ..\scripts\run-tests-e4.lua
PAUSE
RMDIR /s /q reports