You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.8 KiB
Lua
71 lines
1.8 KiB
Lua
print("Startup!")
|
|
|
|
local constants = require("constants")
|
|
protocolVersion = constants.protocolVersion
|
|
local rpc = require("rpc")
|
|
local socket = require("socket")
|
|
local util = require("utils")
|
|
|
|
local commands = {}
|
|
require("commands/players")(commands)
|
|
require("commands/physics")(commands)
|
|
require("commands/drawing")(commands)
|
|
require("commands/chat")(commands)
|
|
|
|
local unpriviligedCommands = {}
|
|
require("unpriviliged_commands/init")(unpriviligedCommands)
|
|
|
|
server = assert(socket.udp())
|
|
local ip, port = nil, nil
|
|
|
|
clients = {}
|
|
|
|
server:settimeout(0)
|
|
assert(server:setsockname("*", 11150))
|
|
print(server:getsockname())
|
|
if not server or not socket then error("failed to listen on socket") end
|
|
|
|
|
|
function broadcast(clientID, message)
|
|
local payload = util.unit(protocolVersion, clientID, message)
|
|
for i, player in pairs(clients) do
|
|
if i ~= clientID then
|
|
server:sendto(payload, player.ip, player.port)
|
|
print("<= " .. payload)
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
function love.update(dt)
|
|
local data, ip, port
|
|
data, ip, port = server:receivefrom()
|
|
if not data then
|
|
local errorCode = ip
|
|
if errorCode == "timeout" then
|
|
return
|
|
end
|
|
print(errorCode)
|
|
return
|
|
end
|
|
|
|
local result = rpc.validate(data)
|
|
--print("=> " .. util.pprint(data))
|
|
|
|
if not result.errorMsg then
|
|
if result.clientID == 0 then
|
|
local response = rpc.unprivligedExecute(unpriviligedCommands, result.record, ip, port)
|
|
if response then
|
|
local payload = protocolVersion .. constants.US .. "0" .. constants.US .. response
|
|
assert(server:sendto(payload, ip, port))
|
|
end
|
|
else
|
|
local response = rpc.execute(commands, result.clientID, result.record)
|
|
if response then
|
|
local payload = protocolVersion .. constants.US .. "0" .. constants.US .. response
|
|
assert(server:sendto(payload, ip, port))
|
|
end
|
|
end
|
|
end
|
|
end
|