Compare commits

..

No commits in common. "a8ed634e0a254285add58f4f41a0fefeaec47fe5" and "3447cbeba95455783a3fa28a26935e8960ff1a42" have entirely different histories.

9 changed files with 52 additions and 30 deletions

View File

@ -6,6 +6,11 @@ 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)
broadcast(clientID, util.unit("chatMessage", args)) for i, player in pairs(clients) do
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,6 +2,17 @@ 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,23 +12,37 @@ return function(commands)
clients[clientID].speedx = speedx clients[clientID].speedx = speedx
clients[clientID].speedy = speedy clients[clientID].speedy = speedy
broadcast(clientID, util.unit("moveUpdate", x, y, speedx, speedy)) for i, player in pairs(clients) do
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
broadcast(clientID, util.unit("moveFlying", args)) for i, player in pairs(clients) do
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
broadcast(clientID, util.unit("deletePhysics", args)) for i, player in pairs(clients) do
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
broadcast(clientID, util.unit("drawPhysics", args)) for i, player in pairs(clients) do
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,6 +4,11 @@ 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
broadcast(clientID, "playerLeave") for i, player in pairs(clients) do
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 = "poppyV003", protocolVersion = "poppyV001",
} }

View File

@ -1,7 +1,6 @@
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")
@ -26,17 +25,6 @@ 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()
@ -56,13 +44,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 = protocolVersion .. constants.US .. "0" .. constants.US .. response local payload = "poppyV002" .. 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 = protocolVersion .. constants.US .. "0" .. constants.US .. response local payload = "poppyV002" .. 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 = "003" PROTOCOL_VERSION = "002"
} }
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 = 3000, x = 1200,
y = 3000, y = 1200,
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, avatar = util.nextStringRecord(args) local nickname = args
clients[clientCount] = { ip = ip, port = port, name = nickname, avatar = avatar } clients[clientCount] = { ip = ip, port = port, name = nickname }
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(protocolVersion, clientCount, "playerJoin", nickname, avatar) local payload = util.unit("poppyV002", clientCount, "playerJoin", nickname)
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(protocolVersion, i, "playerJoin", client.name, client.avatar) local payload = util.unit("poppyV002", i, "playerJoin", client.name)
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(protocolVersion, clientCount, "init", nickname, avatar, world.id, world.x, world.y) local response = util.unit("poppyV002", clientCount, "init", nickname, world.id, world.x, world.y)
server:sendto(response, ip, port) server:sendto(response, ip, port)
end end
end end

View File

@ -2,7 +2,6 @@ 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]