Skip to content

Commit

Permalink
Refactor: simplify reload and save code
Browse files Browse the repository at this point in the history
Lua code format - (Stylua)

maintains alphabetical order of reload

Update save.lua

fix: log messages
  • Loading branch information
omarcopires committed Feb 15, 2024
1 parent 446a915 commit 964f25e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 86 deletions.
12 changes: 11 additions & 1 deletion data/libs/systems/hireling.lua
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,18 @@ end
-- [[ GLOBAL FUNCTIONS DEFINITIONS ]]

function SaveHirelings()
local saved = true
for _, hireling in ipairs(HIRELINGS) do
hireling:save()
if not hireling:save() then
saved = false
break
end
end

if saved then
logger.info("Hirelings successfully saved.")
else
logger.warn("Failed to save hirelings. Please check the logs for details.")
end
end

Expand Down
8 changes: 1 addition & 7 deletions data/scripts/globalevents/hireling_save.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
local hirelingSave = GlobalEvent("HirelingSave")

function hirelingSave.onShutdown()
local saved = SaveHirelings()
if saved then
logger.info("[Server Shutdown] Hirelings successfully saved.")
else
logger.warn("[Server Shutdown] Failed to save hirelings. Please check the logs for details.")
end

SaveHirelings()
return true
end

Expand Down
109 changes: 42 additions & 67 deletions data/scripts/talkactions/god/reload.lua
Original file line number Diff line number Diff line change
@@ -1,95 +1,70 @@
-- NOTE: Using this script might cause unwanted changes.
-- This script forces a reload in the entire server, this means
-- that everything that is stored in memory might stop to work
-- properly and/or completely.
--
-- This script should be used in test environments only.
local reloadTypes = {
["all"] = RELOAD_TYPE_ALL,
["channel"] = RELOAD_TYPE_CHAT,
["chat"] = RELOAD_TYPE_CHAT,
["chatchannels"] = RELOAD_TYPE_CHAT,
["config"] = RELOAD_TYPE_CONFIG,
["configuration"] = RELOAD_TYPE_CONFIG,
["core"] = RELOAD_TYPE_CORE,
["events"] = RELOAD_TYPE_EVENTS,
["global"] = RELOAD_TYPE_CORE,
["group"] = RELOAD_TYPE_GROUPS,
["groups"] = RELOAD_TYPE_GROUPS,
["imbuements"] = RELOAD_TYPE_IMBUEMENTS,
["items"] = RELOAD_TYPE_ITEMS,
["lib"] = RELOAD_TYPE_CORE,
["libs"] = RELOAD_TYPE_CORE,
["module"] = RELOAD_TYPE_MODULES,
["modules"] = RELOAD_TYPE_MODULES,
["monster"] = RELOAD_TYPE_MONSTERS,
["monsters"] = RELOAD_TYPE_MONSTERS,
["mount"] = RELOAD_TYPE_MOUNTS,
["mounts"] = RELOAD_TYPE_MOUNTS,
["npc"] = RELOAD_TYPE_NPCS,
["npcs"] = RELOAD_TYPE_NPCS,
["raid"] = RELOAD_TYPE_RAIDS,
["raids"] = RELOAD_TYPE_RAIDS,
["rate"] = RELOAD_TYPE_CORE,
["rates"] = RELOAD_TYPE_CORE,
["script"] = RELOAD_TYPE_SCRIPTS,
["scripts"] = RELOAD_TYPE_SCRIPTS,
["stage"] = RELOAD_TYPE_CORE,
["stages"] = RELOAD_TYPE_CORE,
}

