2010-08-08 10:06:34 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
|
|
|
#include <kernel/faction.h>
|
2014-05-17 07:49:28 +02:00
|
|
|
#include <kernel/race.h>
|
2015-12-01 12:24:22 +01:00
|
|
|
#include <util/language.h>
|
2014-05-17 07:49:28 +02:00
|
|
|
#include <util/log.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <util/base36.h>
|
|
|
|
#include <util/log.h>
|
2014-05-17 07:49:28 +02:00
|
|
|
#include <quicklist.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
#include <sqlite3.h>
|
|
|
|
#include <assert.h>
|
2014-03-15 19:29:11 +01:00
|
|
|
#include <string.h>
|
2014-05-17 07:49:28 +02:00
|
|
|
#include <stdlib.h>
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
faction *get_faction_by_id(int uid)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
faction *f;
|
|
|
|
for (f = factions; f; f = f->next) {
|
|
|
|
if (f->subscription == uid) {
|
|
|
|
return f;
|
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
return NULL;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2014-05-17 07:49:28 +02:00
|
|
|
/*
|
2010-08-08 10:06:34 +02:00
|
|
|
typedef struct stmt_cache {
|
2015-01-30 20:37:14 +01:00
|
|
|
sqlite3 *db;
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
const char *sql;
|
|
|
|
int inuse;
|
2010-08-08 10:06:34 +02:00
|
|
|
} stmt_cache;
|
|
|
|
|
|
|
|
#define MAX_STMT_CACHE 64
|
|
|
|
static stmt_cache cache[MAX_STMT_CACHE];
|
|
|
|
static int cache_insert;
|
|
|
|
|
2011-03-07 08:02:35 +01:00
|
|
|
static sqlite3_stmt *stmt_cache_get(sqlite3 * db, const char *sql)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
int i;
|
|
|
|
sqlite3_stmt *stmt;
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2015-01-30 20:37:14 +01:00
|
|
|
for (i = 0; i != MAX_STMT_CACHE && cache[i].db; ++i) {
|
|
|
|
if (cache[i].sql == sql && cache[i].db == db) {
|
|
|
|
cache[i].inuse = 1;
|
|
|
|
stmt = cache[i].stmt;
|
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
|
|
|
return stmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == MAX_STMT_CACHE) {
|
|
|
|
while (cache[cache_insert].inuse) {
|
|
|
|
cache[cache_insert].inuse = 0;
|
|
|
|
cache_insert = (cache_insert + 1) & (MAX_STMT_CACHE - 1);
|
|
|
|
}
|
|
|
|
i = cache_insert;
|
|
|
|
stmt = cache[i].stmt;
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
}
|
|
|
|
cache[i].inuse = 1;
|
|
|
|
cache[i].db = db;
|
|
|
|
cache[i].sql = sql;
|
|
|
|
sqlite3_prepare_v2(db, sql, -1, &cache[i].stmt, NULL);
|
|
|
|
return cache[i].stmt;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
2014-05-17 07:49:28 +02:00
|
|
|
*/
|
2010-08-08 10:06:34 +02:00
|
|
|
typedef struct db_faction {
|
2015-01-30 20:37:14 +01:00
|
|
|
int uid;
|
|
|
|
int no;
|
|
|
|
char *email;
|
|
|
|
char *name;
|
2010-08-08 10:06:34 +02:00
|
|
|
} db_faction;
|
|
|
|
|
2014-05-17 07:49:28 +02:00
|
|
|
static struct quicklist *
|
|
|
|
read_factions(sqlite3 * db, int game_id) {
|
2015-01-30 20:37:14 +01:00
|
|
|
int res;
|
|
|
|
quicklist *result = 0;
|
|
|
|
const char * sql =
|
|
|
|
"SELECT f.id, fd.code, fd.name, fd.email FROM faction f"
|
|
|
|
" LEFT OUTER JOIN faction_data fd"
|
|
|
|
" WHERE f.id=fd.faction_id AND f.game_id=? AND"
|
|
|
|
" fd.turn=(SELECT MAX(turn) FROM faction_data fx WHERE fx.faction_id=f.id)"
|
|
|
|
" ORDER BY f.id";
|
|
|
|
sqlite3_stmt *stmt = 0;
|
|
|
|
sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
|
|
|
sqlite3_bind_int(stmt, 1, game_id);
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-05-17 07:49:28 +02:00
|
|
|
res = sqlite3_step(stmt);
|
2015-01-30 20:37:14 +01:00
|
|
|
while (res == SQLITE_ROW) {
|
|
|
|
const char * text;
|
|
|
|
db_faction * dbf = (db_faction*)calloc(1, sizeof(db_faction));
|
|
|
|
dbf->uid = (int)sqlite3_column_int64(stmt, 0);
|
|
|
|
text = (const char *)sqlite3_column_text(stmt, 1);
|
|
|
|
if (text) dbf->no = atoi36(text);
|
|
|
|
text = (const char *)sqlite3_column_text(stmt, 2);
|
|
|
|
if (text) dbf->name = _strdup(text);
|
|
|
|
text = (const char *)sqlite3_column_text(stmt, 3);
|
|
|
|
if (text) dbf->email = _strdup(text);
|
|
|
|
ql_push(&result, dbf);
|
|
|
|
res = sqlite3_step(stmt);
|
|
|
|
}
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
return result;
|
2014-05-17 07:49:28 +02:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-05-17 07:49:28 +02:00
|
|
|
static int insert_faction(sqlite3 *db, int game_id, faction *f) {
|
2015-01-30 20:37:14 +01:00
|
|
|
const char *sql = "INSERT INTO faction (game_id, race) VALUES (?, ?)";
|
|
|
|
sqlite3_stmt *stmt = 0;
|
|
|
|
sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
|
|
|
sqlite3_bind_int(stmt, 1, game_id);
|
|
|
|
sqlite3_bind_text(stmt, 2, f->race->_name, -1, SQLITE_STATIC);
|
|
|
|
sqlite3_step(stmt);
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
return (int)sqlite3_last_insert_rowid(db);
|
2014-05-17 07:49:28 +02:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-05-17 07:49:28 +02:00
|
|
|
static void update_faction(sqlite3 *db, const faction *f) {
|
2015-01-30 20:37:14 +01:00
|
|
|
char code[5];
|
|
|
|
const char *sql =
|
|
|
|
"INSERT INTO faction_data (faction_id, code, name, email, lang, turn)"
|
|
|
|
" VALUES (?, ?, ?, ?, ?, ?)";
|
|
|
|
sqlite3_stmt *stmt = 0;
|
2016-05-04 16:35:07 +02:00
|
|
|
strcpy(code, itoa36(f->no));
|
2015-01-30 20:37:14 +01:00
|
|
|
sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
|
|
|
sqlite3_bind_int(stmt, 1, f->subscription);
|
|
|
|
sqlite3_bind_text(stmt, 2, code, -1, SQLITE_STATIC);
|
|
|
|
sqlite3_bind_text(stmt, 3, f->name, -1, SQLITE_STATIC);
|
|
|
|
sqlite3_bind_text(stmt, 4, f->email, -1, SQLITE_STATIC);
|
2015-12-01 12:24:22 +01:00
|
|
|
sqlite3_bind_text(stmt, 5, locale_name(f->locale), -1, SQLITE_STATIC);
|
2015-01-30 20:37:14 +01:00
|
|
|
sqlite3_bind_int(stmt, 6, turn);
|
|
|
|
sqlite3_step(stmt);
|
|
|
|
sqlite3_finalize(stmt);
|
2014-05-17 07:49:28 +02:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
2014-05-17 07:49:28 +02:00
|
|
|
int db_update_factions(sqlite3 * db, bool force, int game_id) {
|
2015-01-30 20:37:14 +01:00
|
|
|
quicklist *ql = read_factions(db, game_id);
|
|
|
|
faction *f;
|
|
|
|
sqlite3_exec(db, "BEGIN", 0, 0, 0);
|
|
|
|
for (f = factions; f; f = f->next) {
|
|
|
|
bool update = force;
|
|
|
|
db_faction *dbf = 0;
|
|
|
|
ql_iter it = qli_init(&ql);
|
|
|
|
while (qli_more(it)) {
|
|
|
|
db_faction *df = (db_faction*)qli_next(&it);
|
|
|
|
if (f->no == df->no || strcmp(f->email, df->email) == 0 || strcmp(f->name, df->name) == 0) {
|
|
|
|
dbf = df;
|
|
|
|
}
|
|
|
|
if (f->subscription == df->uid) {
|
|
|
|
dbf = df;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dbf) {
|
|
|
|
if (dbf->uid != f->subscription) {
|
|
|
|
log_warning("faction %s(%d) not found in database, but matches %d\n", itoa36(f->no), f->subscription, dbf->uid);
|
|
|
|
f->subscription = dbf->uid;
|
|
|
|
}
|
|
|
|
update = (dbf->no != f->no) || (strcmp(f->email, dbf->email) != 0) || (strcmp(f->name, dbf->name) != 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f->subscription = insert_faction(db, game_id, f);
|
|
|
|
log_warning("faction %s not found in database, created as %d\n", itoa36(f->no), f->subscription);
|
|
|
|
update = true;
|
|
|
|
}
|
|
|
|
if (update) {
|
|
|
|
update_faction(db, f);
|
|
|
|
log_debug("faction %s updated\n", itoa36(f->no));
|
|
|
|
}
|
2014-05-17 07:49:28 +02:00
|
|
|
}
|
2015-01-30 20:37:14 +01:00
|
|
|
sqlite3_exec(db, "COMMIT", 0, 0, 0);
|
|
|
|
return SQLITE_OK;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 07:41:07 +02:00
|
|
|
int db_update_scores(sqlite3 * db, bool force)
|
2010-08-08 10:06:34 +02:00
|
|
|
{
|
2015-01-30 20:37:14 +01:00
|
|
|
/*
|
|
|
|
const char *sql_ins =
|
2011-03-07 08:02:35 +01:00
|
|
|
"INSERT OR FAIL INTO score (value,faction_id,turn) VALUES (?,?,?)";
|
2015-01-30 20:37:14 +01:00
|
|
|
sqlite3_stmt *stmt_ins = stmt_cache_get(db, sql_ins);
|
|
|
|
const char *sql_upd =
|
2011-03-07 08:02:35 +01:00
|
|
|
"UPDATE score set value=? WHERE faction_id=? AND turn=?";
|
2015-01-30 20:37:14 +01:00
|
|
|
sqlite3_stmt *stmt_upd = stmt_cache_get(db, sql_upd);
|
|
|
|
faction *f;
|
|
|
|
sqlite3_exec(db, "BEGIN", 0, 0, 0);
|
|
|
|
for (f = factions; f; f = f->next) {
|
2010-08-08 10:06:34 +02:00
|
|
|
int res;
|
|
|
|
sqlite3_bind_int(stmt_ins, 1, f->score);
|
|
|
|
sqlite3_bind_int64(stmt_ins, 2, f->subscription);
|
|
|
|
sqlite3_bind_int(stmt_ins, 3, turn);
|
|
|
|
res = sqlite3_step(stmt_ins);
|
2011-03-07 08:02:35 +01:00
|
|
|
if (res == SQLITE_CONSTRAINT) {
|
2015-01-30 20:37:14 +01:00
|
|
|
sqlite3_bind_int(stmt_upd, 1, f->score);
|
|
|
|
sqlite3_bind_int64(stmt_upd, 2, f->subscription);
|
|
|
|
sqlite3_bind_int(stmt_upd, 3, turn);
|
|
|
|
res = sqlite3_step(stmt_upd);
|
|
|
|
sqlite3_reset(stmt_upd);
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|
|
|
|
sqlite3_reset(stmt_ins);
|
2015-01-30 20:37:14 +01:00
|
|
|
}
|
|
|
|
sqlite3_exec(db, "COMMIT", 0, 0, 0);
|
|
|
|
*/
|
|
|
|
return SQLITE_OK;
|
2010-08-08 10:06:34 +02:00
|
|
|
}
|