remove some unused code from alliances

start writing test coverage
This commit is contained in:
Enno Rehling 2016-04-08 19:45:27 +02:00
parent 108b4404d2
commit 80b14048be
8 changed files with 72 additions and 108 deletions

View File

@ -2,26 +2,26 @@ project(kernel C)
SET(_TEST_FILES SET(_TEST_FILES
alliance.test.c alliance.test.c
build.test.c
config.test.c
group.test.c
faction.test.c
unit.test.c
save.test.c
ship.test.c
spell.test.c
ally.test.c ally.test.c
build.test.c
building.test.c building.test.c
equipment.test.c command.test.c
config.test.c
curse.test.c curse.test.c
equipment.test.c
faction.test.c
group.test.c
item.test.c item.test.c
messages.test.c
order.test.c order.test.c
pool.test.c pool.test.c
race.test.c race.test.c
save.test.c
ship.test.c
spellbook.test.c spellbook.test.c
curse.test.c spell.test.c
unit.test.c
jsonconf.test.c jsonconf.test.c
messages.test.c
) )
SET(_FILES SET(_FILES

View File

@ -314,6 +314,15 @@ static void execute(const struct syntaxtree *syntax, keyword_t kwd)
} }
} }
const char* alliance_kwd[ALLIANCE_MAX] = {
"kick",
"leave",
"command",
"new",
"invite",
"join"
};
void alliance_cmd(void) void alliance_cmd(void)
{ {
static syntaxtree *stree = NULL; static syntaxtree *stree = NULL;
@ -321,35 +330,21 @@ void alliance_cmd(void)
syntaxtree *slang = stree = stree_create(); syntaxtree *slang = stree = stree_create();
while (slang) { while (slang) {
void *leaf = 0; void *leaf = 0;
add_command(&leaf, NULL, LOC(slang->lang, "new"), &cmd_new); add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_KICK]), &cmd_kick);
add_command(&leaf, NULL, LOC(slang->lang, "invite"), &cmd_invite); add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_LEAVE]), &cmd_leave);
add_command(&leaf, NULL, LOC(slang->lang, "join"), &cmd_join); add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_TRANSFER]), &cmd_transfer);
add_command(&leaf, NULL, LOC(slang->lang, "kick"), &cmd_kick); add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_NEW]), &cmd_new);
add_command(&leaf, NULL, LOC(slang->lang, "leave"), &cmd_leave); add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_INVITE]), &cmd_invite);
add_command(&leaf, NULL, LOC(slang->lang, "command"), &cmd_transfer); add_command(&leaf, NULL, LOC(slang->lang, alliance_kwd[ALLIANCE_JOIN]), &cmd_join);
slang->root = leaf; slang->root = leaf;
slang = slang->next; slang = slang->next;
} }
} }
execute(stree, K_ALLIANCE); execute(stree, K_ALLIANCE);
stree_free(stree);
/* some may have been kicked, must remove f->alliance==NULL */ /* some may have been kicked, must remove f->alliance==NULL */
} }
void alliancejoin(void)
{
static syntaxtree *stree = NULL;
if (stree == NULL) {
syntaxtree *slang = stree = stree_create();
while (slang) {
void *leaf = 0;
add_command(&leaf, NULL, LOC(slang->lang, "join"), &cmd_join);
add_command(&slang->root, leaf, LOC(slang->lang, "alliance"), NULL);
slang = slang->next;
}
}
execute(stree, K_ALLIANCE);
}
void setalliance(faction * f, alliance * al) void setalliance(faction * f, alliance * al)
{ {
if (f->alliance == al) if (f->alliance == al)
@ -442,82 +437,6 @@ void alliancevictory(void)
} }
} }
int victorycondition(const alliance * al, const char *name)
{
const char *gems[] =
{ "opal", "diamond", "zaphire", "topaz", "beryl", "agate", "garnet",
"emerald", NULL };
if (strcmp(name, "gems") == 0) {
const char **igem;
for (igem = gems; *igem; ++igem) {
const struct resource_type *rtype = rt_find(*igem);
quicklist *flist = al->members;
int qi;
bool found = false;
assert(rtype);
for (qi = 0; flist && !found; ql_advance(&flist, &qi, 1)) {
faction *f = (faction *)ql_get(flist, 0);
unit *u;
for (u = f->units; u; u = u->nextF) {
if (i_get(u->items, rtype->itype) > 0) {
found = true;
break;
}
}
}
if (!found)
return 0;
}
return 1;
}
else if (strcmp(name, "phoenix") == 0) {
quicklist *flist = al->members;
int qi;
for (qi = 0; flist; ql_advance(&flist, &qi, 1)) {
faction *f = (faction *)ql_get(flist, qi);
if (key_get(f->attribs, atoi36("phnx"))) {
return 1;
}
}
return 0;
}
else if (strcmp(name, "pyramid") == 0) {
/* Logik:
* - if (pyr > last_passed_size && pyr > all_others) {
* pyr->passed->counter++;
* for(all_other_pyrs) {
* pyr->passed->counter=0;
* }
*
* if(pyr->passed->counter >= 3) {
* set(pyr, passed);
* pyr->owner->set_attrib(pyra);
* }
* last_passed_size = pyr->size;
* }
*/
quicklist *flist = al->members;
int qi;
for (qi = 0; flist; ql_advance(&flist, &qi, 1)) {
faction *f = (faction *)ql_get(flist, qi);
if (key_get(f->attribs, atoi36("pyra"))) {
return 1;
}
}
return 0;
}
return -1;
}
void alliance_setname(alliance * self, const char *name) void alliance_setname(alliance * self, const char *name)
{ {
free(self->name); free(self->name);

View File

@ -38,6 +38,7 @@ extern "C" {
ALLIANCE_MAX ALLIANCE_MAX
}; };
extern const char* alliance_kwd[ALLIANCE_MAX];
#define ALF_NON_ALLIED (1<<0) /* this alliance is just a default for a non-allied faction */ #define ALF_NON_ALLIED (1<<0) /* this alliance is just a default for a non-allied faction */
#define ALLY_ENEMY (1<<0) #define ALLY_ENEMY (1<<0)

View File

@ -1,6 +1,9 @@
#include <platform.h> #include <platform.h>
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/unit.h>
#include <kernel/order.h>
#include <util/base36.h>
#include "alliance.h" #include "alliance.h"
#include <CuTest.h> #include <CuTest.h>
#include <tests.h> #include <tests.h>
@ -83,12 +86,37 @@ static void test_alliance_dead_faction(CuTest *tc) {
test_cleanup(); test_cleanup();
} }
static void test_alliance_cmd(CuTest *tc) {
unit *u1, *u2;
struct region *r;
test_cleanup();
r = test_create_region(0, 0, 0);
u1 = test_create_unit(test_create_faction(0), r);
u2 = test_create_unit(test_create_faction(0), r);
u1->orders = create_order(K_ALLIANCE, u1->faction->locale, "%s %s", alliance_kwd[ALLIANCE_NEW], itoa36(42));
// TODO: INVITE
u2->orders = create_order(K_ALLIANCE, u1->faction->locale, "%s %s", alliance_kwd[ALLIANCE_JOIN], itoa36(42));
CuAssertTrue(tc, is_allied(u1->faction, u1->faction));
CuAssertTrue(tc, !is_allied(u1->faction, u2->faction));
alliance_cmd();
CuAssertPtrNotNull(tc, u1->faction->alliance);
CuAssertIntEquals(tc, 42, u1->faction->alliance->id);
CuAssertPtrNotNull(tc, u1->faction->alliance->members);
CuAssertPtrEquals(tc, u1->faction, alliance_get_leader(u1->faction->alliance));
CuAssertPtrEquals(tc, u1->faction->alliance, findalliance(42));
// CuAssertTrue(tc, is_allied(u1->faction, u1->faction));
// CuAssertPtrNotNull(tc, u2->faction->alliance);
test_cleanup();
}
CuSuite *get_alliance_suite(void) CuSuite *get_alliance_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_alliance_dead_faction); SUITE_ADD_TEST(suite, test_alliance_dead_faction);
SUITE_ADD_TEST(suite, test_alliance_make); SUITE_ADD_TEST(suite, test_alliance_make);
SUITE_ADD_TEST(suite, test_alliance_join); SUITE_ADD_TEST(suite, test_alliance_join);
SUITE_ADD_TEST(suite, test_alliance_cmd);
return suite; return suite;
} }

