72 lines
1.9 KiB
Lua
72 lines
1.9 KiB
Lua
local net = require('libs/net')
|
|
local inferiumPlans = require('config/inferium-plans') -- temporary default plan
|
|
|
|
local INFERIUM_SERVER = 'inferium.com'
|
|
local UPGRADE_SCRIPT = '/upgrade.lua'
|
|
local VERSION = "0.5.1"
|
|
|
|
local defaultPlan = inferiumPlans.default or error('no default plan provided in config', 0)
|
|
|
|
local function getPlanForComputer(computerId)
|
|
return inferiumPlans[tostring(computerId)]
|
|
end
|
|
|
|
local function getPlan(computerId)
|
|
if not computerId == nil then
|
|
print('getplan error: no computerId found')
|
|
return nil
|
|
end
|
|
|
|
local plan = getPlanForComputer(computerId)
|
|
|
|
if not plan then
|
|
print('getplan warning: no plan found for computerID ' .. tostring(computerId))
|
|
plan = defaultPlan
|
|
end
|
|
|
|
return {
|
|
type = "getplan/response",
|
|
payload = plan
|
|
}
|
|
end
|
|
|
|
local ROUTES = {
|
|
['getplan'] = function(_, computerId) return getPlan(computerId) end,
|
|
['exit-server'] = function(_, _, stopServer) stopServer() ; return true end,
|
|
['upgrade-server'] = function() shell.execute(UPGRADE_SCRIPT) ; return true end,
|
|
}
|
|
|
|
local function handleMessage(message, computerId)
|
|
if message.type == 'getplan' then
|
|
return getPlan(computerId)
|
|
elseif message.type == 'exit-server' then
|
|
stopServer()
|
|
return true
|
|
elseif message.type == 'upgrade-server' then
|
|
shell.execute('/upgrade.lua')
|
|
return true
|
|
end
|
|
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') |