-- 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 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} 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 snapLiters(value) value = tonumber(value) if value == nil then return ADFieldSiloConfig.DEFAULT_MIN_LITERS end local best, bestDiff = nil, nil for _, choice in ipairs(ADFieldSiloConfig.MIN_LITERS_CHOICES) do local diff = math.abs(choice - value) if bestDiff == nil or diff < bestDiff then best, bestDiff = choice, diff end end return best end function ADFieldSiloConfig.new() return {enabled = false, storeIds = {}, maxVehicles = ADFieldSiloConfig.DEFAULT_MAX_VEHICLES, minLiters = ADFieldSiloConfig.DEFAULT_MIN_LITERS} 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 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} 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) return cfg end