-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCore.lua
More file actions
514 lines (431 loc) · 14.6 KB
/
Core.lua
File metadata and controls
514 lines (431 loc) · 14.6 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
local Addon = LibStub("AceAddon-3.0"):NewAddon("WoWthing_Collector", "AceConsole-3.0", "AceEvent-3.0")
Addon:SetDefaultModuleLibraries("AceBucket-3.0", "AceEvent-3.0", "AceTimer-3.0")
local ModulePrototype = {
UniqueTimer = function(self, name, seconds, callback, ...)
self.__timers = self.__timers or {}
if self.__timers[name] and self:TimeLeft(self.__timers[name]) > 0 then
-- print('Timer '..name..' already exists')
return
end
self.__timers[name] = self:ScheduleTimer(callback, seconds, ...)
end
}
Addon:SetDefaultModulePrototype(ModulePrototype)
local mfloor = math.floor
local CT_After = C_Timer.After
local CTI_GetHyperlink = C_TooltipInfo.GetHyperlink
-- Default SavedVariables
local defaultWWTCSaved = {
version = 9158,
chars = {},
guilds = {},
heirloomsV2 = {},
questsV2 = {},
scanTimes = {},
toys = {},
warbank = {},
worldQuestIds = {},
}
function Addon:OnInitialize()
if self:DisableOnWeirdRealms() then return end
-- Initialize saved variables to default if required
if WWTCSaved == nil or WWTCSaved.version < defaultWWTCSaved.version then
WWTCSaved = defaultWWTCSaved
end
Addon:Cleanup()
WWTCSaved.heirloomsV2 = WWTCSaved.heirloomsV2 or {}
WWTCSaved.questsV2 = WWTCSaved.questsV2 or {}
WWTCSaved.scanTimes = WWTCSaved.scanTimes or {}
WWTCSaved.warbank = WWTCSaved.warbank or {}
WWTCSaved.worldQuestIds = WWTCSaved.worldQuestIds or {}
WWTCSaved.honorCurrent = WWTCSaved.honorCurrent or 0
WWTCSaved.honorLevel = WWTCSaved.honorLevel or 0
WWTCSaved.honorMax = WWTCSaved.honorMax or 0
self.hasAccountLock = false
self.parseItemLinkCache = {}
self.working = false
self.workloads = {}
self.regionName = GetCurrentRegion()
self.charName = UnitGUID('player')
self.charClassID = select(3, UnitClass("player"))
-- Set up character data table
self.charData = WWTCSaved.chars[self.charName] or {}
self.charData.name = UnitName('player')
self.charData.scanTimes = self.charData.scanTimes or {}
local now = time()
self.charData.dailyReset = now + C_DateAndTime.GetSecondsUntilDailyReset()
self.charData.weeklyReset = now + C_DateAndTime.GetSecondsUntilWeeklyReset()
WWTCSaved.chars[self.charName] = self.charData
self:RegisterChatCommand('wwtc', 'SlashCommand')
self:RegisterEvent('PLAYER_ENTERING_WORLD')
self:RegisterEvent('PLAYER_LOGOUT')
self:RegisterEvent('ACCOUNT_MONEY')
self:RegisterEvent('PLAYER_MONEY')
end
function Addon:Cleanup()
WWTCSaved.heirlooms = nil
WWTCSaved.quests = nil
WWTCSaved.transmogSources = nil
WWTCSaved.transmogSourcesV2 = nil
WWTCSaved.transmogSourcesSquish = nil
-- Remove old guild data with region string
for guildName, _ in pairs(WWTCSaved.guilds) do
local prefix = strsplit('/', guildName)
if #prefix == 2 then
WWTCSaved.guilds[guildName] = nil
end
end
-- Remove data for any characters not seen in the last 3 days
local old = time() - (3 * 24 * 60 * 60)
for charName, charData in pairs(WWTCSaved.chars) do
if not charData.lastSeen or charData.lastSeen < old then
WWTCSaved.chars[charName] = nil
else
-- Wipe any deprecated data
charData.auras = nil
charData.balanceMythic15 = nil
charData.balanceUnleashedMonstrosities = nil
charData.biggerFishToFry = nil
charData.delves = nil
charData.equipment = nil
charData.hiddenDungeons = nil
charData.hiddenKills = nil
charData.hiddenWorldQuests = nil
charData.illusions = nil
charData.mythicPlus = nil
charData.name = nil
charData.transmog = nil
charData.transmogSquish = nil
charData.weeklyQuests = nil
charData.weeklyUghQuests = nil
charData.activeQuests = nil
charData.dailyQuests = nil
charData.otherQuests = nil
if charData.vault ~= nil and
(charData.vault[1] ~= nil or
charData.vault[2] ~= nil or
charData.vault[3] ~= nil)
then
charData.vault = {}
end
end
end
end
function Addon:DisableOnWeirdRealms()
local uhoh = false
if IsPublicTestClient() or IsOnTournamentRealm() then
uhoh = true
end
-- AllTheThings app.IgnoreDataCaching
local realmName = GetRealmName()
if realmName ~= nil and (
realmName:find("Mythic Dungeons") or
realmName:find("Arena Champions") or
realmName:find("US") or
realmName:find("AU") or
realmName:find("EU")
) then
uhoh = true
end
if uhoh then
print('WoWthing_Collector does not like this realm, disabling')
self:Disable()
return true
end
return false
end
function Addon:SlashCommand(command)
if command == 'transmog' then
print('WoWthing_Collector: running full transmog scan...')
self.charData.scanTimes.transmog = 0
local transmogModule = self:GetModule('Transmog')
transmogModule:UpdateTransmog(true)
end
end
function Addon:UpdateLastSeen()
self.charData.lastSeen = time()
end
function Addon:PLAYER_ENTERING_WORLD()
if self:DisableOnWeirdRealms() then return end
self.hasAccountLock = C_PlayerInfo.HasAccountInventoryLock()
self:UpdateLastSeen()
C_Timer.After(2, function()
self:ACCOUNT_MONEY()
self:PLAYER_MONEY()
end)
for _, module in Addon:IterateModules() do
if module.OnEnteringWorld ~= nil then
module:OnEnteringWorld()
end
end
end
function Addon:PLAYER_LOGOUT()
self:UpdateLastSeen()
for _, module in Addon:IterateModules() do
if module.OnLogout ~= nil then
module:OnLogout()
end
end
end
function Addon:ACCOUNT_MONEY()
if self.hasAccountLock then
WWTCSaved.scanTimes.warbankGold = time()
WWTCSaved.warbank.copper = C_Bank.FetchDepositedMoney(Enum.BankType.Account)
end
end
function Addon:PLAYER_MONEY()
self.charData.copper = GetMoney()
end
-- Utils
function Addon:Round(n)
return mfloor(n + 0.5)
end
function Addon:TableKeys(tbl)
local index = 1
local keys = {}
for key, _ in pairs(tbl) do
keys[index] = key
index = index + 1
end
return keys
end
function Addon:PlayerGuidToId(guid)
local parts = strsplittable('-', guid, 3)
if #parts == 3 then
return parts[3]
else
return guid
end
end
function Addon:ParseItemLink(link, quality, count, bound)
local cached = self.parseItemLinkCache[link]
if cached ~= nil then
return table.concat({ count, cached }, ':')
end
-- |cnIQ4:|Hitem:blah
local pipeParts = { strsplit("|", link) }
local parts = { strsplit(":", pipeParts[3]) }
if string.find(parts[1], 'Hbattlepet') then
return table.concat({
'pet',
parts[2], -- speciesID
parts[3], -- level
parts[4], -- quality
}, ':')
end
local item = {
bindType = 0,
count = count,
itemID = tonumber(parts[2]),
itemLevel = 0,
quality = quality,
bonusIDs = {},
gems = {},
modifiers = {},
}
if quality < 0 then
item.quality = C_Item.GetItemQualityByID(link)
end
if parts[3] ~= '' then
item.enchantID = tonumber(parts[3])
end
if parts[4] ~= '' then
for i = 4, 7 do
if parts[i] then
item.gems[#item.gems + 1] = tonumber(parts[i])
end
end
end
if parts[8] ~= '' then
item.suffixID = tonumber(parts[8])
end
-- 9 = uniqueID
-- 10 = linkLevel
-- 11 = specializationID
-- 12 = modifiersMask
if parts[13] ~= '' then
item.context = tonumber(parts[13])
end
local numBonusIDs = tonumber(parts[14])
if numBonusIDs ~= nil then
for i = 15, 14 + numBonusIDs do
item.bonusIDs[#item.bonusIDs + 1] = tonumber(parts[i])
end
end
-- 15 + numBonusIds = numModifiers
local startModifiers = 15 + (numBonusIDs or 0)
local numModifiers = tonumber(parts[startModifiers])
if numModifiers ~= nil then
for i = startModifiers + 1, startModifiers + (numModifiers * 2), 2 do
item.modifiers[#item.modifiers + 1] = parts[i] .. '_' .. parts[i + 1]
end
end
-- local _, _, _, _, _, _, _, _, _, _, _, _, _, bindType = CI_GetItemInfo(link)
-- local effectiveItemLevel, _, _ = CI_GetDetailedItemLevelInfo(link)
-- item.bindType = bindType
-- item.itemLevel = effectiveItemLevel
-- workaround for ^ randomly returning pre-squish item levels
local tooltipData = CTI_GetHyperlink(link)
if tooltipData ~= nil and tooltipData.lines ~= nil then
for _, lineData in ipairs(tooltipData.lines) do
if lineData.type == Enum.TooltipDataType.ItemBinding then
item.bindType = lineData.bonding
elseif lineData.type == Enum.TooltipDataLineType.ItemLevel then
item.itemLevel = lineData.itemLevel
end
end
end
-- count:id:context:enchant:ilvl:quality:suffix:bonusIDs:gems:modifiers:bound:bindType
local ret = table.concat({
item.itemID,
item.context or 0,
item.enchantID or 0,
item.itemLevel or 0,
item.quality or 0,
item.suffixID or 0,
table.concat(item.bonusIDs, ','),
table.concat(item.gems, ','),
table.concat(item.modifiers, ','),
bound,
item.bindType or 0,
}, ':')
self.parseItemLinkCache[link] = ret
return table.concat({ count, ret }, ':')
end
-- https://www.wowinterface.com/forums/showthread.php?t=58916
-- Budgets 50% of target or current FPS to perform a workload.
-- finished = start(workload, onFinish, onDelay)
-- Arguments:
-- workload table Stack (last in, first out) of functions to call.
-- onFinish function? Optional callback when the table is empty.
-- onDelay function? Optional callback each time work delays to the next frame.
-- Returns:
-- finished boolean True when finished without any delay; false otherwise.
function Addon:QueueWorkload(workload, onFinish, onDelay)
-- If there's no workload provided we don't even need to queue
if #workload == 0 then
if onFinish then
onFinish()
end
return
end
tinsert(self.workloads, { workload = workload, onFinish = onFinish, onDelay = onDelay })
if self.working == false then
self.working = true
Addon:BatchWork()
end
end
function Addon:BatchWork()
local maxDuration = 250 / (tonumber(C_CVar.GetCVar("targetFPS")) or GetFrameRate())
-- local startTime = debugprofilestop()
local function continue()
local startTime = debugprofilestop()
while true do
local workloadData = tremove(self.workloads)
if workloadData == nil then
self.working = false
return true
end
local task = tremove(workloadData.workload)
if task == nil then
self.working = false
return true
end
task()
if #workloadData.workload > 0 then
tinsert(self.workloads, 1, workloadData)
else
if workloadData.onFinish then
workloadData.onFinish()
end
if #self.workloads == 0 then
self.working = false
return true
end
end
if (debugprofilestop() - startTime) > maxDuration then
CT_After(0.05, continue)
if workloadData.onDelay then
workloadData.onDelay()
end
return false
end
end
end
return continue()
end
-- Encode a sorted run of numbers into a moderately space-efficient format
-- [questID1].[encoded deltas]|[questID2].[deltas]|...
local CHAR_VALUES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`~!@#$%^&*()-_=+[{]};:,<.>/?'
local CHAR_TO_VALUE = {}
local VALUE_TO_CHAR = {}
for index = 1, strlen(CHAR_VALUES) do
local char = strbyte(CHAR_VALUES, index)
CHAR_TO_VALUE[char] = index
VALUE_TO_CHAR[index] = strchar(char)
end
function Addon:DeltaEncode(ids, onFinish)
local count = #ids
local maxDiff = strlen(CHAR_VALUES)
local anyDiffed = false
local index = 1
local lastId = nil
local output = {}
local workload = {}
for i = 1, count, 1000 do
tinsert(workload, 1, function()
for j = i, min(i + 999, count) do
local currentId = ids[j]
if lastId ~= nil then
local diff = currentId - lastId
if diff <= maxDiff then
if anyDiffed == false then
anyDiffed = true
output[index] = '.'
index = index + 1
end
output[index] = VALUE_TO_CHAR[diff]
else
output[index] = '|'
index = index + 1
lastId = nil
end
end
if lastId == nil then
anyDiffed = false
output[index] = currentId
end
index = index + 1
lastId = currentId
end
end)
end
tinsert(workload, 1, function()
onFinish(table.concat(output, ''))
end)
Addon:QueueWorkload(workload)
end
function Addon:DeltaDecode(squished)
local data = {}
local index = 1
-- local startTime = debugprofilestop()
local parts = { strsplit('|', squished) }
for _, part in ipairs(parts) do
local deltaParts = { strsplit('.', part, 2) }
local id = tonumber(deltaParts[1])
data[index] = id
index = index + 1
local deltas = deltaParts[2]
if deltas ~= nil then
for i = 1, #deltas do
local byte = strbyte(deltas, i)
id = id + CHAR_TO_VALUE[byte]
data[index] = id
index = index + 1
end
end
end
-- local endTime = debugprofilestop()
-- print('DeltaDecode: '..#squished.. ' bytes in '..endTime - startTime..'ms')
return data
end