-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
298 lines (278 loc) · 9.34 KB
/
main.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
297
298
local addon, Pesky = ...
Pesky.availableAdv = {}
Pesky.advDeck = {} -- slots 1-4, 5 is "pushed out by new card"
Pesky.advWorking = {} -- list of adventures in progress
Pesky.advFinished = {} -- list of claimable adventures
Pesky.minionWorking = {} -- list of minion busy with an adventure
Pesky.selectedSlot = 2
function Pesky.TryFreeShuffle(adventure)
Command.Minion.Shuffle(adventure, "none")
--print("trying to shuffle: ", adventure)
end
function Pesky.UpdateAdventureDB(details)
Pesky.lastAdventure = details.id
if not Pesky_AdventureDB[details.id] then
Pesky_AdventureDB[details.id] = details
end
end
function Pesky.InitMinionDB()
local list = Inspect.Minion.Minion.List()
if not list or not next(list) then
print("Minions not available yet!")
end
Pesky.minionDB = Inspect.Minion.Minion.Detail(list)
end
function Pesky.AddToDeck(details)
local slot = Pesky.Adventure.GetSlot(details)
if not slot then
return
end
if Pesky.advDeck[slot] then
Pesky.advDeck[5] = Pesky.advDeck[slot]
print(string.format("Pushed out card in slot %i!", slot))
end
Pesky.advDeck[slot] = details
end
function Pesky.RemoveFromDeck(details)
for slot = 1, 5 do
if Pesky.advDeck[slot] and Pesky.advDeck[slot].id == details.id then
Pesky.advDeck[slot] = nil
if slot == 5 then
print("cleared pushed out card!")
end
return
end
end
print("could not find adventure in deck:")
dump(details)
end
function Pesky.SetSelectedSlot(slot)
assert(slot > 0 and slot < 5, "Invalid argument")
if Pesky.selectedSlot == slot then
return
end
Pesky.selectedSlot = slot
Pesky.UI.UpdateSuitableMinions(Pesky.advDeck[slot])
Pesky.MinionCard0:SetCardDisplay(Pesky.advDeck[slot])
end
function Pesky.GetSelectedSlot()
return Pesky.selectedSlot
end
function Pesky.AdventureChangeHandler(hEvent, adventures)
print("Adventure.Change")
local details
local count = 0
local available, working, finished, removed = {}, {}, {}, {}
for id, _ in pairs(adventures) do
count = count + 1
details = Inspect.Minion.Adventure.Detail(id)
if details then
if details.mode == "available" then
available[id] = details
elseif details.mode == "working" then
working[id] = details
elseif details.mode == "finished" then
finished[id] = details
else
removed[id] = details
end
else
removed[id] = false
end
end
for id, details in pairs(finished) do
Pesky.advFinished[id] = details
if Pesky.advWorking[id] then
Pesky.advWorking[id] = nil
print("Moved adventure from working to finished", id)
else
print("Warning: Expected adventure to be in working state!", id)
end
end
for id, details in pairs(working) do
Pesky.advWorking[id] = details
Pesky.minionWorking[details.minion] = details
--Pesky.RemoveFromDeck(details)
local slot = Pesky.Adventure.GetSlot(details)
if Pesky.advDeck[slot] and Pesky.advDeck[slot].id == id then
Pesky.advDeck[slot] = nil
print("Moved adventure from available to working:", id)
else
print("Warning: Working adventure not in deck:", id)
end
end
for id, details in pairs(available) do
local slot = Pesky.Adventure.GetSlot(details)
--TODO: handle case of undetected slot for new adventures with non-standard parameters
local rem_id = Pesky.advDeck[slot] and Pesky.advDeck[slot].id
if rem_id then
if removed[rem_id] then
removed[rem_id] = nil
else
print("Warning: Expected available adventure to be removed first", slot)
end
--Pesky.RemoveFromDeck(details)
print("Shuffled adventure:", rem_id)
else
print("Refilled deck slot:", slot)
end
Pesky.advDeck[slot] = details
-- Test:
if slot == Pesky.selectedSlot then
Pesky.UI.UpdateSuitableMinions(details)
Pesky.UI.UpdateCardDisplay(details)
end
if slot == 2 or slot == 3 then
Pesky.TryFreeShuffle(id)
end
-- EOTest
end
for id, details in pairs(removed) do
if details then
if Pesky.advFinished[id] then
print("claimed adventure:", id, " minion:", details.minion)
else
print("Leftover remove:", id)
end
-- details.minion already nil, need to search...
for minion, adv_details in pairs(Pesky.minionWorking) do
if adv_details.id == id then
Pesky.minionWorking[minion] = nil
break
end
end
end
end
if count > 1 then print("Updated multiple adventures:", count) end
end
function Pesky.CommandHandler(hEvent, command)
if command == "available" then
for id, details in pairs(Pesky.availableAdv) do
print(string.format("%s (%i) %s", id, details.duration, details.name))
end
for slot = 1, 4 do
local details = Pesky.advDeck[slot]
if details then
print(string.format("slot %i: %imin, %s", slot, details.duration/60, details.name))
end
end
elseif command == "last" then
local details = Pesky_AdventureDB[Pesky.lastAdventure]
print(string.format("%s (%i) %s", details.id, details.duration, details.name))
elseif command == "bl" or command == "blacklist" then
Pesky_AdventureBL[Pesky.lastAdventure] = true
print("Blacklisted: ", Pesky.lastAdventure)
elseif command == "ui" then
--[[if not Pesky.MinionFrame0 then
Pesky.MinionFrame0 = Pesky.UI.CreateMinionIcon("frame0", Pesky.context)
Pesky.MinionFrame0:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 600, 50)
else
Pesky.MinionFrame0:SetVisible(not Pesky.MinionFrame0:GetVisible())
end]]--
if not Pesky.UI.minionWidget then
Pesky.UI.UpdateSuitableMinions(Pesky.advDeck[Pesky.selectedSlot])
else
Pesky.UI.minionWidget:SetVisible(not Pesky.UI.minionWidget:GetVisible())
end
elseif command == "card" then
if not Pesky.MinionCard0 then
Pesky.MinionCard0 = Pesky.UI.CreateCard("card0", Pesky.context)
Pesky.MinionCard0:SetPoint("TOPLEFT", UIParent, "TOPLEFT", 5, 340)
Pesky.MinionCard0:SetCardDisplay(Pesky.advDeck[Pesky.selectedSlot])
else
Pesky.MinionCard0:SetVisible(not Pesky.MinionCard0:GetVisible())
end
elseif command == "noicon" then
for id, details in pairs(Pesky.minionDB) do
if not Pesky.Data.Minion[id] then
print("missing minion: ", id, details.name)
elseif not Pesky.Data.Minion[id].icon then
print("missing icon: ", id, details.name)
end
end
elseif command == "stats" then
local adv_count = {}
for id, details in pairs(Pesky_AdventureDB) do
adv_count[details.duration] = (adv_count[details.duration] or 0) + 1
end
for duration, count in pairs(adv_count) do
print("duration: ", duration, " count: ", count)
end
else
dump(command, #command)
end
end
function Pesky.LoadEndHandler(hEvent, addonID)
--dump(addonID)
if addonID ~= addon.toc.Identifier then return end
if not Pesky_AdventureDB then
Pesky_AdventureDB = {}
end
if not Pesky_AdventureBL then
Pesky_AdventureBL = {}
end
end
function Pesky.AvailabilityHandler(hEvent, units)
-- TODO: Attach System.Update.End handler here if not initialized yet
-- and make it poll until we get minion data
for id, specifier in pairs(units) do
if specifier == "player" then
print("Availability.Full")
Command.Event.Attach(Event.System.Update.End, Pesky.SystemUpdateHandler_Init, "pesky_poll_init")
Command.Event.Detach(Event.Unit.Availability.Full, Pesky.AvailabilityHandler, "pesky_availability")
end
end
end
-- Event.Addon.Startup.End is useless, happens way before Availability.Full
--function Pesky.StartupHandler(hEvent)
function Pesky.SystemUpdateHandler_Init(hEvent)
local adv_list = Inspect.Minion.Adventure.List()
if not adv_list or not next(adv_list) then
--print("Adventures not available yet!")
return
end
local adventures = Inspect.Minion.Adventure.Detail(adv_list)
for id, details in pairs(adventures) do
if details.mode == "available" then
Pesky.availableAdv[id] = details
Pesky.AddToDeck(details)
Pesky.UpdateAdventureDB(details)
elseif details.mode == "working" then
Pesky.advWorking[id] = details
Pesky.minionWorking[details.minion] = details
elseif details.mode == "finished" then
Pesky.advFinished[id] = details
Pesky.minionWorking[details.minion] = details
end
end
Pesky.InitMinionDB()
Command.Event.Detach(Event.System.Update.End, Pesky.SystemUpdateHandler_Init, "pesky_poll_init")
end
function Pesky.MinionChangeHandler(hEvent, minions)
print("Minion.Change")
if not Pesky.minionDB then return end
for id, _ in pairs(minions) do
Pesky.minionDB[id] = Inspect.Minion.Minion.Detail(id)
end
end
function Pesky.ItemSlotChangeHandler(hEvent, updates)
for slot, item in pairs(updates) do
if item and item ~= "nil" then -- string "nil" indicates that a slot became unavailable (bag remove)
local slot_type = Utility.Item.Slot.Parse(slot)
if slot_type == "inventory" then
local details = Inspect.Item.Detail(item)
if details and details.type == "IFAC80140C0E946E2,FCB9F14CDB2B834E,,,,,," then
Command.Item.Destroy(item)
print("Destroyed ", details.name, slot)
end
end
end
end
end
Command.Event.Attach(Event.Minion.Adventure.Change, Pesky.AdventureChangeHandler, "pesky_shuffle")
Command.Event.Attach(Command.Slash.Register("pesky"), Pesky.CommandHandler, "Pesky Minions Command")
Command.Event.Attach(Event.Addon.SavedVariables.Load.End, Pesky.LoadEndHandler, "Pesky Loaded")
Command.Event.Attach(Event.Minion.Minion.Change, Pesky.MinionChangeHandler, "pesky_minion_update")
Command.Event.Attach(Event.Unit.Availability.Full, Pesky.AvailabilityHandler, "pesky_availability")
Command.Event.Attach(Event.Item.Slot, Pesky.ItemSlotChangeHandler, "pesky_item_slot")
--Command.Event.Attach(Event.System.Update.End, Pesky.SystemUpdateHandler_Init, "pesky_poll_init")