From 5fba4ac10c244b6dc590d55ed5d63d2b9c419827 Mon Sep 17 00:00:00 2001 From: dfj Date: Sat, 24 Oct 2026 13:37:33 +0200 Subject: [PATCH] Keep latest-phase progress independent of the selected phase The bar could change when switching phases: latestCompletionRatio was nil until the selected phase's stats were complete AND the latest list was cached, and the UI then fell back to the selected phase's own completion ratio. Right after login the latest list's items are often not cached yet, so switching phases visibly moved the bar. Always compute a latest-phase ratio inside CompareGear: stats-based when both the latest list and the equipped items are cached, otherwise slot completion against the latest list. Return latestStatsComplete so the UI can mark the estimate with "~" and keep retrying until stats load (capped at 20 attempts). The UI now uses latestCompletionRatio directly instead of falling back to the selected phase. Also refresh on PLAYER_EQUIPMENT_CHANGED so gear swaps update the bar. Tests: phase-independent ratio, slot-based fallback while the latest list is uncached, and equal ratios across selected phases once cached. 106 passing. --- AGENTS.md | 1 + Amibis/Amibis.lua | 53 ++++++++++++++++++------- Amibis/Tests/test_compare.lua | 74 ++++++++++++++++++++++++++++++++++- Amibis/UI/ComparisonFrame.lua | 49 ++++++++++++++--------- 4 files changed, 142 insertions(+), 35 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index e9e60eb..631cde9 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -41,4 +41,5 @@ Amibis/ - Color coding: green = BIS, yellow = upgrade available, gray = empty slot - Max 17 visible rows; rows are clickable (Shift+click = chat link, Ctrl+click = dress-up) - Stats panel is **per-spec**: `SPEC_STAT_PROFILES` in `Amibis.lua` defines an ordered `{ stat, weight }` list for every class/spec (casters see spell damage/hit/crit/haste, melee see strength/AP/hit/expertise/armor pen, tanks see stamina/defense/dodge/parry/block, healers see healing/spirit/mp5). The same weights rank top upgrades, score completion, and pick which two gains are shown in the upgrade line. Keep profiles to 8 rows so the panel fits the frame. +- The phase-progress bar always measures the **latest** phase, independent of the selected phase: stats-based once item data is cached, slot-based (prefixed `~`) until then. It refreshes on talent and equipment changes. - Stat values are read via `STAT_MAP` (WoW `GetItemStats` keys → internal stat names); TBC's per-school hit/crit/haste keys are all mapped to one internal stat. diff --git a/Amibis/Amibis.lua b/Amibis/Amibis.lua index 4e8c7c8..9f3ff4e 100644 --- a/Amibis/Amibis.lua +++ b/Amibis/Amibis.lua @@ -502,28 +502,49 @@ function Amibis:CompareGear() end local latestPhase = ns.Data.BISLists:GetLatestPhase(class, spec) - local latestCompletionRatio = nil - if latestPhase and statsComplete then + local latestCompletionRatio = 0 + local latestStatsComplete = false + if latestPhase then local latestList = ns.Data.BISLists:GetBISList(class, spec, latestPhase) if latestList then - local latestStatsComplete = true + -- The bar always measures the latest phase, so it must not depend on + -- which phase is selected. Warm the cache for the latest list and the + -- equipped items, then use stats when available and fall back to slot + -- completion (still latest-phase based) while item data is loading. + local latestItems = {} + local latestItemsCached = true for slotName, bisItem in pairs(latestList) do + latestItems[slotName] = { itemID = bisItem.itemID } if not GetItemInfo(bisItem.itemID) then - latestStatsComplete = false + latestItemsCached = false + end + end + + local equippedCached = true + for _, item in pairs(equipped) do + if item and item.itemID and not GetItemInfo(item.itemID) then + equippedCached = false break end end - if latestStatsComplete then - if latestPhase == phase then - latestCompletionRatio = self:StatRatio(equippedStats, bisStats, class, spec) - else - local latestItems = {} - for slotName, bisItem in pairs(latestList) do - latestItems[slotName] = { itemID = bisItem.itemID } - end - local latestStats = self:AggregateStats(latestItems) - latestCompletionRatio = self:StatRatio(equippedStats, latestStats, class, spec) + + if latestItemsCached and equippedCached then + local latestStats = bisStats + if latestPhase ~= phase then + latestStats = self:AggregateStats(latestItems) end + latestCompletionRatio = self:StatRatio(equippedStats, latestStats, class, spec) + latestStatsComplete = true + else + local matched, total = 0, 0 + for slotName, bisItem in pairs(latestList) do + total = total + 1 + local equippedItem = equipped[slotName] + if equippedItem and equippedItem.itemID == bisItem.itemID then + matched = matched + 1 + end + end + latestCompletionRatio = total > 0 and (matched / total) or 0 end end end @@ -542,6 +563,7 @@ function Amibis:CompareGear() statsComplete = statsComplete, latestPhase = latestPhase, latestCompletionRatio = latestCompletionRatio, + latestStatsComplete = latestStatsComplete, } end @@ -583,6 +605,7 @@ local eventFrame = CreateFrame("Frame") eventFrame:RegisterEvent("ADDON_LOADED") eventFrame:RegisterEvent("PLAYER_LOGIN") eventFrame:RegisterEvent("PLAYER_TALENT_UPDATE") +eventFrame:RegisterEvent("PLAYER_EQUIPMENT_CHANGED") eventFrame:SetScript("OnEvent", function(self, event, ...) if event == "ADDON_LOADED" then @@ -592,7 +615,7 @@ eventFrame:SetScript("OnEvent", function(self, event, ...) end elseif event == "PLAYER_LOGIN" then Amibis:InitializeUI() - elseif event == "PLAYER_TALENT_UPDATE" then + elseif event == "PLAYER_TALENT_UPDATE" or event == "PLAYER_EQUIPMENT_CHANGED" then if ns.UI and ns.UI.RefreshUI then ns.UI.RefreshUI() end diff --git a/Amibis/Tests/test_compare.lua b/Amibis/Tests/test_compare.lua index c229122..43c62a0 100644 --- a/Amibis/Tests/test_compare.lua +++ b/Amibis/Tests/test_compare.lua @@ -303,10 +303,12 @@ describe("latest phase progress", function() AmibisCharDB.overrideSpec = "Shadow" end) - it("exposes the latest phase and nil ratio while stats load", function() + it("reports the latest phase and an estimate while stats load", function() local data = ns.Amibis:CompareGear() expect(data.latestPhase).toEqual("ZA") - expect(data.latestCompletionRatio).toBeNil() + expect(data.latestStatsComplete).toBeFalsy() + expect(type(data.latestCompletionRatio)).toEqual("number") + expect(data.latestCompletionRatio).toEqual(0) end) it("measures progress against the latest phase, not the selected one", function() @@ -349,3 +351,71 @@ describe("latest phase progress", function() expect(data.latestPhase).toEqual("ZA") end) end) + +describe("latest phase progress is independent of the selected phase", function() + before_each(function() + MockItems = {} + MockEquipped = {} + AmibisCharDB.overrideSpec = "Shadow" + AmibisDB.selectedPhase = "T4" + end) + + it("falls back to latest-phase slot completion when the latest list is not cached", function() + local t4 = ns.Data.BISLists:GetBISList("PRIEST", "Shadow", "T4") + local za = ns.Data.BISLists:GetBISList("PRIEST", "Shadow", "ZA") + + -- Make the selected phase look fully cached, but leave the ZA list uncached. + for _, entry in pairs(t4) do + MockItems[entry.itemID] = { + name = entry.name, + stats = { + ["ITEM_MOD_INTELLECT_SHORT"] = 100, + ["ITEM_MOD_SPELL_DAMAGE_DONE_SHORT"] = 50, + }, + } + end + + -- Equip two ZA BIS pieces without caching the rest of the list. + equip("Head", za["Head"].itemID) + equip("Shoulder", za["Shoulder"].itemID) + + local total = 0 + for _ in pairs(za) do total = total + 1 end + + local data = ns.Amibis:CompareGear() + expect(data.latestPhase).toEqual("ZA") + expect(data.latestStatsComplete).toBeFalsy() + expect(data.latestCompletionRatio).toBeCloseTo(2 / total) + end) + + it("reports the same ratio for every selected phase once stats are cached", function() + for _, phase in ipairs({ "T4", "T5", "T6", "ZA" }) do + for _, entry in pairs(ns.Data.BISLists:GetBISList("PRIEST", "Shadow", phase)) do + MockItems[entry.itemID] = { + name = entry.name, + stats = { + ["ITEM_MOD_INTELLECT_SHORT"] = 100, + ["ITEM_MOD_SPELL_DAMAGE_DONE_SHORT"] = 50, + }, + } + end + end + + local t6 = ns.Data.BISLists:GetBISList("PRIEST", "Shadow", "T6") + for slot, entry in pairs(t6) do + equip(slot, entry.itemID) + end + + local results = {} + for _, phase in ipairs({ "T4", "T5", "T6", "ZA" }) do + AmibisDB.selectedPhase = phase + local data = ns.Amibis:CompareGear() + expect(data.latestStatsComplete).toBeTruthy() + table.insert(results, data.latestCompletionRatio) + end + + for i = 2, #results do + expect(results[i]).toBeCloseTo(results[1]) + end + end) +end) diff --git a/Amibis/UI/ComparisonFrame.lua b/Amibis/UI/ComparisonFrame.lua index c79f7f1..182daa9 100644 --- a/Amibis/UI/ComparisonFrame.lua +++ b/Amibis/UI/ComparisonFrame.lua @@ -587,10 +587,30 @@ function ns.UI.ToggleFrame() end else MainFrame:Show() + Amibis._statsRetryAttempts = nil ns.UI.RefreshUI() end end +local function ScheduleStatsRefresh() + if Amibis._statsRetryTimer then return end + Amibis._statsRetryAttempts = (Amibis._statsRetryAttempts or 0) + 1 + if Amibis._statsRetryAttempts > 20 then return end + Amibis._statsRetryTimer = CreateFrame("Frame") + Amibis._statsRetryTimer.elapsed = 0 + Amibis._statsRetryTimer:SetScript("OnUpdate", function(self, elapsed) + self.elapsed = self.elapsed + elapsed + if self.elapsed >= 1.5 then + self:Hide() + Amibis._statsRetryTimer = nil + if MainFrame and MainFrame:IsShown() then + ns.UI.RefreshUI() + end + end + end) + Amibis._statsRetryTimer:Show() +end + function ns.UI.RefreshUI() if not MainFrame or not MainFrame:IsShown() then return @@ -629,13 +649,14 @@ function ns.UI.RefreshUI() if PhaseBar then RebuildPhaseButtons(data.class, data.spec, data.phase) - local ratio = data.latestCompletionRatio or Amibis:GetCompletionRatio(data) + local ratio = data.latestCompletionRatio or 0 local targetPhase = data.latestPhase or data.phase if PhaseProgressFill then PhaseProgressFill:SetWidth(math.max(1, PhaseBar:GetWidth() * ratio)) end if PhaseProgressText then - PhaseProgressText:SetText(string.format("%d%% to %s", math.floor(ratio * 100 + 0.5), targetPhase)) + local prefix = data.latestStatsComplete == false and "~" or "" + PhaseProgressText:SetText(string.format("%s%d%% to %s", prefix, math.floor(ratio * 100 + 0.5), targetPhase)) end end @@ -727,22 +748,6 @@ function ns.UI.RefreshUI() vals.diff:SetText("") end end - - if not Amibis._statsRetryTimer then - Amibis._statsRetryTimer = CreateFrame("Frame") - Amibis._statsRetryTimer.elapsed = 0 - Amibis._statsRetryTimer:SetScript("OnUpdate", function(self, elapsed) - self.elapsed = self.elapsed + elapsed - if self.elapsed >= 1.5 then - self:Hide() - Amibis._statsRetryTimer = nil - if MainFrame and MainFrame:IsShown() then - ns.UI.RefreshUI() - end - end - end) - Amibis._statsRetryTimer:Show() - end else if StatsPanelTitle then StatsPanelTitle:SetText("Stats Comparison") @@ -801,6 +806,14 @@ function ns.UI.RefreshUI() end end end + + -- Keep retrying until both the selected list and the latest-phase progress + -- have item stats; otherwise the bar can be stuck on the slot estimate. + if not data.statsComplete or data.latestStatsComplete == false then + ScheduleStatsRefresh() + else + Amibis._statsRetryAttempts = nil + end end local initFrame = CreateFrame("Frame")