Compare commits

...

3 Commits

Author SHA1 Message Date
Pascal Abresch a8ed634e0a simplify code a bit 2022-07-03 20:34:12 +02:00
Pascal Abresch 0006b977ac add avatar 2022-07-03 20:32:18 +02:00
Pascal Abresch 56b0f6aa97 new assertion 2022-07-03 20:31:03 +02:00
9 changed files with 30 additions and 52 deletions

View File

@ -6,11 +6,6 @@ return function(commands)
function commands.chatMessage(clientID, args) function commands.chatMessage(clientID, args)
assert(clients[clientID]) assert(clients[clientID])
print("Chat " .. clients[clientID].name .. ": " .. args) print("Chat " .. clients[clientID].name .. ": " .. args)
for i, player in pairs(clients) do broadcast(clientID, util.unit("chatMessage", args))
if i ~= clientiD then
local payload = util.unit("poppyV002", clientID, "chatMessage") .. constants.US .. args
server:sendto(payload, player.ip, player.port)
end
end
end end
end end

View File

@ -2,17 +2,6 @@ local constants = require("constants")
local util = require("utils") local util = require("utils")
local rpc = require("rpc") local rpc = require("rpc")
local function broadcast(clientID, message)
local payload = util.unit("poppyV002", 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
return function(commands) return function(commands)
function commands.drawTexture(clientID, args) function commands.drawTexture(clientID, args)
broadcast(clientID, util.unit("drawTexture", args)) broadcast(clientID, util.unit("drawTexture", args))

View File

@ -12,37 +12,23 @@ return function(commands)
clients[clientID].speedx = speedx clients[clientID].speedx = speedx
clients[clientID].speedy = speedy clients[clientID].speedy = speedy
for i, player in pairs(clients) do broadcast(clientID, util.unit("moveUpdate", x, y, speedx, speedy))
if i ~= clientID then
local payload = util.unit("poppyV002", clientID, "moveUpdate", x, y, speedx, speedy)
server:sendto(payload, player.ip, player.port)
end
end
end end
function commands.moveFlying(clientID, args) function commands.moveFlying(clientID, args)
if not clients[clientID] then rpc.print("moveFlying: invalid clientid [" .. clientID .. "]") return end if not clients[clientID] then rpc.print("moveFlying: invalid clientid [" .. clientID .. "]") return end
for i, player in pairs(clients) do broadcast(clientID, util.unit("moveFlying", args))
local payload = util.unit("poppyV002", clientID, "moveFlying", args)
server:sendto(payload, player.ip, player.port)
end
end end
function commands.deletePhysics(clientID, args) function commands.deletePhysics(clientID, args)
if not clients[clientID] then rpc.print("deletePhysics: invalid clientid [" .. clientID .. "]") return end if not clients[clientID] then rpc.print("deletePhysics: invalid clientid [" .. clientID .. "]") return end
for i, player in pairs(clients) do broadcast(clientID, util.unit("deletePhysics", args))
local payload = util.unit("poppyV002", clientID, "deletePhysics") .. constants.US .. args
server:sendto(payload, player.ip, player.port)
end
end end
function commands.drawPhysics(clientID, args) function commands.drawPhysics(clientID, args)
if not clients[clientID] then rpc.print("drawPhysics: invalid clientid [" .. clientID .. "]") return end if not clients[clientID] then rpc.print("drawPhysics: invalid clientid [" .. clientID .. "]") return end
for i, player in pairs(clients) do broadcast(clientID, util.unit("drawPhysics", args))
local payload = util.unit("poppyV002", clientID, "drawPhysics") .. constants.US .. args
server:sendto(payload, player.ip, player.port)
end
end end
end end

View File

@ -4,11 +4,6 @@ return function(commands)
function commands.playerLeave(clientID) function commands.playerLeave(clientID)
print("Old client disconnected. id: [" .. clientID .. "]") print("Old client disconnected. id: [" .. clientID .. "]")
clients[clientID] = nil clients[clientID] = nil
for i, player in pairs(clients) do broadcast(clientID, "playerLeave")
if i ~= clientiD then
local payload = util.unit("poppyV002", clientID,"playerLeave")
server:sendto(payload, player.ip, player.port)
end
end
end end
end end

View File

@ -2,5 +2,5 @@ return {
RS = string.char(30), RS = string.char(30),
US = string.char(31), US = string.char(31),
protocolVersion = "poppyV001", protocolVersion = "poppyV003",
} }

View File

