95 lines
3.7 KiB
Lua
95 lines
3.7 KiB
Lua
-- AD Field Silo Config
|
|
-- Marksilo-flowets opsætning som ren model (ny tabel ud, input muteres ikke) + flade records til XML-laget.
|
|
|
|
ADFieldSiloConfig = {}
|
|
|
|
ADFieldSiloConfig.MAX_VEHICLES = 10
|
|
ADFieldSiloConfig.DEFAULT_MAX_VEHICLES = 2
|
|
ADFieldSiloConfig.MIN_LITERS_CHOICES = {5000, 10000, 25000, 50000}
|
|
ADFieldSiloConfig.DEFAULT_MIN_LITERS = 10000
|
|
-- foderreserve: staldenes forbrug i så mange døgn bliver på gården, før overskud køres til fabrik
|
|
ADFieldSiloConfig.RESERVE_DAYS_CHOICES = {3, 7, 14, 30}
|
|
ADFieldSiloConfig.DEFAULT_RESERVE_DAYS = 7
|
|
|
|
local function copy(cfg)
|
|
local ids = {}
|
|
for index, id in ipairs(cfg.storeIds or {}) do ids[index] = id end
|
|
return {enabled = cfg.enabled == true, storeIds = ids, maxVehicles = cfg.maxVehicles, minLiters = cfg.minLiters,
|
|
toProduction = cfg.toProduction == true, reserveDays = cfg.reserveDays}
|
|
end
|
|
|
|
local function clampVehicles(value)
|
|
value = tonumber(value) or ADFieldSiloConfig.DEFAULT_MAX_VEHICLES
|
|
return math.max(1, math.min(ADFieldSiloConfig.MAX_VEHICLES, math.floor(value)))
|
|
end
|
|
|
|
local function snap(value, choices, default)
|
|
value = tonumber(value)
|
|
if value == nil then return default end
|
|
local best, bestDiff = nil, nil
|
|
for _, choice in ipairs(choices) do
|
|
local diff = math.abs(choice - value)
|
|
if bestDiff == nil or diff < bestDiff then best, bestDiff = choice, diff end
|
|
end
|
|
return best
|
|
end
|
|
|
|
local function snapLiters(value)
|
|
return snap(value, ADFieldSiloConfig.MIN_LITERS_CHOICES, ADFieldSiloConfig.DEFAULT_MIN_LITERS)
|
|
end
|
|
|
|
local function snapDays(value)
|
|
return snap(value, ADFieldSiloConfig.RESERVE_DAYS_CHOICES, ADFieldSiloConfig.DEFAULT_RESERVE_DAYS)
|
|
end
|
|
|
|
function ADFieldSiloConfig.new()
|
|
return {enabled = false, storeIds = {}, maxVehicles = ADFieldSiloConfig.DEFAULT_MAX_VEHICLES, minLiters = ADFieldSiloConfig.DEFAULT_MIN_LITERS,
|
|
toProduction = false, reserveDays = ADFieldSiloConfig.DEFAULT_RESERVE_DAYS}
|
|
end
|
|
|
|
function ADFieldSiloConfig.with(cfg, changes)
|
|
local new = copy(cfg)
|
|
if changes.enabled ~= nil then new.enabled = changes.enabled == true end
|
|
if changes.maxVehicles ~= nil then new.maxVehicles = clampVehicles(changes.maxVehicles) end
|
|
if changes.minLiters ~= nil then new.minLiters = snapLiters(changes.minLiters) end
|
|
if changes.toProduction ~= nil then new.toProduction = changes.toProduction == true end
|
|
if changes.reserveDays ~= nil then new.reserveDays = snapDays(changes.reserveDays) end
|
|
return new
|
|
end
|
|
|
|
function ADFieldSiloConfig.hasStore(cfg, id)
|
|
for _, storeId in ipairs(cfg.storeIds or {}) do
|
|
if storeId == id then return true end
|
|
end
|
|
return false
|
|
end
|
|
|
|
function ADFieldSiloConfig.toggleStore(cfg, id)
|
|
local new = copy(cfg)
|
|
local kept, found = {}, false
|
|
for _, storeId in ipairs(new.storeIds) do
|
|
if storeId == id then found = true else table.insert(kept, storeId) end
|
|
end
|
|
if not found then table.insert(kept, id) end
|
|
new.storeIds = kept
|
|
return new
|
|
end
|
|
|
|
function ADFieldSiloConfig.toRecord(cfg)
|
|
return {enabled = cfg.enabled and 1 or 0, stores = table.concat(cfg.storeIds or {}, " "),
|
|
maxVehicles = cfg.maxVehicles, minLiters = cfg.minLiters, toProduction = cfg.toProduction and 1 or 0,
|
|
reserveDays = cfg.reserveDays}
|
|
end
|
|
|
|
function ADFieldSiloConfig.fromRecord(record)
|
|
local cfg = ADFieldSiloConfig.new()
|
|
if record == nil then return cfg end
|
|
for token in string.gmatch(tostring(record.stores or ""), "%S+") do table.insert(cfg.storeIds, token) end
|
|
cfg.enabled = tonumber(record.enabled) == 1
|
|
cfg.maxVehicles = clampVehicles(record.maxVehicles)
|
|
cfg.minLiters = snapLiters(record.minLiters)
|
|
cfg.toProduction = tonumber(record.toProduction) == 1
|
|
cfg.reserveDays = snapDays(record.reserveDays)
|
|
return cfg
|
|
end
|