From 0aaa49f33bc33aa14e981e560a07cf58e35ce54a Mon Sep 17 00:00:00 2001 From: DuvelCorp Date: Mon, 14 Sep 2026 22:38:06 +0200 Subject: [PATCH] Dedup trainer craft scans to prevent open freeze --- api/core-scan-beasttraining.lua | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/api/core-scan-beasttraining.lua b/api/core-scan-beasttraining.lua index a497e47..6fb0a53 100644 --- a/api/core-scan-beasttraining.lua +++ b/api/core-scan-beasttraining.lua @@ -705,6 +705,24 @@ MTH_PT_IsPetBookVisible = function() return false end +-- Cheap content signature of the trainer's craft rows, so repeated reads of the +-- same list (multiple CRAFT_UPDATE / open events) don't each pay for a full, +-- datastore-wide re-scan (that pile-up was freezing the client). +local MTH_PT_LastScanSignature = nil +local function MTH_PT_ComputeCraftSignature() + local getNumCrafts = MTH_PT_GetGlobal("GetNumCrafts") + local getCraftInfo = MTH_PT_GetGlobal("GetCraftInfo") + if type(getNumCrafts) ~= "function" or type(getCraftInfo) ~= "function" then return nil end + local total = tonumber(getNumCrafts()) or 0 + if total <= 0 then return nil end + local parts = { tostring(total) } + for i = 1, total do + local name, sub, ctype = getCraftInfo(i) + table.insert(parts, tostring(name or "") .. "~" .. tostring(sub or "") .. "~" .. tostring(ctype or "")) + end + return table.concat(parts, "|") +end + local function MTH_PT_RunDoubleScan(triggerBase) local triggerText = tostring(triggerBase or "") local feedbackOnOpen = ( @@ -728,9 +746,15 @@ local function MTH_PT_RunDoubleScan(triggerBase) return false end + local signature = MTH_PT_ComputeCraftSignature() + if signature and signature == MTH_PT_LastScanSignature then + if MTH_PT_DebugLog then MTH_PT_DebugLog("scan skipped (unchanged signature): trigger=" .. triggerText) end + return true + end local t0 = time() local ok, added, baselines, ranks = MTH_PT_ApplyScan(triggerText .. ":single") local t1 = time() + if ok and signature then MTH_PT_LastScanSignature = signature end if MTH_PT_DebugLog then MTH_PT_DebugLog("scan complete: trigger=" .. tostring(triggerBase or "") .. " ok=" .. tostring(ok and true or false) .. " dt=" .. tostring((t1 or 0) - (t0 or 0)) .. "s") @@ -885,6 +909,10 @@ function MTH_PT_InitService() MTH_PT_RunDoubleScanThrottled("beast-training-rows-present", 8) end + if (not hasRows) and MTH_PT_TrainingRowsWerePresent then + -- Trainer closed: allow one fresh scan the next time it opens. + MTH_PT_LastScanSignature = nil + end MTH_PT_TrainingRowsWerePresent = hasRows MTH_PT_LastTrainingRowCount = rowCount