forked from AaronV-T/AutoBiographer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelperFunctions.lua
More file actions
218 lines (171 loc) · 5.26 KB
/
HelperFunctions.lua
File metadata and controls
218 lines (171 loc) · 5.26 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
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
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.GetUnitTypeFromCatalogUnitId(cuid)
if (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
-- 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.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.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)
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 .. " day"
if days ~= 1 then returnString = returnString .. "s" end
returnString = returnString .. ", "
end
if printHours then
returnString = returnString .. hours .. " hour"
if hours ~= 1 then returnString = returnString .. "s" end
returnString = returnString .. ", "
end
if printMinutes then
returnString = returnString .. minutes .. " minute"
if minutes ~= 1 then returnString = returnString .. "s" end
returnString = returnString .. ", "
end
returnString = returnString .. seconds .. " second"
if seconds ~= 1 then returnString = returnString .. "s" end
return returnString
end
function HF.TimestampToDateString(timestamp)
if timestamp == nil then return "" end
return date("%m/%d/%y %H:%M:%S", timestamp)
end