function Player.reloadTalkaction(self, words, param)
local reloadTypes = {
["all"] = RELOAD_TYPE_ALL,

["chat"] = RELOAD_TYPE_CHAT,
["channel"] = RELOAD_TYPE_CHAT,
["chatchannels"] = RELOAD_TYPE_CHAT,

["config"] = RELOAD_TYPE_CONFIG,
["configuration"] = RELOAD_TYPE_CONFIG,

["events"] = RELOAD_TYPE_EVENTS,

["items"] = RELOAD_TYPE_ITEMS,

["module"] = RELOAD_TYPE_MODULES,
["modules"] = RELOAD_TYPE_MODULES,

["monster"] = RELOAD_TYPE_MONSTERS,
["monsters"] = RELOAD_TYPE_MONSTERS,

["mount"] = RELOAD_TYPE_MOUNTS,
["mounts"] = RELOAD_TYPE_MOUNTS,

["npc"] = RELOAD_TYPE_NPCS,
["npcs"] = RELOAD_TYPE_NPCS,

["raid"] = RELOAD_TYPE_RAIDS,
["raids"] = RELOAD_TYPE_RAIDS,

["scripts"] = RELOAD_TYPE_SCRIPTS,
["script"] = RELOAD_TYPE_SCRIPTS,

["rate"] = RELOAD_TYPE_CORE,
["rates"] = RELOAD_TYPE_CORE,
["stage"] = RELOAD_TYPE_CORE,
["stages"] = RELOAD_TYPE_CORE,
["global"] = RELOAD_TYPE_CORE,
["core"] = RELOAD_TYPE_CORE,
["lib"] = RELOAD_TYPE_CORE,
["libs"] = RELOAD_TYPE_CORE,

["imbuements"] = RELOAD_TYPE_IMBUEMENTS,

["group"] = RELOAD_TYPE_GROUPS,
["groups"] = RELOAD_TYPE_GROUPS,
}
local reload = TalkAction("/reload")

function reload.onSay(player, words, param)
if not configManager.getBoolean(configKeys.ALLOW_RELOAD) then
self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Reload command is disabled.")
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Reload command is disabled.")
return true
end

if param == "" then
self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Command param required.")
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Command param required.")
return true
end

-- create log
logCommand(self, "/reload", param)
logCommand(player, "/reload", param)

local reloadType = reloadTypes[param:lower()]
if reloadType then
-- Force save server before reload
saveServer()
SaveHirelings()
logger.info("Saved Hirelings")
self:sendTextMessage(MESSAGE_ADMINISTRADOR, "Server is saved.. Now will reload configs!")

Game.reload(reloadType)
self:sendTextMessage(MESSAGE_LOOK, string.format("Reloaded %s.", param:lower()))
logger.info("Reloaded {}", param:lower())

player:sendTextMessage(MESSAGE_LOOK, string.format("Reloaded %s.", param:lower()))
player:sendTextMessage(MESSAGE_ADMINISTRADOR, "Server is saved. Now will reload configs!")
elseif not reloadType then
self:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Reload type not found.")
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Reload type not found.")
logger.warn("[reload.onSay] - Reload type '{}' not found", param)
end
return true
end

local reload = TalkAction("/reload")

function reload.onSay(player, words, param)
return player:reloadTalkaction(words, param)
end

reload:separator(" ")
reload:groupType("god")
reload:register()
18 changes: 7 additions & 11 deletions data/scripts/talkactions/god/save.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
local savingEvent = 0
function saveLoop(delay)
saveServer()
SaveHirelings()
logger.info("Saved Hirelings")
if delay > 0 then
savingEvent = addEvent(saveLoop, delay, delay)
end
end

local save = TalkAction("/save")

Expand All @@ -16,13 +8,17 @@ function save.onSay(player, words, param)

if isNumber(param) then
stopEvent(savingEvent)
saveLoop(tonumber(param) * 60 * 1000)
local delay = tonumber(param) * 60 * 1000
savingEvent = addEvent(function()
saveServer()
SaveHirelings()
end, delay, delay)
else
saveServer()
SaveHirelings()
logger.info("Saved Hirelings")
player:sendTextMessage(MESSAGE_ADMINISTRADOR, "Server is saved ...")
player:sendTextMessage(MESSAGE_ADMINISTRADOR, "Server has been saved.")
end
return true
end

save:separator(" ")
Expand Down

0 comments on commit 964f25e

Please sign in to comment.