Files

155 lines
6.2 KiB
Lua

-- AD Sell Block
-- Salgssteder spilleren har slået fra (in-game 30/9: "Market Price Liquid Fill Station" fra FarmFillStations gik i stå
-- ved aflæsning af gylle). Konsol: spuSellBlock viser salgsstederne; spuSellBlock <del af navn> slår dem fra/til.
-- Udkørsel (ADSources.findSellMarkers) og priser (ADSources.getMarketQuote) springer udelukkede over. Gemmes pr.
-- savegame i ADSmartPickup_sellblock.xml (skrives fra ADRunsController.save, som de andre filer).
ADSellBlock = {}
ADSellBlock.FILE_NAME = "ADSmartPickup_sellblock.xml"
ADSellBlock.ROOT = "sellBlock"
ADSellBlock.blocked = {} -- {[stationId] = visningsnavn}
-- ---- ren logik ----
-- stations = {{id, names = {...}}}, query = del af et navn (små/store bogstaver ligegyldige) -> matchende stationer
function ADSellBlock.match(stations, query)
local wanted = string.lower(tostring(query or ""))
local hits = {}
if wanted == "" then return hits end
for _, station in ipairs(stations or {}) do
for _, name in ipairs(station.names or {}) do
if string.find(string.lower(tostring(name)), wanted, 1, true) ~= nil then
table.insert(hits, station)
break
end
end
end
return hits
end
-- Ny tabel: hver station skiftes (fra -> til, til -> fra). Første navn huskes til visning.
function ADSellBlock.toggle(blocked, stations)
local result = {}
for id, name in pairs(blocked or {}) do result[id] = name end
for _, station in ipairs(stations or {}) do
if result[station.id] ~= nil then
result[station.id] = nil
else
result[station.id] = tostring((station.names or {})[1] or station.id)
end
end
return result
end
-- ---- spil ----
local function stationId(station)
local placeable = station ~= nil and station.owningPlaceable or nil
if placeable ~= nil and placeable.uniqueId ~= nil then return tostring(placeable.uniqueId) end
return nil
end
function ADSellBlock.isBlocked(station)
local id = stationId(station)
return id ~= nil and ADSellBlock.blocked[id] ~= nil
end
local function stationNames(station)
local names = {}
local placeable = station.owningPlaceable
if placeable ~= nil and placeable.getName ~= nil then
local ok, name = pcall(placeable.getName, placeable)
if ok and name ~= nil then table.insert(names, name) end
end
if station.getName ~= nil then
local ok, name = pcall(station.getName, station)
if ok and name ~= nil then table.insert(names, name) end
end
return names
end
-- Alle salgssteder med AD-markør -> {{id, names}} (markørens navn først: det spilleren ser i AutoDrive).
local function sellStations(adEnv)
local list, byId = {}, {}
local farmId = g_currentMission:getFarmId()
for _, marker in pairs(adEnv.ADGraphManager:getMapMarkers()) do
local station = ADSmartPickup.getUnloadStationAtWayPointId(adEnv, marker.id, farmId)
local id = station ~= nil and ADPrices.isSellingStation(station) and stationId(station) or nil
if id ~= nil then
if byId[id] == nil then
byId[id] = {id = id, names = {}}
table.insert(list, byId[id])
for _, name in ipairs(stationNames(station)) do table.insert(byId[id].names, name) end
end
table.insert(byId[id].names, 1, marker.name)
end
end
table.sort(list, function(a, b) return tostring(a.names[1]) < tostring(b.names[1]) end)
return list
end
function ADSellBlock:consoleSellBlock(...)
local query = table.concat({...}, " ")
local adEnv = FS25_AutoDrive
if adEnv == nil or adEnv.ADGraphManager == nil then return "AutoDrive ikke fundet" end
local stations = sellStations(adEnv)
if query == "" then
local lines = {"Salgssteder (spuSellBlock <del af navn> slår fra/til):"}
for _, station in ipairs(stations) do
table.insert(lines, string.format(" %s %s", ADSellBlock.blocked[station.id] ~= nil and "[FRA]" or "[til]", table.concat(station.names, " / ")))
end
return table.concat(lines, "\n")
end
local hits = ADSellBlock.match(stations, query)
if #hits == 0 then return string.format("Intet salgssted matcher '%s' (spuSellBlock uden navn viser listen)", query) end
ADSellBlock.blocked = ADSellBlock.toggle(ADSellBlock.blocked, hits)
local lines = {}
for _, station in ipairs(hits) do
local text = string.format("%s: %s", station.names[1], ADSellBlock.blocked[station.id] ~= nil and "slået FRA som salgssted" or "bruges igen")
table.insert(lines, text)
Logging.info("ADSmartPickup: salgssted " .. text)
end
if ADRunsController ~= nil and ADRunsController.save ~= nil then pcall(ADRunsController.save) end
return table.concat(lines, "\n")
end
local function getPath()
local path = ADRunsStorage ~= nil and ADRunsStorage.getPath() or nil
if path == nil then return nil end
return (path:gsub(ADRunsStorage.FILE_NAME .. "$", ADSellBlock.FILE_NAME))
end
function ADSellBlock.save()
local path = getPath()
if path == nil then return false end
local xmlFile = XMLFile.create("ADSmartPickupSellBlock", path, ADSellBlock.ROOT)
if xmlFile == nil then return false end
local index = 0
for id, name in pairs(ADSellBlock.blocked) do
local key = string.format("%s.station(%d)", ADSellBlock.ROOT, index)
xmlFile:setString(key .. "#id", id)
xmlFile:setString(key .. "#name", name)
index = index + 1
end
xmlFile:save()
xmlFile:delete()
return true
end
function ADSellBlock.load()
ADSellBlock.blocked = {}
local path = getPath()
if path == nil or not fileExists(path) then return end
local xmlFile = XMLFile.load("ADSmartPickupSellBlock", path)
if xmlFile == nil then return end
xmlFile:iterate(ADSellBlock.ROOT .. ".station", function(_, key)
local id = xmlFile:getString(key .. "#id")
if id ~= nil then ADSellBlock.blocked[id] = xmlFile:getString(key .. "#name") or id end
end)
xmlFile:delete()
end
if addConsoleCommand ~= nil then
addConsoleCommand("spuSellBlock", "Salgssteder: uden navn = liste; spuSellBlock <del af navn> slår fra/til", "consoleSellBlock", ADSellBlock)
end