From 5e4eb8e4b14b58b506236c405039f165fb6e998b Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 22 Sep 2023 18:04:46 +0300 Subject: [PATCH 25/25] fcdb: Add game_start lua function It creates dbid for the game, if existing one is not good. See osdn #48595 Signed-off-by: Marko Lindqvist --- common/game.c | 1 + common/game.h | 1 + doc/README.fcdb | 3 +++ lua/database.lua | 21 +++++++++++++++++++++ server/savegame/savegame3.c | 4 ++++ server/scripting/script_fcdb.c | 4 ++++ server/srv_main.c | 8 +++++++- 7 files changed, 41 insertions(+), 1 deletion(-) diff --git a/common/game.c b/common/game.c index 770d7a12bb..d610c64a64 100644 --- a/common/game.c +++ b/common/game.c @@ -536,6 +536,7 @@ void game_ruleset_init(void) if (is_server()) { game.server.luadata = NULL; + game.server.dbid = -1; game.server.ruledit.nationlist = NULL; game.server.ruledit.embedded_nations = NULL; game.server.ruledit.embedded_nations_count = 0; diff --git a/common/game.h b/common/game.h index 116ceb987c..4585c1d0bd 100644 --- a/common/game.h +++ b/common/game.h @@ -244,6 +244,7 @@ struct civ_game { struct rgbcolor_list *plr_colors; struct section_file *luadata; + int dbid; struct { int turns; diff --git a/doc/README.fcdb b/doc/README.fcdb index 67adc2672e..abc5d3a921 100644 --- a/doc/README.fcdb +++ b/doc/README.fcdb @@ -209,6 +209,9 @@ Freeciv expects the following lua functions to be defined in database.lua: -- Called when connection has been fully established function conn_established(conn) + -- Called when the game starts. Return dbid to use now on + function game_start(oldid) + Where 'conn' is an object representing the connection to the client which requests access. diff --git a/lua/database.lua b/lua/database.lua index d0afa5dbd1..f3624ef63f 100644 --- a/lua/database.lua +++ b/lua/database.lua @@ -313,6 +313,27 @@ function database_capstr() return string.format('%s', caps.capstr) end +function game_start(oldid) + local table_meta = get_option("table_meta") + + if oldid > 0 then + return oldid + end + + query = string.format([[SELECT gamecount FROM %s]], table_meta) + local res = assert(dbh:execute(query)) + + local count_row = res:fetch({}, "a") + count = count_row.gamecount + 1 + + res:close() + + query = string.format([[UPDATE %s set gamecount = %d]], table_meta, count) + assert(dbh:execute(query)) + + return count +end + -- ************************************************************************** -- freeciv database entry functions -- ************************************************************************** diff --git a/server/savegame/savegame3.c b/server/savegame/savegame3.c index fdbb9a6c06..87258eb407 100644 --- a/server/savegame/savegame3.c +++ b/server/savegame/savegame3.c @@ -1399,6 +1399,8 @@ static void sg_load_savefile(struct loaddata *loading) } } + game.server.dbid = secfile_lookup_int_default(loading->file, 0, "savefile.dbid"); + /* This is in the savegame only if the game has been started before savegame3.c time, * and in that case it's TRUE. If it's missing, it's to be considered FALSE. */ game.server.last_updated_year = secfile_lookup_bool_default(loading->file, FALSE, @@ -1735,6 +1737,8 @@ static void sg_save_savefile(struct savedata *saving) secfile_insert_bool(saving->file, TRUE, "savefile.last_updated_as_year"); } + secfile_insert_int(saving->file, game.server.dbid, "savefile.dbid"); + /* Save improvement order in savegame, so we are not dependent on ruleset * order. If the game isn't started improvements aren't loaded so we can * not save the order. */ diff --git a/server/scripting/script_fcdb.c b/server/scripting/script_fcdb.c index 7b88683c4d..a308759a48 100644 --- a/server/scripting/script_fcdb.c +++ b/server/scripting/script_fcdb.c @@ -112,6 +112,8 @@ static struct fc_lua *fcl = NULL; conn_established(Connection pconn) - called when connection has been fully established + gase_start(int oldid) + - called when game starts. Should return game db id to use now on. If an error occurred, the functions return a non-NULL string error message as the last return value. @@ -137,6 +139,8 @@ static void script_fcdb_functions_define(void) API_TYPE_CONNECTION, API_TYPE_PLAYER, API_TYPE_BOOL, API_TYPE_BOOL); luascript_func_add(fcl, "conn_established", FALSE, 1, 0, API_TYPE_CONNECTION); + luascript_func_add(fcl, "game_start", FALSE, 1, 1, + API_TYPE_INT, API_TYPE_INT); } /**********************************************************************//** diff --git a/server/srv_main.c b/server/srv_main.c index 3713030265..492704074a 100644 --- a/server/srv_main.c +++ b/server/srv_main.c @@ -132,6 +132,7 @@ #include "savemain.h" /* server/scripting */ +#include "script_fcdb.h" #include "script_server.h" #include "luascript_types.h" @@ -3428,11 +3429,16 @@ static void srv_ready(void) } players_iterate_end; } + if (srvarg.fcdb_enabled) { + script_fcdb_call("game_start", 0, &game.server.dbid); + log_debug("dbid: %d", game.server.dbid); + } + CALL_FUNC_EACH_AI(game_start); } /**********************************************************************//** - Initialize game data for the server (corresponds to server_game_free). + Initialize game data for the server (corresponds to server_game_free() ). **************************************************************************/ void server_game_init(bool keep_ruleset_value) { -- 2.40.1