Compare commits
No commits in common. "a8ed634e0a254285add58f4f41a0fefeaec47fe5" and "3447cbeba95455783a3fa28a26935e8960ff1a42" have entirely different histories.
a8ed634e0a
...
3447cbeba9
|
@ -6,6 +6,11 @@ return function(commands)
|
|||
function commands.chatMessage(clientID, args)
|
||||
assert(clients[clientID])
|
||||
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
|
||||
|
|
|
@ -2,6 +2,17 @@ local constants = require("constants")
|
|||
local util = require("utils")
|
||||
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)
|
||||
function commands.drawTexture(clientID, args)
|
||||
broadcast(clientID, util.unit("drawTexture", args))
|
||||
|
|
|
@ -12,23 +12,37 @@ return function(commands)
|
|||
clients[clientID].speedx = speedx
|
||||
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
|
||||
|
||||
|
||||
function commands.moveFlying(clientID, args)
|
||||
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
|
||||
|
||||
|
||||
function commands.deletePhysics(clientID, args)
|
||||
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
|
||||
|
||||
function commands.drawPhysics(clientID, args)
|
||||
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
|
||||
|
|
|
@ -4,6 +4,11 @@ return function(commands)
|
|||
function commands.playerLeave(clientID)
|
||||
print("Old client disconnected. id: [" .. clientID .. "]")
|
||||
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
|
||||
|
|
|
@ -2,5 +2,5 @@ return {
|
|||
RS = string.char(30),
|
||||
US = string.char(31),
|
||||
|
||||
protocolVersion = "poppyV003",
|
||||
protocolVersion = "poppyV001",
|
||||
}
|
||||
|
|
16
main.lua
16
main.lua
|
@ -1,7 +1,6 @@
|
|||
print("Startup!")
|
||||
|
||||
local constants = require("constants")
|
||||
protocolVersion = constants.protocolVersion
|
||||
local rpc = require("rpc")
|
||||
local socket = require("socket")
|
||||
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
|
||||
|
||||
|
||||
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()
|
||||
|
@ -56,13 +44,13 @@ function love.update(dt)
|
|||
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
|
||||
local payload = "poppyV002" .. 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
|
||||
local payload = "poppyV002" .. constants.US .. "0" .. constants.US .. response
|
||||
assert(server:sendto(payload, ip, port))
|
||||
end
|
||||
end
|
||||
|
|
2
rpc.lua
2
rpc.lua
|
@ -1,5 +1,5 @@
|
|||
local rpc = {
|
||||
PROTOCOL_VERSION = "003"
|
||||
PROTOCOL_VERSION = "002"
|
||||
}
|
||||
|
||||
function rpc.clean(record)
|
||||
|
|
|
@ -5,34 +5,34 @@ local rpc = require("rpc")
|
|||
local clientCount = 0
|
||||
|
||||
local world = {
|
||||
x = 3000,
|
||||
y = 3000,
|
||||
x = 1200,
|
||||
y = 1200,
|
||||
id = "NYAAA"
|
||||
}
|
||||
|
||||
return function(commands)
|
||||
function commands.init(args, ip, port)
|
||||
clientCount = clientCount +1
|
||||
local nickname, avatar = util.nextStringRecord(args)
|
||||
clients[clientCount] = { ip = ip, port = port, name = nickname, avatar = avatar }
|
||||
local nickname = args
|
||||
clients[clientCount] = { ip = ip, port = port, name = nickname }
|
||||
|
||||
print("New client connected. Granting id: [" .. clientCount .. "] IP: " .. ip .. " Port:" .. port)
|
||||
for i, client in pairs(clients) do
|
||||
if i ~= clientCount then
|
||||
print("Introducing client[" .. i .. "] and client[" .. clientCount .. "]")
|
||||
-- to Existing client
|
||||
local payload = util.unit(protocolVersion, clientCount, "playerJoin", nickname, avatar)
|
||||
local payload = util.unit("poppyV002", clientCount, "playerJoin", nickname)
|
||||
print("<= " .. util.pprint(payload))
|
||||
server:sendto(payload, client.ip, client.port)
|
||||
-- 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))
|
||||
server:sendto(payload, ip, port)
|
||||
end
|
||||
end
|
||||
|
||||
-- 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)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue