From 3bdfec3eda6dac8b1014c429acad780336b07a65 Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Wed, 22 May 2024 20:54:14 +0200 Subject: [PATCH] feat(inferium-server): add listharvesters --- inferium-server.lua | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/inferium-server.lua b/inferium-server.lua index edde063..48ea124 100644 --- a/inferium-server.lua +++ b/inferium-server.lua @@ -2,7 +2,8 @@ local net = require('libs/net') local utils = require('libs/utils') 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 PERSISTED_CONFIGS = '/data/inferium-configs' @@ -42,7 +43,7 @@ local function getConfigForComputer(computerId) return LOCAL_CONFIGS[tostring(computerId)] end -local function setConfigForComputer(computerId, config) +local function saveConfigForComputer(computerId, config) LOCAL_CONFIGS[tostring(computerId)] = config savePersistedConfigs(LOCAL_CONFIGS) end @@ -55,7 +56,7 @@ local function getConfigWithLength(config) end -local function getConfig(computerId) +local function getConfig(_, computerId) if not computerId == nil then print('getconfig error: no computerId found') return nil @@ -65,7 +66,7 @@ local function getConfig(computerId) if not config then print('new harvester detected: ' .. tostring(computerId)) - setConfigForComputer(computerId, defaultConfig) + saveConfigForComputer(computerId, defaultConfig) config = defaultConfig end @@ -75,10 +76,32 @@ local function getConfig(computerId) } 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 = { - ['getconfig'] = function(_, computerId) return getConfig(computerId) end, - ['exit-server'] = function(_, _, stopServer) stopServer() ; return true end, - ['upgrade-server'] = function() shell.execute(UPGRADE_SCRIPT) ; return true end, + ['getconfig'] = getConfig, + ['listHarvesters'] = listHarvesters, + ['exit-server'] = exitServer, + ['upgrade-server'] = upgradeServer, } net.openRednet()