-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildCatalogService.lua
More file actions
456 lines (396 loc) · 18.6 KB
/
BuildCatalogService.lua
File metadata and controls
456 lines (396 loc) · 18.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
local _, ns = ...
local Constants = ns.Constants
local BuildHash = ns.BuildHash
local BuildCatalogService = {}
-- ──────────────────────────────────────────────────────────────────────────────
-- Internal helpers
-- ──────────────────────────────────────────────────────────────────────────────
local function getStore()
return ns.Addon:GetModule("CombatStore")
end
local function getSnapshotService()
return ns.Addon:GetModule("SnapshotService")
end
-- Clear isCurrentBuild on every profile belonging to characterKey+specId except
-- the one being set as current. Called before marking the new current profile.
local function clearCurrentBuildFlags(characterKey, specId, exceptBuildId)
local store = getStore()
if not store then return end
local profiles = store:GetAllBuildProfiles(characterKey)
for _, p in ipairs(profiles) do
if p.isCurrentBuild and p.buildId ~= exceptBuildId then
-- Only clear flag if same spec (don't disturb other specs' current build).
if not specId or p.specId == specId then
store:UpdateBuildProfileFlag(p.buildId, "isCurrentBuild", false)
end
end
end
end
-- Build a human-readable display label for a profile. Uses WoW APIs for
-- spec and hero talent names; falls back gracefully when unavailable.
local function buildDisplayLabel(profile)
if not profile then return "Unknown Build" end
-- Spec name.
local specName = nil
if profile.specId then
local ok, _, name = pcall(GetSpecializationInfoByID, profile.specId)
if ok and name then specName = name end
end
specName = specName or ("Spec " .. tostring(profile.specId or "?"))
-- Hero talent tree name (optional).
local heroName = nil
if profile.heroTalentSpecId and profile.heroTalentSpecId ~= 0 then
-- GetHeroTalentSpecInfo is not always available; guard it.
if C_ClassTalents and C_ClassTalents.GetHeroTalentSpecInfo then
local ok2, info = pcall(C_ClassTalents.GetHeroTalentSpecInfo, profile.heroTalentSpecId)
if ok2 and info and info.name then
heroName = info.name
end
end
if not heroName then
-- Fallback: use the spec name suffix from seeded archetype data if available.
heroName = "Hero " .. tostring(profile.heroTalentSpecId)
end
end
-- Abbreviated PvP talent summary (first two talent names or IDs).
local pvpParts = {}
if profile.pvpTalentSignature and profile.pvpTalentSignature ~= "" then
for idStr in profile.pvpTalentSignature:gmatch("[^,]+") do
local talentId = tonumber(idStr)
if talentId then
local ok3, n = pcall(ns.ApiCompat.GetSpellName, talentId)
local spellName = ok3 and n or nil
pvpParts[#pvpParts + 1] = spellName or tostring(talentId)
end
if #pvpParts >= 2 then break end
end
end
local pvpSuffix = #pvpParts > 0 and (" / " .. table.concat(pvpParts, " + ")) or ""
if heroName then
return specName .. " / " .. heroName .. pvpSuffix
else
return specName .. pvpSuffix
end
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Core catalog operations (T010)
-- ──────────────────────────────────────────────────────────────────────────────
-- Register or update the catalog entry for the talent setup described by
-- |snapshot|. Sets isCurrentBuild=true on the new/updated profile and clears
-- the flag on all other profiles for the same character+spec.
-- Returns the buildId string, or nil if snapshot is nil/insufficient.
function BuildCatalogService:RefreshFromSnapshot(snapshot)
if not snapshot then return nil end
local store = getStore()
if not store then return nil end
local buildId = snapshot.buildId or BuildHash.ComputeBuildId(snapshot)
if not buildId then return nil end
local loadoutId = snapshot.loadoutId or BuildHash.ComputeLoadoutId(snapshot)
local characterKey = snapshot.name and snapshot.realm
and (snapshot.name .. "-" .. snapshot.realm) or nil
local specId = snapshot.specId
-- Build the initial display label.
local label = buildDisplayLabel({
specId = specId,
heroTalentSpecId = snapshot.heroTalentSpecId,
pvpTalentSignature = snapshot.pvpTalents and table.concat(snapshot.pvpTalents, ",") or "",
})
-- Serialize identity signatures for storage.
local pvpSig = ""
if snapshot.pvpTalents then
local sorted = {}
for _, v in ipairs(snapshot.pvpTalents) do sorted[#sorted + 1] = tostring(v) end
table.sort(sorted)
pvpSig = table.concat(sorted, ",")
end
local talentSig = ""
if snapshot.talentNodes then
local parts = {}
for _, node in ipairs(snapshot.talentNodes) do
parts[#parts + 1] = table.concat({
tostring(node.nodeId or 0),
tostring(node.entryId or 0),
tostring(node.activeRank or 0),
}, ":")
end
table.sort(parts)
talentSig = table.concat(parts, "|")
end
-- Legacy hash for audit/migration.
local legacyHash = snapshot.buildHash
local legacyHashes = legacyHash and { legacyHash } or {}
-- Loadout ID list.
local existingProfile = store:GetBuildProfile(buildId)
local loadoutIds = existingProfile and existingProfile.associatedLoadoutIds or {}
if loadoutId then
local found = false
for _, lid in ipairs(loadoutIds) do
if lid == loadoutId then found = true; break end
end
if not found then
local newList = {}
for _, lid in ipairs(loadoutIds) do newList[#newList + 1] = lid end
newList[#newList + 1] = loadoutId
loadoutIds = newList
end
end
-- Session count: preserve existing + 1 only on new session attachment.
-- Actual sessionCount is authoritative from migration/PersistSession callbacks;
-- here we just ensure the field exists.
local sessionCount = existingProfile and existingProfile.sessionCount or 0
clearCurrentBuildFlags(characterKey, specId, buildId)
store:UpsertBuildProfile(buildId, {
buildIdentityVersion = Constants.BUILD_IDENTITY_VERSION,
classId = snapshot.classId,
specId = specId,
heroTalentSpecId = snapshot.heroTalentSpecId,
talentSignature = talentSig,
pvpTalentSignature = pvpSig,
displayNames = { label },
associatedLoadoutIds = loadoutIds,
legacyBuildHashes = legacyHashes,
lastSeenAt = GetTime(),
sessionCount = sessionCount,
characterKey = characterKey,
isCurrentBuild = true,
isLowConfidence = sessionCount < Constants.CONFIDENCE_TIER_THRESHOLDS.LOW_MIN,
})
ns.Addon:Trace("catalog.refresh", {
buildId = buildId,
specId = specId or 0,
freshness = snapshot.snapshotFreshness or "unknown",
isCurrentBuild = true,
})
return buildId
end
-- Return the catalog profile where isCurrentBuild == true for the current
-- character. Falls back to constructing a transient (non-persisted) profile
-- from the live snapshot if none is flagged.
function BuildCatalogService:GetCurrentLiveBuild()
local store = getStore()
if not store then return nil end
local svc = getSnapshotService()
local snapshot = svc and svc:GetLatestPlayerSnapshot()
if not snapshot then return nil end
local characterKey = snapshot.name and snapshot.realm
and (snapshot.name .. "-" .. snapshot.realm) or nil
-- Look for the flagged current profile first.
if characterKey then
local profiles = store:GetAllBuildProfiles(characterKey)
for _, p in ipairs(profiles) do
if p.isCurrentBuild then return p end
end
end
-- No flagged profile: build and return a transient one without persisting.
local buildId = snapshot.buildId or BuildHash.ComputeBuildId(snapshot)
if not buildId then return nil end
local pvpSig = ""
if snapshot.pvpTalents then
local sorted = {}
for _, v in ipairs(snapshot.pvpTalents) do sorted[#sorted + 1] = tostring(v) end
table.sort(sorted)
pvpSig = table.concat(sorted, ",")
end
return {
buildId = buildId,
specId = snapshot.specId,
heroTalentSpecId = snapshot.heroTalentSpecId,
pvpTalentSignature = pvpSig,
characterKey = characterKey,
isCurrentBuild = true,
sessionCount = 0,
firstSeenAt = GetTime(),
lastSeenAt = GetTime(),
displayNames = { buildDisplayLabel({
specId = snapshot.specId,
heroTalentSpecId = snapshot.heroTalentSpecId,
pvpTalentSignature = pvpSig,
})},
_isTransient = true, -- marker: not persisted to catalog
}
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Catalog query methods (T011)
-- ──────────────────────────────────────────────────────────────────────────────
-- Return all non-archived profiles for characterKey, sorted by lastSeenAt desc
-- with the current live build pinned first.
function BuildCatalogService:GetAllProfiles(characterKey)
local store = getStore()
if not store then return {} end
-- Default to current character key if nil.
if not characterKey then
local svc = getSnapshotService()
local snap = svc and svc:GetLatestPlayerSnapshot()
if snap and snap.name and snap.realm then
characterKey = snap.name .. "-" .. snap.realm
end
end
local profiles = store:GetAllBuildProfiles(characterKey)
-- Pin current live build at index 1.
local result = {}
local currentIdx = nil
for i, p in ipairs(profiles) do
if p.isCurrentBuild then currentIdx = i; break end
end
if currentIdx then
result[1] = profiles[currentIdx]
for i, p in ipairs(profiles) do
if i ~= currentIdx then
result[#result + 1] = p
end
end
else
-- No persisted current build; prepend a transient one.
local live = self:GetCurrentLiveBuild()
if live then result[1] = live end
for _, p in ipairs(profiles) do result[#result + 1] = p end
end
return result
end
-- Return a single profile by buildId.
function BuildCatalogService:GetProfile(buildId)
if not buildId then return nil end
local store = getStore()
return store and store:GetBuildProfile(buildId) or nil
end
-- Return a human-readable label for a buildId. Never returns nil.
-- If the profile has at least one user-assigned alias (FR-039), the first alias
-- is appended in brackets so it is visible everywhere the label is shown.
function BuildCatalogService:GetDisplayLabel(buildId)
if not buildId then return "Unknown Build" end
local profile = self:GetProfile(buildId)
if profile then
local base
if profile.displayNames and profile.displayNames[1] then
base = profile.displayNames[1]
else
base = buildDisplayLabel(profile)
end
-- Append the first user alias when present (FR-039: aliases visible in comparator).
if profile.aliases and profile.aliases[1] then
return base .. " [" .. profile.aliases[1] .. "]"
end
return base
end
return "Unknown Build"
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Management methods (T012)
-- ──────────────────────────────────────────────────────────────────────────────
-- Add an alias to a build profile. Deduplicates case-insensitively.
-- Returns true on success, false if buildId not found or alias is invalid.
function BuildCatalogService:SetAlias(buildId, alias)
if not buildId or type(alias) ~= "string" or alias == "" or #alias > 64 then
return false
end
local store = getStore()
if not store then return false end
local profile = store:GetBuildProfile(buildId)
if not profile then return false end
profile.aliases = profile.aliases or {}
local aliasLower = alias:lower()
for _, existing in ipairs(profile.aliases) do
if existing:lower() == aliasLower then return true end -- already present
end
profile.aliases[#profile.aliases + 1] = alias
return true
end
-- Set isArchived=true on a profile, removing it from selector listings.
function BuildCatalogService:ArchiveProfile(buildId)
if not buildId then return false end
local store = getStore()
if not store then return false end
if not store:GetBuildProfile(buildId) then return false end
store:UpdateBuildProfileFlag(buildId, "isArchived", true)
return true
end
-- Return migration warnings recorded during v6→v7 migration.
-- Returns an empty table until the migration phase populates this.
function BuildCatalogService:GetMigrationWarnings()
local store = getStore()
if not store then return {} end
local db = store:GetDB()
return (db.maintenance and db.maintenance.buildMigrationWarnings) or {}
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Event wiring (T013)
-- Called from SnapshotService after each successful refresh.
-- ──────────────────────────────────────────────────────────────────────────────
-- This is the post-refresh callback wired in SnapshotService (T013).
-- Called after every snapshot refresh; updates the catalog with the new live build.
function BuildCatalogService:OnSnapshotRefreshed(snapshot)
if not snapshot then return end
local ok, err = pcall(function()
self:RefreshFromSnapshot(snapshot)
end)
if not ok then
ns.Addon:Warn("BuildCatalogService:OnSnapshotRefreshed error: %s", tostring(err))
end
end
-- ──────────────────────────────────────────────────────────────────────────────
-- Stat profile persistence (T036–T039)
-- ──────────────────────────────────────────────────────────────────────────────
-- T036: Append a StatProfile to the build's profile history (FIFO, max 10).
-- Returns true on success, false if the build entry does not exist.
function BuildCatalogService:PersistStatProfile(buildId, statProfile)
if not buildId or not statProfile then return false end
local store = getStore()
if not store then return false end
local p = store:GetBuildProfile(buildId)
if not p then return false end
p.statProfiles = p.statProfiles or {}
p.statProfiles[#p.statProfiles + 1] = statProfile
if #p.statProfiles > 10 then
table.remove(p.statProfiles, 1)
end
store:UpsertBuildProfile(buildId, {
latestStatProfile = statProfile,
statProfiles = p.statProfiles,
lastCapturedAt = statProfile.capturedAt,
})
return true
end
-- T037: Capture the current secondary stats and persist them to the catalog.
-- Returns { ok=true, buildId=..., statProfile=... } or { ok=false, reason=... }.
function BuildCatalogService:CaptureAndPersistCurrentBuild()
local svc = getSnapshotService()
if not svc then
return { ok = false, reason = "snapshot_service_unavailable" }
end
local snapshot = svc:GetLatestPlayerSnapshot()
if not snapshot then
return { ok = false, reason = "no_snapshot" }
end
local buildId = snapshot.buildId or BuildHash.ComputeBuildId(snapshot)
if not buildId then
return { ok = false, reason = "cannot_compute_build_id" }
end
-- Fetch the existing latest profile so the overwrite guard can apply.
local store = getStore()
local existingProfile = nil
if store then
local entry = store:GetBuildProfile(buildId)
existingProfile = entry and entry.latestStatProfile or nil
end
local statProfile = svc:CaptureStatProfile(existingProfile)
self:PersistStatProfile(buildId, statProfile)
return { ok = true, buildId = buildId, statProfile = statProfile }
end
-- T038: Capture live secondary stats without persisting. Used by the Build
-- Comparator "Current Build" selector to show live stats for the current gear.
function BuildCatalogService:GetCurrentLiveStatProfile()
local svc = getSnapshotService()
if not svc then return nil end
return svc:CaptureStatProfile()
end
-- T039: Return the full StatProfile history array for a build.
-- Returns {} if the build entry does not exist or has no profiles.
function BuildCatalogService:GetStatProfilesForBuild(buildId)
if not buildId then return {} end
local store = getStore()
if not store then return {} end
local p = store:GetBuildProfile(buildId)
if not p then return {} end
return p.statProfiles or {}
end
ns.Addon:RegisterModule("BuildCatalogService", BuildCatalogService)