From 799c3366b6f8b734329e223a56daedbd9aac9405 Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 15 Feb 2024 16:53:29 -0300 Subject: [PATCH] refactor: server save logic and improve readability (#2243) --- .../others/global_server_save.lua | 47 ------------------- .../globalevents/global_server_save.lua | 31 ++++++------ 2 files changed, 17 insertions(+), 61 deletions(-) delete mode 100644 data-otservbr-global/scripts/globalevents/others/global_server_save.lua rename {data-canary => data}/scripts/globalevents/global_server_save.lua (59%) diff --git a/data-otservbr-global/scripts/globalevents/others/global_server_save.lua b/data-otservbr-global/scripts/globalevents/others/global_server_save.lua deleted file mode 100644 index 84303a356da..00000000000 --- a/data-otservbr-global/scripts/globalevents/others/global_server_save.lua +++ /dev/null @@ -1,47 +0,0 @@ -local function ServerSave() - if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_CLEAN_MAP) then - cleanMap() - end - if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_CLOSE) then - Game.setGameState(GAME_STATE_CLOSED, true) - end - if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_SHUTDOWN) then - Game.setGameState(GAME_STATE_SHUTDOWN, true) - end - -- Updating daily reward next server save. - UpdateDailyRewardGlobalStorage(DailyReward.storages.lastServerSave, os.time()) -end - -local function ServerSaveWarning(time) - -- minus one minutes - local remainingTime = tonumber(time) - 60000 - if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_NOTIFY_MESSAGE) then - local message = "Server is saving game in " .. (remainingTime / 60000) .. " minute(s). Please logout." - Webhook.sendMessage(":floppy_disk: " .. message, announcementChannels["serverAnnouncements"]) - Game.broadcastMessage(message, MESSAGE_GAME_HIGHLIGHT) - end - -- if greater than one minute, schedule another warning - -- else the next event will be the server save - if remainingTime > 60000 then - addEvent(ServerSaveWarning, 60000, remainingTime) - else - addEvent(ServerSave, 60000) - end -end - --- Function that is called by the global events when it reaches the time configured --- interval is the time between the event start and the the effective save, it will send an notify message every minute -local serverSaveEvent = GlobalEvent("serversave") -function serverSaveEvent.onTime(interval) - local remainingTime = configManager.getNumber(configKeys.GLOBAL_SERVER_SAVE_NOTIFY_DURATION) * 60000 - if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_NOTIFY_MESSAGE) then - local message = "Server is saving game in " .. (remainingTime / 60000) .. " minute(s). Please logout." - Webhook.sendMessage(":floppy_disk: " .. message, announcementChannels["serverAnnouncements"]) - Game.broadcastMessage(message, MESSAGE_GAME_HIGHLIGHT) - end - addEvent(ServerSaveWarning, 60000, remainingTime) -- Schedule next event in 1 minute(60000) - return not configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_SHUTDOWN) -end - -serverSaveEvent:time(configManager.getString(configKeys.GLOBAL_SERVER_SAVE_TIME)) -serverSaveEvent:register() diff --git a/data-canary/scripts/globalevents/global_server_save.lua b/data/scripts/globalevents/global_server_save.lua similarity index 59% rename from data-canary/scripts/globalevents/global_server_save.lua rename to data/scripts/globalevents/global_server_save.lua index 51e1ed1fc35..00eaebbc3c3 100644 --- a/data-canary/scripts/globalevents/global_server_save.lua +++ b/data/scripts/globalevents/global_server_save.lua @@ -2,26 +2,26 @@ local function ServerSave() if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_CLEAN_MAP) then cleanMap() end + if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_CLOSE) then Game.setGameState(GAME_STATE_CLOSED) - end - if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_SHUTDOWN) then + elseif configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_SHUTDOWN) then Game.setGameState(GAME_STATE_SHUTDOWN) end - -- Updating daily reward next server save + + -- Update daily reward next server save timestamp UpdateDailyRewardGlobalStorage(DailyReward.storages.lastServerSave, os.time()) end local function ServerSaveWarning(time) - -- minus one minutes + -- Calculate remaining time, minus one minute local remainingTime = tonumber(time) - 60000 if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_NOTIFY_MESSAGE) then - local message = "Server is saving game in " .. (remainingTime / 60000) .. " minute(s). Please logout." + local message = "Server is saving the game in " .. (remainingTime / 60000) .. " minute(s). Please logout." Webhook.sendMessage("Server save", message, WEBHOOK_COLOR_WARNING) Game.broadcastMessage(message, MESSAGE_GAME_HIGHLIGHT) end - -- if greater than one minute, schedule another warning - -- else the next event will be the server save + if remainingTime > 60000 then addEvent(ServerSaveWarning, 60000, remainingTime) else @@ -29,19 +29,22 @@ local function ServerSaveWarning(time) end end +local globalServerSave = GlobalEvent("GlobalServerSave") + -- Function that is called by the global events when it reaches the time configured --- interval is the time between the event start and the the effective save, it will send an notify message every minute -local serversave = GlobalEvent("serversave") -function serversave.onTime(interval) +-- Interval is the time between the event start and the effective save, it will send a notify message every minute +function globalServerSave.onTime(interval) local remainingTime = configManager.getNumber(configKeys.GLOBAL_SERVER_SAVE_NOTIFY_DURATION) * 60000 if configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_NOTIFY_MESSAGE) then - local message = "Server is saving game in " .. (remainingTime / 60000) .. " minute(s). Please logout." + local message = "Server is saving the game in " .. (remainingTime / 60000) .. " minute(s). Please logout." Webhook.sendMessage("Server save", message, WEBHOOK_COLOR_WARNING) Game.broadcastMessage(message, MESSAGE_GAME_HIGHLIGHT) end - addEvent(ServerSaveWarning, 60000, remainingTime) -- Schedule next event in 1 minute(60000) + + -- Schedule the next warning event in 1 minute (60000 milliseconds) + addEvent(ServerSaveWarning, 60000, remainingTime) return not configManager.getBoolean(configKeys.GLOBAL_SERVER_SAVE_SHUTDOWN) end -serversave:time(configManager.getString(configKeys.GLOBAL_SERVER_SAVE_TIME)) -serversave:register() +globalServerSave:time(configManager.getString(configKeys.GLOBAL_SERVER_SAVE_TIME)) +globalServerSave:register()