perf(kæde): priser slås kun op for kandidatmarker, fanen genbruger resultater under 30 s og kæden måles af profileren

This commit is contained in:
2026-09-30 14:27:08 +02:00
parent 9e34a16dfa
commit 83394aabfd
4 changed files with 41 additions and 18 deletions
+27 -5
View File
@@ -141,6 +141,27 @@ local function readPlaceables(data, farmId, byChain, nameOf)
table.sort(data.recipes, function(a, b) return a.key < b.key end)
end
-- Markedspris for en afgrøde, først når kæden spørger (kun kandidatmarkernes afgrøder, kun ved mangel);
-- hver afgrøde slås højst op én gang pr. læsning. Fejl -> 0 (logget én gang).
local function priceResolver(data)
local cache = {}
return function(fruitName)
if cache[fruitName] ~= nil then return cache[fruitName] end
local index = data.fruitFillIndex[fruitName]
local price = 0
if index ~= nil then
local ok, quote = pcall(ADSources.getMarketQuote, index)
if ok then
price = quote ~= nil and quote.price or 0
else
ADChainData.warnOnce("price:" .. tostring(fruitName), "pris for %s kunne ikke læses: %s", tostring(fruitName), tostring(quote))
end
end
cache[fruitName] = price
return price
end
end
local function readCrops(data, nameOf)
local fruits = g_fruitTypeManager ~= nil and g_fruitTypeManager.fruitTypes or {}
for _, fruit in ipairs(fruits) do
@@ -151,8 +172,7 @@ local function readCrops(data, nameOf)
if fruit.fillType.index ~= nil then nameOf(fruit.fillType.index) end
data.crops[fillName] = fruit.name
data.yields[fruit.name] = (tonumber(fruit.literPerSqm) or 0) * 10000
local quote = fruit.fillType.index ~= nil and ADSources.getMarketQuote(fruit.fillType.index) or nil
data.prices[fruit.name] = quote ~= nil and quote.price or 0
data.fruitFillIndex[fruit.name] = fruit.fillType.index
end)
if not ok then failed(data, "fruit:" .. tostring(fruit.name), "afgrøde %s kunne ikke læses: %s", tostring(fruit.name), tostring(err)) end
end
@@ -169,7 +189,8 @@ local function readStock(data, adEnv, farmId, nameOf)
if not ok then failed(data, "stock", "lager kunne ikke læses: %s", tostring(err)) end
end
-- Afgrøder gården selv bruger: input i en tændt produktion eller foder/halm i en stald.
-- Afgrøder gården selv bruger: input i en tændt produktion eller foder/halm i en stald (kun staldenes forbrug
-- læses: feedOnly springer fabrikkernes rater over — dem dækker opskrifterne allerede).
local function readUsedFruits(data, adEnv, farmId, nameOf)
for _, recipe in ipairs(data.recipes) do
if recipe.enabled then
@@ -179,7 +200,7 @@ local function readUsedFruits(data, adEnv, farmId, nameOf)
end
end
local ok, err = pcall(function()
for index, rate in pairs(ADSources.getFarmRates(adEnv, farmId) or {}) do
for index, rate in pairs(ADSources.getFarmRates(adEnv, farmId, true) or {}) do
local fruit = data.crops[nameOf(index) or ""]
if fruit ~= nil and (rate.consumption or 0) > 0 then data.usedFruits[fruit] = true end
end
@@ -216,9 +237,10 @@ end
-- data.partial = true når noget ikke kunne læses i dette kald; data.fieldsIncomplete = true mens markscanningen
-- ikke har nået alle marker.
function ADChainData.collect(adEnv, farmId, byChain)
local data = {recipes = {}, husbandry = {}, crops = {}, stock = {}, fields = {}, yields = {}, prices = {},
local data = {recipes = {}, husbandry = {}, crops = {}, stock = {}, fields = {}, yields = {}, fruitFillIndex = {},
usedFruits = {}, points = {}, fillTitles = {}, fillIndex = {}}
local nameOf = nameResolver(data)
data.priceOf = priceResolver(data)
readPlaceables(data, farmId, byChain, nameOf)
readCrops(data, nameOf)
readStock(data, adEnv, farmId, nameOf)