-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCore.lua
More file actions
546 lines (444 loc) · 14.8 KB
/
Copy pathCore.lua
File metadata and controls
546 lines (444 loc) · 14.8 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
local addonName, addonTable = ...
local addon = addonTable.addon
local L = LibStub("AceLocale-3.0"):GetLocale("TaskManager")
local MAXINT = 10 ^ 300
local SECONDS_PER_DAY = 24 * 60 * 60
function addon:Expires(reset)
if reset == "weekly" then
return time() + C_DateAndTime.GetSecondsUntilWeeklyReset()
end
if reset == "daily" then
local secs = GetQuestResetTime()
if not secs or secs < 1 then return 0 end -- error handling
return time() + secs
end
-- default to never
return MAXINT
end
function addon:AddStandardTask(key, title, category, reset, priority)
if reset ~= "weekly" and reset ~= "daily" then
-- default to never
reset = "never"
end
key = key or ("s" .. tostring(time()))
TM_TASKS[key] = {
title = title,
category = category,
reset = reset,
priority = priority
}
addon:NormalizeTaskPriority()
end
function addon:UpdateStandardTask(key, completed, guid)
if not addon:IsStandardTask(key) then return false end -- sanity check
if not TM_TASKS[key] then return false end -- not tracked
if not guid then guid = addon.guid end -- default to current character
if not TM_STATUS[guid][key] then TM_STATUS[guid][key] = {} end
local entry = TM_STATUS[guid][key]
if completed then
entry.expires = addon:Expires(TM_TASKS[key].reset)
entry.completed = true
else
entry.expires = nil
entry.completed = nil
end
return true
end
function addon:IsStandardTask(key)
return string.sub(key, 1, 1) == "s"
end
function addon:AddQuestTask(id, title, category, reset, priority)
if not id then return end
if reset ~= "weekly" and reset ~= "daily" then
-- default to never
reset = "never"
end
local key = "q" .. tostring(id)
TM_TASKS[key] = {
questid = id,
title = title,
category = category,
reset = reset,
priority = priority
}
addon:NormalizeTaskPriority()
end
function addon:UpdateQuest(id)
local key = "q" .. tostring(id)
if not TM_TASKS[key] then return false end -- not tracked
if not TM_STATUS[addon.guid][key] then TM_STATUS[addon.guid][key] = {} end
local entry = TM_STATUS[addon.guid][key]
local be, bc, bp = entry.expires, entry.completed, entry.progress
-- update quest status
local reset = C_TaskQuest.GetQuestTimeLeftSeconds(id)
if reset and reset > 0 then
entry.expires = reset + time()
else
entry.expires = addon:Expires(TM_TASKS[key].reset)
end
if C_QuestLog.IsQuestFlaggedCompleted(id) then
entry.completed = true
entry.progress = nil
elseif C_QuestLog.IsOnQuest(id) then
local progress = 0
local required = 0
local objectives = C_QuestLog.GetQuestObjectives(id)
if objectives and objectives[1] and not objectives[2] then
-- only 1 objective, so track it
progress = objectives[1].numFulfilled
required = objectives[1].numRequired
end
entry.completed = false
if required > 0 then
entry.progress = tostring(progress) .. "/" .. tostring(required)
else
entry.progress = L["ProgressOnQuest"]
end
else
entry.completed = false
entry.progress = nil
end
-- return true if anything changed
return bc ~= entry.completed or be ~= entry.expires or bp ~= entry.progress
end
function addon:AddProfessionTask(id, title, category, reset, priority)
if not id then return end
if reset ~= "weekly" and reset ~= "daily" then
-- default to never
reset = "never"
end
local key = "p" .. tostring(id)
TM_TASKS[key] = {
spellid = id,
title = title,
category = category,
reset = reset,
priority = priority
}
addon:NormalizeTaskPriority()
end
function addon:UpdateProfession(id)
local key = "p" .. tostring(id)
if not TM_TASKS[key] then return false end -- not tracked
if not TM_STATUS[addon.guid][key] then TM_STATUS[addon.guid][key] = {} end
local entry = TM_STATUS[addon.guid][key]
local be, bc, bp = entry.expires, entry.completed
-- check if we have valid cooldown info
local exists = select('#', C_TradeSkillUI.GetRecipeCooldown(id))
if not exists or exists == 0 then return false end
-- update profession status
local cooldown, daily, charges, maxCharges = C_TradeSkillUI.GetRecipeCooldown(id)
if charges and charges == 0 and cooldown and cooldown > 0 then
entry.completed = true
if daily and cooldown < SECONDS_PER_DAY then
entry.expires = addon:Expires("daily")
else
entry.expires = floor(cooldown + time())
end
else
entry.completed = false
entry.expires = nil
end
-- return true if anything changed
return bc ~= entry.completed or be ~= entry.expires
end
function addon:AddItemTask(id, title, category, reset, priority)
if not id then return end
if reset ~= "weekly" and reset ~= "daily" then
-- default to never
reset = "never"
end
local key = "item" .. tostring(id)
TM_TASKS[key] = {
itemid = id,
title = title,
category = category,
reset = reset,
priority = priority
}
addon:NormalizeTaskPriority()
end
function addon:UpdateItem(id)
local key = "item" .. tostring(id)
if not TM_TASKS[key] then return false end -- not tracked
if not TM_STATUS[addon.guid][key] then TM_STATUS[addon.guid][key] = {} end
local entry = TM_STATUS[addon.guid][key]
local be, bc, bp = entry.expires, entry.completed
-- check if we have valid cooldown info
local exists = select('#', C_Container.GetItemCooldown(id))
if not exists or exists == 0 then return false end
-- update item status
local start, duration, enable = C_Container.GetItemCooldown(id)
if start > 0 and duration > 0 then
entry.completed = true
entry.expires = floor(start + duration - GetTime() + time())
else
entry.completed = false
entry.expires = nil
end
-- return true if anything changed
return bc ~= entry.completed or be ~= entry.expires
end
function addon:AddBossTask(id, difficulty, boss, title, category, reset, priority)
if not id then return end
if not difficulty then return end
if not boss then return end
local key = "i" .. tostring(id) .. "d" .. tostring(difficulty) .. "b" .. tostring(boss)
TM_TASKS[key] = {
instanceid = id,
difficulty = difficulty,
boss = boss,
title = title,
category = category,
reset = reset,
priority = priority
}
addon:NormalizeTaskPriority()
end
function addon:UpdateBosses()
local num = GetNumSavedInstances()
local changed = false
if num > 0 then
for i = 1, num do
local _, _, reset, difficulty, locked, _, _, _, _, _, bosses, _, _, instanceid = GetSavedInstanceInfo(i)
for j = 1, bosses do
local key = "i" .. tostring(instanceid) .. "d" .. tostring(difficulty) .. "b" .. tostring(j)
if TM_TASKS[key] then
local defeated = false
if locked then
_, _, defeated, _ = GetSavedInstanceEncounterInfo(i, j)
end
-- update status
if not TM_STATUS[addon.guid][key] then TM_STATUS[addon.guid][key] = {} end
local entry = TM_STATUS[addon.guid][key]
local be, bc = entry.expires, entry.completed
if defeated then
entry.completed = true
entry.expires = reset + time()
--entry.expires = addon:Expires(TM_TASKS[key].reset)
else
entry.completed = false
entry.expires = nil
end
-- expiration can vary by a little until cache updates, so don't rely on it
changed = changed or bc ~= entry.completed -- or be ~= entry.expires
end
end
end
end
return changed
end
function addon:AddVaultTask(title, category, reset, priority)
local key = "vault"
TM_TASKS[key] = {
title = title,
category = category,
reset = reset,
priority = priority
}
addon:NormalizeTaskPriority()
end
function addon:UpdateVaults()
local key = "vault"
if TM_TASKS[key] then
-- update status
if not TM_STATUS[addon.guid][key] then TM_STATUS[addon.guid][key] = {} end
local entry = TM_STATUS[addon.guid][key]
local be, bc = entry.expires, entry.completed
entry.completed = not (C_WeeklyRewards.HasAvailableRewards() or C_WeeklyRewards.CanClaimRewards())
-- reset next week ONLY if rewards are available
entry.expires = addon:Expires("never")
local rewards = C_WeeklyRewards.GetActivities()
for i, reward in ipairs(rewards) do
if reward.progress >= reward.threshold then
entry.expires = addon:Expires("weekly")
break
end
end
-- return true if anything changed
return bc ~= entry.completed or be ~= entry.expires
end
return false
end
function addon:RemoveTask(key)
TM_TASKS[key] = nil
addon:NormalizeTaskPriority()
addon:PurgeExpired() -- removes task from characters
end
function addon:SkipTask(guid, key, skip)
if not TM_STATUS[guid][key] then TM_STATUS[guid][key] = {} end
local entry = TM_STATUS[guid][key]
if skip then
entry.skipexpires = addon:Expires(TM_TASKS[key].reset)
entry.skip = true
else
entry.skipexpires = nil
entry.skip = nil
end
end
function addon:SkipAllTasks(key, skip)
for guid, toon in pairs(TM_STATUS) do
addon:SkipTask(guid, key, skip)
end
end
function addon:IsSkipped(guid, key)
local toon = TM_STATUS[guid]
if not toon then return false end
local status = toon[key]
return status and status.skip
end
function addon:UpdateCharacter()
if not TM_STATUS[addon.guid] then TM_STATUS[addon.guid] = {} end
if not TM_STATUS[addon.guid].info then TM_STATUS[addon.guid].info = {} end
local entry = TM_STATUS[addon.guid].info
local bl = entry.level
local name, realm = UnitFullName("player")
local _, class = UnitClass("player")
local level = UnitLevel("player")
entry.name = name
entry.realm = realm
entry.class = class
entry.level = level
-- level is the only thing that should really change
return bl ~= entry.level
end
function addon:UpdateAll()
local changed = false
-- character
changed = addon:UpdateCharacter() or changed
for key, task in pairs(TM_TASKS) do
-- quests
if task.questid then
changed = addon:UpdateQuest(task.questid) or changed
end
-- professions
if task.spellid then
changed = addon:UpdateProfession(task.spellid) or changed
end
-- items
if task.itemid then
changed = addon:UpdateItem(task.itemid) or changed
end
end
-- instances
changed = addon:UpdateBosses() or changed
-- great vault
changed = addon:UpdateVaults() or changed
return changed
end
function addon:PurgeExpired()
local now = time()
for guid, toon in pairs(TM_STATUS) do
for key, task in pairs(toon) do
if key ~= "info" and not TM_TASKS[key] then
-- task was removed
TM_STATUS[guid][key] = nil
else
if task.expires and task.expires < now then
task.completed = nil
task.progress = nil
task.expires = nil
end
if task.skipexpires and task.skipexpires < now then
task.skip = nil
task.skipexpires = nil
end
end
end
end
end
function addon:DeleteCharacter(guid)
TM_STATUS[guid] = nil
end
function addon:IgnoreAllTasks(guid, ignore)
local toon = TM_STATUS[guid]
if not toon then return end
if ignore then
toon.info.ignored = true
else
toon.info.ignored = nil
end
end
function addon:IgnoreTask(guid, key, ignore)
local toon = TM_STATUS[guid]
if not toon then return end
if not toon[key] then toon[key] = {} end
if ignore then
if toon.info and toon.info.ignored then
-- reset
toon[key].ignored = nil
else
toon[key].ignored = true
end
else
if toon.info and toon.info.ignored then
-- specifically unignore
toon[key].ignored = false
else
toon[key].ignored = nil
end
end
end
function addon:IsIgnored(guid, key)
local toon = TM_STATUS[guid]
if not toon then return false end
-- Ignore
local status = toon[key]
if status and status.ignored ~= nil then return status.ignored end
-- IgnoreAll
return toon.info and toon.info.ignored
end
function addon:TimeLeft(guid, key)
local toon = TM_STATUS[guid]
if toon then
local status = toon[key]
if status and status.expires then
-- check for never expiring
if status.expires == MAXINT then return nil end
return status.expires - time()
end
end
local task = TM_TASKS[key]
if task and task.reset then
local expires = addon:Expires(task.reset)
if expires > 0 and expires < MAXINT then
return expires - time()
end
end
-- couldn't determine time left
return nil
end
function addon:NormalizeTaskPriority()
local sorted = {}
local categories = {}
-- gather tasks and categories
for key, task in pairs(TM_TASKS) do
task.category = addon:Trim(task.category) or L["MissingCategory"]
task.priority = task.priority or MAXINT
categories[task.category] = min(task.priority, categories[task.category] or MAXINT)
table.insert(sorted, task)
end
-- sort by category and then priority
table.sort(sorted, function(a, b)
if a.category == b.category then
return a.priority < b.priority
else
-- position category according to its lowest task
return categories[a.category] < categories[b.category]
end
end)
-- assign the normalized priority
for i, task in ipairs(sorted) do
task.priority = i
end
end
function addon:RenameCategory(from, to)
to = addon:Trim(to)
if to ~= nil then
for key, task in pairs(TM_TASKS) do
if task.category == from then
task.category = to
end
end
end
end