-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHelperFunctions.lua
296 lines (241 loc) · 7.03 KB
/
HelperFunctions.lua
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
HelperFunctions = {}
local HF = HelperFunctions
local Hbd = LibStub("HereBeDragons-2.0")
local HbdPins = LibStub("HereBeDragons-Pins-2.0")
function HF.GetCatalogIdFromGuid(guid)
if (not guid) then return nil end
local splitGuid = HF.SplitString(guid)
if splitGuid[1] == "GameObject" then return "go" .. splitGuid[6] -- goID
elseif splitGuid[1] == "Player" then return splitGuid[2] .. "-" .. splitGuid[3] -- serverID-playerUID
elseif splitGuid[1] == "Pet" then return "pet" .. splitGuid[6] -- petID
elseif splitGuid[1] == "Creature" then return tonumber(splitGuid[6]) -- ID
elseif splitGuid[1] == "Vehicle" then return "v" .. splitGuid[6] -- vID
elseif guid == "" then return "_unknown_"
else error("Unsupported GUID: '" .. guid .. "'.")
end
end
function HF.GetCoordinatesByUnitId(unitId)
local worldX, worldY, worldInstance = Hbd:GetUnitWorldPosition(unitId)
local mapId = C_Map.GetBestMapForUnit(unitId)
local x = nil
local y = nil
if (mapId) then
local position = C_Map.GetPlayerMapPosition(mapId, unitId)
if (position) then
x = HF.Round(position.x * 100, 2)
y = HF.Round(position.y * 100, 2)
end
end
return Coordinates.New(worldInstance, mapId, x, y)
end
function HF.GetGameVersion()
local v1, v2, v3 = strsplit(".", GetBuildInfo())
v1 = tonumber(v1)
v2 = tonumber(v2)
v3 = tonumber(v3)
return v1, v2, v3
end
function HF.GetUnitTypeFromCatalogUnitId(cuid)
if (cuid == "_unknown_") then
return AutoBiographerEnum.UnitType.Unknown
elseif (string.match(cuid, "go%d+")) then
return AutoBiographerEnum.UnitType.GameObject
elseif (string.match(cuid, "%w+%-%w+")) then
return AutoBiographerEnum.UnitType.Player
elseif (string.match(cuid, "pet%d+")) then
return AutoBiographerEnum.UnitType.Pet
else
return AutoBiographerEnum.UnitType.Creature
end
end
function HF.GetItemIdFromTextWithChatItemLink(textWithChatItemLink)
for idMatch in string.gmatch(textWithChatItemLink, "item:%d+") do
return string.sub(idMatch, 6, #idMatch)
end
end
-- Lua Helpers
function HF.RemoveElementsFromArrayAtIndexes(array, indexesToRemove)
local originalLength = #array
-- Delete entries in the array.
for i = 1, originalLength do
if (indexesToRemove[i]) then
array[i] = nil
end
end
-- Compact the array.
local j = 0
for i = 1, originalLength do
if (array[i] ~= nil) then
j = j + 1
array[j] = array[i]
end
end
for i = j + 1, originalLength do
array[i] = nil
end
end
function HF.Round(number, precision)
if (not precision) then precision = 0 end
local factor = math.pow(10, precision)
number = number * factor
number = math.floor(number + 0.5)
return number / factor
end
function HF.SplitString(str)
local index = 0
local splitString = {}
for text in string.gmatch(str, "[%w|%d]+") do
index = index + 1
splitString[index] = text
end
return splitString
end
function HF.GetTableLength(tab)
local count = 0
for _ in pairs(tab) do count = count + 1 end
return count
end
function HF.GetKeysFromTable(tab, sort)
if (not tab) then return nil end
local keys = {}
local index = 0
for k,v in pairs(tab) do
index = index + 1
keys[index] = k
end
if (sort) then table.sort(keys) end
return keys
end
function HF.GetKeysFromTableReverse(tab, sort)
if (not tab) then return nil end
local keys = HF.GetKeysFromTable(tab, sort)
local reverseKeys = {}
local index = 0
for i = #keys, 1, -1 do
index = index + 1
reverseKeys[index] = keys[i]
end
return reverseKeys
end
function HF.PrintKeysAndValuesFromTable(tab, noRecurse, indentLevel)
if tab == nil then return end
local indentation = ""
if (not indentLevel) then
indentLevel = 0
end
for i = 1, indentLevel do
indentation = indentation .. " "
end
for k,v in pairs(tab) do
print(indentation .. k .. ": " .. tostring(v))
if (not noRecurse and type(v) == "table") then
HF.PrintKeysAndValuesFromTable(v, noRecurse, indentLevel + 1)
end
end
end
function HF.GetLastKeyFromTable(tab)
local keys = HF.GetKeysFromTable(tab, true)
return keys[#keys]
end
function HF.SubtractFloats(left, right, precision)
if (not precision) then precision = 1 end
local difference = left - right
return HF.Round(difference, precision)
end
-- Text Formatting Helpers
function HF.AbbreviatedValue(n)
local sign = 1
if (n < 0) then
sign = -1
n = -n
end
local c = ""
local val = n
if (n >= 1000000000) then
c = "B"
val = val / 1000000000
elseif (n >= 1000000) then
c = "M"
val = val / 1000000
elseif (n >= 1000) then
c = "K"
val = val / 1000
end
return tostring(HF.Round(val, 0)) .. c
end
function HF.CommaValue(n) -- credit http://richard.warburton.it
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
function HF.SecondsToTimeString(totalSeconds, shorten)
if totalSeconds == nil then return "" end
local days = math.floor(totalSeconds / 86400)
local hours = math.floor(totalSeconds / 3600) % 24
local minutes = math.floor(totalSeconds / 60) % 60
local seconds = HF.Round(totalSeconds % 60)
local printDays = false
local printHours = false
local printMinutes = false
if (days > 0) then
printDays = true
printHours = true
printMinutes = true
elseif (hours > 0) then
printHours = true
printMinutes = true
elseif (minutes > 0) then
printMinutes = true
end
local returnString = ""
if (printDays) then
returnString = returnString .. days
if (shorten) then
returnString = returnString .. "d "
else
returnString = returnString .. " day"
if (days ~= 1) then returnString = returnString .. "s" end
returnString = returnString .. ", "
end
end
if (printHours) then
returnString = returnString .. hours
if (shorten) then
returnString = returnString .. "h "
else
returnString = returnString .. " hour"
if (hours ~= 1) then returnString = returnString .. "s" end
returnString = returnString .. ", "
end
end
if (printMinutes) then
returnString = returnString .. minutes
if (shorten) then
returnString = returnString .. "m "
else
returnString = returnString .. " minute"
if (minutes ~= 1) then returnString = returnString .. "s" end
returnString = returnString .. ", "
end
end
if (shorten) then
if (returnString:len() > 0) then returnString = returnString:sub(1, -2) end
else
returnString = returnString .. seconds .. " second"
if (seconds ~= 1) then returnString = returnString .. "s" end
end
return returnString
end
function HF.ShortenString(s, length)
s = s:gsub("\n\r?", " ")
if (s:len() > length) then
s = s:sub(0, length) .. "..."
end
return s
end
function HF.TimestampToDateString(timestamp, excludeHoursAndSeconds)
if timestamp == nil then return "" end
if (excludeHoursAndSeconds) then
return date("%y-%m-%d", timestamp)
end
return date("%m/%d/%y %H:%M:%S", timestamp)
end