minecraft-cc-tools/inferium-server.lua

50 lines
1.4 KiB
Lua

local inferiumPlans = require('config/inferium-plans') -- temporary default plan
local VERSION = "0.2.1"
local INFERIUM_QUERY_PORT = 111
local INFERIUM_REPLY_PORT = 112
local defaultPlan = inferiumPlans.default or error('no default plan provided in config')
local modem = peripheral.find("modem") or error("No modem attached", 0)
modem.open(INFERIUM_QUERY_PORT)
local function getPlanForComputer(computerId)
return inferiumPlans[tostring(computerId)] or defaultPlan
end
local function messageReceived(message)
if message.type == 'getplan' then
local payload = message.payload or {}
if payload.computerId == nil then
print('getplan error: no computerId found in received payload')
return nil
end
return {
type = "getplan/response",
payload = getPlanForComputer(payload.computerId)
}
end
end
print('> inferium server v' .. VERSION .. ' listening on port ' .. INFERIUM_QUERY_PORT)
while true do
local _, _, channel, replyChannel, rawMessage, _ = os.pullEvent("modem_message")
if channel == INFERIUM_QUERY_PORT and replyChannel == INFERIUM_REPLY_PORT then
local message = textutils.unserialize(rawMessage)
local replyMessage = messageReceived(message)
if replyMessage then
local rawReplyMessage = textutils.serialize(replyMessage)
modem.transmit(INFERIUM_REPLY_PORT, INFERIUM_QUERY_PORT, rawReplyMessage)
else
print('warning: unknown message received')
end
end
end