-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.lua
230 lines (195 loc) · 5.91 KB
/
functions.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
local addonName, addonTable = ...
local C_ToyBox = C_ToyBox
local GetContainerNumSlots = C_Container.GetContainerNumSlots
local GetContainerItemLink = C_Container.GetContainerItemLink
local GetContainerItemInfo = C_Container.GetContainerItemInfo
local GetItemInfo = GetItemInfo
local GetInventoryItemLink = GetInventoryItemLink
local GetItemCooldown = GetItemCooldown
local GetSpellBookItemName = GetSpellBookItemName
local GetSpellInfo = GetSpellInfo
local GetSpellCooldown = GetSpellCooldown
local GetTime = GetTime
local IsPlayerSpell = IsPlayerSpell
local PlayerHasToy = PlayerHasToy
-- ==== GENERIC FUNCTIONS ====
local function getCooldownText(time)
local seconds = math.floor(time)
if (seconds < 60) then
return seconds .. 's'
end
local minutes = math.floor(seconds / 60)
if (minutes < 60) then
return minutes .. 'm'
end
local hours = math.floor(minutes / 60)
if (hours < 60) then
return hours .. 'h'
end
local days = math.floor(hours / 24)
return days .. 'd'
end
local function getTextWithCooldown(text, cooldown)
local colorCD = "ff0000"
if (cooldown > 0) then
local cooldownText = getCooldownText(cooldown)
return "|cff"..colorCD..text.." "..cooldownText.."|r"
end
return text
end
-- ==== ITEM FUNCTIONS ====
local function getItemCD(itemID)
local startTime, duration, cooldown
startTime, duration = GetItemCooldown(itemID)
cooldown = duration - (GetTime() - startTime)
return cooldown
end
-- returns true, if player has item with given ID in inventory or bags
local function hasItem(itemID)
local item, found, id
-- scan inventory
for slotId = 1, 19 do
item = GetInventoryItemLink('player', slotId)
if item then
found, _, id = item:find('^|c%x+|Hitem:(%d+):.+')
if found and tonumber(id) == itemID then
return true
end
end
end
-- scan bags
for bag = 0, 4 do
for slot = 1, GetContainerNumSlots(bag) do
item = GetContainerItemLink(bag, slot)
if item then
found, _, id = item:find('^|c%x+|Hitem:(%d+):.+')
if found and tonumber(id) == itemID then
return true
end
end
end
end
-- check Toybox
if PlayerHasToy(itemID) and C_ToyBox.IsToyUsable(itemID) then
return true
end
return false
end
local function getReagentCount(name)
local count = 0
for bag = 0, 4 do
for slot = 1, GetContainerNumSlots(bag) do
local item = GetContainerItemLink(bag, slot)
if item then
if item:find(name) then
local _, itemCount = GetContainerItemInfo(bag, slot)
count = count + itemCount
end
end
end
end
return count
end
-- load item async & adds link
local function loadItem(itemID, links)
local item = Item:CreateFromItemID(itemID)
item:ContinueOnItemLoad(function()
if (hasItem(itemID)) then
local name, _, _, _, _, _, _, _, _, icon = GetItemInfo(item:GetItemLink())
-- create item link
links[itemID] = {
hasItem = true,
name = name,
icon = icon,
secure = {
type = 'item',
item = name
}
}
else
-- create item link dummy
links[itemID] = {
hasItem = false
}
end
end)
end
-- loads all items async to itemLinks
local function updateItems()
for i = 1, #addonTable.whistles do
local itemID = addonTable.whistles[i]
loadItem(itemID, addonTable.itemLinks)
end
for i = 1, #addonTable.scrolls do
local itemID = addonTable.scrolls[i]
loadItem(itemID, addonTable.itemLinks)
end
for i = 1, #addonTable.items do
local itemID = addonTable.items[i]
loadItem(itemID, addonTable.itemLinks)
end
end
-- ==== SPELL FUNCTIONS ====
local function getSpellCD(spellID)
local startTime, duration, cooldown
startTime, duration = GetSpellCooldown(spellID)
cooldown = duration - (GetTime() - startTime)
return cooldown
end
-- returns true, if player has spell in his book
local function hasSpell(spellName)
local i = 1
while true do
local s = GetSpellBookItemName(i, BOOKTYPE_SPELL)
if not s then
break
end
if s == spellName then
return true
end
i = i + 1
end
return false
end
local function loadSpell(spellID, spellFlag, links)
if IsPlayerSpell(spellID) then
local name, _, icon = GetSpellInfo(spellID)
local location = nill
if (spellFlag ~= 'TRUE' and spellFlag ~= 'P_RUNE' and spellFlag ~= 'TP_RUNE') then
location = spellFlag
end
if hasSpell(name) then
links[name] = {
name = name,
icon = icon,
isPortal = spellFlag == 'P_RUNE',
location = location,
secure = {
type = 'spell',
spell = name
}
}
end
end
end
local function loadSpells(spells)
local links = {}
for _, spellPair in ipairs(spells) do
loadSpell(spellPair[1], spellPair[2], links)
end
return links
end
local function updateClassSpells()
return loadSpells(addonTable.portals)
end
local function updateChallengeSpells()
return loadSpells(addonTable.challengeSpells)
end
-- ==== EXPORT ====
addonTable.itemLinks = {}
addonTable.updateItems = updateItems
addonTable.updateClassSpells = updateClassSpells
addonTable.updateChallengeSpells = updateChallengeSpells
addonTable.getItemCD = getItemCD
addonTable.getSpellCD = getSpellCD
addonTable.getTextWithCooldown = getTextWithCooldown