From 99bc41219b954aa1dc0ec1fb65eb21a49460b652 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 14 Dec 2003 16:34:00 +0000 Subject: [PATCH] Lua steuert die Auswertung: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Möglichkeit, mit -n und -s numerische oder string-parameter an lua zu geben - Auswertung in ein Skript verlegt. Server macht nur nach initialisierung, skriptaufruf, destruktion, rest passiert im skript. - "default.lua" skript macht Auswertungen wie bisher. --- src/eressea/default.lua | 29 +++++ src/eressea/lua/alliance.cpp | 7 ++ src/eressea/lua/eressea.cpp | 58 ++++++--- src/eressea/lua/faction.cpp | 10 ++ src/eressea/lua/unit.cpp | 8 ++ src/eressea/main.cpp | 231 ++++++++++++++--------------------- 6 files changed, 182 insertions(+), 161 deletions(-) create mode 100644 src/eressea/default.lua diff --git a/src/eressea/default.lua b/src/eressea/default.lua new file mode 100644 index 000000000..50bd6f54b --- /dev/null +++ b/src/eressea/default.lua @@ -0,0 +1,29 @@ +function process(orders) + if read_game()~=0 then + print("could not read game") + return -1 + end + + read_orders(orders) + process_orders() + + write_passwords() + write_reports() + + if write_game()~=0 then + print("could not write game") + return -1 + end +end + + +-- +-- main body of script +-- + +-- orderfile: contains the name of the orders. +if orderfile==nil then + print "you must specify an orderfile" +else + process(orderfile) +end diff --git a/src/eressea/lua/alliance.cpp b/src/eressea/lua/alliance.cpp index 5faff1c75..c96205bbd 100644 --- a/src/eressea/lua/alliance.cpp +++ b/src/eressea/lua/alliance.cpp @@ -13,6 +13,12 @@ using namespace luabind; +static alliance * +add_alliance(int id, const char * name) +{ + return makealliance(id, name); +} + static eressea::list get_alliances(void) { return eressea::list(alliances); @@ -24,6 +30,7 @@ bind_alliance(lua_State * L) module(L)[ def("alliances", &get_alliances, return_stl_iterator), def("get_alliance", &findalliance), + def("add_alliance", &add_alliance), class_("alliance") .def_readonly("name", &alliance::name) diff --git a/src/eressea/lua/eressea.cpp b/src/eressea/lua/eressea.cpp index eeb0000c0..fb34eb1d4 100644 --- a/src/eressea/lua/eressea.cpp +++ b/src/eressea/lua/eressea.cpp @@ -3,10 +3,10 @@ #include // kernel includes -#include -#include +#include +#include #include -#include +#include #include // lua includes @@ -16,34 +16,52 @@ using namespace luabind; -static faction * -add_faction(const char * email, const char * passwd, const char * racename, const char * lang) +static int +get_turn(void) { - const race * frace = findrace(racename, default_locale); - locale * loc = find_locale(lang); - faction * f = addfaction(email, passwd, frace, loc, 0); - return f; + return turn; } -static unit * -add_unit(faction * f, region * r) +static int +read_game(void) { - if (f->units==NULL) return addplayer(r, f); - return createunit(r, f, 0, f->race); + return readgame(false); } -static alliance * -add_alliance(int id, const char * name) +static int +write_game(void) { - return makealliance(id, name); + char ztext[64]; + + free_units(); + remove_empty_factions(true); + + sprintf(ztext, "%s/%d", datapath(), turn); + writegame(ztext, 0); + return 0; } +extern int writepasswd(void); + +static int +write_reports() +{ + reports(); + return 0; +} + +extern int process_orders(void); + void -bind_eressea(lua_State * L) +bind_eressea(lua_State * L) { module(L)[ - def("add_unit", &add_unit), - def("add_faction", &add_faction), - def("add_alliance", &add_alliance) + def("read_game", &read_game), + def("write_game", &write_game), + def("write_passwords", &writepasswd), + def("write_reports", &write_reports), + def("read_orders", &readorders), + def("process_orders", &process_orders), + def("get_turn", &get_turn) ]; } diff --git a/src/eressea/lua/faction.cpp b/src/eressea/lua/faction.cpp index 5f7414add..7e621f64f 100644 --- a/src/eressea/lua/faction.cpp +++ b/src/eressea/lua/faction.cpp @@ -14,6 +14,15 @@ using namespace luabind; +static faction * +add_faction(const char * email, const char * passwd, const char * racename, const char * lang) +{ + const race * frace = findrace(racename, default_locale); + locale * loc = find_locale(lang); + faction * f = addfaction(email, passwd, frace, loc, 0); + return f; +} + static eressea::list get_factions(void) { return eressea::list(factions); @@ -51,6 +60,7 @@ bind_faction(lua_State * L) module(L)[ def("factions", &get_factions, return_stl_iterator), def("get_faction", &findfaction), + def("add_faction", &add_faction), class_("faction") .def_readonly("name", &faction::name) diff --git a/src/eressea/lua/unit.cpp b/src/eressea/lua/unit.cpp index ea1c4bf50..06a4954a7 100644 --- a/src/eressea/lua/unit.cpp +++ b/src/eressea/lua/unit.cpp @@ -15,6 +15,13 @@ using namespace luabind; +static unit * +add_unit(faction * f, region * r) +{ + if (f->units==NULL) return addplayer(r, f); + return createunit(r, f, 0, f->race); +} + static void unit_setnumber(unit& u, int number) { @@ -85,6 +92,7 @@ bind_unit(lua_State * L) { module(L)[ def("get_unit", &findunit), + def("add_unit", &add_unit), class_("unit") .def_readonly("name", &unit::name) diff --git a/src/eressea/main.cpp b/src/eressea/main.cpp index 5860e5b3d..b2e0d5f93 100644 --- a/src/eressea/main.cpp +++ b/src/eressea/main.cpp @@ -82,11 +82,9 @@ #include /* lua includes */ -#ifdef HAVE_LUA #include "lua/bindings.h" #include #include -#endif /* libc includes */ #include @@ -137,8 +135,8 @@ static char * orders = NULL; static char * xmlfile = NULL; static int nowrite = 0; static boolean g_writemap = false; -static boolean g_killeiswald = false; static boolean opt_reportonly = false; +static const char * luafile = "default.lua"; struct settings global = { "Eressea", /* gamename */ @@ -241,7 +239,7 @@ writefactiondata(void) { FILE *F; char zText[128]; - + sprintf(zText, "%s/faction-data.xml", basepath()); F = cfopen(zText, "w"); if(F) { @@ -263,24 +261,26 @@ writefactiondata(void) } #endif -static void +int writepasswd(void) { - FILE * F; - char zText[128]; + FILE * F; + char zText[128]; - sprintf(zText, "%s/passwd", basepath()); - F = cfopen(zText, "w"); - if (F) { - faction *f; - puts("Schreibe Passwörter..."); + sprintf(zText, "%s/passwd", basepath()); + F = cfopen(zText, "w"); + if (F) { + faction *f; + puts("Schreibe Passwörter..."); - for (f = factions; f; f = f->next) { - fprintf(F, "%s:%s:%s:%s\n", - factionid(f), f->email, f->passw, f->override); - } - fclose(F); - } + for (f = factions; f; f = f->next) { + fprintf(F, "%s:%s:%s:%s\n", + factionid(f), f->email, f->passw, f->override); + } + fclose(F); + return 0; + } + return 1; } #ifdef SHORTPWDS @@ -314,13 +314,10 @@ readshortpwds() } #endif -#ifdef HAVE_LUA -lua_State * luaState; - -void +lua_State * lua_init(void) { - luaState = lua_open(); + lua_State * luaState = lua_open(); luaopen_base(luaState); luabind::open(luaState); bind_eressea(luaState); @@ -330,20 +327,19 @@ lua_init(void) bind_unit(luaState); bind_ship(luaState); bind_building(luaState); + return luaState; } -void -lua_done(void) +void +lua_done(lua_State * luaState) { lua_close(luaState); } -#endif -static int -processturn(char *filename) +int +process_orders() { struct summary * begin, * end; - int i; #ifdef SHORTPWDS readshortpwds("passwords"); @@ -351,14 +347,10 @@ processturn(char *filename) begin = make_summary(false); printf(" - Korrekturen Runde %d\n", turn); korrektur(); -#ifdef HAVE_LUA - lua_dofile(luaState, "test.lua"); -#endif turn++; puts(" - entferne Texte der letzten Runde"); getgarbage(); puts(" - Nehme Korrekturen am Datenbestand vor"); - if ((i=readorders(filename))!=0) return i; #if BENCHMARK exit(0); #endif @@ -368,27 +360,15 @@ processturn(char *filename) remove_unequipped_guarded(); #endif korrektur_end(); - if (!noreports) reports(); - free_units(); - puts(" - Beseitige leere Parteien"); - remove_empty_factions(true); - end = make_summary(true); - report_summary(end, begin, false); - report_summary(end, begin, true); - free(end); - free(begin); - writepasswd(); -#ifdef FUZZY_BASE36 - fputs("==--------------------------==\n", stdout); - fprintf(stdout, "## fuzzy base10 hits: %5d ##\n", fuzzy_hits); - fputs("==--------------------------==\n", stdout); -#endif /* FUZZY_BASE36 */ - if (!nowrite) { - char ztext[64]; - sprintf(ztext, "%s/%d", datapath(), turn); - writegame(ztext, 0); - } - return 0; + + end = make_summary(true); + report_summary(end, begin, false); + report_summary(end, begin, true); + free(end); + free(begin); + + update_subscriptions(); + return 0; } #ifdef CLEANUP_CODE @@ -558,10 +538,25 @@ usage(const char * prog, const char * arg) return -1; } +static void +setLuaString(lua_State * luaState, const char * name, const char * value) +{ + luabind::object globals = luabind::get_globals(luaState); + globals[name] = value; +} + +static void +setLuaNumber(lua_State * luaState, const char * name, double value) +{ + luabind::object globals = luabind::get_globals(luaState); + globals[name] = value; +} + static int -read_args(int argc, char **argv) +read_args(int argc, char **argv, lua_State * luaState) { int i; + char * c; for (i=1;i!=argc;++i) { if (argv[i][0]!='-') { return usage(argv[0], argv[i]); @@ -577,7 +572,6 @@ read_args(int argc, char **argv) else if (strcmp(argv[i]+2, "dirtyload")==0) dirtyload = true; else if (strcmp(argv[i]+2, "nonr")==0) nonr = true; else if (strcmp(argv[i]+2, "nomsg")==0) nomsg = true; - else if (strcmp(argv[i]+2, "noeiswald")==0) g_killeiswald = true; else if (strcmp(argv[i]+2, "nobattle")==0) nobattle = true; else if (strcmp(argv[i]+2, "nomonsters")==0) nomonsters = true; else if (strcmp(argv[i]+2, "nodebug")==0) nobattledebug = true; @@ -593,6 +587,9 @@ read_args(int argc, char **argv) case 'o': g_reportdir = argv[++i]; break; + case 'e': + luafile = argv[++i]; + break; case 'D': /* DEBUG */ demonfix = atoi(argv[++i]); break; @@ -619,8 +616,12 @@ read_args(int argc, char **argv) quiet = 1; break; case 'v': - if (isubscription) { - fprintf(sqlstream, "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); - } - } - f = f->next; - } -} - void update_subscriptions(void) { @@ -699,6 +700,16 @@ update_subscriptions(void) f->subscription=subscription; } } + fclose(F); + + sprintf(zText, "subscriptions.%u", turn); + F = fopen(zText, "w"); + for (f=factions;f!=NULL;f=f->next) { + fprintf(F, "%s:%u:%s:%s:%s:%u:\n", + itoa36(f->no), f->subscription, f->email, f->override, + dbrace(f->race), f->lastorders); + } + fclose(F); } int @@ -726,85 +737,23 @@ main(int argc, char *argv[]) init_malloc_debug(); #endif - if ((i=read_args(argc, argv))!=0) return i; - - printf( - "version %d.%d\n" - "turn %d.\n" - "orders %s.\n", - global.data_version / 10, global.data_version % 10, turn, orders); + lua_State * luaState = lua_init(); + if ((i=read_args(argc, argv, luaState))!=0) return i; strcat(strcpy(zText, resourcepath()), "/timestrings"); if ((i=read_datenames(zText))!=0) return i; kernel_init(); -#ifdef HAVE_LUA - lua_init(); -#endif game_init(); -#if defined(BETA_CODE) - /* xml_writeships(); */ - /* xml_writebuildings(); */ - xml_writeitems("items.xml"); - return 0; -#endif - if ((i=readgame(false))!=0) return i; - confirm_newbies(); - update_subscriptions(); - { - char zText[128]; - FILE * F; - faction * f = factions; - sprintf(zText, "subscriptions.%u", turn); - F = fopen(zText, "w"); - while (f!=NULL) { - fprintf(F, "%s:%u:%s:%s:%s:%u:\n", - itoa36(f->no), f->subscription, f->email, f->override, - dbrace(f->race), f->lastorders); - f = f->next; - } - fclose(F); - } - -#ifdef DUNGEON_MODULE - if (dungeonstyles) { - struct dungeon * d = dungeonstyles; - struct region * r = make_dungeon(d); - make_dungeongate(findregion(0, 0), r, d); - } -#endif - writepasswd(); - if (g_killeiswald) { - region * r = findregion(0, 25); - if (r) { - /* mach sie alle zur schnecke... */ - unit * u; - terraform(r, T_OCEAN); - for (u=r->units;u;u=u->next) scale_number(u, 1); - } - } - - if (opt_reportonly) { - reports(); - return 0; - } - - if (g_writemap) { - return crwritemap(); - } - - if ((i=processturn(orders))!=0) { - return i; - } + // run the main script + lua_dofile(luaState, luafile); #ifdef CLEANUP_CODE game_done(); -#endif -#ifdef HAVE_LUA - lua_done(); #endif kernel_done(); + lua_done(luaState); log_close(); fclose(updatelog); return 0;