From b59d39b701430d88ecc221aa69a94ab5733af65c Mon Sep 17 00:00:00 2001 From: Kenny <48362370+masterred92@users.noreply.github.com> Date: Thu, 3 Dec 2026 09:30:39 -0800 Subject: [PATCH] Fix NeutralFlip scan lifecycle and market isolation for 0.4.1 --- CHANGELOG.md | 6 + NeutralFlip.lua | 207 +++++++++++++++++++++++++++------- NeutralFlip.toc | 2 +- README.md | 8 +- tests/test_neutralflip.lua | 6 +- tests/test_scan_lifecycle.lua | 115 +++++++++++++++++++ 6 files changed, 299 insertions(+), 45 deletions(-) create mode 100644 tests/test_scan_lifecycle.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index 483d4a5..b44cb4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.4.1 +- Cancel pending and chunked scans on a new result, AH close, market correction or price wipe; never mark empty, incomplete or timed-out snapshots successful. +- Keep known faction and neutral price buckets isolated; pause recording at unknown locations or deposit-rate conflicts until confirmed. +- Bind delayed deposit checks to their own AH session, and show generation-safe scan status and market diagnostics in /nflip status and /nflip report. +- Preserve saved data, settings, integrations and packaging. Add mock regressions for scan and market lifecycle. + ## 0.4.0 - Adds conditional cash recovery: minimum units that must sell at the observed listing price to recover acquisition outlay and one failed posting's estimated deposit on unsold units. Unsold stock is assigned no cash proceeds, not zero asset value. - Unknown deposits remain unknown; listing quantity is not demand. diff --git a/NeutralFlip.lua b/NeutralFlip.lua index 7c9300c..0c8ede2 100644 --- a/NeutralFlip.lua +++ b/NeutralFlip.lua @@ -10,6 +10,7 @@ KennyNeutralFlip = NF local SCHEMA = 2 local SCAN_COOLDOWN = 15 * 60 +local SCAN_TIMEOUT = 120 local NEUTRAL = "Neutral" local DEFAULTS = { cutFaction = 0.05, -- faction AH cut (ahledger.com/wow-forever/auction-house); override with /nflip cut @@ -31,7 +32,16 @@ NF.NEUTRAL_NAMES = { ["Booty Bay"] = true, ["Gadgetzan"] = true, ["Everlook"] = NF.NEUTRAL_MAPS = { [1434] = true, [1446] = true, [1452] = true } local db, settings -local ahOpen, house, scanToken, processing = false, nil, 0, false +local ahOpen, house, currentKind, location, classification = false, nil, nil, nil, "closed" +local session, scanToken, scanState = 0, 0, nil +local recordingReady = false +local lastScanStatus = "none" +local FACTION_NAMES = { + ["Stormwind City"] = "Alliance", ["Stormwind"] = "Alliance", ["Ironforge"] = "Alliance", + ["Darnassus"] = "Alliance", ["Exodar"] = "Alliance", ["The Exodar"] = "Alliance", + ["Orgrimmar"] = "Horde", ["Thunder Bluff"] = "Horde", ["Undercity"] = "Horde", + ["Silvermoon City"] = "Horde", +} local migrations = { [1] = function(old) return { houses = {}, overrides = {}, rates = {}, settings = old.settings or {} } end, @@ -59,13 +69,24 @@ end -- "Neutral" or the player's faction, plus how we decided. function NF.DetectKind() local key, zone, sub = NF.LocationKey() - local o = db.overrides[key] + local o = key ~= "/" and db.overrides[key] or nil if o == "neutral" then return NEUTRAL, "your /nflip here setting" end - if o == "faction" then return playerFaction(), "your /nflip here setting" end + if o == "faction" then + local faction = playerFaction() + if faction == "Alliance" or faction == "Horde" then return faction, "your /nflip here setting" end + return nil, "player faction unavailable" + end local mapID = C_Map and C_Map.GetBestMapForUnit and C.SafeNumber(C_Map.GetBestMapForUnit("player")) - if mapID and NF.NEUTRAL_MAPS[mapID] then return NEUTRAL, "map " .. mapID end - if NF.NEUTRAL_NAMES[sub] or NF.NEUTRAL_NAMES[zone] then return NEUTRAL, "zone " .. key end - return playerFaction(), "zone " .. key + local namedNeutral = NF.NEUTRAL_NAMES[sub] or NF.NEUTRAL_NAMES[zone] + local namedFaction = FACTION_NAMES[sub] or FACTION_NAMES[zone] + local mappedNeutral = mapID and NF.NEUTRAL_MAPS[mapID] + if namedFaction and (namedNeutral or mappedNeutral or namedFaction ~= playerFaction()) then + return nil, "conflicting location or player faction " .. key + end + if namedNeutral then return NEUTRAL, "zone " .. key end + if namedFaction then return namedFaction, "zone " .. key end + if mappedNeutral then return NEUTRAL, "map " .. mapID end + return nil, "unrecognized location " .. key end local function houseData(kind) @@ -139,47 +160,118 @@ end -- Capturing prices (only what the AH already shows you) ------------------------------------- -function NF.ProcessReplicate() - if processing or not house then return end +-- A session owns its market and each scan owns its callbacks. Never commit a partial snapshot. +local function sameHouse() + return ahOpen and house and NF.LocationKey() == location +end + +local function activeHouse() + return recordingReady and sameHouse() +end + +local function cancelScan(reason, notify) + if scanState and reason then + lastScanStatus = reason + if notify then Core.Print("full scan " .. reason .. "; no full scan recorded.") end + end + scanToken = scanToken + 1 + scanState = nil +end + +local function beginScan(phase) + cancelScan() + local state = { token = scanToken, session = session, house = house, kind = currentKind, + phase = phase, deadline = C.Clock() + SCAN_TIMEOUT } + scanState = state + C_Timer.After(SCAN_TIMEOUT, function() + if scanState == state and C.Clock() >= state.deadline then cancelScan("timed out (" .. state.phase .. ")", true) end + end) + return state +end + +local function validScan(state) + return scanState == state and state.token == scanToken and state.session == session + and activeHouse() and state.house == house and state.kind == currentKind +end + +local function positiveInteger(v) + return v and v > 0 and v < math.huge and v == math.floor(v) +end + +-- The shared helper conflates incomplete rows and valid bid-only auctions. Inspect +-- the documented fields here so a bid-only auction can be skipped safely. +local function scanRow(i) + local _, _, count, _, _, _, _, _, _, buyout, _, _, _, _, _, _, itemID, hasAll = + C_AuctionHouse.GetReplicateItemInfo(i) + if C.Safe(hasAll) ~= true then return nil, "incomplete row" end + itemID, count, buyout = C.SafeNumber(itemID), C.SafeNumber(count), C.SafeNumber(buyout) + if not (positiveInteger(itemID) and positiveInteger(count) and buyout and buyout >= 0 + and buyout < math.huge and buyout == math.floor(buyout)) then return nil, "invalid row" end + if buyout == 0 then return nil end -- complete bid-only auction, not a price observation + if buyout < count then return nil, "invalid unit price" end + return itemID, nil, count, math.floor(buyout / count) +end + +function NF.ProcessReplicate(state) + state = state or scanState + if not state or not validScan(state) or state.phase == "processing" then return end local n = C.AHNumReplicate() - if n == 0 then return end - processing = true - local h, acc, i, now = house, {}, 0, C.Now() + if not positiveInteger(n) then cancelScan("returned no usable rows", true); return end + state.phase = "processing" + local acc, i, accepted, now = {}, 0, 0, C.Now() local function finish() + if not validScan(state) then return end + if accepted == 0 then cancelScan("contained no usable buyouts", true); return end local k = 0 - for id, a in pairs(acc) do NF.Record(h, id, a.min, a.qmin, a.qty, now); k = k + 1 end - h.lastScan = now - processing = false - Core.Print(("saved %d prices for the %s auction house."):format(k, NF.KindLabel(NF.CurrentKind()))) + for id, a in pairs(acc) do + local previous = state.house.items[id] + if not previous or not C.SafeNumber(previous.t) or previous.t <= now then + NF.Record(state.house, id, a.min, a.qmin, a.qty, now) + end + k = k + 1 + end + state.house.lastScan = now + lastScanStatus = ("complete: %d rows, %d buyouts, %d items (%s)"):format(n, accepted, k, NF.KindLabel(state.kind)) + cancelScan() + Core.Print(("saved %d prices for the %s auction house."):format(k, NF.KindLabel(state.kind))) end - local function step() - if C.InCombat() then C_Timer.After(1, step) return end + local step, run + step = function() + if not validScan(state) then return end + if C.Clock() >= state.deadline then cancelScan("timed out (processing)", true); return end + if C.InCombat() then C_Timer.After(1, run); return end + if C.AHNumReplicate() ~= n then cancelScan("row count changed during processing", true); return end local stop = math.min(i + 500, n) while i < stop do - local itemID, _, count, buyout = C.AHReplicateRow(i) + local itemID, err, count, unit = scanRow(i) + if err then cancelScan(err .. " at " .. (i + 1), true); return end if itemID then - local unit = math.floor(buyout / count) local a = acc[itemID] if not a then a = { min = unit, qmin = 0, qty = 0 }; acc[itemID] = a end if unit < a.min then a.min, a.qmin = unit, 0 end if unit == a.min then a.qmin = a.qmin + count end a.qty = a.qty + count + accepted = accepted + 1 end i = i + 1 end - if i < n then C_Timer.After(0, function() if not Core.SafeCall("scan", step) then processing = false end end) else finish() end + if i < n then C_Timer.After(0, run) else finish() end end - if not Core.SafeCall("scan", step) then processing = false end + run = function() + if not validScan(state) then return end + if not Core.SafeCall("scan", step) and scanState == state then cancelScan("failed", true) end + end + run() end local function OnBrowse() - if not house then return end + if not activeHouse() then return end for _, r in ipairs(C.AHBrowse()) do NF.Record(house, r.itemID, r.price, nil, r.quantity) end end local function OnCommodity(itemID) itemID = C.SafeNumber(itemID) - if not (house and itemID) then return end + if not (activeHouse() and itemID) then return end local minP, qmin, total = nil, 0, 0 for _, r in ipairs(C.AHCommodity(itemID)) do if not minP or r.price < minP then minP, qmin = r.price, 0 end @@ -191,7 +283,6 @@ end -- The verdict ------------------------------------------------------------------------------- -local currentKind function NF.CurrentKind() return currentKind end function NF.KindLabel(kind) return kind == NEUTRAL and "neutral" or (kind or "?") end @@ -348,10 +439,18 @@ f:RegisterEvent("COMMODITY_SEARCH_RESULTS_UPDATED") local announced = {} function NF.OnAHShow() - ahOpen = true + cancelScan("cancelled: market changed", true) + session = session + 1 + local generation = session + ahOpen, recordingReady = true, false local kind, why = NF.DetectKind() + location, classification = NF.LocationKey(), why currentKind = kind - house = houseData(kind) + house = kind and houseData(kind) or nil + if not kind then + Core.Print("auction house market unknown (" .. why .. "); recording paused. Use /nflip here neutral|faction.") + return + end -- One line per auction house per session (not on every visit), unless there are flips to report. local _, n = NF.ListText() local key = NF.LocationKey() @@ -361,13 +460,25 @@ function NF.OnAHShow() n > 0 and (n .. " flips found: /nflip") or "Search or /nflip scan to record prices.")) end C_Timer.After(1, function() - if not ahOpen then return end - Core.SafeCall("deposit", function() - local rate = NF.ProbeDepositRate() + if not sameHouse() or session ~= generation or currentKind ~= kind then return end + local ok = Core.SafeCall("deposit", function() + local rate = not C.InCombat() and NF.ProbeDepositRate() or nil local warn = NF.CrossCheck(kind, rate) - if warn then Core.Print(warn) + if warn then + Core.Print(warn) + if not db.overrides[location] then + classification = "deposit mismatch; recording paused pending /nflip here override" + cancelScan("cancelled: deposit mismatch", true) + house = nil + return + end elseif rate then db.rates[kind == NEUTRAL and "neutral" or "faction"] = rate end + recordingReady = true end) + if not ok then + classification = "deposit check failed; recording paused" + cancelScan("cancelled: deposit check failed", true) + end end) end @@ -390,12 +501,20 @@ f:SetScript("OnEvent", function(_, event, arg1) if next(h.items) == nil then db.houses[key] = nil end end elseif event == "AUCTION_HOUSE_SHOW" then Core.SafeCall("show", NF.OnAHShow) - elseif event == "AUCTION_HOUSE_CLOSED" then ahOpen, house = false, nil + elseif event == "AUCTION_HOUSE_CLOSED" then + cancelScan("cancelled: auction house closed", true) + session = session + 1 + ahOpen, house, currentKind, location, classification = false, nil, nil, nil, "closed" + recordingReady = false elseif event == "REPLICATE_ITEM_LIST_UPDATE" then -- Also catches a full scan started by PriceLog or MatTooltip, so one scan feeds all three. - scanToken = scanToken + 1 - local token = scanToken - C_Timer.After(2, function() if token == scanToken then NF.ProcessReplicate() end end) + if not sameHouse() then return end + local state = beginScan("waiting for rows") + C_Timer.After(2, function() + if validScan(state) and not Core.SafeCall("scan start", NF.ProcessReplicate, state) and scanState == state then + cancelScan("failed to start", true) + end + end) elseif C.InCombat() then return elseif event == "AUCTION_HOUSE_BROWSE_RESULTS_UPDATED" then Core.SafeCall("browse", OnBrowse) elseif event == "COMMODITY_SEARCH_RESULTS_UPDATED" then Core.SafeCall("commodity", OnCommodity, arg1) @@ -450,17 +569,22 @@ SlashCmdList.KENNYNEUTRALFLIP = function(msg) Core.OpenView(function() Core.ShowText("List", "NeutralFlip: what to carry where (Ctrl+C copies)", (NF.ListText())) end) elseif cmd == "scan" then if not ahOpen then Core.Print("open an auction house first.") return end + if not activeHouse() then Core.Print("market not ready; wait for the deposit check or use /nflip here neutral|faction if unknown.") return end + if scanState then Core.Print("a full scan is already " .. scanState.phase .. ".") return end if C.InCombat() then Core.Print("not in combat.") return end local wait = SCAN_COOLDOWN - (C.Now() - (house.lastScan or 0)) if wait > 0 then Core.Print(("full scan available in %d min. Your searches are still recorded."):format(math.ceil(wait / 60))) return end if not C.AHReady() then Core.Print("AH busy, try again shortly.") return end local mine, by, left = ns.Hub.ClaimScan(ADDON) if not mine then Core.Print(("%s %s; %s %d min. %s"):format(L["a full scan was already started by"], by or "?", L["next one in"], math.ceil(left / 60), L["Its results are recorded here too."])) return end - C.AHReplicate() + beginScan("waiting for results") + local ok, requested = Core.SafeCall("replicate", C.AHReplicate) + if not ok or not requested then cancelScan("request failed", true); return end Core.Print("full scan requested. Stay at the AH until it reports.") elseif cmd == "here" then local key = NF.LocationKey() local v = rest:lower() + if key == "/" then Core.Print("location unavailable; cannot set a location override.") return end if v == "neutral" or v == "faction" then db.overrides[key] = v elseif v == "auto" then db.overrides[key] = nil else Core.Print("usage: /nflip here neutral|faction|auto") return end @@ -477,6 +601,8 @@ SlashCmdList.KENNYNEUTRALFLIP = function(msg) settings.tooltip = not settings.tooltip Core.Print("tooltip lines " .. (settings.tooltip and "on." or "off.")) elseif cmd == "status" then + Core.Print("market: " .. (ahOpen and NF.KindLabel(currentKind) or "closed") .. " (" .. classification .. "); recording " .. (activeHouse() and "enabled" or "paused") .. ".") + Core.Print("scan: " .. (scanState and scanState.phase or "idle") .. "; last result: " .. lastScanStatus .. ".") if next(db.houses) == nil then Core.Print("no prices recorded yet. Search or /nflip scan at an auction house.") end for key, h in pairs(db.houses) do local n = 0 @@ -486,19 +612,24 @@ SlashCmdList.KENNYNEUTRALFLIP = function(msg) Core.Print(("deposit/vendor rate learned: faction %s, neutral %s."):format( db.rates.faction and ("%.2f"):format(db.rates.faction) or "?", db.rates.neutral and ("%.2f"):format(db.rates.neutral) or "?")) elseif cmd == "report" then - local lines = { "schema " .. tostring(db.schema), "kind " .. tostring(currentKind), "location " .. NF.LocationKey() } + local lines = { "schema " .. tostring(db.schema), "kind " .. tostring(currentKind), "location " .. NF.LocationKey(), + "classification " .. classification, "AH open " .. tostring(ahOpen), + "recording " .. (activeHouse() and "enabled" or "paused"), + "scan " .. (scanState and scanState.phase or "idle"), "last scan result " .. lastScanStatus, + "deposit rates: faction " .. tostring(db.rates.faction) .. ", neutral " .. tostring(db.rates.neutral) } for key, h in pairs(db.houses) do local n = 0 for _ in pairs(h.items) do n = n + 1 end - lines[#lines + 1] = key .. " items " .. n + lines[#lines + 1] = key .. " items " .. n .. ", last full scan " .. tostring(h.lastScan or 0) end Core.ShowReport(lines) elseif cmd == "debug" then Core.PrintDebug() elseif cmd == "way" then NF.Waypoint(rest) elseif cmd == "wipe" then + cancelScan("cancelled: prices wiped", true) db.houses = {} - if ahOpen and currentKind then house = houseData(currentKind) end -- keep recording at the open AH + if sameHouse() and currentKind then house = houseData(currentKind) end -- keep recording at the open AH Core.Print("all recorded prices wiped.") else Core.Print(HELP) diff --git a/NeutralFlip.toc b/NeutralFlip.toc index 66d5244..19a6ead 100644 --- a/NeutralFlip.toc +++ b/NeutralFlip.toc @@ -2,7 +2,7 @@ ## Title: NeutralFlip ## Notes: Compares your faction AH with the neutral (Booty Bay / Gadgetzan / Everlook) AH and lists buys worth carrying across, after the 5%/15% cuts. You press every button. ## Author: Kenny -## Version: 0.4.0 +## Version: 0.4.1 ## SavedVariables: KennyNeutralFlipDB ## OptionalDeps: PriceLog, MatTooltip, LibDataBroker-1.1, TomTom ## X-License: MIT diff --git a/README.md b/README.md index 23f29c2..db09ddc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@
