Skip to content

Commit c335f3b

Browse files
committed
Minor cleanups, only keep locals where they're needed
1 parent 9841cb7 commit c335f3b

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

lua/custom_chat/client/main.lua

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,49 +32,56 @@ function CustomChat.GetLanguageText( id )
3232
return language.GetPhrase( "custom_chat." .. id )
3333
end
3434

35-
local year = 60 * 60 * 24 * 365
36-
local month = 60 * 60 * 24 * 30
37-
local day = 60 * 60 * 24
38-
local hour = 60 * 60
39-
local minute = 60
40-
function CustomChat.NiceTime( time )
41-
local L = CustomChat.GetLanguageText
35+
do
36+
local year = 60 * 60 * 24 * 365
37+
local month = 60 * 60 * 24 * 30
38+
local day = 60 * 60 * 24
39+
local hour = 60 * 60
40+
local minute = 60
41+
42+
function CustomChat.NiceTime( time )
43+
local L = CustomChat.GetLanguageText
44+
45+
local timeUnits = {
46+
{ value = Floor( time / year ), name = "time.years" },
47+
{ value = Floor( time / month ) % 12, name = "time.months" },
48+
{ value = Floor( time / day ) % 30, name = "time.days" },
49+
{ value = Floor( time / hour ) % 24, name = "time.hours" },
50+
{ value = Floor( time / minute ) % 60, name = "time.minutes" },
51+
{ value = time % 60, name = "time.seconds" }
52+
}
53+
54+
local nonZeroUnits = {}
4255

43-
local timeUnits = {
44-
{ value = math.floor( time / year ), name = "time.years" },
45-
{ value = math.floor( time / month ) % 12, name = "time.months" },
46-
{ value = math.floor( time / day ) % 30, name = "time.days" },
47-
{ value = math.floor( time / hour ) % 24, name = "time.hours" },
48-
{ value = math.floor( time / minute ) % 60, name = "time.minutes" },
49-
{ value = time % 60, name = "time.seconds" }
50-
}
51-
52-
local nonZeroUnits = {}
53-
for _, unit in ipairs( timeUnits ) do
54-
if unit.value > 0 then
55-
table.insert( nonZeroUnits, unit )
56+
for _, unit in ipairs( timeUnits ) do
57+
if unit.value > 0 then
58+
table.insert( nonZeroUnits, unit )
59+
end
5660
end
57-
end
5861

59-
local selectedUnits = {}
60-
local unitsToShow = 1
61-
if time > month then
62-
unitsToShow = 2
63-
end
64-
for i = 1, math.min( unitsToShow, #nonZeroUnits ) do
65-
table.insert( selectedUnits, nonZeroUnits[i] )
66-
end
62+
local selectedUnits = {}
63+
local unitsToShow = 1
6764

68-
if #selectedUnits == 0 then
69-
return "0 " .. L( "time.seconds" )
70-
end
65+
if time > month then
66+
unitsToShow = 2
67+
end
7168

72-
local parts = {}
73-
for _, unit in ipairs( selectedUnits ) do
74-
table.insert( parts, unit.value .. " " .. L( unit.name ) )
75-
end
69+
for i = 1, math.min( unitsToShow, #nonZeroUnits ) do
70+
table.insert( selectedUnits, nonZeroUnits[i] )
71+
end
72+
73+
if #selectedUnits == 0 then
74+
return "0 " .. L( "time.seconds" )
75+
end
7676

77-
return table.concat( parts, ", " )
77+
local parts = {}
78+
79+
for _, unit in ipairs( selectedUnits ) do
80+
table.insert( parts, unit.value .. " " .. L( unit.name ) )
81+
end
82+
83+
return table.concat( parts, ", " )
84+
end
7885
end
7986

8087
function CustomChat.PrintMessage( text )

0 commit comments

Comments
 (0)