feat: én traktor, flere vogne i samme flow — skifter vogn mellem ture; kæden laver kombinerede flows (1.62.337)

Brugerønske 18/10 18:36: færre traktorer. spareWagons pr. traktor, ADWagonSwap vurderer vognene før hver tur og bruger flowets vognbytte.
This commit is contained in:
2026-10-18 18:56:23 +02:00
parent 3924516cbe
commit fae9761f1a
17 changed files with 536 additions and 34 deletions
+60
View File
@@ -89,6 +89,62 @@ function ADRuns.parseWagons(text)
return wagons
end
-- Ekstra vogne pr. traktor (én traktor, flere vogntyper i samme flow — brugerønske 18/10 18:36): run.wagons er den
-- AKTIVE vogn, run.spareWagons = {[traktorId] = {vognId, ...}} de andre, traktoren kan skifte til mellem ture.
function ADRuns.copySpares(spares)
local copy = {}
for tractorId, list in pairs(spares or {}) do
local ids = {}
for _, id in ipairs(list) do table.insert(ids, id) end
copy[tractorId] = ids
end
return copy
end
-- "t1=w2,w3;t2=w4" (traktorer sorteret; tomme lister udelades)
function ADRuns.serializeSpares(spares)
local keys = {}
for tractorId, list in pairs(spares or {}) do if #list > 0 then table.insert(keys, tractorId) end end
table.sort(keys)
local parts = {}
for _, tractorId in ipairs(keys) do table.insert(parts, tractorId .. "=" .. table.concat(spares[tractorId], ",")) end
return table.concat(parts, ";")
end
function ADRuns.parseSpares(text)
local spares = {}
for tractorId, list in string.gmatch(tostring(text or ""), "([^=;]+)=([^;]+)") do
local ids = {}
for id in string.gmatch(list, "[^,]+") do table.insert(ids, id) end
if #ids > 0 then spares[tractorId] = ids end
end
return spares
end
-- Gør newId til traktorens aktive vogn: den hidtidige aktive bliver ekstra, newId fjernes fra de ekstra.
-- -> nye tabeller (wagons, spares)
function ADRuns.swapActive(wagons, spares, tractorId, newId)
local newWagons = ADRuns.copyWagons(wagons)
local newSpares = ADRuns.copySpares(spares)
local old = newWagons[tractorId]
local list = {}
for _, id in ipairs(newSpares[tractorId] or {}) do if id ~= newId then table.insert(list, id) end end
if old ~= nil and old ~= newId then table.insert(list, old) end
newWagons[tractorId] = newId
newSpares[tractorId] = list
return newWagons, newSpares
end
-- Alle flowets vogne (aktive og ekstra) som liste.
function ADRuns.wagonIdsOf(run)
local ids = {}
for _, id in pairs((run or {}).wagons or {}) do table.insert(ids, id) end
for _, list in pairs((run or {}).spareWagons or {}) do
for _, id in ipairs(list) do table.insert(ids, id) end
end
return ids
end
local function copyRun(run)
return {
id = run.id,
@@ -119,6 +175,7 @@ local function copyRun(run)
wagons = ADRuns.copyWagons(run.wagons),
chain = run.chain or "",
chainIdle = run.chainIdle == true or nil, -- kæden oprettede flowet uden traktor (bemandes senere)
spareWagons = ADRuns.copySpares(run.spareWagons),
}
end
@@ -382,6 +439,7 @@ function ADRuns.unassignVehicle(store, vehicleId)
end
-- traktoren forlader flowet: dens huskede vogn hører ikke længere til flowet
if run.wagons ~= nil then run.wagons[vehicleId] = nil end
if run.spareWagons ~= nil then run.spareWagons[vehicleId] = nil end
end
return new
end
@@ -507,6 +565,7 @@ function ADRuns.toRecords(store)
wagons = ADRuns.serializeWagons(run.wagons),
chain = run.chain or "",
chainIdle = run.chainIdle and 1 or 0,
spareWagons = ADRuns.serializeSpares(run.spareWagons),
}
end
return records
@@ -567,6 +626,7 @@ function ADRuns.fromRecords(records)
wagons = ADRuns.parseWagons(record.wagons),
chain = record.chain or "",
chainIdle = ADRuns.parseChainIdle(record),
spareWagons = ADRuns.parseSpares(record.spareWagons),
})
store.nextId = math.max(store.nextId, id + 1)
end