Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: server save logic and improve readability #2243

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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()
Loading