View File

@ -43,6 +43,15 @@ void *stree_find(const syntaxtree * stree, const struct locale *lang)
return NULL; return NULL;
} }
void stree_free(syntaxtree *stree) {
while (stree) {
syntaxtree *snext = stree->next;
freetokens(stree->root);
free(stree);
stree = snext;
}
}
syntaxtree *stree_create(void) syntaxtree *stree_create(void)
{ {
syntaxtree *sroot = NULL; syntaxtree *sroot = NULL;
@ -65,6 +74,7 @@ const char *str, parser fun)
command *cmd = (command *)malloc(sizeof(command)); command *cmd = (command *)malloc(sizeof(command));
variant var; variant var;
assert(str);
cmd->fun = fun; cmd->fun = fun;
cmd->nodes = tnext; cmd->nodes = tnext;
var.v = cmd; var.v = cmd;

View File

@ -32,6 +32,7 @@ extern "C" {
void do_command(const void *troot, struct unit *u, struct order *); void do_command(const void *troot, struct unit *u, struct order *);
struct syntaxtree *stree_create(void); struct syntaxtree *stree_create(void);
void stree_free(struct syntaxtree *);
void *stree_find(const struct syntaxtree *stree, void *stree_find(const struct syntaxtree *stree,
const struct locale *lang); const struct locale *lang);

View File

@ -86,6 +86,7 @@ int RunAllTests(int argc, char *argv[])
ADD_SUITE(xerewards); ADD_SUITE(xerewards);
/* kernel */ /* kernel */
ADD_SUITE(alliance); ADD_SUITE(alliance);
ADD_SUITE(command);
ADD_SUITE(unit); ADD_SUITE(unit);
ADD_SUITE(faction); ADD_SUITE(faction);
ADD_SUITE(group); ADD_SUITE(group);

View File

@ -6,6 +6,7 @@
#include "reports.h" #include "reports.h"
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/alliance.h>
#include <kernel/plane.h> #include <kernel/plane.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/terrain.h> #include <kernel/terrain.h>
@ -78,6 +79,9 @@ struct locale * test_create_locale(void) {
if (!locale_getstring(loc, mkname("skill", skillnames[i]))) if (!locale_getstring(loc, mkname("skill", skillnames[i])))
locale_setstring(loc, mkname("skill", skillnames[i]), skillnames[i]); locale_setstring(loc, mkname("skill", skillnames[i]), skillnames[i]);
} }
for (i = 0; i != ALLIANCE_MAX; ++i) {
locale_setstring(loc, alliance_kwd[i], alliance_kwd[i]);
}
for (i = 0; i != MAXDIRECTIONS; ++i) { for (i = 0; i != MAXDIRECTIONS; ++i) {
locale_setstring(loc, directions[i], directions[i]); locale_setstring(loc, directions[i], directions[i]);
init_direction(loc, i, directions[i]); init_direction(loc, i, directions[i]);