feat(inferium-server): add setconfig
This commit is contained in:
parent
3bdfec3eda
commit
49669c6e59
@ -11,11 +11,13 @@ local UPGRADE_SCRIPT = '/upgrade.lua'
|
|||||||
|
|
||||||
local defaultConfig = inferium.defaultConfig or error('no default config provided in config', 0)
|
local defaultConfig = inferium.defaultConfig or error('no default config provided in config', 0)
|
||||||
|
|
||||||
|
-- Persistance utils
|
||||||
|
|
||||||
local savePersistedConfigs = function(configs)
|
local savePersistedConfigs = function(configs)
|
||||||
local file = fs.open(PERSISTED_CONFIGS, 'w')
|
local file = fs.open(PERSISTED_CONFIGS, 'w')
|
||||||
|
|
||||||
if not file then
|
if not file then
|
||||||
error('savePersistedConfigs: cannot open .minerstate file!')
|
error('savePersistedConfigs: cannot open ' .. PERSISTED_CONFIGS .. ' file!')
|
||||||
end
|
end
|
||||||
|
|
||||||
file.write(textutils.serialize(configs))
|
file.write(textutils.serialize(configs))
|
||||||
@ -37,6 +39,7 @@ local readPersistedConfigs = function()
|
|||||||
return textutils.unserialize(serializedConfigs)
|
return textutils.unserialize(serializedConfigs)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- State
|
||||||
local LOCAL_CONFIGS = readPersistedConfigs()
|
local LOCAL_CONFIGS = readPersistedConfigs()
|
||||||
|
|
||||||
local function getConfigForComputer(computerId)
|
local function getConfigForComputer(computerId)
|
||||||
@ -48,6 +51,8 @@ local function saveConfigForComputer(computerId, config)
|
|||||||
savePersistedConfigs(LOCAL_CONFIGS)
|
savePersistedConfigs(LOCAL_CONFIGS)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Utils
|
||||||
|
|
||||||
local function getConfigWithLength(config)
|
local function getConfigWithLength(config)
|
||||||
local configWithLength = utils.shallowClone(config)
|
local configWithLength = utils.shallowClone(config)
|
||||||
configWithLength.length = utils.sizeof(config.plan)
|
configWithLength.length = utils.sizeof(config.plan)
|
||||||
@ -55,6 +60,21 @@ local function getConfigWithLength(config)
|
|||||||
return configWithLength
|
return configWithLength
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function isValidConfig(config)
|
||||||
|
if not config or type(config.plan) ~= 'table' then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
if config.firstCropZ == nil or config.firstCropZ < 1 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
if config.fertilizedBoost == nil then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Routes
|
||||||
|
|
||||||
local function getConfig(_, computerId)
|
local function getConfig(_, computerId)
|
||||||
if not computerId == nil then
|
if not computerId == nil then
|
||||||
@ -76,6 +96,21 @@ local function getConfig(_, computerId)
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function setConfig(message)
|
||||||
|
local payload = message and message.payload
|
||||||
|
local harvesterId = payload and payload.id
|
||||||
|
local config = payload and payload.config
|
||||||
|
|
||||||
|
if not isValidConfig(config) or not harvesterId then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
LOCAL_CONFIGS[harvesterId] = config
|
||||||
|
saveConfigForComputer(harvesterId, config)
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
-- return a table of harvesters { id: string, name: string }[]
|
-- return a table of harvesters { id: string, name: string }[]
|
||||||
local function listHarvesters()
|
local function listHarvesters()
|
||||||
local result = {}
|
local result = {}
|
||||||
@ -99,7 +134,8 @@ end
|
|||||||
|
|
||||||
local ROUTES = {
|
local ROUTES = {
|
||||||
['getconfig'] = getConfig,
|
['getconfig'] = getConfig,
|
||||||
['listHarvesters'] = listHarvesters,
|
['setconfig'] = setConfig,
|
||||||
|
['list-harvesters'] = listHarvesters,
|
||||||
['exit-server'] = exitServer,
|
['exit-server'] = exitServer,
|
||||||
['upgrade-server'] = upgradeServer,
|
['upgrade-server'] = upgradeServer,
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user