Skip to content

Commit

Permalink
refactor: server save logic and improve readability
Browse files Browse the repository at this point in the history
Lua code format - (Stylua)

Update global_server_save.lua
  • Loading branch information
omarcopires committed Feb 15, 2024
1 parent 93f362f commit 5a48673
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 61 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,49 @@ 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
addEvent(ServerSave, 60000)
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()

0 comments on commit 5a48673

Please sign in to comment.