Repository navigation
Expand file tree
/
Copy pathHelper.lua
More file actions
executable file
·149 lines (123 loc) · 3.43 KB
/
Copy pathHelper.lua
File metadata and controls
executable file
·149 lines (123 loc) · 3.43 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
---@class DTCore
local _, core = ...
---@class DTHelper
local helper = {}
core.helper = helper
---Returns the size for the given table
function helper:GetTableSize(table)
local size = 0
for _ in pairs(table) do
size = size + 1
end
return size
end
---Returns value if not nil otherwise defaultValue
---@param value any
---@param defaultValue any
---@return any
function helper:IfNil(value, defaultValue)
if (value == nil) then
return defaultValue
end
return value
end
---Converts a boolean value to a number (true = 1, false = 0)
function helper:BoolToNumber(booleanValue)
if (booleanValue) then
return 1
end
return 0
end
---Calculates the percentage for loot
---@param timesLooted number
---@param foundItems number
---@return number
function helper:CalculatePercentage(timesLooted, foundItems)
if (not timesLooted or timesLooted <= 0) then
return 0
end
local percentage = foundItems / timesLooted
if (percentage > 1) then
percentage = 1
end
return percentage
end
---Returns the unit type from unit guid
---@param unitGuid string
---@return string
function helper:GetUnitTypeByGuid(unitGuid)
-- Cast-[type]-[serverID]-[instanceID]-[zoneUID]-[spellID]-[castUID]
return select(1, strsplit('-', unitGuid))
end
---Returns true if the unit should be tracked
---@param unitGuid string?
---@return boolean
function helper:IsTrackableUnit(unitGuid)
if (unitGuid) then
local type = self:GetUnitTypeByGuid(unitGuid)
if (type == 'Creature' or type == 'Vehicle') then
return true
end
end
return false
end
---Parses the item id from item link
---@param link string
---@return number
function helper:GetItemIdFromLink(link)
core.logging:Verbose('GetItemIdFromLink, link = ', link)
if (link) then
local _, _, idCode = string.find(link, "|Hitem:(%d*):(%d*):(%d*):")
return tonumber(idCode) or -1
end
return -1
end
---Calculated the avarage gold amount for the given unit at given level.
---@param unitInfo table
---@param level number
---@return number|nil
function helper:CalculateAvgUnitGoldCount(unitInfo, level)
local copperInfo = unitInfo.cpi
if (not copperInfo) then
return nil -- no copper info in general
end
local levelCopperInfo = nil
if (level) then
levelCopperInfo = copperInfo['l:' .. level]
end
-- fallback: none level based units e.g. boss
if (levelCopperInfo == nil) then
levelCopperInfo = copperInfo['_']
end
if (not levelCopperInfo) then
return nil -- no copper info for level
end
local totalCopper = levelCopperInfo.tot
local timesLooted = levelCopperInfo.ltd
if (totalCopper > 0 and timesLooted > 0) then
return math.floor(totalCopper / timesLooted)
end
return nil
end
---Converts UnitGuid to UnitID
---@param guid string
---@return integer
function helper:GetUnitIdFromGuid(guid)
local id = select(6, strsplit('-', guid))
return tonumber(id, 10)
end
---formats a percentage value
---@param percentage number percentage value between 1 and 100
---@return string
function helper:FormatPercentage(percentage)
local result = ''
if (percentage > 0) then
local p = percentage * 100
if (p < 1) then
result = string.format('%.2f', p) .. ' %'
else
result = string.format('%u', p) .. ' %'
end
end
return result
end