minecraft-cc-tools/inferium-server.lua

182 lines
4.1 KiB
Lua

local net = require('libs/net')
local utils = require('libs/utils')
local inferium = require('config/inferium')
local DEFAULT_HARVESTER_NAME = 'harvester'
local VERSION = "2.0.1"
local INFERIUM_SERVER = 'inferium.com'
local PERSISTED_CONFIGS = '/data/inferium-configs'
local UPGRADE_SCRIPT = '/upgrade.lua'
local defaultConfig = inferium.defaultConfig or error('no default config provided in config', 0)
-- Persistance utils
local savePersistedConfigs = function(configs)
local file = fs.open(PERSISTED_CONFIGS, 'w')
if not file then
error('savePersistedConfigs: cannot open ' .. PERSISTED_CONFIGS .. ' file!')
end
file.write(textutils.serialize(configs))
file.close()
end
local readPersistedConfigs = function()
local file = fs.open(PERSISTED_CONFIGS, 'r')
if not file then
local initialConfig = {}
savePersistedConfigs(initialConfig)
return initialConfig
end
local serializedConfigs = file.readAll()
file.close()
return textutils.unserialize(serializedConfigs)
end
-- State
local LOCAL_CONFIGS = readPersistedConfigs()
local function getConfigForComputer(computerId)
return LOCAL_CONFIGS[tostring(computerId)]
end
local function saveConfigForComputer(computerId, config)
LOCAL_CONFIGS[tostring(computerId)] = config
savePersistedConfigs(LOCAL_CONFIGS)
end
-- Utils
local function getConfigWithLength(config)
local configWithLength = utils.shallowClone(config)
configWithLength.length = utils.sizeof(config.plan)
return configWithLength
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(message, computerId)
if not computerId == nil then
print('get-config error: no computerId found')
return nil
end
local givenHarvesterId = message and message.payload and message.payload.id
local id = givenHarvesterId or computerId
local config = getConfigForComputer(id)
if not givenHarvesterId and not config then
print('new harvester detected: ' .. tostring(computerId))
saveConfigForComputer(computerId, defaultConfig)
config = defaultConfig
end
return {
type = "get-config/response",
payload = getConfigWithLength(config)
}
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
saveConfigForComputer(harvesterId, config)
return true
end
local function deleteConfig(message)
local payload = message and message.payload
local harvesterId = payload and payload.id
if not harvesterId then
return false
end
if LOCAL_CONFIGS[harvesterId] then
saveConfigForComputer(harvesterId, nil)
return true
end
return false
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 = {
['get-config'] = getConfig,
['set-config'] = setConfig,
['delete-config'] = deleteConfig,
['list-harvesters'] = listHarvesters,
['exit-server'] = exitServer,
['upgrade-server'] = upgradeServer,
}
net.openRednet()
print('> inferium server v' .. VERSION .. ' started')
net.listenQuery(INFERIUM_SERVER, function(message, computerId, stopServer)
if type(message) ~= 'table' then
print('error: malformed message received', textutils.serialize(message))
return {}
end
local router = ROUTES[message.type]
if not router then
print('warning: unknown type of message received', message.type)
return {}
end
return router(message, computerId, stopServer)
end)
print('> server stopped')
net.closeRednet()
print('> rednet closed')