This commit is contained in:
2026-04-28 23:08:35 +03:00
parent 53f1176ef8
commit 76ab83620b
4 changed files with 48 additions and 15 deletions

View File

@@ -74,16 +74,43 @@ function gatherState() {
} catch (e) { log(`storage capacity lookup failed: ${e}`); }
// ---- Market / Trade capacity -----------------------------------------
// Uses multiple fallback paths to robustly read capacity across game versions.
let marketCapacity = 0;
let availableTradeCapacity = 0;
try {
const marketLevel = buildings.market ?? 0;
const gd = uw.GameData?.buildingData?.market;
marketCapacity = gd?.capacity_per_level?.[marketLevel] || 0;
if (buildings.trade_office && buildings.trade_office > 0) {
marketCapacity += (uw.GameData?.buildingData?.trade_office?.capacity_extra_per_level || 500) * marketLevel;
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;
}
// 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);
}
availableTradeCapacity = town.getAvailableTradeCapacity?.() ?? marketCapacity;
} catch (e) { log(`market capacity lookup failed: ${e}`); }
// ---- Coordinates & sea zone -----------------------------------------