disable features by name that are not keywords

This commit is contained in:
Enno Rehling 2015-09-12 17:37:29 +02:00
parent e80f53d35e
commit a01955e06a
9 changed files with 45 additions and 30 deletions

View File

@ -4,8 +4,9 @@
"prefixes.json",
"e2/terrains.json"
],
"disable": [
"pay"
"disabled": [
"pay",
"jsreport"
],
"settings": {
"game.id": 2,

View File

@ -4,7 +4,7 @@
"prefixes.json",
"e3/terrains.json"
],
"disable": [
"disabled": [
"besiege",
"steal",
"buy",
@ -13,7 +13,8 @@
"spy",
"tax",
"entertain",
"sell"
"sell",
"jsreport"
],
"settings": {
"game.id": 3,

View File

@ -4,7 +4,7 @@
"prefixes.json",
"e3/terrains.json"
],
"disable": [
"disabled": [
"besiege",
"steal",
"buy",
@ -13,7 +13,8 @@
"spy",
"tax",
"entertain",
"sell"
"sell",
"jsreport"
],
"settings": {
"game.id": 4,

View File

@ -1,7 +1,7 @@
local pkg = {}
function pkg.init()
eressea.settings.set("feature.jsreport.enable", "1")
eressea.settings.set("jsreport.enabled", "1")
end
function pkg.update()

View File

@ -25,7 +25,7 @@ static void coor_from_tiled(int *x, int *y) {
static int report_json(const char *filename, report_context * ctx, const char *charset)
{
if (get_param_int(global.parameters, "feature.jsreport.enable", 0) != 0) {
if (get_param_int(global.parameters, "jsreport.enabled", 0) != 0) {
FILE * F = fopen(filename, "w");
if (F) {
int x, y, minx = INT_MAX, maxx = INT_MIN, miny = INT_MAX, maxy = INT_MIN;

View File

@ -501,14 +501,38 @@ static void json_prefixes(cJSON *json) {
}
}
static void json_disable_keywords(cJSON *json) {
/** disable a feature.
* features are identified by eone of:
* 1. the keyword for their orders,
* 2. the name of the skill they use,
* 3. a "module.enabled" flag in the settings
*/
static void disable_feature(const char *str) {
// FIXME: this is slower than balls.
int k;
for (k = 0; k != MAXKEYWORDS; ++k) {
if (strcmp(keywords[k], str) == 0) {
log_info("disable keyword %s\n", str);
enable_keyword(k, false);
break;
}
}
if (k == MAXKEYWORDS) {
char name[32];
_snprintf(name, sizeof(name), "%s.enabled", str);
log_info("disable feature %s\n", name);
set_param(&global.parameters, name, "0");
}
}
static void json_disable_features(cJSON *json) {
cJSON *child;
if (json->type != cJSON_Array) {
log_error("disable is not a json array: %d", json->type);
log_error("disabled is not a json array: %d", json->type);
return;
}
for (child = json->child; child; child = child->next) {
disable_keyword_str(child->valuestring);
disable_feature(child->valuestring);
}
}
@ -865,8 +889,8 @@ void json_config(cJSON *json) {
else if (strcmp(child->string, "prefixes") == 0) {
json_prefixes(child);
}
else if (strcmp(child->string, "disable") == 0) {
json_disable_keywords(child);
else if (strcmp(child->string, "disabled") == 0) {
json_disable_features(child);
}
else if (strcmp(child->string, "terrains") == 0) {
json_terrains(child);

View File

@ -101,9 +101,10 @@ static void test_prefixes(CuTest * tc)
static void test_disable(CuTest * tc)
{
const char * data = "{\"disable\": [ "
const char * data = "{\"disabled\": [ "
"\"pay\","
"\"besiege\""
"\"besiege\","
"\"module\""
"]}";
cJSON *json = cJSON_Parse(data);
@ -111,10 +112,12 @@ static void test_disable(CuTest * tc)
CuAssertTrue(tc, !keyword_disabled(K_BANNER));
CuAssertTrue(tc, !keyword_disabled(K_PAY));
CuAssertTrue(tc, !keyword_disabled(K_BESIEGE));
CuAssertIntEquals(tc, 1, get_param_int(global.parameters, "module.enabled", 1));
json_config(json);
CuAssertTrue(tc, !keyword_disabled(K_BANNER));
CuAssertTrue(tc, keyword_disabled(K_PAY));
CuAssertTrue(tc, keyword_disabled(K_BESIEGE));
CuAssertIntEquals(tc, 0, get_param_int(global.parameters, "module.enabled", 1));
test_cleanup();
}

View File

@ -74,20 +74,6 @@ keyword_t get_keyword(const char *s, const struct locale *lang) {
static bool disabled_kwd[MAXKEYWORDS];
void disable_keyword_str(const char *str) {
// FIXME: this is slower than balls.
int k;
for (k = 0; k != MAXKEYWORDS; ++k) {
if (strcmp(keywords[k], str) == 0) {
enable_keyword(k, false);
break;
}
}
if (k == MAXKEYWORDS) {
log_error("trying to disable unknown command %s\n", str);
}
}
void enable_keyword(keyword_t kwd, bool enabled) {
assert(kwd < MAXKEYWORDS);
disabled_kwd[kwd] = !enabled;

View File

@ -81,7 +81,6 @@ extern "C"
void init_keyword(const struct locale *lang, keyword_t kwd, const char *str);
bool keyword_disabled(keyword_t kwd);
void enable_keyword(keyword_t kwd, bool enabled);
void disable_keyword_str(const char *str);
const char *keyword(keyword_t kwd);
// #define keyword(kwd) mkname("keyword", keywords[kwd])