Merge pull request #632 from ennorehling/develop

configurable starting equipment
This commit is contained in:
Enno Rehling 2017-01-22 18:58:25 +01:00 committed by GitHub
commit 4598bdf917
17 changed files with 112 additions and 72 deletions

View File

@ -29,7 +29,7 @@
<xi:include href="config://game/strings.xml"/>
<xi:include href="config://default/adamantium.xml"/>
<equipment>
<set name="first_unit">
<set name="autoseed_unit">
<item name="log" amount="50"/>
<item name="stone" amount="50"/>
<item name="iron" amount="50"/>

View File

@ -3306,11 +3306,10 @@
</message>
<message name="wrongpasswd" section="events">
<type>
<arg name="faction" type="int"/>
<arg name="password" type="string"/>
</type>
<text locale="de">"ERESSEA $int36($faction) \"${password}\" - Deine Befehle hatten ein falsches Passwort."</text>
<text locale="en">"ERESSEA $int36($faction) \"${password}\" - Your orders had the wrong password."</text>
<text locale="de">"Deine Befehle hatten ein falsches Passwort (${password})."</text>
<text locale="en">"Your orders had the wrong password (${password})."</text>
</message>
<message name="changepasswd" section="events">
<type>

View File

@ -26,15 +26,17 @@ function callbacks(rules, name, ...)
end
local function dbupdate()
update_scores()
dbname = config.dbname or 'eressea.db'
edb = db.open(config.basepath..'/'..dbname)
if edb~=nil then
edb:update_factions()
edb:update_scores()
else
eressea.log.error("could not open "..config.basepath..'/'..dbname)
end
update_scores()
if config.dbname then
dbname = config.basepath..'/'..config.dbname
edb = db.open(dbame)
if edb~=nil then
edb:update_factions()
edb:update_scores()
else
eressea.log.error("could not open "..dbname)
end
end
end
local function write_emails(locales)

View File

@ -17,7 +17,7 @@ function setup()
end
function test_faction_flags()
assert_equal(2, f.flags) -- FFL_ISNEW
assert_equal(6, f.flags) -- FFL_ISNEW|FFL_PWMSG
f.flags = 42
assert_equal(42, f.flags)
end

View File

