-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.lua
More file actions
326 lines (274 loc) · 7.91 KB
/
Utils.lua
File metadata and controls
326 lines (274 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
RaidCalendar = RaidCalendar or {}
---@class RaidCalendar
local M = RaidCalendar
local bot_online = false
local bot_check_time = 0
--- @param hex string
--- @return number r
--- @return number g
--- @return number b
--- @return number a
function M.hex_to_rgba( hex )
local rr, gg, bb, aa = string.match( hex, "^#?(%x%x)(%x%x)(%x%x)(%x?%x?)$" )
local r, g, b = tonumber( rr, 16 ) / 255, tonumber( gg, 16 ) / 255, tonumber( bb, 16 ) / 255
local a = aa ~= "" and tonumber( aa, 16 ) / 255 or 1
return r, g, b, a
end
---@param name string
---@param class string
---@return string
function M.colorize_player_by_class( name, class )
if not class then return name end
local color = RAID_CLASS_COLORS[ string.upper( class ) ]
if not color.colorStr then
color.colorStr = string.format( "ff%02x%02x%02x", color.r * 255, color.g * 255, color.b * 255 )
end
return "|c" .. color.colorStr .. name .. "|r"
end
---@param diff number
---@return string
function M.format_time_difference( diff )
local abs_diff = math.abs( diff )
if abs_diff >= 86400 then
local days = math.floor( abs_diff / 86400 )
if diff < 0 then
return days .. " day" .. (days ~= 1 and "s" or "") .. " ago"
else
return "in " .. days .. " day" .. (days ~= 1 and "s" or "")
end
elseif abs_diff >= 3600 then
local hours = math.floor( (abs_diff + 1800) / 3600 ) -- round to nearest hour
if diff < 0 then
return hours .. " hour" .. (hours ~= 1 and "s" or "") .. " ago"
else
return "in " .. hours .. " hour" .. (hours ~= 1 and "s" or "")
end
else
local minutes = math.floor( (abs_diff + 30) / 60 ) -- round to nearest minute
if diff < 0 then
return minutes .. " minute" .. (minutes ~= 1 and "s" or "") .. " ago"
else
return "in " .. minutes .. " minute" .. (minutes ~= 1 and "s" or "")
end
end
end
---@return number r
---@return number g
---@return number b
---@return number a
---@nodiscard
function M.bot_online_status( is_online )
if is_online then
bot_online = true
elseif time() - bot_check_time > 3600 or (time() - bot_check_time > 30 and not bot_online) then
M.debug( "Checking bot online status" )
bot_check_time = time()
if M.db.user_settings.bot_name and M.db.user_settings.bot_name ~= "" then
M.debug( "Checking guild bot online status" )
for i = 1, GetNumGuildMembers() do
local name, _, _, _, _, _, _, _, online = GetGuildRosterInfo( i )
if name == M.db.user_settings.bot_name and online == 1 then
bot_online = true
break
end
end
elseif M.table_size( M.db.bots ) > 0 then
M.debug( "Checking channel bot online status" )
M.msg_channel.ping( M.db.bots )
end
end
if bot_online then
return 0, 1, 0, 0.9
else
return 1, 0, 0, 0.9
end
end
---@param value string|number
---@param t table
---@param extract_field string?
function M.find( value, t, extract_field )
if type( t ) ~= "table" or M.count( t ) == 0 then return nil end
for i, v in pairs( t ) do
local val = extract_field and v[ extract_field ] or v
if val == value then return v, i end
end
return nil
end
function M.wipe( tbl )
if type( tbl ) ~= "table" then return end
for k in pairs( tbl ) do
tbl[ k ] = nil
end
end
---@param t table
---@return number
function M.count( t )
local count = 0
for _ in pairs( t ) do
count = count + 1
end
return count
end
---@param item_name string
---@param item_quality string
---@return string
function M.get_item_name_colorized( item_name, item_quality )
local color = ITEM_QUALITY_COLORS[ item_quality ]
local link = color.hex .. item_name .. "|r"
return link
end
function M.capitalize_words( str )
return string.gsub( str, "(%w)(%w*)", function( first, rest )
return string.upper( first ) .. string.lower( rest )
end )
end
function M.is_new_version( mine, theirs )
local function parse_version( v )
local parts = {}
for part in string.gmatch( v, "%d+" ) do
table.insert( parts, tonumber( part ) )
end
return parts
end
local my_version = parse_version( mine )
local their_version = parse_version( theirs )
for i = 1, math.max( getn( my_version ), getn( their_version ) ) do
local my_part = my_version[ i ] or 0
local their_part = their_version[ i ] or 0
if their_part > my_part then
return true
elseif their_part < my_part then
return false
end
end
return false
end
---@param scrollbar string
---@param max number
function M.update_scrollbar_buttons( scrollbar, max )
local slider = M.api[ scrollbar ]
local value = math.min( max, slider:GetValue() )
slider:SetMinMaxValues( 0, max )
slider:SetValue( value )
if value == 0 then
M.api[ scrollbar .. "ScrollUpButton" ]:Disable()
else
M.api[ scrollbar .. "ScrollUpButton" ]:Enable()
end
if value == max then
M.api[ scrollbar .. "ScrollDownButton" ]:Disable()
else
M.api[ scrollbar .. "ScrollDownButton" ]:Enable()
end
end
---@param message string
---@param short boolean?
function M.info( message, short )
local tag = string.format( "|c%s%s|r", M.tagcolor, short and "RC" or "RaidCalendar" )
DEFAULT_CHAT_FRAME:AddMessage( string.format( "%s: %s", tag, message ) )
end
---@param message string
function M.error( message )
local tag = string.format( "|c%s%s|r|cffff0000%s|r", M.tagcolor, "RC", "ERROR" )
DEFAULT_CHAT_FRAME:AddMessage( string.format( "%s: %s", tag, message ) )
end
---@param message string
function M.debug( message )
if M.debug_enabled then
M.info( message, true )
end
end
---@param o any
---@return string
function M.dump( o )
--if not o then return "nil" end
if type( o ) ~= 'table' then return tostring( o ) end
local entries = 0
local s = "{"
for k, v in pairs( o ) do
if (entries == 0) then s = s .. " " end
local key = type( k ) ~= "number" and '"' .. k .. '"' or k
if (entries > 0) then s = s .. ", " end
s = s .. "[" .. key .. "] = " .. M.dump( v )
entries = entries + 1
end
if (entries > 0) then s = s .. " " end
return s .. "}"
end
function M.is_array( t )
local count = 0
for k, _ in pairs( t ) do
if type( k ) ~= "number" then return false end
count = count + 1
end
for i = 1, count do
if t[ i ] == nil then return false end
end
return true
end
function M.flatten( value )
local value_type = type( value )
if value_type == "table" then
if M.is_array( value ) then
-- JSON array
local items = {}
for i = 1, getn( value ) do
table.insert( items, M.flatten( value[ i ] ) )
end
return "{" .. table.concat( items, ", " ) .. "}"
else
-- JSON object
local items = {}
for k, v in pairs( value ) do
table.insert( items, '["' .. tostring( k ) .. '"]=' .. M.flatten( v ) )
end
return "{" .. table.concat( items, "," ) .. "}"
end
elseif value_type == "string" then
return '"' .. string.gsub( value, '"', '\\"' ) .. '"'
elseif value_type == "number" or value_type == "boolean" then
return tostring( value )
elseif value_type == "nil" then
return "null"
end
error( "Unsupported type: " .. value_type )
end
---@param table table
---@return integer size
function M.table_size( table )
local size = 0
for _ in table do
size = size + 1
end
return size
end
function M.explode( str, delimiter )
delimiter = delimiter or ":"
local result = {}
local from = 1
local delim_from, delim_to = string.find( str, delimiter, from, true )
while delim_from do
table.insert( result, string.sub( str, from, delim_from - 1 ) )
from = delim_to + 1
delim_from, delim_to = string.find( str, delimiter, from, true )
end
table.insert( result, string.sub( str, from ) )
return result
end
---@diagnostic disable-next-line: undefined-field
if not string.gmatch then string.gmatch = string.gfind end
---@diagnostic disable-next-line: duplicate-set-field
string.match = function( str, pattern )
if not str then return nil end
local _, _, r1, r2, r3, r4, r5, r6, r7, r8, r9 = string.find( str, pattern )
return r1, r2, r3, r4, r5, r6, r7, r8, r9
end
---@diagnostic disable-next-line: lowercase-global
---@return string
strtrim = strtrim or function( s )
if type( s ) ~= "string" then
return ""
end
return string.match( s, "^%s*(.-)%s*$" )
end
math.huge = 1e99