-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskArea.lua
More file actions
469 lines (402 loc) · 15.8 KB
/
TaskArea.lua
File metadata and controls
469 lines (402 loc) · 15.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
--[[
QuestTogether Task Area Subsystem
This subsystem owns task-area truth for world quests and bonus objectives.
It resolves active area state from addon-owned quest snapshots only.
All other addon code should consume the resolver snapshot or the active
world/bonus state stores exposed here.
]]
local QuestTogether = _G.QuestTogether
local function SafeText(value, fallback)
return QuestTogether:SafeToString(value, fallback or "")
end
local function NormalizeQuestId(addon, questId)
if not addon then
return nil
end
if addon.NormalizeQuestID then
return addon:NormalizeQuestID(questId)
end
local numericQuestId = addon.SafeToNumber and addon:SafeToNumber(questId) or nil
if not numericQuestId or numericQuestId <= 0 then
return nil
end
return math.floor(numericQuestId + 0.5)
end
local function CountKeys(tableValue)
local count = 0
for _ in pairs(tableValue or {}) do
count = count + 1
end
return count
end
local function SortedQuestIdKeys(tableValue)
local keys = {}
for questId in pairs(tableValue or {}) do
keys[#keys + 1] = questId
end
table.sort(keys, function(a, b)
local aNum = QuestTogether:SafeToNumber(a)
local bNum = QuestTogether:SafeToNumber(b)
if aNum ~= nil and bNum ~= nil then
return aNum < bNum
end
return SafeText(a, "") < SafeText(b, "")
end)
return keys
end
local function BuildTaskAreaStateSummary(stateByQuestId)
local parts = {}
local keys = SortedQuestIdKeys(stateByQuestId)
local maxParts = math.min(#keys, 5)
for index = 1, maxParts do
local questId = keys[index]
local questTitle = stateByQuestId and stateByQuestId[questId] or nil
parts[#parts + 1] = tostring(questId) .. ":" .. SafeText(questTitle, "Unknown")
end
if #keys > maxParts then
parts[#parts + 1] = string.format("...(%d more)", #keys - maxParts)
end
return table.concat(parts, " | ")
end
local function BuildMergedTaskAreaQuestInfo(addon, questId, liveQuestInfo, snapshotInfo)
local mergedQuestInfo = {}
local liveInfo = type(liveQuestInfo) == "table" and liveQuestInfo or nil
local snapshot = type(snapshotInfo) == "table" and snapshotInfo or nil
mergedQuestInfo.questID = liveInfo and liveInfo.questID or (snapshot and snapshot.questID) or questId
mergedQuestInfo.title = liveInfo and liveInfo.title or (snapshot and snapshot.title) or nil
mergedQuestInfo.questLogIndex = liveInfo and liveInfo.questLogIndex or (snapshot and snapshot.questLogIndex) or nil
mergedQuestInfo.isHeader = liveInfo and liveInfo.isHeader == true or false
mergedQuestInfo.isTask = liveInfo and liveInfo.isTask == true or (snapshot and snapshot.isTask == true) or false
mergedQuestInfo.isOnMap = liveInfo and liveInfo.isOnMap == true or false
mergedQuestInfo.hasLocalPOI = liveInfo and liveInfo.hasLocalPOI == true or false
mergedQuestInfo.isComplete = liveInfo and liveInfo.isComplete == true or (snapshot and snapshot.isComplete == true) or false
mergedQuestInfo.isWorldQuest = liveInfo and liveInfo.isWorldQuest == true or (snapshot and snapshot.isWorldQuest == true) or false
mergedQuestInfo.displayAsObjective = snapshot and snapshot.displayAsObjective == true or false
mergedQuestInfo.isBonusObjective = snapshot and snapshot.isBonusObjective == true or false
mergedQuestInfo.taskAnnouncementType = snapshot and snapshot.taskAnnouncementType or nil
local shouldTreatHiddenAsRelevant = mergedQuestInfo.isTask == true
or mergedQuestInfo.isWorldQuest == true
or mergedQuestInfo.displayAsObjective == true
or mergedQuestInfo.isBonusObjective == true
or mergedQuestInfo.taskAnnouncementType == "world"
or mergedQuestInfo.taskAnnouncementType == "bonus"
if liveInfo and liveInfo.isHidden == true and not shouldTreatHiddenAsRelevant then
mergedQuestInfo.isHidden = true
else
mergedQuestInfo.isHidden = false
end
if mergedQuestInfo.isWorldQuest ~= true and addon and addon.API and addon.API.IsWorldQuest then
mergedQuestInfo.isWorldQuest = addon.API.IsWorldQuest(questId) == true
end
if
mergedQuestInfo.isWorldQuest ~= true
and mergedQuestInfo.displayAsObjective ~= true
and mergedQuestInfo.isBonusObjective ~= true
and addon
and addon.API
and addon.API.GetTaskQuestInfoByQuestID
then
local taskQuestInfo = addon.API.GetTaskQuestInfoByQuestID(questId)
if type(taskQuestInfo) == "table" and taskQuestInfo.displayAsObjective == true then
mergedQuestInfo.displayAsObjective = true
mergedQuestInfo.isBonusObjective = true
end
end
return mergedQuestInfo
end
local function BuildQuestLogQuestInfoIndex(addon)
local questInfoByQuestId = {}
if not (addon and addon.API and addon.API.GetNumQuestLogEntries and addon.API.GetQuestLogInfo) then
return questInfoByQuestId
end
if addon.EnsureQuestSnapshotStore then
addon:EnsureQuestSnapshotStore()
end
local snapshotByQuestID = addon.GetQuestSnapshotByQuestID and addon:GetQuestSnapshotByQuestID() or nil
local totalEntries = addon:SafeToNumber(addon.API.GetNumQuestLogEntries()) or 0
for entryIndex = 1, totalEntries do
local liveQuestInfo = addon.API.GetQuestLogInfo(entryIndex)
if type(liveQuestInfo) == "table" then
local normalizedQuestId = NormalizeQuestId(addon, liveQuestInfo.questID)
if normalizedQuestId and not questInfoByQuestId[normalizedQuestId] then
local snapshotInfo = snapshotByQuestID and snapshotByQuestID[normalizedQuestId] or nil
local mergedQuestInfo = BuildMergedTaskAreaQuestInfo(addon, normalizedQuestId, liveQuestInfo, snapshotInfo)
if mergedQuestInfo.isHeader ~= true and mergedQuestInfo.isHidden ~= true then
questInfoByQuestId[normalizedQuestId] = mergedQuestInfo
end
end
end
end
return questInfoByQuestId
end
local function BuildTaskAreaCandidateQuestIds(addon, questInfoByQuestId)
local candidateQuestIds = {}
for normalizedQuestId in pairs(questInfoByQuestId or {}) do
candidateQuestIds[normalizedQuestId] = true
end
return candidateQuestIds
end
local function BuildActiveTaskAreaSnapshot(taskAreaState, taskType)
local activeByQuestId = {}
if type(taskAreaState) ~= "table" then
return activeByQuestId
end
local resolvedByQuestID = taskAreaState.resolvedByQuestID or {}
local resolutionOrder = taskAreaState.resolutionOrder or {}
for index = 1, #resolutionOrder do
local questId = resolutionOrder[index]
local resolution = resolvedByQuestID[questId]
if type(resolution) == "table" then
if taskType == "bonus" then
if resolution.includeBonus then
activeByQuestId[questId] = resolution.title
end
elseif resolution.includeWorld then
activeByQuestId[questId] = resolution.title
end
end
end
return activeByQuestId
end
local function ResolveQuestAreaSignals(addon, taskType, questInfo, normalizedQuestId, isBonusObjective)
local isOnMap = questInfo and questInfo.isOnMap == true or false
local hasLocalPOI = questInfo and questInfo.hasLocalPOI == true or false
local mapFlags = (isOnMap or hasLocalPOI) and true or false
local areaActive = false
if taskType == "world" then
areaActive = isOnMap
elseif mapFlags and isBonusObjective == true then
areaActive = true
end
return {
mapFlags = mapFlags,
areaActive = areaActive and true or false,
taskActiveForArea = false,
canUseTaskActiveWorldFallback = false,
}
end
local function BuildTaskAreaResolution(addon, normalizedQuestId, questInfo)
local title = addon:GetQuestTitle(normalizedQuestId, questInfo)
local explicitWorld = questInfo and questInfo.isWorldQuest == true or false
local explicitTask = questInfo and questInfo.isTask == true or false
local fallbackWorld = addon:IsWorldQuest(normalizedQuestId)
local isWorldQuest = explicitWorld or fallbackWorld
local explicitBonus = questInfo and (questInfo.isBonusObjective == true or questInfo.displayAsObjective == true) or false
local isBonusObjective = isWorldQuest ~= true and explicitBonus
local taskAnnouncementType = isWorldQuest and "world" or (isBonusObjective and "bonus" or nil)
local isTask = explicitTask or isWorldQuest or isBonusObjective
local worldSignals = ResolveQuestAreaSignals(addon, "world", questInfo, normalizedQuestId, isBonusObjective)
local bonusSignals = ResolveQuestAreaSignals(addon, "bonus", questInfo, normalizedQuestId, isBonusObjective)
local isWorldQuestByActiveTaskFallback = false
if not isWorldQuest and worldSignals.canUseTaskActiveWorldFallback and not isBonusObjective then
isWorldQuest = true
isWorldQuestByActiveTaskFallback = true
end
local shouldPromoteToTask = worldSignals.areaActive == true or bonusSignals.areaActive == true
if bonusSignals.taskActiveForArea == true then
shouldPromoteToTask = true
end
if not isTask and shouldPromoteToTask then
isTask = true
end
local includeWorld = isTask and worldSignals.areaActive == true and isWorldQuest == true
local includeBonus = isTask and bonusSignals.areaActive == true and isWorldQuest ~= true
if taskAnnouncementType == "world" and addon and addon.Debugf then
addon:Debugf(
"DEBUG",
"world_area_resolve questId=%s title=%s task=%s onMap=%s poi=%s explicitWorld=%s fallbackWorld=%s includeWorld=%s",
tostring(normalizedQuestId),
SafeText(title, "Unknown"),
tostring(explicitTask),
tostring(questInfo and questInfo.isOnMap == true or false),
tostring(questInfo and questInfo.hasLocalPOI == true or false),
tostring(explicitWorld),
tostring(fallbackWorld),
tostring(includeWorld)
)
end
return {
questID = normalizedQuestId,
title = title,
taskAnnouncementType = taskAnnouncementType,
explicitWorld = explicitWorld,
fallbackWorld = fallbackWorld,
explicitTask = explicitTask,
explicitBonus = explicitBonus,
isWorldQuest = isWorldQuest and true or false,
isBonusObjective = isBonusObjective and true or false,
isTask = isTask and true or false,
isWorldQuestByActiveTaskFallback = isWorldQuestByActiveTaskFallback,
includeWorld = includeWorld and true or false,
includeBonus = includeBonus and true or false,
worldSignals = worldSignals,
bonusSignals = bonusSignals,
}
end
function QuestTogether:RebuildTaskAreaResolverStore()
local taskAreaState = self.GetTaskAreaSubsystemStateStore and self:GetTaskAreaSubsystemStateStore() or nil
if type(taskAreaState) ~= "table" then
return nil
end
local resolvedByQuestID = taskAreaState.resolvedByQuestID or {}
local resolutionOrder = taskAreaState.resolutionOrder or {}
wipe(resolvedByQuestID)
wipe(resolutionOrder)
local questInfoByQuestId = BuildQuestLogQuestInfoIndex(self)
local candidateQuestIds = BuildTaskAreaCandidateQuestIds(self, questInfoByQuestId)
self:Debugf(
"DEBUG",
"task_area_scan rows=%d candidates=%d",
CountKeys(questInfoByQuestId),
CountKeys(candidateQuestIds)
)
for _, normalizedQuestId in ipairs(SortedQuestIdKeys(candidateQuestIds)) do
local questInfo = questInfoByQuestId[normalizedQuestId]
local resolution = BuildTaskAreaResolution(self, normalizedQuestId, questInfo)
resolvedByQuestID[normalizedQuestId] = resolution
resolutionOrder[#resolutionOrder + 1] = normalizedQuestId
end
taskAreaState.resolvedByQuestID = resolvedByQuestID
taskAreaState.resolutionOrder = resolutionOrder
taskAreaState.generation = (taskAreaState.generation or 0) + 1
return taskAreaState
end
function QuestTogether:GetTaskAreaSnapshot(taskType)
local taskAreaState = self.GetTaskAreaSubsystemStateStore and self:GetTaskAreaSubsystemStateStore() or nil
if type(taskAreaState) ~= "table" then
return {}
end
local sourceState = self.GetTaskAreaStateStore and self:GetTaskAreaStateStore(taskType) or {}
local activeByQuestId = {}
for questId, questTitle in pairs(sourceState) do
local normalizedQuestId = NormalizeQuestId(self, questId)
if normalizedQuestId then
activeByQuestId[normalizedQuestId] = questTitle
end
end
return activeByQuestId
end
function QuestTogether:GetActiveWorldQuestAreaSnapshot()
return self:GetTaskAreaSnapshot("world")
end
function QuestTogether:GetActiveBonusObjectiveAreaSnapshot()
return self:GetTaskAreaSnapshot("bonus")
end
function QuestTogether:RefreshTaskAreaState(taskType, shouldAnnounce)
local configByType = {
world = {
enterEvent = "WORLD_QUEST_ENTERED",
leftEvent = "WORLD_QUEST_LEFT",
enterPrefix = "World Quest Entered: ",
leftPrefix = "Left World Quest: ",
debugLabel = "World quest",
},
bonus = {
enterEvent = "BONUS_OBJECTIVE_ENTERED",
leftEvent = "BONUS_OBJECTIVE_LEFT",
enterPrefix = "Bonus Objective Entered: ",
leftPrefix = "Left Bonus Objective: ",
debugLabel = "Bonus objective",
},
}
local config = configByType[taskType]
if not config then
return
end
local taskAreaState = self.GetTaskAreaSubsystemStateStore and self:GetTaskAreaSubsystemStateStore() or nil
local previousStateRaw = self.GetTaskAreaStateStore and self:GetTaskAreaStateStore(taskType) or {}
local currentStateRaw = BuildActiveTaskAreaSnapshot(taskAreaState, taskType)
local previousState = {}
local currentState = {}
for questId, questTitle in pairs(previousStateRaw) do
local normalizedQuestId = NormalizeQuestId(self, questId)
if normalizedQuestId then
previousState[normalizedQuestId] = questTitle
end
end
for questId, questTitle in pairs(currentStateRaw) do
local normalizedQuestId = NormalizeQuestId(self, questId)
if normalizedQuestId then
currentState[normalizedQuestId] = questTitle
end
end
if taskType == "world" then
self:Debugf(
"DEBUG",
"world_area_state announce=%s prev=%d curr=%d current=%s",
tostring(shouldAnnounce and true or false),
CountKeys(previousState),
CountKeys(currentState),
BuildTaskAreaStateSummary(currentState)
)
end
if taskAreaState and self.GetTaskAreaStateStore then
local stateStore = self:GetTaskAreaStateStore(taskType)
wipe(stateStore)
for questId, questTitle in pairs(currentState) do
stateStore[questId] = questTitle
end
end
for questId, questTitle in pairs(currentState) do
if not previousState[questId] and shouldAnnounce then
if taskType == "world" then
self:Debugf(
"DEBUG",
"world_area_enter questId=%s title=%s",
tostring(questId),
SafeText(questTitle, "Unknown")
)
end
self:PublishAnnouncementEvent(config.enterEvent, config.enterPrefix .. SafeText(questTitle, "Unknown"), questId)
end
end
for questId, previousTitle in pairs(previousState) do
if not currentState[questId] then
local wasCompleted = self.questsCompleted[questId] ~= nil
if shouldAnnounce and not wasCompleted then
local questTitle = previousTitle or self:GetQuestTitle(questId)
if taskType == "world" then
self:Debugf(
"DEBUG",
"world_area_left questId=%s title=%s",
tostring(questId),
SafeText(questTitle, "Unknown")
)
end
self:PublishAnnouncementEvent(config.leftEvent, config.leftPrefix .. SafeText(questTitle, "Unknown"), questId)
end
end
end
end
function QuestTogether:RefreshWorldQuestAreaState(shouldAnnounce)
self:RefreshTaskAreaState("world", shouldAnnounce)
end
function QuestTogether:RefreshBonusObjectiveAreaState(shouldAnnounce)
self:RefreshTaskAreaState("bonus", shouldAnnounce)
end
function QuestTogether:ScheduleTaskAreaRefresh(shouldAnnounce, delaySeconds)
if delaySeconds ~= nil and delaySeconds <= 0 then
delaySeconds = nil
end
if self.ScheduleTaskAreaRefreshWork then
self:ScheduleTaskAreaRefreshWork(shouldAnnounce, delaySeconds, "ScheduleTaskAreaRefresh")
return
end
self:RefreshTaskAreaStates(shouldAnnounce)
end
function QuestTogether:RefreshTaskAreaStates(shouldAnnounce)
if self.IsWorkBlocked and self:IsWorkBlocked("task_area_refresh") then
if shouldAnnounce then
self:SetRuntimeFlag("pendingScheduledTaskAreaRefreshShouldAnnounce", true)
end
self:ScheduleTaskAreaRefresh(shouldAnnounce)
return false
end
local pendingAnnounce = self:GetRuntimeFlag("pendingScheduledTaskAreaRefreshShouldAnnounce", false)
local resolvedShouldAnnounce = shouldAnnounce or (pendingAnnounce and true or false)
self:SetRuntimeFlag("pendingScheduledTaskAreaRefreshShouldAnnounce", false)
self:RebuildTaskAreaResolverStore()
self:RefreshWorldQuestAreaState(resolvedShouldAnnounce)
self:RefreshBonusObjectiveAreaState(resolvedShouldAnnounce)
return true
end