Repository navigation
Expand file tree
/
Copy pathPreyTracker.lua
More file actions
265 lines (232 loc) · 7.04 KB
/
Copy pathPreyTracker.lua
File metadata and controls
265 lines (232 loc) · 7.04 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
local _, ns = ...
local PreyTracker = {}
ns.PreyTracker = PreyTracker
local CACHE_QUEST_ID = 93910
local DEFAULT_CACHE_MAX = 3
local DEFAULT_WEEKLY_MAX = 15
local DUPLICATE_EVENT_WINDOW = 10
local function Clamp(value, minimum, maximum)
return math.max(minimum, math.min(value, maximum))
end
local function GetNow()
if GetServerTime then
return GetServerTime()
end
if time then
return time()
end
return os.time()
end
-- These are the target quests, not the helper/objective quests used during a
-- hunt. The live active-Prey API remains the primary match for new targets.
function PreyTracker:IsKnownHuntQuest(questID)
return (questID >= 91095 and questID <= 91124)
or (questID >= 91210 and questID <= 91269)
or questID == 95022
or questID == 95023
end
function PreyTracker:GetState()
if not ns.CharConfig then
return nil
end
ns.CharConfig.prey = ns.CharConfig.prey or {}
local state = ns.CharConfig.prey
state.count = tonumber(state.count) or 0
if state.partial == nil then
state.partial = true
end
state.nextReset = tonumber(state.nextReset) or 0
state.activeQuestID = tonumber(state.activeQuestID) or 0
state.lastActiveQuestID = tonumber(state.lastActiveQuestID) or 0
state.lastTurnInQuestID = tonumber(state.lastTurnInQuestID) or 0
state.lastTurnInAt = tonumber(state.lastTurnInAt) or 0
return state
end
function PreyTracker:GetNextReset(now)
if ns.WeeklyReset then
return ns.WeeklyReset:GetObservedNextReset(now)
end
if not C_DateAndTime or not C_DateAndTime.GetSecondsUntilWeeklyReset then
return nil
end
local ok, seconds = pcall(C_DateAndTime.GetSecondsUntilWeeklyReset)
if not ok or type(seconds) ~= "number" or seconds <= 0 then
return nil
end
return now + seconds
end
-- Public and deterministic so the reset behavior can be covered by unit tests.
function PreyTracker:CheckReset(state, now, observedNextReset)
if not state then
return false
end
local firstObservation = state.nextReset <= 0
local resetOccurred
if ns.WeeklyReset then
resetOccurred = ns.WeeklyReset:Check(state, now, observedNextReset)
else
-- Test/dev fallback when this module is loaded in isolation.
if firstObservation then
state.nextReset = observedNextReset or 0
resetOccurred = false
else
local resetPassed = now >= state.nextReset
local apiRolledForward = observedNextReset and observedNextReset > state.nextReset + (6 * 60 * 60)
resetOccurred = resetPassed or apiRolledForward
if resetOccurred then
state.nextReset = observedNextReset or 0
end
end
end
if firstObservation then
state.partial = true
return false
end
if not resetOccurred then
return false
end
state.count = 0
state.partial = false
state.activeQuestID = 0
state.lastActiveQuestID = 0
state.lastTurnInQuestID = 0
state.lastTurnInAt = 0
return true
end
function PreyTracker:RefreshReset()
local state = self:GetState()
if not state then
return false
end
local now = GetNow()
return self:CheckReset(state, now, self:GetNextReset(now))
end
function PreyTracker:RefreshActiveQuest()
local state = self:GetState()
if not state or not C_QuestLog or not C_QuestLog.GetActivePreyQuest then
return 0
end
local ok, questID = pcall(C_QuestLog.GetActivePreyQuest)
if ok and type(questID) == "number" and questID > 0 then
state.activeQuestID = questID
state.lastActiveQuestID = questID
return questID
end
state.activeQuestID = 0
return 0
end
function PreyTracker:GetCacheStatus(questID, cacheMax)
questID = questID or CACHE_QUEST_ID
cacheMax = cacheMax or DEFAULT_CACHE_MAX
if not C_QuestLog then
return false, 0, cacheMax, false
end
local completed = ns.Context and ns.Context:IsCharacterQuestCompleted(questID) or false
if completed then
return true, cacheMax, cacheMax, false
end
local active = C_QuestLog.IsOnQuest
and C_QuestLog.IsOnQuest(questID)
and (not C_QuestLog.GetLogIndexForQuestID or C_QuestLog.GetLogIndexForQuestID(questID) ~= nil)
if not active or not C_QuestLog.GetQuestObjectives then
return false, 0, cacheMax, false
end
local objectives = C_QuestLog.GetQuestObjectives(questID)
local bestProgress, bestMax = 0, cacheMax
for _, objective in ipairs(objectives or {}) do
local required = tonumber(objective.numRequired) or 0
if required > 0 and (required > bestMax or required == cacheMax) then
bestProgress = tonumber(objective.numFulfilled) or 0
bestMax = required
end
end
bestMax = bestMax > 0 and bestMax or cacheMax
return bestProgress >= bestMax, Clamp(bestProgress, 0, bestMax), bestMax, true
end
function PreyTracker:ReconcileCacheProgress(state, questID, cacheMax)
local completed, progress = self:GetCacheStatus(questID, cacheMax)
if completed then
progress = cacheMax or DEFAULT_CACHE_MAX
end
if progress > state.count then
state.count = progress
end
end
function PreyTracker:RecordCompletion(state, questID, now, weeklyMax)
if not state or type(questID) ~= "number" then
return false
end
now = now or GetNow()
weeklyMax = weeklyMax or DEFAULT_WEEKLY_MAX
if state.lastTurnInQuestID == questID and now - state.lastTurnInAt <= DUPLICATE_EVENT_WINDOW then
return false
end
state.count = Clamp((state.count or 0) + 1, 0, weeklyMax)
state.lastTurnInQuestID = questID
state.lastTurnInAt = now
state.activeQuestID = 0
state.lastActiveQuestID = 0
return true
end
function PreyTracker:OnQuestTurnedIn(questID)
local state = self:GetState()
if not state then
return
end
self:RefreshReset()
local isTrackedHunt = questID == state.activeQuestID
or questID == state.lastActiveQuestID
or self:IsKnownHuntQuest(questID)
if not isTrackedHunt or not self:RecordCompletion(state, questID) then
return
end
if ns.UI and ns.UI.QueueRefresh then
ns.UI:QueueRefresh()
elseif ns.UI and ns.UI.RenderRows then
ns.UI:RenderRows()
end
end
function PreyTracker:GetStatus(weeklyMax, cacheQuestID, cacheMax)
weeklyMax = weeklyMax or DEFAULT_WEEKLY_MAX
local state = self:GetState()
if not state then
return false, 0, weeklyMax, false, 0
end
self:RefreshReset()
self:RefreshActiveQuest()
self:ReconcileCacheProgress(state, cacheQuestID or CACHE_QUEST_ID, cacheMax or DEFAULT_CACHE_MAX)
local displayCount = Clamp(state.count, 0, weeklyMax)
local activeCount = state.activeQuestID > 0 and 1 or 0
return displayCount >= weeklyMax, displayCount, weeklyMax, state.partial, activeCount
end
function PreyTracker:Initialize()
if self.initialized then
return
end
self.initialized = true
self:RefreshReset()
self:RefreshActiveQuest()
if not CreateFrame then
return
end
self.eventFrame = self.eventFrame or CreateFrame("Frame")
self.eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
self.eventFrame:RegisterEvent("QUEST_ACCEPTED")
self.eventFrame:RegisterEvent("QUEST_LOG_UPDATE")
self.eventFrame:RegisterEvent("QUEST_TURNED_IN")
self.eventFrame:SetScript("OnEvent", function(_, event, arg1)
if event == "QUEST_TURNED_IN" then
self:OnQuestTurnedIn(arg1)
else
self:RefreshReset()
self:RefreshActiveQuest()
end
end)
end
function PreyTracker:Shutdown()
if self.eventFrame then
self.eventFrame:UnregisterAllEvents()
self.eventFrame:SetScript("OnEvent", nil)
end
self.initialized = false
end