feat(inferium-server): add listharvesters

This commit is contained in:
Guillaume ARM 2024-05-22 20:54:14 +02:00
parent 877773bb5a
commit 3bdfec3eda

View File

@ -2,7 +2,8 @@ local net = require('libs/net')
local utils = require('libs/utils') local utils = require('libs/utils')
local inferium = require('config/inferium') local inferium = require('config/inferium')
local VERSION = "1.1.2" local DEFAULT_HARVESTER_NAME = 'harvester'
local VERSION = "1.2.0"
local INFERIUM_SERVER = 'inferium.com' local INFERIUM_SERVER = 'inferium.com'
local PERSISTED_CONFIGS = '/data/inferium-configs' local PERSISTED_CONFIGS = '/data/inferium-configs'
@ -42,7 +43,7 @@ local function getConfigForComputer(computerId)
return LOCAL_CONFIGS[tostring(computerId)] return LOCAL_CONFIGS[tostring(computerId)]
end end
local function setConfigForComputer(computerId, config) local function saveConfigForComputer(computerId, config)
LOCAL_CONFIGS[tostring(computerId)] = config LOCAL_CONFIGS[tostring(computerId)] = config
savePersistedConfigs(LOCAL_CONFIGS) savePersistedConfigs(LOCAL_CONFIGS)
end end
@ -55,7 +56,7 @@ local function getConfigWithLength(config)
end end
local function getConfig(computerId) local function getConfig(_, computerId)
if not computerId == nil then if not computerId == nil then
print('getconfig error: no computerId found') print('getconfig error: no computerId found')
return nil return nil
@ -65,7 +66,7 @@ local function getConfig(computerId)
if not config then if not config then
print('new harvester detected: ' .. tostring(computerId)) print('new harvester detected: ' .. tostring(computerId))
setConfigForComputer(computerId, defaultConfig) saveConfigForComputer(computerId, defaultConfig)
config = defaultConfig config = defaultConfig
end end
@ -75,10 +76,32 @@ local function getConfig(computerId)
} }
end end
-- return a table of harvesters { id: string, name: string }[]
local function listHarvesters()
local result = {}
for k, v in pairs(LOCAL_CONFIGS) do
table.insert(result, { id = k, name = v.name or DEFAULT_HARVESTER_NAME })
end
return result
end
local function exitServer(_, _, stopServer)
stopServer();
return true
end
local function upgradeServer()
shell.execute(UPGRADE_SCRIPT)
return true
end
local ROUTES = { local ROUTES = {
['getconfig'] = function(_, computerId) return getConfig(computerId) end, ['getconfig'] = getConfig,
['exit-server'] = function(_, _, stopServer) stopServer() ; return true end, ['listHarvesters'] = listHarvesters,
['upgrade-server'] = function() shell.execute(UPGRADE_SCRIPT) ; return true end, ['exit-server'] = exitServer,
['upgrade-server'] = upgradeServer,
} }
net.openRednet() net.openRednet()