updating eressea.db, not with a fixed game-id

This commit is contained in:
Enno Rehling 2014-05-04 22:49:06 +02:00
parent 1318c8976a
commit cb458db9a1
4 changed files with 130 additions and 116 deletions

View File

@ -15,16 +15,17 @@ without prior permission by the authors of Eressea.
#include "bind_unit.h" #include "bind_unit.h"
#include "bindings.h" #include "bindings.h"
#include <kernel/config.h>
#include <sqlite3.h> #include <sqlite3.h>
#include <tolua.h> #include <tolua.h>
#define LTYPE_DB TOLUA_CAST "db" #define LTYPE_DB TOLUA_CAST "db"
extern int db_update_factions(sqlite3 * db, bool force); extern int db_update_factions(sqlite3 * db, bool force, int game);
static int tolua_db_update_factions(lua_State * L) static int tolua_db_update_factions(lua_State * L)
{ {
sqlite3 *db = (sqlite3 *) tolua_tousertype(L, 1, 0); sqlite3 *db = (sqlite3 *) tolua_tousertype(L, 1, 0);
db_update_factions(db, tolua_toboolean(L, 2, 0)); db_update_factions(db, tolua_toboolean(L, 2, 0), global.game_id);
return 0; return 0;
} }

View File

@ -93,6 +93,7 @@ struct settings global = {
"Eressea", /* gamename */ "Eressea", /* gamename */
}; };
bool lomem = false;
FILE *logfile; FILE *logfile;
FILE *updatelog; FILE *updatelog;
const struct race *new_race[MAXRACES]; const struct race *new_race[MAXRACES];
@ -1578,10 +1579,6 @@ void freestrlist(strlist * s)
} }
} }
/* - Meldungen und Fehler ------------------------------------------------- */
bool lomem = false;
/* - Namen der Strukturen -------------------------------------- */ /* - Namen der Strukturen -------------------------------------- */
typedef char name[OBJECTIDSIZE + 1]; typedef char name[OBJECTIDSIZE + 1];
static name idbuf[8]; static name idbuf[8];
@ -3168,7 +3165,6 @@ void load_inifile(dictionary * d)
make_locales(str); make_locales(str);
/* excerpt from [config] (the rest is used in bindings.c) */ /* excerpt from [config] (the rest is used in bindings.c) */
game_name = iniparser_getstring(d, "config:game", game_name); global.game_id = iniparser_getint(d, "config:game_id", 0);
global.inifile = d; global.inifile = d;
} }

View File

