feat: kæden planlægger hø og ensilage — græs med produkt, mangel/afvisning pr. vare (v1.62.29)

This commit is contained in:
2026-10-05 14:01:11 +02:00
parent 59022e99d9
commit 9971cdde8d
12 changed files with 144 additions and 61 deletions
+30 -22
View File
@@ -26,7 +26,7 @@ ADChainView.TEXT_KEYS = {"spu_chain_optIn", "spu_chain_optOut", "spu_chain_notCh
"spu_chain_pause", "spu_chain_resume", "spu_chain_scanning", "spu_chain_rootOk", "spu_chain_rootError", "spu_chain_rootWaiting",
"spu_chain_rootPaused", "spu_chain_rootSummary", "spu_chain_free", "spu_chain_switches", "spu_chain_gone", "spu_chain_col_item",
"spu_chain_col_source", "spu_chain_col_perMonth", "spu_chain_col_status", "spu_chain_col_stock",
"spu_chain_flowAuto", "spu_chain_problem_buy", "spu_chain_problem_bottleneck", "spu_chain_problem_off", "spu_chain_problem_cycle", "spu_chain_problem_barn"}
"spu_chain_flowAuto", "spu_chain_problem_buy", "spu_chain_problem_bottleneck", "spu_chain_problem_off", "spu_chain_problem_cycle", "spu_chain_problem_barn", "spu_field_plan_hay", "spu_field_plan_silage"}
local FLOW_KEYS = {ok = "spu_chain_flowOk", missing = "spu_chain_flowNone", stopped = "spu_chain_flowStopped", noVehicle = "spu_chain_flowMissing",
pallet = "spu_chain_flowPallet", auto = "spu_chain_flowAuto"}
@@ -86,12 +86,15 @@ local function edgeKey(node)
return tostring(node.consumer) .. "|" .. tostring(node.fillType)
end
-- Mangler pr. nøgle: afgrøden, eller varen for en markvare med produkt (hø: DRYGRASS_WINDROW)
local function shortageByFruit(last)
local result = {}
for _, shortage in ipairs(last.shortages or {}) do result[shortage.fruit] = shortage end
for _, shortage in ipairs(last.shortages or {}) do result[shortage.key or shortage.fruit] = shortage end
return result
end
local function shortKey(node) return node.shortKey or node.fruit end
local function stockOf(last, fruit)
local stock = last.data ~= nil and last.data.stock or nil
return stock ~= nil and fruit ~= nil and stock[fruit] or nil
@@ -99,15 +102,15 @@ end
-- Lager for en mark-node: afgrødens lager, eller varens eget lager for en markvare uden afgrøde (hø, hakkelse …).
local function fieldStockOf(last, node)
if node.fruit ~= nil then return stockOf(last, node.fruit) end
if node.fruit ~= nil and node.product == nil then return stockOf(last, node.fruit) end
local byFill = last.data ~= nil and last.data.stockByFill or nil
return byFill ~= nil and byFill[node.fillType] or nil
end
-- Mark-afgrøde uden mangel men med lager under TIGHT_MONTHS måneders behov.
local function isTight(node, last, short)
if node.source ~= "field" or node.fruit == nil or short[node.fruit] ~= nil then return false end
return (stockOf(last, node.fruit) or 0) < ADChainView.TIGHT_MONTHS * (node.perMonth or 0)
if node.source ~= "field" or node.fruit == nil or short[shortKey(node)] ~= nil then return false end
return (fieldStockOf(last, node) or 0) < ADChainView.TIGHT_MONTHS * (node.perMonth or 0)
end
local function eachNode(nodes, fn, depth)
@@ -126,7 +129,7 @@ function ADChainView.rootLevel(root, last)
local short, flowStatus = shortageByFruit(last), last.flowStatus or {}
local hasShortage, hasWaiting = false, false
eachNode(root.children, function(node)
if node.fruit ~= nil and short[node.fruit] ~= nil then hasShortage = true end
if node.fruit ~= nil and short[shortKey(node)] ~= nil then hasShortage = true end
if (node.shortPerMonth or 0) > 0 then hasShortage = true end
local status = node.consumer ~= nil and flowStatus[edgeKey(node)] or nil
if (status ~= nil and status ~= "ok" and status ~= "auto") or isTight(node, last, short) then hasWaiting = true end
@@ -197,7 +200,9 @@ local function sourceText(node, last, textOf)
-- markvare uden egen afgrøde (hø, hakkelse …): ingen afgrødeplan, bare "fra marker"
if node.fruit == nil then return textOf("spu_chain_fromFields") end
for _, field in ipairs(last.fields or {}) do
if plannedFruit(field) == node.fruit then table.insert(names, "F" .. tostring(field.id)) end
if plannedFruit(field) == node.fruit and (node.product == nil or ADCropPlan.fieldProduct(field) == node.product) then
table.insert(names, "F" .. tostring(field.id))
end
end
if #names == 0 then return textOf("spu_chain_noField") end
elseif node.source == "buy" then
@@ -215,7 +220,7 @@ local function nodeStatus(node, last, fmt, textOf, short)
local key = node.source == "production" and "spu_chain_bottleneck" or "spu_chain_buy"
return string.format(textOf(key), fmt(node.shortPerMonth)), "error"
end
local shortage = node.fruit ~= nil and short[node.fruit] or nil
local shortage = node.fruit ~= nil and short[shortKey(node)] or nil
if shortage ~= nil then return string.format(textOf("spu_chain_fieldShort"), haText(shortage.missingHa)), "error" end
local status = node.consumer ~= nil and (last.flowStatus or {})[edgeKey(node)] or nil
-- udgangen står på Fordel: spillet leverer selv
@@ -242,7 +247,9 @@ end
local function suggestRow(action, indent, titleOf, textOf)
local pad = string.rep(ADChainView.INDENT, indent)
local from = (action.from ~= nil and action.from ~= "") and titleOf(action.from) or "-"
local cells = {pad .. "F" .. tostring(action.fieldId) .. " " .. from, "→ " .. titleOf(action.to), haText(action.hectares) .. " ha"}
local to = titleOf(action.to)
if action.product ~= nil then to = to .. " (" .. textOf("spu_field_plan_" .. tostring(action.product)) .. ")" end
local cells = {pad .. "F" .. tostring(action.fieldId) .. " " .. from, "→ " .. to, haText(action.hectares) .. " ha"}
if action.mode == "suggest" then
cells[4], cells[5] = textOf("spu_chain_approve"), textOf("spu_chain_dismiss")
return makeRow("suggest", indent, "waiting", cells,
@@ -252,22 +259,23 @@ local function suggestRow(action, indent, titleOf, textOf)
return makeRow("auto", indent, "running", cells)
end
-- Handlinger for de afgrøder (i rækkefølge) der hentes fra mark under roden.
-- Mark-noderne (i rækkefølge, én pr. mangel-nøgle) under roden: afgrøden, eller hø/ensilage hver for sig.
local function rootFruits(root)
local fruits, seen = {}, {}
local nodes, seen = {}, {}
eachNode(root.children, function(node)
if node.source == "field" and node.fruit ~= nil and not seen[node.fruit] then
seen[node.fruit] = true
table.insert(fruits, node.fruit)
if node.source == "field" and node.fruit ~= nil and not seen[shortKey(node)] then
seen[shortKey(node)] = true
table.insert(nodes, node)
end
end)
return fruits
return nodes
end
local function actionsFor(last, fruit, modes)
-- Handlinger for en mark-node: samme afgrøde og samme produkt (hø-forslag hører ikke under en ensilage-node)
local function actionsFor(last, node, modes)
local list = {}
for _, action in ipairs(last.actions or {}) do
if action.to == fruit and modes[action.mode] then table.insert(list, action) end
if action.to == node.fruit and action.product == node.product and modes[action.mode] then table.insert(list, action) end
end
return list
end
@@ -309,8 +317,8 @@ local function rootRows(rows, root, last, ctx, isExpanded)
local level = ADChainView.rootLevel(root, last)
local paused = (last.paused or {})[root.key] == true
local suggestions = {}
for _, fruit in ipairs(rootFruits(root)) do
for _, action in ipairs(actionsFor(last, fruit, {suggest = true})) do table.insert(suggestions, action) end
for _, fieldNode in ipairs(rootFruits(root)) do
for _, action in ipairs(actionsFor(last, fieldNode, {suggest = true})) do table.insert(suggestions, action) end
end
local status = ctx.textOf(ROOT_STATUS[level])
if #suggestions > 0 then status = string.format(ctx.textOf("spu_chain_rootSummary"), status, #suggestions) end
@@ -349,9 +357,9 @@ local function rootRows(rows, root, last, ctx, isExpanded)
if node.consumer ~= nil then row.openConsumer, row.openFillType = node.consumer, node.fillType end
table.insert(rows, row)
problemRows(node.fillType, depth + 1)
if node.source == "field" and node.fruit ~= nil and not shown[node.fruit] then
shown[node.fruit] = true
for _, action in ipairs(actionsFor(last, node.fruit, {auto = true})) do
if node.source == "field" and node.fruit ~= nil and not shown[shortKey(node)] then
shown[shortKey(node)] = true
for _, action in ipairs(actionsFor(last, node, {auto = true})) do
table.insert(rows, suggestRow(action, depth + 1, ctx.titleOf, ctx.textOf))
end
end