From edd7666905d56961e4df5709d7ce5908674c15df Mon Sep 17 00:00:00 2001 From: haunter Date: Tue, 28 Apr 2026 23:17:36 +0300 Subject: [PATCH] fix 4 --- bot_modules/02_state.js | 42 ++++++++++------------------------------ templates/dashboard.html | 12 ++++++------ 2 files changed, 16 insertions(+), 38 deletions(-) diff --git a/bot_modules/02_state.js b/bot_modules/02_state.js index 00cf8f4..7d060f7 100644 --- a/bot_modules/02_state.js +++ b/bot_modules/02_state.js @@ -74,42 +74,20 @@ function gatherState() { } catch (e) { log(`storage capacity lookup failed: ${e}`); } // ---- Market / Trade capacity ----------------------------------------- - // Uses multiple fallback paths to robustly read capacity across game versions. + // Reads from the same live MM model the game UI uses (CreateOffers). + // This model is populated once the player opens their market tab. let marketCapacity = 0; let availableTradeCapacity = 0; try { - const marketLevel = buildings.market ?? 0; - const gd = uw.GameData?.buildingData?.market; - if (gd) { - // Try various key names the game uses across versions - marketCapacity = - gd?.capacity_per_level?.[marketLevel] || - gd?.max_capacity?.[marketLevel] || - gd?.merchant_count?.[marketLevel] || - gd?.merchants?.[marketLevel] || - 0; + const createOffers = uw.MM.getModels?.()?.CreateOffers?.[town.id]; + if (createOffers?.attributes) { + marketCapacity = createOffers.attributes.trade_capacity ?? 0; + availableTradeCapacity = createOffers.attributes.available_trade_capacity ?? marketCapacity; } - // Try the town object's built-in method first (most accurate) - if (town.getTradeCapacity) { - marketCapacity = town.getTradeCapacity() || marketCapacity; - } - // Available = total minus what is already used by pending trades - if (town.getAvailableTradeCapacity) { - availableTradeCapacity = town.getAvailableTradeCapacity() ?? marketCapacity; - } else { - // Fallback: read from trade collection - let usedCapacity = 0; - try { - const tradeColl = uw.MM.getOnlyCollectionByName('Trade'); - if (tradeColl) { - tradeColl.models.forEach(m => { - if (String(m.attributes?.town_id) === String(town.id)) { - usedCapacity += (m.attributes?.capacity || 0); - } - }); - } - } catch (e2) {} - availableTradeCapacity = Math.max(0, marketCapacity - usedCapacity); + // Fallback: use town method if model not yet loaded (market not yet opened) + if (!marketCapacity && town.getTradeCapacity) { + marketCapacity = town.getTradeCapacity() || 0; + availableTradeCapacity = town.getAvailableTradeCapacity?.() ?? marketCapacity; } } catch (e) { log(`market capacity lookup failed: ${e}`); } diff --git a/templates/dashboard.html b/templates/dashboard.html index b580a41..d704f77 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -258,11 +258,11 @@ - - - - - - + + + + + +