@ -393,6 +393,7 @@ extern "C" {
void *vm_state; void *vm_state;
float producexpchance; float producexpchance;
int cookie; int cookie;
int game_id;
int data_version; /* TODO: eliminate in favor of gamedata.version */ int data_version; /* TODO: eliminate in favor of gamedata.version */
struct _dictionary_ *inifile; struct _dictionary_ *inifile;

View File

@ -129,117 +129,133 @@ db_update_email(sqlite3 * db, const faction * f, const db_faction * dbstate,
return SQLITE_OK; return SQLITE_OK;
} }
/*
int db_update_factions(sqlite3 * db, bool force) int db_get_game(sqlite3 *db) {
{ int game_id = 0;
int game_id = 6; const char sql_game[] =
const char sql_select[] = "SELECT id FROM game WHERE name=?";
"SELECT faction.id, faction.email_id, faction.code, email.email, faction.password_md5, faction.name, faction.lastturn FROM email, faction" sqlite3_stmt *stmt_game = stmt_cache(db, sql_game);
" WHERE email.id=faction.email_id AND faction.game_id=? AND (lastturn IS NULL OR lastturn>?)"; res = sqlite3_bind_text(stmt_game, 1, gamename, -1, SQLITE_TRANSIENT);
sqlite3_stmt *stmt_select = stmt_cache_get(db, sql_select); SQL_EXPECT(res, SQLITE_OK);
faction *f;
int res;
res = sqlite3_bind_int(stmt_select, 1, game_id);
SQL_EXPECT(res, SQLITE_OK);
res = sqlite3_bind_int(stmt_select, 2, turn - 2);
SQL_EXPECT(res, SQLITE_OK);
for (;;) {
sqlite3_uint64 id_faction;
int lastturn;
res = sqlite3_step(stmt_select); res = sqlite3_step(stmt_select);
if (res != SQLITE_ROW) return game_id;
break; }
*/
int db_update_factions(sqlite3 * db, bool force, int game_id)
{
const char sql_select[] =
"SELECT faction.id, faction.email_id, faction.code, email.email, faction.password_md5, faction.name, faction.lastturn FROM email, faction"
" WHERE email.id=faction.email_id AND faction.game_id=? AND (lastturn IS NULL OR lastturn>?)";
sqlite3_stmt *stmt_select = stmt_cache_get(db, sql_select);
faction *f;
int res;
id_faction = sqlite3_column_int64(stmt_select, 0); res = sqlite3_bind_int(stmt_select, 1, game_id);
lastturn = sqlite3_column_int(stmt_select, 6); SQL_EXPECT(res, SQLITE_OK);
f = get_faction_by_id((int)id_faction); res = sqlite3_bind_int(stmt_select, 2, turn - 2);
SQL_EXPECT(res, SQLITE_OK);
for (;;) {
sqlite3_uint64 id_faction;
int lastturn;
const char * no_b36;
if (f == NULL || !f->alive) { res = sqlite3_step(stmt_select);
if (lastturn == 0) { if (res != SQLITE_ROW)
const char sql_update[] = "UPDATE faction SET lastturn=? WHERE id=?"; break;
sqlite3_stmt *stmt = stmt_cache_get(db, sql_update);
lastturn = f ? f->lastorders : turn - 1; id_faction = sqlite3_column_int64(stmt_select, 0);
sqlite3_bind_int(stmt, 1, lastturn); lastturn = sqlite3_column_int(stmt_select, 6);
sqlite3_bind_int64(stmt, 2, id_faction); no_b36 = (const char *)sqlite3_column_text(stmt_select, 2);
res = sqlite3_step(stmt); f = get_faction_by_id((int)id_faction);
SQL_EXPECT(res, SQLITE_DONE); if (!f) {
} int no = atoi36(no_b36);
} else { f = findfaction(no);
md5_state_t ms; if (f) {
md5_byte_t digest[16]; f->subscription = (int)id_faction;
int i; }
char passwd_md5[MD5_LENGTH_0]; }
sqlite3_uint64 id_email; if (!f || !f->alive) {
bool update = force; if (lastturn == 0) {
db_faction dbstate; const char sql_update[] = "UPDATE faction SET lastturn=? WHERE id=?";
const char *no_b36; sqlite3_stmt *stmt = stmt_cache_get(db, sql_update);
fset(f, FFL_MARK); lastturn = f ? f->lastorders : turn - 1;
dbstate.id_faction = id_faction; sqlite3_bind_int(stmt, 1, lastturn);
dbstate.id_email = sqlite3_column_int64(stmt_select, 1); sqlite3_bind_int64(stmt, 2, id_faction);
no_b36 = (const char *)sqlite3_column_text(stmt_select, 2); res = sqlite3_step(stmt);
dbstate.no = no_b36 ? atoi36(no_b36) : -1; SQL_EXPECT(res, SQLITE_DONE);
dbstate.email = (const char *)sqlite3_column_text(stmt_select, 3); }
dbstate.passwd_md5 = (const char *)sqlite3_column_text(stmt_select, 4); } else {
dbstate.name = (const char *)sqlite3_column_text(stmt_select, 5); md5_state_t ms;
md5_byte_t digest[16];
int i;
char passwd_md5[MD5_LENGTH_0];
sqlite3_uint64 id_email;
bool update = force;
db_faction dbstate;
id_email = dbstate.id_email; fset(f, FFL_MARK);
res = db_update_email(db, f, &dbstate, force, &id_email); dbstate.id_faction = id_faction;
SQL_EXPECT(res, SQLITE_OK); dbstate.id_email = sqlite3_column_int64(stmt_select, 1);
dbstate.no = no_b36 ? atoi36(no_b36) : -1;
dbstate.email = (const char *)sqlite3_column_text(stmt_select, 3);
dbstate.passwd_md5 = (const char *)sqlite3_column_text(stmt_select, 4);
dbstate.name = (const char *)sqlite3_column_text(stmt_select, 5);
md5_init(&ms); id_email = dbstate.id_email;
md5_append(&ms, (md5_byte_t *) f->passw, (int)strlen(f->passw)); res = db_update_email(db, f, &dbstate, force, &id_email);
md5_finish(&ms, digest); SQL_EXPECT(res, SQLITE_OK);
for (i = 0; i != 16; ++i)
sprintf(passwd_md5 + 2 * i, "%.02x", digest[i]);
if (!update) { md5_init(&ms);
update = ((id_email != 0 && dbstate.id_email != id_email) md5_append(&ms, (md5_byte_t *) f->passw, (int)strlen(f->passw));
|| dbstate.no != f->no || dbstate.passwd_md5 == NULL md5_finish(&ms, digest);
|| strcmp(passwd_md5, dbstate.passwd_md5) != 0 || dbstate.name == NULL for (i = 0; i != 16; ++i)
|| strncmp(f->name, dbstate.name, MAX_FACTION_NAME) != 0); sprintf(passwd_md5 + 2 * i, "%.02x", digest[i]);
}
if (update) {
const char sql_update_faction[] =
"UPDATE faction SET email_id=?, password_md5=?, code=?, name=?, firstturn=? WHERE id=?";
sqlite3_stmt *stmt_update_faction =
stmt_cache_get(db, sql_update_faction);
res = sqlite3_bind_int64(stmt_update_faction, 1, id_email); if (!update) {
SQL_EXPECT(res, SQLITE_OK); update = ((id_email != 0 && dbstate.id_email != id_email)
res = || dbstate.no != f->no || dbstate.passwd_md5 == NULL
sqlite3_bind_text(stmt_update_faction, 2, passwd_md5, MD5_LENGTH, || strcmp(passwd_md5, dbstate.passwd_md5) != 0 || dbstate.name == NULL
SQLITE_TRANSIENT); || strncmp(f->name, dbstate.name, MAX_FACTION_NAME) != 0);
SQL_EXPECT(res, SQLITE_OK); }
res = if (update) {
sqlite3_bind_text(stmt_update_faction, 3, no_b36, -1, const char sql_update_faction[] =
SQLITE_TRANSIENT); "UPDATE faction SET email_id=?, password_md5=?, code=?, name=?, firstturn=? WHERE id=?";
SQL_EXPECT(res, SQLITE_OK); sqlite3_stmt *stmt_update_faction =
res = stmt_cache_get(db, sql_update_faction);
sqlite3_bind_text(stmt_update_faction, 4, f->name, -1,
SQLITE_TRANSIENT); res = sqlite3_bind_int64(stmt_update_faction, 1, id_email);
SQL_EXPECT(res, SQLITE_OK); SQL_EXPECT(res, SQLITE_OK);
res = sqlite3_bind_int(stmt_update_faction, 5, turn - f->age); res =
SQL_EXPECT(res, SQLITE_OK); sqlite3_bind_text(stmt_update_faction, 2, passwd_md5, MD5_LENGTH,
res = sqlite3_bind_int64(stmt_update_faction, 6, f->subscription); SQLITE_TRANSIENT);
SQL_EXPECT(res, SQLITE_OK); SQL_EXPECT(res, SQLITE_OK);
res = sqlite3_step(stmt_update_faction); res =
SQL_EXPECT(res, SQLITE_DONE); sqlite3_bind_text(stmt_update_faction, 3, no_b36, -1,
} SQLITE_TRANSIENT);
SQL_EXPECT(res, SQLITE_OK);
res =
sqlite3_bind_text(stmt_update_faction, 4, f->name, -1,
SQLITE_TRANSIENT);
SQL_EXPECT(res, SQLITE_OK);
res = sqlite3_bind_int(stmt_update_faction, 5, turn - f->age);
SQL_EXPECT(res, SQLITE_OK);
res = sqlite3_bind_int64(stmt_update_faction, 6, f->subscription);
SQL_EXPECT(res, SQLITE_OK);
res = sqlite3_step(stmt_update_faction);
SQL_EXPECT(res, SQLITE_DONE);
}
}
} }
}
for (f = factions; f; f = f->next) { for (f = factions; f; f = f->next) {
if (!fval(f, FFL_MARK)) { if (!fval(f, FFL_MARK)) {
log_error("%s (sub=%d, email=%s) has no entry in the database\n", factionname(f), f->subscription, f->email); log_error("%s (sub=%d, email=%s) has no entry in the database\n", factionname(f), f->subscription, f->email);
} else { } else {
freset(f, FFL_MARK); freset(f, FFL_MARK);
}
} }
} return SQLITE_OK;
return SQLITE_OK;
} }
int db_update_scores(sqlite3 * db, bool force) int db_update_scores(sqlite3 * db, bool force)