2021-12-31 18:04:12 +00:00
|
|
|
print("Startup!")
|
|
|
|
|
2021-12-31 21:19:26 +00:00
|
|
|
local constants = require("constants")
|
2022-07-03 18:32:45 +00:00
|
|
|
protocolVersion = constants.protocolVersion
|
2021-12-31 21:19:26 +00:00
|
|
|
local rpc = require("rpc")
|
|
|
|
local socket = require("socket")
|
2022-01-02 17:40:30 +00:00
|
|
|
local util = require("utils")
|
2020-08-13 19:01:44 +00:00
|
|
|
|
2022-06-23 17:29:08 +00:00
|
|
|
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)
|
2020-08-18 03:35:57 +00:00
|
|
|
|
2022-01-02 17:40:30 +00:00
|
|
|
server = assert(socket.udp())
|
2021-12-31 21:19:26 +00:00
|
|
|
local ip, port = nil, nil
|
2020-08-13 19:01:44 +00:00
|
|
|
|
2022-01-02 17:40:30 +00:00
|
|
|
clients = {}
|
2022-01-01 01:32:52 +00:00
|
|
|
|
2022-02-06 19:32:00 +00:00
|
|
|
server:settimeout(0)
|
2022-06-25 09:25:33 +00:00
|
|
|
assert(server:setsockname("*", 11150))
|
2022-06-24 13:09:43 +00:00
|
|
|
print(server:getsockname())
|
2021-12-31 21:19:26 +00:00
|
|
|
if not server or not socket then error("failed to listen on socket") end
|
2020-08-13 19:01:44 +00:00
|
|
|
|
2022-06-19 09:29:10 +00:00
|
|
|
|
2022-07-03 18:32:45 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-02-06 19:32:00 +00:00
|
|
|
function love.update(dt)
|
|
|
|
local data, ip, port
|
2020-08-18 03:35:57 +00:00
|
|
|
data, ip, port = server:receivefrom()
|
2021-12-31 18:04:12 +00:00
|
|
|
if not data then
|
2022-02-06 19:32:00 +00:00
|
|
|
local errorCode = ip
|
|
|
|
if errorCode == "timeout" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
print(errorCode)
|
|
|
|
return
|
2021-12-31 18:04:12 +00:00
|
|
|
end
|
2020-08-18 03:35:57 +00:00
|
|
|
|
2022-01-02 16:18:44 +00:00
|
|
|
local result = rpc.validate(data)
|
2022-06-23 17:29:08 +00:00
|
|
|
--print("=> " .. util.pprint(data))
|
2022-01-02 16:18:44 +00:00
|
|
|
|
|
|
|
if not result.errorMsg then
|
2022-06-23 17:29:08 +00:00
|
|
|
if result.clientID == 0 then
|
|
|
|
local response = rpc.unprivligedExecute(unpriviligedCommands, result.record, ip, port)
|
|
|
|
if response then
|
2022-07-03 18:32:45 +00:00
|
|
|
local payload = protocolVersion .. constants.US .. "0" .. constants.US .. response
|
2022-06-23 17:29:08 +00:00
|
|
|
assert(server:sendto(payload, ip, port))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local response = rpc.execute(commands, result.clientID, result.record)
|
|
|
|
if response then
|
2022-07-03 18:32:45 +00:00
|
|
|
local payload = protocolVersion .. constants.US .. "0" .. constants.US .. response
|
2022-06-23 17:29:08 +00:00
|
|
|
assert(server:sendto(payload, ip, port))
|
2022-01-02 20:24:34 +00:00
|
|
|
end
|
2021-12-31 19:13:10 +00:00
|
|
|
end
|
2020-08-18 03:35:57 +00:00
|
|
|
end
|
2020-08-13 19:01:44 +00:00
|
|
|
end
|