From 1af76e2e04951fe048da5d319c5c3e57a1dc5d6e Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 17 Oct 2024 22:06:04 -0300 Subject: [PATCH] refactor: optimize time formatting function for better performance (#2904) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refactors the getTimeInWords function to improve performance and code readability. The new implementation adopts a more efficient approach for calculating and formatting time in days, hours, minutes, and seconds, reducing redundancy and simplifying the logical flow. • Optimized the function for better execution speed. • Simplified the conditions and formatting structure. • Maintained the same functionality with clearer and more efficient code. --- .../movements_boss_entrance.lua | 2 +- .../actions_portal_brain_head.lua | 2 +- data/events/scripts/player.lua | 2 +- data/libs/functions/boss_lever.lua | 2 +- data/libs/functions/functions.lua | 62 ------------------- data/libs/functions/game.lua | 34 ++++++++++ data/libs/systems/concoctions.lua | 6 +- .../scripts/eventcallbacks/player/on_look.lua | 2 +- 8 files changed, 42 insertions(+), 70 deletions(-) diff --git a/data-otservbr-global/scripts/quests/dangerous_depth/movements_boss_entrance.lua b/data-otservbr-global/scripts/quests/dangerous_depth/movements_boss_entrance.lua index 20300f3ac5f..a33dccd762d 100644 --- a/data-otservbr-global/scripts/quests/dangerous_depth/movements_boss_entrance.lua +++ b/data-otservbr-global/scripts/quests/dangerous_depth/movements_boss_entrance.lua @@ -37,7 +37,7 @@ function bossEntrance.onStepIn(creature, item, position, fromPosition, toPositio if timeLeft > 0 then player:teleportTo(fromPosition, true) player:getPosition():sendMagicEffect(CONST_ME_TELEPORT) - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to wait " .. getTimeInWords(timeLeft) .. " to face " .. bossName .. " again!") + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to wait " .. Game.getTimeInWords(timeLeft) .. " to face " .. bossName .. " again!") player:getPosition():sendMagicEffect(CONST_ME_POFF) return true end diff --git a/data-otservbr-global/scripts/quests/feaster_of_souls/actions_portal_brain_head.lua b/data-otservbr-global/scripts/quests/feaster_of_souls/actions_portal_brain_head.lua index aebc0b3ca7a..badaffad5a2 100644 --- a/data-otservbr-global/scripts/quests/feaster_of_souls/actions_portal_brain_head.lua +++ b/data-otservbr-global/scripts/quests/feaster_of_souls/actions_portal_brain_head.lua @@ -124,7 +124,7 @@ function teleportBoss.onStepIn(creature, item, position, fromPosition) if timeLeft > 0 then player:teleportTo(config.exitPosition, true) player:getPosition():sendMagicEffect(CONST_ME_TELEPORT) - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to wait " .. getTimeInWords(timeLeft) .. " to face " .. config.bossName .. " again!") + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to wait " .. Game.getTimeInWords(timeLeft) .. " to face " .. config.bossName .. " again!") player:getPosition():sendMagicEffect(CONST_ME_POFF) return false end diff --git a/data/events/scripts/player.lua b/data/events/scripts/player.lua index ec1a92f3528..eee28b632d6 100644 --- a/data/events/scripts/player.lua +++ b/data/events/scripts/player.lua @@ -216,7 +216,7 @@ function Player:onLookInBattleList(creature, distance) if master and table.contains(summons, creature:getName():lower()) then local familiarSummonTime = master:kv():get("familiar-summon-time") or 0 description = description .. " (Master: " .. master:getName() .. "). \z - It will disappear in " .. getTimeInWords(familiarSummonTime - os.time()) + It will disappear in " .. Game.getTimeInWords(familiarSummonTime - os.time()) end end if self:getGroup():getAccess() then diff --git a/data/libs/functions/boss_lever.lua b/data/libs/functions/boss_lever.lua index 9cd577ee911..b1141619ce2 100644 --- a/data/libs/functions/boss_lever.lua +++ b/data/libs/functions/boss_lever.lua @@ -191,7 +191,7 @@ function BossLever:onUse(player) local currentTime = os.time() if lastEncounter and currentTime < lastEncounter then local timeLeft = lastEncounter - currentTime - local timeMessage = getTimeInWords(timeLeft) .. " to face " .. self.name .. " again!" + local timeMessage = Game.getTimeInWords(timeLeft) .. " to face " .. self.name .. " again!" local message = "You have to wait " .. timeMessage if currentPlayer ~= player then diff --git a/data/libs/functions/functions.lua b/data/libs/functions/functions.lua index c2349ce7fd6..2327a1cf937 100644 --- a/data/libs/functions/functions.lua +++ b/data/libs/functions/functions.lua @@ -65,43 +65,6 @@ function getTitle(uid) return false end -function getTimeInWords(secsParam) - local secs = tonumber(secsParam) - local days = math.floor(secs / (24 * 3600)) - secs = secs - (days * 24 * 3600) - local hours, minutes, seconds = getHours(secs), getMinutes(secs), getSeconds(secs) - local timeStr = "" - - if days > 0 then - timeStr = days .. (days > 1 and " days" or " day") - end - - if hours > 0 then - if timeStr ~= "" then - timeStr = timeStr .. ", " - end - - timeStr = timeStr .. hours .. (hours > 1 and " hours" or " hour") - end - - if minutes > 0 then - if timeStr ~= "" then - timeStr = timeStr .. ", " - end - - timeStr = timeStr .. minutes .. (minutes > 1 and " minutes" or " minute") - end - - if seconds > 0 then - if timeStr ~= "" then - timeStr = timeStr .. " and " - end - - timeStr = timeStr .. seconds .. (seconds > 1 and " seconds" or " second") - end - return timeStr -end - function getLootRandom(modifier) local multi = (configManager.getNumber(configKeys.RATE_LOOT) * SCHEDULE_LOOT_RATE) * (modifier or 1) return math.random(0, MAX_LOOTCHANCE) * 100 / math.max(1, multi) @@ -949,31 +912,6 @@ function SetInfluenced(monsterType, monster, player, influencedLevel) monster:setForgeStack(influencedLevel) end -function getHours(seconds) - return math.floor((seconds / 60) / 60) -end - -function getMinutes(seconds) - return math.floor(seconds / 60) % 60 -end - -function getSeconds(seconds) - return seconds % 60 -end - -function getTime(seconds) - local hours, minutes = getHours(seconds), getMinutes(seconds) - if minutes > 59 then - minutes = minutes - hours * 60 - end - - if minutes < 10 then - minutes = "0" .. minutes - end - - return hours .. ":" .. minutes .. "h" -end - function ReloadDataEvent(cid) local player = Player(cid) if not player then diff --git a/data/libs/functions/game.lua b/data/libs/functions/game.lua index e4d40bef318..a7c7f7617ce 100644 --- a/data/libs/functions/game.lua +++ b/data/libs/functions/game.lua @@ -133,3 +133,37 @@ function Game.setStorageValue(key, value) globalStorageTable[key] = value end + +function Game.getTimeInWords(seconds) + local days = math.floor(seconds / (24 * 3600)) + seconds = seconds % (24 * 3600) + local hours = math.floor(seconds / 3600) + seconds = seconds % 3600 + local minutes = math.floor(seconds / 60) + seconds = seconds % 60 + + local timeParts = {} + + if days > 0 then + table.insert(timeParts, days .. (days > 1 and " days" or " day")) + end + + if hours > 0 then + table.insert(timeParts, hours .. (hours > 1 and " hours" or " hour")) + end + + if minutes > 0 then + table.insert(timeParts, minutes .. (minutes > 1 and " minutes" or " minute")) + end + + if seconds > 0 or #timeParts == 0 then + table.insert(timeParts, seconds .. (seconds > 1 and " seconds" or " second")) + end + + local timeStr = table.concat(timeParts, ", ") + local lastComma = timeStr:find(", [%a%d]+$") + if lastComma then + timeStr = timeStr:sub(1, lastComma - 1) .. " and" .. timeStr:sub(lastComma + 1) + end + return timeStr +end diff --git a/data/libs/systems/concoctions.lua b/data/libs/systems/concoctions.lua index 72547a8a1ab..ee856e16482 100644 --- a/data/libs/systems/concoctions.lua +++ b/data/libs/systems/concoctions.lua @@ -158,7 +158,7 @@ function Concoction:init(player, sendMessage) return end eventPlayer:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your concoction " .. name .. " is still active for another " .. duration .. ".") - end, 500, player:getId(), self.name, getTimeInWords(self:timeLeft(player))) + end, 500, player:getId(), self.name, Game.getTimeInWords(self:timeLeft(player))) end end @@ -180,7 +180,7 @@ function Concoction:activate(player, item) local cooldown = self:cooldown() if self:lastActivatedAt(player) + cooldown > os.time() then local cooldownLeft = self:lastActivatedAt(player) + cooldown - os.time() - player:sendTextMessage(MESSAGE_FAILURE, "You must wait " .. getTimeInWords(cooldownLeft) .. " before using " .. item:getName() .. " again.") + player:sendTextMessage(MESSAGE_FAILURE, "You must wait " .. Game.getTimeInWords(cooldownLeft) .. " before using " .. item:getName() .. " again.") return true end self:timeLeft(player, self:totalDuration()) @@ -191,7 +191,7 @@ function Concoction:activate(player, item) self.config.callback(player, self.config) else self:addCondition(player) - player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have activated " .. item:getName() .. ". It will last for " .. getTimeInWords(self:totalDuration()) .. consumptionString .. ".") + player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have activated " .. item:getName() .. ". It will last for " .. Game.getTimeInWords(self:totalDuration()) .. consumptionString .. ".") if self:tickType() == ConcoctionTickType.Online then addEvent(tick, updateInterval * 1000, self.id, player:getId(), updateInterval) end diff --git a/data/scripts/eventcallbacks/player/on_look.lua b/data/scripts/eventcallbacks/player/on_look.lua index 68824cb9b44..e0a1ab86788 100644 --- a/data/scripts/eventcallbacks/player/on_look.lua +++ b/data/scripts/eventcallbacks/player/on_look.lua @@ -44,7 +44,7 @@ local function handleCreatureDescription(inspectedThing, lookDistance) local monsterMaster = inspectedThing:getMaster() if monsterMaster and table.contains({ "sorcerer familiar", "knight familiar", "druid familiar", "paladin familiar" }, inspectedThing:getName():lower()) then local summonTimeRemaining = monsterMaster:kv():get("familiar-summon-time") or 0 - descriptionText = string.format("%s (Master: %s). It will disappear in %s", descriptionText, monsterMaster:getName(), getTimeInWords(summonTimeRemaining - os.time())) + descriptionText = string.format("%s (Master: %s). It will disappear in %s", descriptionText, monsterMaster:getName(), Game.getTimeInWords(summonTimeRemaining - os.time())) end end