Skip to content

Commit 1af76e2

Browse files
authored
refactor: optimize time formatting function for better performance (opentibiabr#2904)
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.
1 parent 2735b8c commit 1af76e2

8 files changed

Lines changed: 42 additions & 70 deletions

File tree

data-otservbr-global/scripts/quests/dangerous_depth/movements_boss_entrance.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function bossEntrance.onStepIn(creature, item, position, fromPosition, toPositio
3737
if timeLeft > 0 then
3838
player:teleportTo(fromPosition, true)
3939
player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
40-
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to wait " .. getTimeInWords(timeLeft) .. " to face " .. bossName .. " again!")
40+
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to wait " .. Game.getTimeInWords(timeLeft) .. " to face " .. bossName .. " again!")
4141
player:getPosition():sendMagicEffect(CONST_ME_POFF)
4242
return true
4343
end

data-otservbr-global/scripts/quests/feaster_of_souls/actions_portal_brain_head.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function teleportBoss.onStepIn(creature, item, position, fromPosition)
124124
if timeLeft > 0 then
125125
player:teleportTo(config.exitPosition, true)
126126
player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
127-
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to wait " .. getTimeInWords(timeLeft) .. " to face " .. config.bossName .. " again!")
127+
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have to wait " .. Game.getTimeInWords(timeLeft) .. " to face " .. config.bossName .. " again!")
128128
player:getPosition():sendMagicEffect(CONST_ME_POFF)
129129
return false
130130
end

data/events/scripts/player.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ function Player:onLookInBattleList(creature, distance)
216216
if master and table.contains(summons, creature:getName():lower()) then
217217
local familiarSummonTime = master:kv():get("familiar-summon-time") or 0
218218
description = description .. " (Master: " .. master:getName() .. "). \z
219-
It will disappear in " .. getTimeInWords(familiarSummonTime - os.time())
219+
It will disappear in " .. Game.getTimeInWords(familiarSummonTime - os.time())
220220
end
221221
end
222222
if self:getGroup():getAccess() then

data/libs/functions/boss_lever.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ function BossLever:onUse(player)
191191
local currentTime = os.time()
192192
if lastEncounter and currentTime < lastEncounter then
193193
local timeLeft = lastEncounter - currentTime
194-
local timeMessage = getTimeInWords(timeLeft) .. " to face " .. self.name .. " again!"
194+
local timeMessage = Game.getTimeInWords(timeLeft) .. " to face " .. self.name .. " again!"
195195
local message = "You have to wait " .. timeMessage
196196

197197
if currentPlayer ~= player then

data/libs/functions/functions.lua

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -65,43 +65,6 @@ function getTitle(uid)
6565
return false
6666
end
6767

68-
function getTimeInWords(secsParam)
69-
local secs = tonumber(secsParam)
70-
local days = math.floor(secs / (24 * 3600))
71-
secs = secs - (days * 24 * 3600)
72-
local hours, minutes, seconds = getHours(secs), getMinutes(secs), getSeconds(secs)
73-
local timeStr = ""
74-
75-
if days > 0 then
76-
timeStr = days .. (days > 1 and " days" or " day")
77-
end
78-
79-
if hours > 0 then
80-
if timeStr ~= "" then
81-
timeStr = timeStr .. ", "
82-
end
83-
84-
timeStr = timeStr .. hours .. (hours > 1 and " hours" or " hour")
85-
end
86-
87-
if minutes > 0 then
88-
if timeStr ~= "" then
89-
timeStr = timeStr .. ", "
90-
end
91-
92-
timeStr = timeStr .. minutes .. (minutes > 1 and " minutes" or " minute")
93-
end
94-
95-
if seconds > 0 then
96-
if timeStr ~= "" then
97-
timeStr = timeStr .. " and "
98-
end
99-
100-
timeStr = timeStr .. seconds .. (seconds > 1 and " seconds" or " second")
101-
end
102-
return timeStr
103-
end
104-
10568
function getLootRandom(modifier)
10669
local multi = (configManager.getNumber(configKeys.RATE_LOOT) * SCHEDULE_LOOT_RATE) * (modifier or 1)
10770
return math.random(0, MAX_LOOTCHANCE) * 100 / math.max(1, multi)
@@ -949,31 +912,6 @@ function SetInfluenced(monsterType, monster, player, influencedLevel)
949912
monster:setForgeStack(influencedLevel)
950913
end
951914

952-
function getHours(seconds)
953-
return math.floor((seconds / 60) / 60)
954-
end
955-
956-
function getMinutes(seconds)
957-
return math.floor(seconds / 60) % 60
958-
end
959-
960-
function getSeconds(seconds)
961-
return seconds % 60
962-
end
963-
964-
function getTime(seconds)
965-
local hours, minutes = getHours(seconds), getMinutes(seconds)
966-
if minutes > 59 then
967-
minutes = minutes - hours * 60
968-
end
969-
970-
if minutes < 10 then
971-
minutes = "0" .. minutes
972-
end
973-
974-
return hours .. ":" .. minutes .. "h"
975-
end
976-
977915
function ReloadDataEvent(cid)
978916
local player = Player(cid)
979917
if not player then

data/libs/functions/game.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,37 @@ function Game.setStorageValue(key, value)
133133

134134
globalStorageTable[key] = value
135135
end
136+
137+
function Game.getTimeInWords(seconds)
138+
local days = math.floor(seconds / (24 * 3600))
139+
seconds = seconds % (24 * 3600)
140+
local hours = math.floor(seconds / 3600)
141+
seconds = seconds % 3600
142+
local minutes = math.floor(seconds / 60)
143+
seconds = seconds % 60
144+
145+
local timeParts = {}
146+
147+
if days > 0 then
148+
table.insert(timeParts, days .. (days > 1 and " days" or " day"))
149+
end
150+
151+
if hours > 0 then
152+
table.insert(timeParts, hours .. (hours > 1 and " hours" or " hour"))
153+
end
154+
155+
if minutes > 0 then
156+
table.insert(timeParts, minutes .. (minutes > 1 and " minutes" or " minute"))
157+
end
158+
159+
if seconds > 0 or #timeParts == 0 then
160+
table.insert(timeParts, seconds .. (seconds > 1 and " seconds" or " second"))
161+
end
162+
163+
local timeStr = table.concat(timeParts, ", ")
164+
local lastComma = timeStr:find(", [%a%d]+$")
165+
if lastComma then
166+
timeStr = timeStr:sub(1, lastComma - 1) .. " and" .. timeStr:sub(lastComma + 1)
167+
end
168+
return timeStr
169+
end

data/libs/systems/concoctions.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function Concoction:init(player, sendMessage)
158158
return
159159
end
160160
eventPlayer:sendTextMessage(MESSAGE_EVENT_ADVANCE, "Your concoction " .. name .. " is still active for another " .. duration .. ".")
161-
end, 500, player:getId(), self.name, getTimeInWords(self:timeLeft(player)))
161+
end, 500, player:getId(), self.name, Game.getTimeInWords(self:timeLeft(player)))
162162
end
163163
end
164164

