Lua steuert die Auswertung:

- 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.
This commit is contained in:
Enno Rehling 2003-12-14 16:34:00 +00:00
parent 2476e2cc97
commit 99bc41219b
6 changed files with 182 additions and 161 deletions

29
src/eressea/default.lua Normal file
View File

@ -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

View File

@ -13,6 +13,12 @@
using namespace luabind; using namespace luabind;
static alliance *
add_alliance(int id, const char * name)
{
return makealliance(id, name);
}
static eressea::list<alliance> static eressea::list<alliance>
get_alliances(void) { get_alliances(void) {
return eressea::list<alliance>(alliances); return eressea::list<alliance>(alliances);
@ -24,6 +30,7 @@ bind_alliance(lua_State * L)
module(L)[ module(L)[
def("alliances", &get_alliances, return_stl_iterator), def("alliances", &get_alliances, return_stl_iterator),
def("get_alliance", &findalliance), def("get_alliance", &findalliance),
def("add_alliance", &add_alliance),
class_<struct alliance>("alliance") class_<struct alliance>("alliance")
.def_readonly("name", &alliance::name) .def_readonly("name", &alliance::name)

View File

@ -3,10 +3,10 @@
#include <eressea.h> #include <eressea.h>
// kernel includes // kernel includes
#include <modules/alliance.h> #include <gamecode/laws.h>
#include <kernel/faction.h> #include <kernel/save.h>
#include <kernel/unit.h> #include <kernel/unit.h>
#include <kernel/region.h> #include <kernel/reports.h>
#include <util/language.h> #include <util/language.h>
// lua includes // lua includes
@ -16,34 +16,52 @@
using namespace luabind; using namespace luabind;
static faction * static int
add_faction(const char * email, const char * passwd, const char * racename, const char * lang) get_turn(void)
{ {
const race * frace = findrace(racename, default_locale); return turn;
locale * loc = find_locale(lang);
faction * f = addfaction(email, passwd, frace, loc, 0);
return f;
} }
static unit * static int
add_unit(faction * f, region * r) read_game(void)
{ {
if (f->units==NULL) return addplayer(r, f); return readgame(false);
return createunit(r, f, 0, f->race);
} }
static alliance * static int
add_alliance(int id, const char * name) 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 void
bind_eressea(lua_State * L) bind_eressea(lua_State * L)
{ {
module(L)[ module(L)[
def("add_unit", &add_unit), def("read_game", &read_game),
def("add_faction", &add_faction), def("write_game", &write_game),
def("add_alliance", &add_alliance) def("write_passwords", &writepasswd),
def("write_reports", &write_reports),
def("read_orders", &readorders),
def("process_orders", &process_orders),
def("get_turn", &get_turn)
]; ];
} }

View File

@ -14,6 +14,15 @@
using namespace luabind; 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<faction> static eressea::list<faction>
get_factions(void) { get_factions(void) {
return eressea::list<faction>(factions); return eressea::list<faction>(factions);
@ -51,6 +60,7 @@ bind_faction(lua_State * L)
module(L)[ module(L)[
def("factions", &get_factions, return_stl_iterator), def("factions", &get_factions, return_stl_iterator),
def("get_faction", &findfaction), def("get_faction", &findfaction),
def("add_faction", &add_faction),
class_<struct faction>("faction") class_<struct faction>("faction")
.def_readonly("name", &faction::name) .def_readonly("name", &faction::name)

View File

@ -15,6 +15,13 @@
using namespace luabind; 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 static void
unit_setnumber(unit& u, int number) unit_setnumber(unit& u, int number)
{ {
@ -85,6 +92,7 @@ bind_unit(lua_State * L)
{ {
module(L)[ module(L)[
def("get_unit", &findunit), def("get_unit", &findunit),
def("add_unit", &add_unit),
class_<struct unit>("unit") class_<struct unit>("unit")
.def_readonly("name", &unit::name) .def_readonly("name", &unit::name)

View File

@ -82,11 +82,9 @@
#include <base36.h> #include <base36.h>
/* lua includes */ /* lua includes */
#ifdef HAVE_LUA
#include "lua/bindings.h" #include "lua/bindings.h"
#include <lua.hpp> #include <lua.hpp>
#include <luabind/luabind.hpp> #include <luabind/luabind.hpp>
#endif
/* libc includes */ /* libc includes */
#include <cstdio> #include <cstdio>
@ -137,8 +135,8 @@ static char * orders = NULL;
static char * xmlfile = NULL; static char * xmlfile = NULL;
static int nowrite = 0; static int nowrite = 0;
static boolean g_writemap = false; static boolean g_writemap = false;
static boolean g_killeiswald = false;
static boolean opt_reportonly = false; static boolean opt_reportonly = false;
static const char * luafile = "default.lua";
struct settings global = { struct settings global = {
"Eressea", /* gamename */ "Eressea", /* gamename */
@ -241,7 +239,7 @@ writefactiondata(void)
{ {
FILE *F; FILE *F;
char zText[128]; char zText[128];
sprintf(zText, "%s/faction-data.xml", basepath()); sprintf(zText, "%s/faction-data.xml", basepath());
F = cfopen(zText, "w"); F = cfopen(zText, "w");
if(F) { if(F) {
@ -263,24 +261,26 @@ writefactiondata(void)
} }
#endif #endif
static void int
writepasswd(void) writepasswd(void)
{ {
FILE * F; FILE * F;
char zText[128]; char zText[128];
sprintf(zText, "%s/passwd", basepath()); sprintf(zText, "%s/passwd", basepath());
F = cfopen(zText, "w"); F = cfopen(zText, "w");
if (F) { if (F) {
faction *f; faction *f;
puts("Schreibe Passwörter..."); puts("Schreibe Passwörter...");
for (f = factions; f; f = f->next) { for (f = factions; f; f = f->next) {
fprintf(F, "%s:%s:%s:%s\n", fprintf(F, "%s:%s:%s:%s\n",
factionid(f), f->email, f->passw, f->override); factionid(f), f->email, f->passw, f->override);
} }
fclose(F); fclose(F);
} return 0;
}
return 1;
} }
#ifdef SHORTPWDS #ifdef SHORTPWDS
@ -314,13 +314,10 @@ readshortpwds()
} }
#endif #endif
#ifdef HAVE_LUA lua_State *
lua_State * luaState;
void
lua_init(void) lua_init(void)
{ {
luaState = lua_open(); lua_State * luaState = lua_open();
luaopen_base(luaState); luaopen_base(luaState);
luabind::open(luaState); luabind::open(luaState);
bind_eressea(luaState); bind_eressea(luaState);
@ -330,20 +327,19 @@ lua_init(void)
bind_unit(luaState); bind_unit(luaState);
bind_ship(luaState); bind_ship(luaState);
bind_building(luaState); bind_building(luaState);
return luaState;
} }
void void
lua_done(void) lua_done(lua_State * luaState)
{ {
lua_close(luaState); lua_close(luaState);
} }
#endif
static int int
processturn(char *filename) process_orders()
{ {
struct summary * begin, * end; struct summary * begin, * end;
int i;
#ifdef SHORTPWDS #ifdef SHORTPWDS
readshortpwds("passwords"); readshortpwds("passwords");
@ -351,14 +347,10 @@ processturn(char *filename)
begin = make_summary(false); begin = make_summary(false);
printf(" - Korrekturen Runde %d\n", turn); printf(" - Korrekturen Runde %d\n", turn);
korrektur(); korrektur();
#ifdef HAVE_LUA
lua_dofile(luaState, "test.lua");
#endif
turn++; turn++;
puts(" - entferne Texte der letzten Runde"); puts(" - entferne Texte der letzten Runde");
getgarbage(); getgarbage();
puts(" - Nehme Korrekturen am Datenbestand vor"); puts(" - Nehme Korrekturen am Datenbestand vor");
if ((i=readorders(filename))!=0) return i;
#if BENCHMARK #if BENCHMARK
exit(0); exit(0);
#endif #endif
@ -368,27 +360,15 @@ processturn(char *filename)
remove_unequipped_guarded(); remove_unequipped_guarded();
#endif #endif
korrektur_end(); korrektur_end();
if (!noreports) reports();
free_units(); end = make_summary(true);
puts(" - Beseitige leere Parteien"); report_summary(end, begin, false);
remove_empty_factions(true); report_summary(end, begin, true);
end = make_summary(true); free(end);
report_summary(end, begin, false); free(begin);
report_summary(end, begin, true);
free(end); update_subscriptions();
free(begin); return 0;
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;
} }
#ifdef CLEANUP_CODE #ifdef CLEANUP_CODE
@ -558,10 +538,25 @@ usage(const char * prog, const char * arg)
return -1; 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 static int
read_args(int argc, char **argv) read_args(int argc, char **argv, lua_State * luaState)
{ {
int i; int i;
char * c;
for (i=1;i!=argc;++i) { for (i=1;i!=argc;++i) {
if (argv[i][0]!='-') { if (argv[i][0]!='-') {
return usage(argv[0], argv[i]); 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, "dirtyload")==0) dirtyload = true;
else if (strcmp(argv[i]+2, "nonr")==0) nonr = 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, "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, "nobattle")==0) nobattle = true;
else if (strcmp(argv[i]+2, "nomonsters")==0) nomonsters = true; else if (strcmp(argv[i]+2, "nomonsters")==0) nomonsters = true;
else if (strcmp(argv[i]+2, "nodebug")==0) nobattledebug = true; else if (strcmp(argv[i]+2, "nodebug")==0) nobattledebug = true;
@ -593,6 +587,9 @@ read_args(int argc, char **argv)
case 'o': case 'o':
g_reportdir = argv[++i]; g_reportdir = argv[++i];
break; break;
case 'e':
luafile = argv[++i];
break;
case 'D': /* DEBUG */ case 'D': /* DEBUG */
demonfix = atoi(argv[++i]); demonfix = atoi(argv[++i]);
break; break;
@ -619,8 +616,12 @@ read_args(int argc, char **argv)
quiet = 1; quiet = 1;
break; break;
case 'v': case 'v':
if (i<argc) orders = argv[++i]; if (i<argc) {
else return usage(argv[0], argv[i]); orders = argv[++i];
setLuaString(luaState, "orderfile", orders);
} else {
return usage(argv[0], argv[i]);
}
break; break;
case 'p': case 'p':
loadplane = atoi(argv[++i]); loadplane = atoi(argv[++i]);
@ -632,6 +633,22 @@ read_args(int argc, char **argv)
case 'X': case 'X':
dirtyload = true; dirtyload = true;
break; break;
case 's':
c = argv[++i];
while (*c && (*c!='=')) ++c;
if (*c) {
*c++ = 0;
setLuaString(luaState, argv[i], c);
}
break;
case 'n':
c = argv[++i];
while (*c && (*c!='=')) ++c;
if (*c) {
*c++ = 0;
setLuaNumber(luaState, argv[i], atof(c));
}
break;
case 'l': case 'l':
log_open(argv[++i]); log_open(argv[++i]);
break; break;
@ -661,22 +678,6 @@ typedef struct lostdata {
int ship; int ship;
} lostdata; } lostdata;
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);
fset(f, FFL_DBENTRY);
}
}
f = f->next;
}
}
void void
update_subscriptions(void) update_subscriptions(void)
{ {
@ -699,6 +700,16 @@ update_subscriptions(void)
f->subscription=subscription; 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 int
@ -726,85 +737,23 @@ main(int argc, char *argv[])
init_malloc_debug(); init_malloc_debug();
#endif #endif
if ((i=read_args(argc, argv))!=0) return i; lua_State * luaState = lua_init();
if ((i=read_args(argc, argv, luaState))!=0) return i;
printf(
"version %d.%d\n"
"turn %d.\n"
"orders %s.\n",
global.data_version / 10, global.data_version % 10, turn, orders);
strcat(strcpy(zText, resourcepath()), "/timestrings"); strcat(strcpy(zText, resourcepath()), "/timestrings");
if ((i=read_datenames(zText))!=0) return i; if ((i=read_datenames(zText))!=0) return i;
kernel_init(); kernel_init();
#ifdef HAVE_LUA
lua_init();
#endif
game_init(); game_init();
#if defined(BETA_CODE)
/* xml_writeships(); */
/* xml_writebuildings(); */
xml_writeitems("items.xml");
return 0;
#endif
if ((i=readgame(false))!=0) return i; // run the main script
confirm_newbies(); lua_dofile(luaState, luafile);
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;
}
#ifdef CLEANUP_CODE #ifdef CLEANUP_CODE
game_done(); game_done();
#endif
#ifdef HAVE_LUA
lua_done();
#endif #endif
kernel_done(); kernel_done();
lua_done(luaState);
log_close(); log_close();
fclose(updatelog); fclose(updatelog);
return 0; return 0;