@ -80,9 +80,10 @@ without prior permission by the authors of Eressea.
#include <lauxlib.h>
#include <time.h>
#include <errno.h>
#include <assert.h>
#define TOLUA_PKG(NAME) extern void tolua_##NAME##_open(lua_State * L)
#define TOLUA_PKG(NAME) void tolua_##NAME##_open(lua_State * L)
TOLUA_PKG(eressea);
TOLUA_PKG(process);
@ -318,23 +319,6 @@ static int tolua_dice_rand(lua_State * L)
return 1;
}
static int tolua_addequipment(lua_State * L)
{
const char *eqname = tolua_tostring(L, 1, 0);
const char *iname = tolua_tostring(L, 2, 0);
const char *value = tolua_tostring(L, 3, 0);
int result = -1;
if (iname != NULL) {
const struct item_type *itype = it_find(iname);
if (itype != NULL) {
equipment_setitem(create_equipment(eqname), itype, value);
result = 0;
}
}
lua_pushinteger(L, result);
return 1;
}
static int tolua_get_season(lua_State * L)
{
int turnno = (int)tolua_tonumber(L, 1, 0);
@ -460,7 +444,7 @@ static int tolua_equipment_setitem(lua_State * L)
if (iname != NULL) {
const struct item_type *itype = it_find(iname);
if (itype != NULL) {
equipment_setitem(create_equipment(eqname), itype, value);
equipment_setitem(get_or_create_equipment(eqname), itype, value);
result = 0;
}
}
@ -1111,7 +1095,6 @@ int tolua_bindings_open(lua_State * L, const dictionary *inifile)
tolua_function(L, TOLUA_CAST "get_season", tolua_get_season);
tolua_function(L, TOLUA_CAST "equipment_setitem", tolua_equipment_setitem);
tolua_function(L, TOLUA_CAST "equip_unit", tolua_equipunit);
tolua_function(L, TOLUA_CAST "add_equipment", tolua_addequipment);
tolua_function(L, TOLUA_CAST "atoi36", tolua_atoi36);
tolua_function(L, TOLUA_CAST "itoa36", tolua_itoa36);
tolua_function(L, TOLUA_CAST "dice_roll", tolua_dice_rand);
@ -1168,32 +1151,56 @@ lua_State *lua_init(const dictionary *inifile) {
return L;
}
static int run_script(lua_State *L, const char *luafile) {
int err;
FILE *F;
F = fopen(luafile, "r");
if (!F) {
log_debug("dofile('%s'): %s", luafile, strerror(errno));
return errno;
}
fclose(F);
log_debug("executing script %s", luafile);
lua_getglobal(L, "dofile");
lua_pushstring(L, luafile);
err = lua_pcall(L, 1, 1, -3); /* error handler (debug.traceback) is now at stack -3 */
if (err != 0) {
log_lua_error(L);
assert(!"Lua syntax error? check log.");
}
else {
if (lua_isnumber(L, -1)) {
err = (int)lua_tonumber(L, -1);
}
lua_pop(L, 1);
}
return err;
}
int eressea_run(lua_State *L, const char *luafile)
{
int err = 0;
int err;
global.vm_state = L;
/* push an error handling function on the stack: */
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_remove(L, -2);
/* try to run configuration scripts: */
err = run_script(L, "config.lua");
err = run_script(L, "custom.lua");
/* run the main script */
if (luafile) {
log_debug("executing script %s\n", luafile);
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_remove(L, -2);
lua_getglobal(L, "dofile");
lua_pushstring(L, luafile);
err = lua_pcall(L, 1, 1, -3);
if (err != 0) {
log_lua_error(L);
assert(!"Lua syntax error? check log.");
}
else {
if (lua_isnumber(L, -1)) {
err = (int)lua_tonumber(L, -1);
}
lua_pop(L, 1);
}
return err;
err = run_script(L, luafile);
}
return lua_console(L);
else {
err = lua_console(L);
}
/* pop error handler off the stack: */
lua_pop(L, 1);
return err;
}

View File

@ -39,7 +39,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
static equipment *equipment_sets;
equipment *create_equipment(const char *eqname)
equipment *get_or_create_equipment(const char *eqname)
{
equipment **eqp = &equipment_sets;
for (;;) {

View File

@ -56,7 +56,7 @@ extern "C" {
void equipment_done(void);
struct equipment *create_equipment(const char *eqname);
struct equipment *get_or_create_equipment(const char *eqname);
struct equipment *get_equipment(const char *eqname);
void equipment_setitem(struct equipment *eq,

View File

@ -29,7 +29,7 @@ void test_equipment(CuTest * tc)
CuAssertPtrNotNull(tc, sp);
CuAssertPtrEquals(tc, 0, get_equipment("herpderp"));
eq = create_equipment("herpderp");
eq = get_or_create_equipment("herpderp");
CuAssertPtrEquals(tc, eq, get_equipment("herpderp"));
equipment_setitem(eq, it_horses, "1");

View File

@ -22,6 +22,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "alliance.h"
#include "ally.h"
#include "curse.h"
#include "equipment.h"
#include "group.h"
#include "item.h"
#include "messages.h"
@ -246,10 +247,6 @@ faction *addfaction(const char *email, const char *password,
log_warning("Invalid email address for faction %s: %s\n", itoa36(f->no), email);
}
if (!password) password = itoa36(rng_int());
faction_setpassword(f, password_encode(password, PASSWORD_DEFAULT));
ADDMSG(&f->msgs, msg_message("changepasswd", "value", password));
f->alliance_joindate = turn;
f->lastorders = turn;
f->_alive = true;
@ -258,7 +255,11 @@ faction *addfaction(const char *email, const char *password,
f->magiegebiet = 0;
f->locale = loc;
f->subscription = subscription;
f->flags = FFL_ISNEW;
f->flags = FFL_ISNEW|FFL_PWMSG;
if (!password) password = itoa36(rng_int());
faction_setpassword(f, password_encode(password, PASSWORD_DEFAULT));
ADDMSG(&f->msgs, msg_message("changepasswd", "value", password));
f->options =
want(O_REPORT) | want(O_ZUGVORLAGE) | want(O_COMPUTER) | want(O_COMPRESS) |
@ -285,10 +286,15 @@ faction *addfaction(const char *email, const char *password,
unit *addplayer(region * r, faction * f)
{
unit *u;
const struct equipment* eq;
assert(f->units == NULL);
faction_setorigin(f, 0, r->x, r->y);
u = create_unit(r, f, 1, f->race, 0, NULL, NULL);
eq = get_equipment("first_unit");
if (eq) {
equip_items(&u->items, eq);
}
u->hp = unit_max_hp(u) * u->number;
fset(u, UFL_ISNEW);
if (f->race == get_race(RC_DAEMON)) {

View File

@ -37,10 +37,11 @@ extern "C" {
extern struct attrib_type at_maxmagicians;
/* faction flags */
#define FFL_NEWID (1<<0) /* Die Partei hat bereits einmal ihre no gewechselt */
#define FFL_NEWID (1<<0) // Die Partei hat bereits einmal ihre no gewechselt
#define FFL_ISNEW (1<<1)
#define FFL_PWMSG (1<<2) // received a "new password" message
#define FFL_QUIT (1<<3)
#define FFL_CURSED (1<<4) /* you're going to have a bad time */
#define FFL_CURSED (1<<4) // you're going to have a bad time
#define FFL_DEFENDER (1<<10)
#define FFL_SELECT (1<<18) /* ehemals f->dh, u->dh, r->dh, etc... */
#define FFL_NOAID (1<<21) /* Hilfsflag Kampf */

View File

@ -117,7 +117,7 @@ static void test_addfaction(CuTest *tc) {
CuAssertTrue(tc, checkpasswd(f, "hurrdurr"));
CuAssertPtrEquals(tc, (void *)lang, (void *)f->locale);
CuAssertIntEquals(tc, 1234, f->subscription);
CuAssertIntEquals(tc, FFL_ISNEW, f->flags);
CuAssertIntEquals(tc, FFL_ISNEW|FFL_PWMSG, f->flags);
CuAssertIntEquals(tc, 0, f->age);
CuAssertTrue(tc, faction_alive(f));
CuAssertIntEquals(tc, M_GRAY, f->magiegebiet);

View File

@ -235,8 +235,7 @@ static faction *factionorders(void)
if (!checkpasswd(f, (const char *)pass)) {
log_debug("Invalid password for faction %s", itoa36(fid));
ADDMSG(&f->msgs, msg_message("wrongpasswd", "faction password",
f->no, pass));
ADDMSG(&f->msgs, msg_message("wrongpasswd", "password", pass));
return 0;
}
/* Die Partei hat sich zumindest gemeldet, so dass sie noch

View File

@ -44,7 +44,6 @@ extern "C" {
extern int enc_gamedata;
int readorders(const char *filename);
int creategame(void);
int readgame(const char *filename);
int writegame(const char *filename);

View File

@ -1266,7 +1266,7 @@ add_subsets(xmlDocPtr doc, equipment * eq, xmlNodeSetPtr nsetSubsets)
assert(propValue != NULL);
eq->subsets[i].sets[set].chance = chance;
eq->subsets[i].sets[set].set =
create_equipment((const char *)propValue);
get_or_create_equipment((const char *)propValue);
xmlFree(propValue);
}
}
@ -1296,7 +1296,7 @@ static int parse_equipment(xmlDocPtr doc)
xmlChar *propName = xmlGetProp(node, BAD_CAST "name");
if (propName != NULL) {
equipment *eq = create_equipment((const char *)propName);
equipment *eq = get_or_create_equipment((const char *)propName);
xmlXPathObjectPtr xpathResult;
xpath->node = node;

View File

@ -2214,6 +2214,7 @@ int password_cmd(unit * u, struct order *ord)
faction_setpassword(u->faction, password_encode(pwbuf, PASSWORD_DEFAULT));
ADDMSG(&u->faction->msgs, msg_message("changepasswd",
"value", pwbuf));
u->faction->flags |= FFL_PWMSG;
return 0;
}

View File

@ -1336,6 +1336,14 @@ void prepare_report(report_context *ctx, faction *f)
rule_region_owners = config_token("rules.region_owner_pay_building", bt_lighthouse->_name);
}
if (f->age<=2) {
if ((f->flags&FFL_PWMSG)==0) {
// TODO: this assumes unencrypted passwords
f->flags |= FFL_PWMSG;
ADDMSG(&f->msgs, msg_message("changepasswd", "value", f->_password));
}
}
ctx->f = f;
ctx->report_time = time(NULL);
ctx->addresses = NULL;

View File

@ -226,6 +226,23 @@ static void test_arg_resources(CuTest *tc) {
test_cleanup();
}
static void test_newbie_password_message(CuTest *tc) {
report_context ctx;
faction *f;
test_setup();
f = test_create_faction(0);
f->age = 5;
f->flags = 0;
prepare_report(&ctx, f);
CuAssertIntEquals(tc, 0, f->flags&FFL_PWMSG);
CuAssertPtrEquals(tc, 0, test_find_messagetype(f->msgs, "changepasswd"));
f->age=2;
prepare_report(&ctx, f);
CuAssertIntEquals(tc, FFL_PWMSG, f->flags&FFL_PWMSG);
CuAssertPtrNotNull(tc, test_find_messagetype(f->msgs, "changepasswd"));
test_cleanup();
}
static void test_prepare_travelthru(CuTest *tc) {
report_context ctx;
faction *f, *f2;
@ -465,6 +482,7 @@ static void test_seen_travelthru(CuTest *tc) {
CuSuite *get_reports_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_newbie_password_message);
SUITE_ADD_TEST(suite, test_prepare_report);
SUITE_ADD_TEST(suite, test_seen_neighbours);
SUITE_ADD_TEST(suite, test_seen_travelthru);