Skip to content

Instantly share code, notes, and snippets.

@AmariNoa
Created July 13, 2022 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AmariNoa/ea3e0e88ea6e5af1a2fe2b8224dfd1d3 to your computer and use it in GitHub Desktop.
Save AmariNoa/ea3e0e88ea6e5af1a2fe2b8224dfd1d3 to your computer and use it in GitHub Desktop.
Master server for ComputerCraft
-- MasterServer
-- Ver. 0.1
-- Include external lib
dofile("/lib/amari/string/StringUtil.lua")
local REDNET_SIDE = "back"
local REDNET_PROTOCOL = "MasterServer"
local serverMap = {}
local function SetServerId(serverType, serverId)
serverMap[serverType] = serverId
end
-- TODO Remove id from server map
local stopServer = false
local function readConsoleThread()
local input = ""
while (not stopServer) do
term.write(">> ")
input = read()
-- TODO Add console command here
if string.lower(input) == "stop" then
stopServer = true
print("Stopping server...")
else
print("Unknown command: "..input)
end
end
end
local function serverThread()
-- TODO request send id
local isOpen = true
local sender, receive, protocol
while isOpen do
sender, receive, protocol = rednet.receive(3)
if (sender ~= nil) and (receive ~= nil) then
-- print("")
-- print("Message received: ".. receive)
-- Message received
local cmdSplit = string.split(receive, ":")
local cmd = (#cmdSplit > 0) and cmdSplit[1] or receive
cmd = string.lower(cmd)
-- Execute command
local result = false
local data = "Unknown command"
if cmd == string.lower("SetServerId") then
if #cmdSplit >= 2 then
if protocol ~= nil then
SetServerId(protocol, cmdSplit[2])
result = true
data = "Success"
-- print("SetServerId: "..protocol.." = "..cmdSplit[2])
else
result = false
data = "Protocol is nil"
-- print("SetServerId failed.")
-- print(data)
end
else
result = false
data = "Invalid argument count"
-- print("SetServerId failed.")
-- print(data)
end
elseif cmd == string.lower("GetServerId") then
if protocol ~= nil then
local serverId = serverMap[protocol]
if serverId ~= nil then
result = true
data = serverId
else
result = false
data = "Server not registered (Protocol: "..protocol..")"
end
else
result = false
data = "Protocol is nil"
end
end
-- Message format => response:cmdName:result:data
local sendMsg = "response:"..cmd..":"..tostring(result)..":"..data
-- print("SendMsg: "..sendMsg)
-- term.write(">> ")
rednet.send(sender, sendMsg, REDNET_PROTOCOL)
end
if stopServer == true then
break
end
isOpen = rednet.isOpen(REDNET_SIDE)
if not isOpen then
break
end
end
end
-- Script main
local computerId = os.getComputerID()
print("ComputerId = "..computerId)
rednet.open(REDNET_SIDE)
local isOpen = rednet.isOpen(REDNET_SIDE)
if not isOpen then
print("Rednet open failed.")
return
end
print("MasterServer started.")
SetServerId(REDNET_PROTOCOL, computerId)
-- print("Sending master server id...")
-- rednet.broadcast("ComputerId:"..computerId, REDNET_PROTOCOL)
parallel.waitForAll(readConsoleThread, serverThread)
rednet.close(REDNET_SIZE)
print("MasterServer stopped.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment