static void reports(void) { FILE * F; ql_iter fli; _mkdir("reports"); for (fli = qli_init(&factions); qli_more(fli);) { faction * f = (faction *)qli_next(&fli); cJSON * json; char buf[256]; stream strm; sprintf(buf, "reports/%d-%d.json", turn, f->no); fstream_init(&strm, fopen(buf, "w")); json = json_report(f); json_write(json, &strm); cJSON_Delete(json); fstream_done(&strm); report(f); } F = fopen("send", "w"); puts("Writing send file..."); for (fli = qli_init(&factions); qli_more(fli);) { faction * f = (faction *)qli_next(&fli); const char * addr = faction_getaddr(f); if (addr) { fprintf(F, "mail %d-%d.r\n", turn, f->no); fprintf(F, "in%%\"%s\"\n", addr); fprintf(F, "Atlantis Report for %s\n", gamedate()); } } fclose(F); F = fopen("maillist", "w"); puts("Writing maillist file..."); for (fli = qli_init(&factions); qli_more(fli);) { faction * f = (faction *)qli_next(&fli); const char * addr = faction_getaddr(f); if (addr) { fprintf(F, "%s\n", addr); } } fclose(F); }
static void fixme() { if (turn==0) { /* forgot to initialize regions with money */ ql_iter rli; for (rli = qli_init(®ions); qli_more(rli);) { region * r = (region *)qli_next(&rli); r->money = r->peasants * 3 / 2; } } }
struct terrain *get_terrain_by_name(const char *name) { ql_iter qli; for (qli=qli_init(&terrains);qli_more(qli);) { terrain *t = (terrain *)qli_next(&qli); if (strcmp(name, t->name)==0) { return t; } } return 0; }
void report_donations(void) { ql_iter qli = qli_init(&transfers); while (qli_more(qli)) { transfer *tf = (transfer *)qli_next(&qli); if (tf->amount > 0) { report_transfer(tf->f1, tf->f2, tf->r, tf->amount); } } }
region *findregion(int x, int y) { ql_iter rli; for (rli = qli_init(®ions); qli_more(rli);) { region *r = (region *)qli_next(&rli); if (r->x == x && r->y == y) return r; } return 0; }
keyword_t searchDirections(const char *s) { ql_iter qli; for (qli=qli_init(&dirData);qli_more(qli);) { dirStruct *d = (dirStruct *)qli_next(&qli); if ( (!_strcmpl(s, d->shortName)) || (!_strcmpl(s, d->longName)) ) { return d->token; } } return -1; }
static void test_loop(CuTest * tc) { ql_iter iter; quicklist * ql = 0; int result = 0; ql_push(&ql, (void *)data); ql_push(&ql, (void *)(data + 1)); for (iter = qli_init(&ql); qli_more(iter);) { const char * c = (const char *)qli_next(&iter); result += *c; } CuAssertIntEquals(tc, data[0] + data[1], result); }
static void test_insert_atend(CuTest * tc) { struct quicklist *ql = NULL; ql_iter qi; int i; for (i = 0; i != QL_MAXSIZE; ++i) { ql_insert(&ql, i, (void *)(data + i)); } CuAssertIntEquals(tc, QL_MAXSIZE, ql_length(ql)); qi = qli_init(&ql); for (i = 0; qli_more(qi); ++i) { void *ptr = qli_next(&qi); CuAssertPtrEquals(tc, (void *)(data + i), ptr); if (i < QL_MAXSIZE - 1) { CuAssertPtrEquals(tc, ql, qi.l); } else { CuAssertPtrEquals(tc, 0, qi.l); } } ql_free(ql); }
int db_update_factions(sqlite3 * db, bool force, int game_id) { quicklist *ql = read_factions(db, game_id); faction *f; sqlite3_exec(db, "BEGIN", 0, 0, 0); for (f = factions; f; f = f->next) { bool update = force; db_faction *dbf = 0; ql_iter it = qli_init(&ql); while (qli_more(it)) { db_faction *df = (db_faction*)qli_next(&it); if (f->no == df->no || strcmp(f->email, df->email) == 0 || strcmp(f->name, df->name) == 0) { dbf = df; } if (f->subscription == df->uid) { dbf = df; break; } } if (dbf) { if (dbf->uid != f->subscription) { log_warning("faction %s(%d) not found in database, but matches %d\n", itoa36(f->no), f->subscription, dbf->uid); f->subscription = dbf->uid; } update = (dbf->no != f->no) || (strcmp(f->email, dbf->email) != 0) || (strcmp(f->name, dbf->name) != 0); } else { f->subscription = insert_faction(db, game_id, f); log_warning("faction %s not found in database, created as %d\n", itoa36(f->no), f->subscription); update = true; } if (update) { update_faction(db, f); log_debug("faction %s updated\n", itoa36(f->no)); } } sqlite3_exec(db, "COMMIT", 0, 0, 0); return SQLITE_OK; }
void region_addunit(struct region *r, struct unit *u, struct unit **hint) { unit **up = &r->units; unit **ui = up, **ur = 0; ql_iter bli = qli_init(&r->buildings); ql_iter sli = qli_init(&r->ships); assert(u); assert(!u->region || u->region==r); if (hint) { u->region = r; u->next = *hint; *hint = u; return; } while (*up) { unit *x = *up; if (x==u) ur = up; if (u->stack) { if (x->stack==u->stack || x==u->stack) { ui = &x->next; } } else if (u->building) { if (!x->building) { ui = up; break; } else if (x->building==u->building) { ui = &x->next; } else { struct building *b = 0; while (qli_more(bli)) { b = (struct building *)qli_get(bli); if (b==u->building) { ui = up; break; } else if (b==x->building) { break; } qli_next(&bli); } if (b==u->building) { break; } } } else if (u->ship) { if (!x->ship) { ui = up; break; } else if (x->ship==u->ship) { ui = &x->next; } else { struct ship *s = 0; while (qli_more(sli)) { s = (struct ship *)qli_get(sli); if (s==u->ship) { ui = up; break; } else if (s==x->ship) { break; } qli_next(&sli); } if (s==u->ship) { break; } } } else if (!x->next) { ui = &x->next; break; } up=&x->next; } if (u->region) { while (!ur && *up) { unit *x = *up; if (x==u) ur = up; up = &x->next; } } reorder_unit(u, ur, ui); u->region = r; }