diff --git a/src/common/gamecode/laws.c b/src/common/gamecode/laws.c index 627f7c718..36a018564 100644 --- a/src/common/gamecode/laws.c +++ b/src/common/gamecode/laws.c @@ -121,8 +121,10 @@ restart_race(unit *u, const race * rc) order ** ordp = &u->orders; f->subscription = u->faction->subscription; fset(f, FFL_RESTART); - if (f->subscription) fprintf(sqlstream, "UPDATE subscriptions set faction='%s', race='%s' where id=%u;\n", - itoa36(f->no), dbrace(rc), f->subscription); + if (f->subscription) { + sql_print(("UPDATE subscriptions set faction='%s', race='%s' where id=%u;\n", + itoa36(f->no), dbrace(rc), f->subscription)); + } f->magiegebiet = u->faction->magiegebiet; f->options = u->faction->options; free_orders(&nu->orders); @@ -1208,15 +1210,15 @@ parse_quit(void) char info[256]; sprintf(info, "%d Einheiten, %d Personen, %d Silber", f->no_units, f->number, f->money); - if (f->subscription) fprintf(sqlstream, - "UPDATE subscriptions SET lastturn=%d, password='%s', info='%s' " - "WHERE id=%u;\n", - f->lastorders, f->override, info, f->subscription); + if (f->subscription) { + sql_print(("UPDATE subscriptions SET lastturn=%d, password='%s', info='%s' WHERE id=%u;\n", + f->lastorders, f->override, info, f->subscription)); + } } else { - if (f->subscription) fprintf(sqlstream, - "UPDATE subscriptions SET status='ACTIVE', lastturn=%d, password='%s' " - "WHERE id=%u;\n", - f->lastorders, f->override, f->subscription); + if (f->subscription) { + sql_print(("UPDATE subscriptions SET status='ACTIVE', lastturn=%d, password='%s' WHERE id=%u;\n", + f->lastorders, f->override, f->subscription)); + } } if (NMRTimeout()>0 && turn - f->lastorders >= (NMRTimeout() - 1)) { @@ -2797,9 +2799,10 @@ renumber_factions(void) for (rp=renum;rp;rp=rp->next) { f = rp->faction; a_remove(&f->attribs, rp->attrib); - if (f->subscription) fprintf(sqlstream, "UPDATE subscriptions set faction='%s' where " - "id=%u;\n", itoa36(rp->want), - f->subscription); + if (f->subscription) { + sql_print(("UPDATE subscriptions set faction='%s' where id=%u;\n", + itoa36(rp->want), f->subscription)); + } f->no = rp->want; register_faction_id(rp->want); fset(f, FF_NEWID); diff --git a/src/common/kernel/eressea.c b/src/common/kernel/eressea.c index 34baaf0d8..b2a86efb2 100644 --- a/src/common/kernel/eressea.c +++ b/src/common/kernel/eressea.c @@ -2354,7 +2354,7 @@ kernel_done(void) translation_done(); skill_done(); gc_done(); - if (sqlstream!=NULL) sql_done(); + sql_done(); } const char * localenames[] = { @@ -2494,14 +2494,12 @@ remove_empty_factions(boolean writedropouts) } } } - if (f->subscription) fprintf(sqlstream, "UPDATE subscriptions set status='DEAD' where " - "id=%u\n;", f->subscription); + if (f->subscription) { + sql_print(("UPDATE subscriptions set status='DEAD' where id=%u\n;", + f->subscription)); + } *fp = f->next; -/* stripfaction(f); - * free(f); - * Wir können die nicht löschen, weil sie evtl. noch in attributen - * referenziert sind ! */ } else fp = &(*fp)->next; } diff --git a/src/common/modules/infocmd.c b/src/common/modules/infocmd.c index 166445810..1ca3a69ee 100644 --- a/src/common/modules/infocmd.c +++ b/src/common/modules/infocmd.c @@ -49,30 +49,24 @@ info_name(const tnode * tnext, const char * str, void * data, struct order * ord unused(str); unused(data); unused(ord); - if (sqlstream!=NULL) { - } } static void info_address(const tnode * tnext, const char * str, void * data, struct order * ord) { - if (sqlstream!=NULL) { - } } static void info_phone(const tnode * tnext, const char * str, void * data, struct order * ord) { - if (sqlstream!=NULL) { - } } static void info_vacation(const tnode * tnext, const char * str, void * data, struct order * ord) { - if (sqlstream!=NULL) { #ifdef SQLOUTPUT + if (sqlstream!=NULL) { unit * u = (unit*)data; faction * f = u->faction; const char * email = sqlquote(igetstrtoken(str)); @@ -86,8 +80,8 @@ info_vacation(const tnode * tnext, const char * str, void * data, struct order * start.tm_year, start.tm_mon, start.tm_mday, itoa36(f->no)); fprintf(sqlstream, "UPDATE factions SET vacation_end = '%04d-%02d-%02d' WHERE id = '%s';\n", end.tm_year, end.tm_mon, end.tm_mday, itoa36(f->no)); -#endif } +#endif } static tnode g_keys; @@ -112,7 +106,6 @@ infocommands(void) } if (*rp==r) rp = &r->next; } - fflush(sqlstream); } void diff --git a/src/common/triggers/unitmessage.c b/src/common/triggers/unitmessage.c index 070f98a38..3de07ff5b 100644 --- a/src/common/triggers/unitmessage.c +++ b/src/common/triggers/unitmessage.c @@ -77,7 +77,7 @@ unitmessage_write(const trigger * t, FILE * F) unitmessage_data * td = (unitmessage_data*)t->data.v; fprintf(F, "%s ", itoa36(td->target->no)); fwritestr(F, td->string); - fprintf(F, "%d %d ", td->type, td->level); + fprintf(F, " %d %d ", td->type, td->level); } static int diff --git a/src/common/util/sql.c b/src/common/util/sql.c index 654f992aa..c02c0f6fb 100644 --- a/src/common/util/sql.c +++ b/src/common/util/sql.c @@ -24,26 +24,42 @@ #include "log.h" -FILE * sqlstream; +#include +#include + +static FILE * sqlstream = NULL; +static char * sqlfilename = NULL; void sql_init(const char * filename) { - static boolean init = false; - if (!init) { - sqlstream = fopen(filename, "wt+"); - if (sqlstream==NULL) { - log_error(("could not open file: %s\n", filename)); - } - init = true; - } + if (sqlfilename!=NULL) free(sqlfilename); + sqlfilename = strdup(filename); +} + +void +_sql_print(const char * format, ...) +{ + if (!sqlstream && sqlfilename) { + sqlstream=fopen(sqlfilename, "wt+"); + free(sqlfilename); + sqlfilename=NULL; + } + if (sqlstream!=NULL) { + va_list marker; + va_start(marker, format); + vfprintf(sqlstream, format, marker); + va_end(marker); + } } void sql_done(void) { if (sqlstream) fclose(sqlstream); - /* TODO */ + if (sqlfilename) free(sqlfilename); + sqlstream=NULL; + sqlfilename=NULL; } const char * diff --git a/src/common/util/sql.h b/src/common/util/sql.h index 957eca016..66bf8bb28 100644 --- a/src/common/util/sql.h +++ b/src/common/util/sql.h @@ -25,10 +25,12 @@ extern "C" { #endif -extern FILE * sqlstream; -extern void sql_init(const char * filename); -extern void sql_done(void); -extern const char * sqlquote(const char * str); + extern void sql_init(const char * filename); + extern void sql_done(void); + extern const char * sqlquote(const char * str); + extern void _sql_print(const char * format, ...); + +#define sql_print(x) _sql_print x #ifdef __cplusplus } diff --git a/src/eressea/lua/building.cpp b/src/eressea/lua/building.cpp index 2f05b09dd..2e0590ca2 100644 --- a/src/eressea/lua/building.cpp +++ b/src/eressea/lua/building.cpp @@ -71,6 +71,7 @@ lc_write(const struct attrib * a, FILE* F) write_building_reference(b, F); fwritestr(F, fname); + fputc(' ', F); } static int diff --git a/src/eressea/lua/unit.cpp b/src/eressea/lua/unit.cpp index 8c547b81b..19e43311f 100644 --- a/src/eressea/lua/unit.cpp +++ b/src/eressea/lua/unit.cpp @@ -8,6 +8,7 @@ #include // kernel includes +#include #include #include #include @@ -232,7 +233,7 @@ unit_getregion(const unit& u) static void unit_setbuilding(unit& u, building& b) { - leave(u.region, u); + leave(u.region, &u); u.building = &b; } diff --git a/src/eressea/main.c b/src/eressea/main.c index 05762582c..632a349f9 100644 --- a/src/eressea/main.c +++ b/src/eressea/main.c @@ -549,11 +549,11 @@ void confirm_newbies(void) { faction * f = factions; - if (sqlstream==NULL) return; while (f) { if (!fval(f, FFL_DBENTRY)) { if (f->subscription) { - fprintf(sqlstream, "UPDATE subscriptions SET status='ACTIVE', faction='%s', race='%s' WHERE id=%u;\n", itoa36(f->no), dbrace(f->race), f->subscription); + sql_print(("UPDATE subscriptions SET status='ACTIVE', faction='%s', race='%s' WHERE id=%u;\n", + itoa36(f->no), dbrace(f->race), f->subscription)); fset(f, FFL_DBENTRY); } } diff --git a/src/scripts/wedding-jadee.lua b/src/scripts/wedding-jadee.lua index e6d92ecb7..71791cc97 100644 --- a/src/scripts/wedding-jadee.lua +++ b/src/scripts/wedding-jadee.lua @@ -6,20 +6,29 @@ hellgate = nil peacegate = nil function gate_exchange(b1, b2) - local units = {} + local units1 = {} + local units2 = {} local u for u in b1.units do - units[u.no] = u + if u:get_flag("wdgt") then + units1[u.no] = u + end end for u in b2.units do - u.region = b1.region - u.building = b1 + if u:get_flag("wdgt") then + units2[u.no] = u + end end - for id in units do - u = units[id] + for id in units1 do + u = units1[id] u.region = b2.region u.building = b2 end + for id in units2 do + u = units2[id] + u.region = b1.region + u.building = b1 + end end function hellgate_action(b)