minecraft-cc-tools/inferium-server.lua
2024-05-22 18:40:00 +02:00

68 lines
1.8 KiB
Lua

local net = require('libs/net')
local inferium = require('config/inferium')
local INFERIUM_SERVER = 'inferium.com'
local UPGRADE_SCRIPT = '/upgrade.lua'
local VERSION = "1.0.0"
local defaultConfig= inferium.defaultConfig or error('no default config provided in config', 0)
-- TODO: persistance
local LOCAL_CONFIGS = {}
local function getConfigForComputer(computerId)
return LOCAL_CONFIGS[tostring(computerId)]
end
local function setConfigForComputer(computerId, config)
LOCAL_CONFIGS[tostring(computerId)] = config
end
local function getConfig(computerId)
if not computerId == nil then
print('getconfig error: no computerId found')
return nil
end
local config = getConfigForComputer(computerId)
if not config then
print('getconfig warning: no plan found for computerID ' .. tostring(computerId))
setConfigForComputer(computerId, defaultConfig)
config = defaultConfig
end
return {
type = "getconfig/response",
payload = config
}
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,
}
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')