forked from github/server
Merge pull request #896 from ennorehling/develop
kill read_orders, rewrite tests to use parseorders.
This commit is contained in:
commit
a92d756449
176
src/orderfile.c
176
src/orderfile.c
|
@ -48,182 +48,6 @@ static void begin_orders(unit *u) {
|
|||
u->orders = NULL;
|
||||
}
|
||||
|
||||
static void unitorders(input *in, faction *f)
|
||||
{
|
||||
int i;
|
||||
unit *u;
|
||||
|
||||
assert(f);
|
||||
|
||||
i = getid();
|
||||
u = findunit(i);
|
||||
|
||||
if (u && u->faction == f) {
|
||||
order **ordp;
|
||||
|
||||
begin_orders(u);
|
||||
ordp = &u->orders;
|
||||
|
||||
for (;;) {
|
||||
const char *s;
|
||||
/* Erst wenn wir sicher sind, dass kein Befehl
|
||||
* eingegeben wurde, checken wir, ob nun eine neue
|
||||
* Einheit oder ein neuer Spieler drankommt */
|
||||
|
||||
s = in->getbuf(in->data);
|
||||
if (s == NULL)
|
||||
break;
|
||||
|
||||
if (s[0]) {
|
||||
if (s[0] != '@') {
|
||||
char token[64];
|
||||
const char *stok = s;
|
||||
stok = parse_token(&stok, token, sizeof(token));
|
||||
|
||||
if (stok) {
|
||||
bool quit = false;
|
||||
param_t param = findparam(stok, u->faction->locale);
|
||||
switch (param) {
|
||||
case P_UNIT:
|
||||
case P_REGION:
|
||||
quit = true;
|
||||
break;
|
||||
case P_FACTION:
|
||||
case P_NEXT:
|
||||
case P_GAMENAME:
|
||||
/* these terminate the orders, so we apply extra checking */
|
||||
if (strlen(stok) >= 3) {
|
||||
quit = true;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
quit = false;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (quit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Nun wird der Befehl erzeut und eingehaengt */
|
||||
*ordp = parse_order(s, u->faction->locale);
|
||||
if (*ordp) {
|
||||
ordp = &(*ordp)->next;
|
||||
}
|
||||
else {
|
||||
ADDMSG(&f->msgs, msg_message("parse_error", "unit command", u, s));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static faction *factionorders(void)
|
||||
{
|
||||
int fid = getid();
|
||||
faction *f = findfaction(fid);
|
||||
|
||||
if (f != NULL && (f->flags & FFL_NPC) == 0) {
|
||||
char token[PASSWORD_MAXSIZE];
|
||||
const char *pass = gettoken(token, sizeof(token));
|
||||
|
||||
if (!checkpasswd(f, (const char *)pass)) {
|
||||
log_debug("Invalid password for faction %s", itoa36(fid));
|
||||
ADDMSG(&f->msgs, msg_message("wrongpasswd", "password", pass));
|
||||
return 0;
|
||||
}
|
||||
/* Die Partei hat sich zumindest gemeldet, so dass sie noch
|
||||
* nicht als untaetig gilt */
|
||||
f->lastorders = turn;
|
||||
|
||||
}
|
||||
else {
|
||||
log_debug("orders for invalid faction %s", itoa36(fid));
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
int read_orders(input *in)
|
||||
{
|
||||
const char *b;
|
||||
int nfactions = 0;
|
||||
struct faction *f = NULL;
|
||||
const struct locale *lang = default_locale;
|
||||
|
||||
/* TODO: recognize UTF8 BOM */
|
||||
b = in->getbuf(in->data);
|
||||
|
||||
/* Auffinden der ersten Partei, und danach abarbeiten bis zur letzten
|
||||
* Partei */
|
||||
|
||||
while (b) {
|
||||
char token[128];
|
||||
param_t p;
|
||||
const char *s;
|
||||
init_tokens_str(b);
|
||||
s = gettoken(token, sizeof(token));
|
||||
p = findparam_block(s, lang, true);
|
||||
switch (p) {
|
||||
case P_GAMENAME:
|
||||
case P_FACTION:
|
||||
f = factionorders();
|
||||
if (f) {
|
||||
lang = f->locale;
|
||||
++nfactions;
|
||||
}
|
||||
|
||||
b = in->getbuf(in->data);
|
||||
break;
|
||||
|
||||
/* in factionorders wird nur eine zeile gelesen:
|
||||
* diejenige mit dem passwort. Die befehle der units
|
||||
* werden geloescht, und die Partei wird als aktiv
|
||||
* vermerkt. */
|
||||
|
||||
case P_UNIT:
|
||||
if (f) {
|
||||
unitorders(in, f);
|
||||
do {
|
||||
b = in->getbuf(in->data);
|
||||
if (!b) {
|
||||
break;
|
||||
}
|
||||
init_tokens_str(b);
|
||||
s = gettoken(token, sizeof(token));
|
||||
p = (s && s[0] != '@') ? findparam(s, lang) : NOPARAM;
|
||||
} while ((p != P_UNIT || !f) && p != P_FACTION && p != P_NEXT
|
||||
&& p != P_GAMENAME);
|
||||
}
|
||||
break;
|
||||
|
||||
/* Falls in unitorders() abgebrochen wird, steht dort entweder eine neue
|
||||
* Partei, eine neue Einheit oder das File-Ende. Das switch() wird erneut
|
||||
* durchlaufen, und die entsprechende Funktion aufgerufen. Man darf buf
|
||||
* auf alle Faelle nicht ueberschreiben! Bei allen anderen Eintraegen hier
|
||||
* muss buf erneut gefaellt werden, da die betreffende Information in nur
|
||||
* einer Zeile steht, und nun die naechste gelesen werden muss.
|
||||
*/
|
||||
|
||||
case P_NEXT:
|
||||
f = NULL;
|
||||
lang = default_locale;
|
||||
b = in->getbuf(in->data);
|
||||
break;
|
||||
|
||||
default:
|
||||
b = in->getbuf(in->data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
log_info("done reading orders for %d factions", nfactions);
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct parser_state {
|
||||
unit *u;
|
||||
faction *f;
|
||||
|
|
|
@ -12,7 +12,6 @@ extern "C" {
|
|||
void *data;
|
||||
} input;
|
||||
|
||||
int read_orders(struct input *in);
|
||||
int parseorders(FILE *F);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -21,82 +21,36 @@
|
|||
#include <CuTest.h>
|
||||
#include <tests.h>
|
||||
|
||||
static const char *getbuf_null(void *data)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void test_read_orders(CuTest *tc) {
|
||||
input in;
|
||||
test_setup();
|
||||
in.getbuf = getbuf_null;
|
||||
in.data = NULL;
|
||||
CuAssertIntEquals(tc, 0, read_orders(&in));
|
||||
test_teardown();
|
||||
}
|
||||
|
||||
static const char *getbuf_strings(void *data)
|
||||
{
|
||||
strlist **listp = (strlist **)data;
|
||||
if (listp && *listp) {
|
||||
strlist *list = *listp;
|
||||
*listp = list->next;
|
||||
return list->s;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void test_unit_orders(CuTest *tc) {
|
||||
input in;
|
||||
unit *u;
|
||||
faction *f;
|
||||
strlist *orders = NULL, *backup;
|
||||
char buf[64];
|
||||
FILE *F = tmpfile();
|
||||
|
||||
test_setup();
|
||||
u = test_create_unit(f = test_create_faction(NULL), test_create_plain(0, 0));
|
||||
f->locale = test_create_locale();
|
||||
u->orders = create_order(K_ENTERTAIN, f->locale, NULL);
|
||||
faction_setpassword(f, password_hash("password", PASSWORD_DEFAULT));
|
||||
snprintf(buf, sizeof(buf), "%s %s %s",
|
||||
fprintf(F, "%s %s %s\n",
|
||||
LOC(f->locale, parameters[P_FACTION]), itoa36(f->no), "password");
|
||||
addstrlist(&orders, buf);
|
||||
snprintf(buf, sizeof(buf), "%s %s",
|
||||
fprintf(F, "%s %s\n",
|
||||
LOC(f->locale, parameters[P_UNIT]), itoa36(u->no));
|
||||
addstrlist(&orders, buf);
|
||||
snprintf(buf, sizeof(buf), "%s %s", keyword_name(K_MOVE, f->locale),
|
||||
fprintf(F, "%s %s\n", keyword_name(K_MOVE, f->locale),
|
||||
LOC(f->locale, shortdirections[D_WEST]));
|
||||
backup = orders;
|
||||
addstrlist(&orders, buf);
|
||||
in.data = &orders;
|
||||
in.getbuf = getbuf_strings;
|
||||
CuAssertIntEquals(tc, 0, read_orders(&in));
|
||||
rewind(F);
|
||||
CuAssertIntEquals(tc, 0, parseorders(F));
|
||||
CuAssertPtrNotNull(tc, u->old_orders);
|
||||
CuAssertPtrNotNull(tc, u->orders);
|
||||
CuAssertPtrEquals(tc, NULL, orders);
|
||||
CuAssertIntEquals(tc, K_MOVE, getkeyword(u->orders));
|
||||
CuAssertIntEquals(tc, K_ENTERTAIN, getkeyword(u->old_orders));
|
||||
CuAssertIntEquals(tc, UFL_ORDERS, u->flags & UFL_ORDERS);
|
||||
freestrlist(backup);
|
||||
fclose(F);
|
||||
test_teardown();
|
||||
}
|
||||
|
||||
typedef struct order_list {
|
||||
const char **orders;
|
||||
int next;
|
||||
} order_list;
|
||||
|
||||
static const char *getbuf_list(void *data)
|
||||
{
|
||||
order_list * olist = (order_list *)data;
|
||||
return olist->orders[olist->next++];
|
||||
}
|
||||
|
||||
static void test_faction_password_okay(CuTest *tc) {
|
||||
input in;
|
||||
faction *f;
|
||||
order_list olist;
|
||||
const char *orders[] = { "ERESSEA 1 password", NULL };
|
||||
FILE *F;
|
||||
|
||||
test_setup();
|
||||
f = test_create_faction(NULL);
|
||||
|
@ -104,21 +58,18 @@ static void test_faction_password_okay(CuTest *tc) {
|
|||
CuAssertIntEquals(tc, 1, f->no);
|
||||
faction_setpassword(f, "password");
|
||||
f->lastorders = turn - 1;
|
||||
olist.orders = orders;
|
||||
olist.next = 0;
|
||||
in.getbuf = getbuf_list;
|
||||
in.data = &olist;
|
||||
CuAssertIntEquals(tc, 0, read_orders(&in));
|
||||
CuAssertIntEquals(tc, 2, olist.next);
|
||||
F = tmpfile();
|
||||
fprintf(F, "ERESSEA 1 password\n");
|
||||
rewind(F);
|
||||
CuAssertIntEquals(tc, 0, parseorders(F));
|
||||
CuAssertIntEquals(tc, turn, f->lastorders);
|
||||
fclose(F);
|
||||
test_teardown();
|
||||
}
|
||||
|
||||
static void test_faction_password_bad(CuTest *tc) {
|
||||
input in;
|
||||
faction *f;
|
||||
order_list olist;
|
||||
const char *orders[] = { "ERESSEA 1 password", NULL };
|
||||
FILE *F;
|
||||
|
||||
test_setup();
|
||||
mt_create_va(mt_new("wrongpasswd", NULL), "password:string", MT_NEW_END);
|
||||
|
@ -128,20 +79,18 @@ static void test_faction_password_bad(CuTest *tc) {
|
|||
CuAssertIntEquals(tc, 1, f->no);
|
||||
faction_setpassword(f, "patzword");
|
||||
f->lastorders = turn - 1;
|
||||
olist.orders = orders;
|
||||
olist.next = 0;
|
||||
in.getbuf = getbuf_list;
|
||||
in.data = &olist;
|
||||
CuAssertIntEquals(tc, 0, read_orders(&in));
|
||||
CuAssertIntEquals(tc, 2, olist.next);
|
||||
F = tmpfile();
|
||||
fprintf(F, "ERESSEA 1 password\n");
|
||||
rewind(F);
|
||||
CuAssertIntEquals(tc, 0, parseorders(F));
|
||||
CuAssertIntEquals(tc, turn - 1, f->lastorders);
|
||||
fclose(F);
|
||||
test_teardown();
|
||||
}
|
||||
|
||||
CuSuite *get_orderfile_suite(void)
|
||||
{
|
||||
CuSuite *suite = CuSuiteNew();
|
||||
SUITE_ADD_TEST(suite, test_read_orders);
|
||||
SUITE_ADD_TEST(suite, test_unit_orders);
|
||||
SUITE_ADD_TEST(suite, test_faction_password_okay);
|
||||
SUITE_ADD_TEST(suite, test_faction_password_bad);
|
||||
|
|
Loading…
Reference in New Issue