@ -1,6 +1,7 @@
print("Startup!") print("Startup!")
local constants = require("constants") local constants = require("constants")
protocolVersion = constants.protocolVersion
local rpc = require("rpc") local rpc = require("rpc")
local socket = require("socket") local socket = require("socket")
local util = require("utils") local util = require("utils")
@ -25,6 +26,17 @@ print(server:getsockname())
if not server or not socket then error("failed to listen on socket") end 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) function love.update(dt)
local data, ip, port local data, ip, port
data, ip, port = server:receivefrom() data, ip, port = server:receivefrom()
@ -44,13 +56,13 @@ function love.update(dt)
if result.clientID == 0 then if result.clientID == 0 then
local response = rpc.unprivligedExecute(unpriviligedCommands, result.record, ip, port) local response = rpc.unprivligedExecute(unpriviligedCommands, result.record, ip, port)
if response then if response then
local payload = "poppyV002" .. constants.US .. "0" .. constants.US .. response local payload = protocolVersion .. constants.US .. "0" .. constants.US .. response
assert(server:sendto(payload, ip, port)) assert(server:sendto(payload, ip, port))
end end
else else
local response = rpc.execute(commands, result.clientID, result.record) local response = rpc.execute(commands, result.clientID, result.record)
if response then if response then
local payload = "poppyV002" .. constants.US .. "0" .. constants.US .. response local payload = protocolVersion .. constants.US .. "0" .. constants.US .. response
assert(server:sendto(payload, ip, port)) assert(server:sendto(payload, ip, port))
end end
end end

View File

@ -1,5 +1,5 @@
local rpc = { local rpc = {
PROTOCOL_VERSION = "002" PROTOCOL_VERSION = "003"
} }
function rpc.clean(record) function rpc.clean(record)

View File

@ -5,34 +5,34 @@ local rpc = require("rpc")
local clientCount = 0 local clientCount = 0
local world = { local world = {
x = 1200, x = 3000,
y = 1200, y = 3000,
id = "NYAAA" id = "NYAAA"
} }
return function(commands) return function(commands)
function commands.init(args, ip, port) function commands.init(args, ip, port)
clientCount = clientCount +1 clientCount = clientCount +1
local nickname = args local nickname, avatar = util.nextStringRecord(args)
clients[clientCount] = { ip = ip, port = port, name = nickname } clients[clientCount] = { ip = ip, port = port, name = nickname, avatar = avatar }
print("New client connected. Granting id: [" .. clientCount .. "] IP: " .. ip .. " Port:" .. port) print("New client connected. Granting id: [" .. clientCount .. "] IP: " .. ip .. " Port:" .. port)
for i, client in pairs(clients) do for i, client in pairs(clients) do
if i ~= clientCount then if i ~= clientCount then
print("Introducing client[" .. i .. "] and client[" .. clientCount .. "]") print("Introducing client[" .. i .. "] and client[" .. clientCount .. "]")
-- to Existing client -- to Existing client
local payload = util.unit("poppyV002", clientCount, "playerJoin", nickname) local payload = util.unit(protocolVersion, clientCount, "playerJoin", nickname, avatar)
print("<= " .. util.pprint(payload)) print("<= " .. util.pprint(payload))
server:sendto(payload, client.ip, client.port) server:sendto(payload, client.ip, client.port)
-- to New client -- to New client
local payload = util.unit("poppyV002", i, "playerJoin", client.name) local payload = util.unit(protocolVersion, i, "playerJoin", client.name, client.avatar)
print("<= " .. util.pprint(payload)) print("<= " .. util.pprint(payload))
server:sendto(payload, ip, port) server:sendto(payload, ip, port)
end end
end end
-- Client response -- Client response
local response = util.unit("poppyV002", clientCount, "init", nickname, world.id, world.x, world.y) local response = util.unit(protocolVersion, clientCount, "init", nickname, avatar, world.id, world.x, world.y)
server:sendto(response, ip, port) server:sendto(response, ip, port)
end end
end end

View File

@ -2,6 +2,7 @@ local tempTable = {}
function tempTable.unit(...) function tempTable.unit(...)
local args = { ... } local args = { ... }
local string = args[1] local string = args[1]
assert(string, "No args to unit!")
for i=2, #args do for i=2, #args do
--print(type(string) .. "with" .. type(args[i])) --print(type(string) .. "with" .. type(args[i]))
string = string .. string.char(31) .. args[i] string = string .. string.char(31) .. args[i]