190 lines
7.9 KiB
Lua
190 lines
7.9 KiB
Lua
-- AD Status View
|
|
-- Ren logik for statusvinduet på skærmen (slås til/fra med en tast, se adStatusHud.lua): øjebliksbilledet
|
|
-- -> færdige linjer {text, level, header}. Sektioner i fast rækkefølge Mangler/Flows/Marker/Kæder/Advarsler, kun med
|
|
-- indhold (Mangler øverst, så den aldrig skæres væk); højst maxLines linjer (resten "+k mere"); hver tekst højst MAX_CHARS tegn (UTF-8-sikkert, "..").
|
|
--
|
|
-- snapshot = {
|
|
-- missing = {{text, level}}, -- standard rød; redskab der slet ikke findes på gården (ikke optaget)
|
|
-- flows = {{vehicle, action, fillPercent (0-100|nil), level}},
|
|
-- fields = {{fieldId, work, vehicle|nil, progress (0-1|nil), level}},
|
|
-- chains = {{text, level}}, -- standard rød
|
|
-- warnings = {{text, level}}, -- standard gul
|
|
-- }
|
|
-- level = "running"|"waiting"|"error"|"stopped" (farver: ADFlowView.LEVEL_COLORS); overskrifter har level "header".
|
|
|
|
ADStatusView = {}
|
|
|
|
ADStatusView.MAX_LINES = 18
|
|
ADStatusView.MAX_CHARS = 48
|
|
ADStatusView.VEHICLE_CHARS = 18 -- traktornavnet må ikke æde hele linjen
|
|
ADStatusView.MIN_FLEX_CHARS = 6
|
|
ADStatusView.SEPARATOR = " · "
|
|
ADStatusView.ELLIPSIS = ".."
|
|
|
|
local LEVELS = {running = true, waiting = true, error = true, stopped = true}
|
|
|
|
local SECTIONS = {
|
|
{key = "missing", header = "spu_status_missing", defaultLevel = "error"},
|
|
{key = "flows", header = "spu_status_flows", defaultLevel = "running"},
|
|
{key = "fields", header = "spu_status_fields", defaultLevel = "running"},
|
|
{key = "chains", header = "spu_status_chains", defaultLevel = "error"},
|
|
{key = "warnings", header = "spu_status_warnings", defaultLevel = "waiting"},
|
|
}
|
|
|
|
-- ---- UTF-8-sikker længde/afkortning (æøå er flere bytes) ----
|
|
|
|
function ADStatusView.textLength(text)
|
|
local count = 0
|
|
for _ in tostring(text):gmatch("[^\128-\191]") do count = count + 1 end
|
|
return count
|
|
end
|
|
|
|
local function utf8Prefix(text, chars)
|
|
local result, count = {}, 0
|
|
for char in text:gmatch("[^\128-\191][\128-\191]*") do
|
|
if count >= chars then break end
|
|
table.insert(result, char)
|
|
count = count + 1
|
|
end
|
|
return table.concat(result)
|
|
end
|
|
|
|
function ADStatusView.fit(text, chars)
|
|
text = tostring(text or "")
|
|
if ADStatusView.textLength(text) <= chars then return text end
|
|
local ellipsis = ADStatusView.ELLIPSIS
|
|
return utf8Prefix(text, math.max(0, chars - ADStatusView.textLength(ellipsis))) .. ellipsis
|
|
end
|
|
|
|
-- Dele samlet med " · "; delen på flexIndex får den plads der er tilbage, så fx fyldprocenten bevares.
|
|
local function joinFitted(parts, flexIndex)
|
|
local sep = ADStatusView.SEPARATOR
|
|
local fixed = ADStatusView.textLength(sep) * (#parts - 1)
|
|
for index, part in ipairs(parts) do
|
|
if index ~= flexIndex then fixed = fixed + ADStatusView.textLength(part) end
|
|
end
|
|
local budget = math.max(ADStatusView.MIN_FLEX_CHARS, ADStatusView.MAX_CHARS - fixed)
|
|
local fitted = {}
|
|
for index, part in ipairs(parts) do
|
|
fitted[index] = index == flexIndex and ADStatusView.fit(part, budget) or part
|
|
end
|
|
return ADStatusView.fit(table.concat(fitted, sep), ADStatusView.MAX_CHARS)
|
|
end
|
|
|
|
local function percentText(value)
|
|
return string.format("%d %%", math.floor(value + 0.5))
|
|
end
|
|
|
|
-- ---- manglende redskaber ----
|
|
|
|
-- Markårsager der betyder "findes ikke på gården" (noRig) eller "ingen af dem kan opgaven" (cannotDo).
|
|
-- allBusy/noPoolTractor/poolBusy er kø og vises ikke her.
|
|
ADStatusView.MISSING_KINDS = {noRig = 1, cannotDo = 2}
|
|
|
|
-- {{fieldId, role, kind}} -> {{kind, role, fieldIds}}: én gruppe pr. art+rolle, noRig før cannotDo, roller
|
|
-- alfabetisk, marker sorteret uden dubletter. Poster uden rolle eller med anden art springes over.
|
|
function ADStatusView.groupMissing(entries)
|
|
local byKey, groups = {}, {}
|
|
for _, entry in ipairs(entries or {}) do
|
|
if entry.role ~= nil and ADStatusView.MISSING_KINDS[entry.kind] ~= nil then
|
|
local key = entry.kind .. ":" .. tostring(entry.role)
|
|
local group = byKey[key]
|
|
if group == nil then
|
|
group = {kind = entry.kind, role = entry.role, fieldIds = {}, seen = {}}
|
|
byKey[key] = group
|
|
table.insert(groups, group)
|
|
end
|
|
if not group.seen[entry.fieldId] then
|
|
group.seen[entry.fieldId] = true
|
|
table.insert(group.fieldIds, entry.fieldId)
|
|
end
|
|
end
|
|
end
|
|
for _, group in ipairs(groups) do
|
|
group.seen = nil
|
|
table.sort(group.fieldIds, function(a, b) return (tonumber(a) or 0) < (tonumber(b) or 0) end)
|
|
end
|
|
table.sort(groups, function(a, b)
|
|
local ka, kb = ADStatusView.MISSING_KINDS[a.kind], ADStatusView.MISSING_KINDS[b.kind]
|
|
if ka ~= kb then return ka < kb end
|
|
return tostring(a.role) < tostring(b.role)
|
|
end)
|
|
return groups
|
|
end
|
|
|
|
-- {7, 12} -> "F7, F12"
|
|
function ADStatusView.fieldList(fieldIds)
|
|
local parts = {}
|
|
for _, id in ipairs(fieldIds or {}) do table.insert(parts, "F" .. tostring(id)) end
|
|
return table.concat(parts, ", ")
|
|
end
|
|
|
|
-- ---- linjer pr. sektion ----
|
|
|
|
local function flowText(entry)
|
|
local parts = {ADStatusView.fit(entry.vehicle or "?", ADStatusView.VEHICLE_CHARS), tostring(entry.action or "")}
|
|
if type(entry.fillPercent) == "number" then table.insert(parts, percentText(entry.fillPercent)) end
|
|
return joinFitted(parts, 2)
|
|
end
|
|
|
|
local function fieldText(entry)
|
|
local parts = {string.format("F%s %s", tostring(entry.fieldId or "?"), tostring(entry.work or ""))}
|
|
if entry.vehicle ~= nil and entry.vehicle ~= "" then
|
|
table.insert(parts, ADStatusView.fit(entry.vehicle, ADStatusView.VEHICLE_CHARS))
|
|
end
|
|
if type(entry.progress) == "number" then table.insert(parts, percentText(entry.progress * 100)) end
|
|
return joinFitted(parts, 1)
|
|
end
|
|
|
|
local function plainText(entry)
|
|
return ADStatusView.fit(entry.text or "", ADStatusView.MAX_CHARS)
|
|
end
|
|
|
|
local TEXT_OF_SECTION = {missing = plainText, flows = flowText, fields = fieldText, chains = plainText, warnings = plainText}
|
|
|
|
local function levelOf(entry, default)
|
|
return LEVELS[entry.level] and entry.level or default
|
|
end
|
|
|
|
-- Alle linjer uden loft.
|
|
local function allLines(snapshot, textOf)
|
|
local lines = {}
|
|
for _, section in ipairs(SECTIONS) do
|
|
local entries = snapshot[section.key] or {}
|
|
if #entries > 0 then
|
|
table.insert(lines, {text = ADStatusView.fit(textOf(section.header), ADStatusView.MAX_CHARS), level = "header", header = true})
|
|
for _, entry in ipairs(entries) do
|
|
table.insert(lines, {text = TEXT_OF_SECTION[section.key](entry), level = levelOf(entry, section.defaultLevel), header = false})
|
|
end
|
|
end
|
|
end
|
|
return lines
|
|
end
|
|
|
|
-- Afkort til maxLines: sidste plads bliver "+k mere" (k = skjulte indholdslinjer); en overskrift uden
|
|
-- indhold lige før afkortningen droppes.
|
|
local function capLines(lines, maxLines, textOf)
|
|
if #lines <= maxLines then return lines end
|
|
local kept = {}
|
|
for index = 1, math.max(0, maxLines - 1) do kept[index] = lines[index] end
|
|
while #kept > 0 and kept[#kept].header do table.remove(kept) end
|
|
local hidden = 0
|
|
for index = #kept + 1, #lines do
|
|
if not lines[index].header then hidden = hidden + 1 end
|
|
end
|
|
local ok, text = pcall(string.format, textOf("spu_status_more"), hidden)
|
|
table.insert(kept, {text = ADStatusView.fit(ok and text or ("+" .. hidden), ADStatusView.MAX_CHARS), level = "stopped", header = false})
|
|
return kept
|
|
end
|
|
|
|
function ADStatusView.lines(snapshot, textOf, maxLines)
|
|
snapshot = snapshot or {}
|
|
textOf = textOf or function(key) return key end
|
|
maxLines = math.max(1, math.floor(maxLines or ADStatusView.MAX_LINES))
|
|
local lines = allLines(snapshot, textOf)
|
|
if #lines == 0 then
|
|
return {{text = ADStatusView.fit(textOf("spu_status_idle"), ADStatusView.MAX_CHARS), level = "stopped", header = false}}
|
|
end
|
|
return capLines(lines, maxLines, textOf)
|
|
end
|