@@ -180,7 +180,7 @@ function Concoction:activate(player, item)
180180
local cooldown = self:cooldown()
181181
if self:lastActivatedAt(player) + cooldown > os.time() then
182182
local cooldownLeft = self:lastActivatedAt(player) + cooldown - os.time()
183-
player:sendTextMessage(MESSAGE_FAILURE, "You must wait " .. getTimeInWords(cooldownLeft) .. " before using " .. item:getName() .. " again.")
183+
player:sendTextMessage(MESSAGE_FAILURE, "You must wait " .. Game.getTimeInWords(cooldownLeft) .. " before using " .. item:getName() .. " again.")
184184
return true
185185
end
186186
self:timeLeft(player, self:totalDuration())
@@ -191,7 +191,7 @@ function Concoction:activate(player, item)
191191
self.config.callback(player, self.config)
192192
else
193193
self:addCondition(player)
194-
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have activated " .. item:getName() .. ". It will last for " .. getTimeInWords(self:totalDuration()) .. consumptionString .. ".")
194+
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have activated " .. item:getName() .. ". It will last for " .. Game.getTimeInWords(self:totalDuration()) .. consumptionString .. ".")
195195
if self:tickType() == ConcoctionTickType.Online then
196196
addEvent(tick, updateInterval * 1000, self.id, player:getId(), updateInterval)
197197
end

data/scripts/eventcallbacks/player/on_look.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ local function handleCreatureDescription(inspectedThing, lookDistance)
4444
local monsterMaster = inspectedThing:getMaster()
4545
if monsterMaster and table.contains({ "sorcerer familiar", "knight familiar", "druid familiar", "paladin familiar" }, inspectedThing:getName():lower()) then
4646
local summonTimeRemaining = monsterMaster:kv():get("familiar-summon-time") or 0
47-
descriptionText = string.format("%s (Master: %s). It will disappear in %s", descriptionText, monsterMaster:getName(), getTimeInWords(summonTimeRemaining - os.time()))
47+
descriptionText = string.format("%s (Master: %s). It will disappear in %s", descriptionText, monsterMaster:getName(), Game.getTimeInWords(summonTimeRemaining - os.time()))
4848
end
4949
end
5050

0 commit comments

Comments
 (0)