diff options
author | ubq323 <ubq323@ubq323.website> | 2023-01-30 20:17:41 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2023-01-30 20:17:41 +0000 |
commit | b4dc431b6f7d655142295e33b9228be243ab2969 (patch) | |
tree | 0d08286b18710de63f3037324bcaeaf2c44f7b04 /server/server.lua | |
parent | ebe12a65c07bccfbc704667c88d8125be33067cf (diff) |
switch to using json for packet formatting
Diffstat (limited to 'server/server.lua')
-rw-r--r-- | server/server.lua | 35 |
1 files changed, 16 insertions, 19 deletions
diff --git a/server/server.lua b/server/server.lua index 93b4e3d..12d70e5 100644 --- a/server/server.lua +++ b/server/server.lua @@ -1,5 +1,5 @@ local enet = require"enet" -local words = require"common.words" +local json = require"common.dkjson" local unpack = unpack or table.unpack math.randomseed(os.time()) @@ -35,28 +35,25 @@ local function make_player(peer) return p end --- maybe json could be used for this --- or something local function player_info_part(player) - return words.join( - player.id, - player.pos[1], - player.pos[2], - player.color[1], - player.color[2], - player.color[3]) + return { + id=player.id, + x=player.pos[1], + y=player.pos[2], + color=player.color, + } end local function player_join_packet(player) - return "join "..player_info_part(player) + return json.encode{t="join",pl=player_info_part(player)} end local function player_you_packet(player) - return "you "..player_info_part(player) + return json.encode{t="you",pl=player_info_part(player)} end local function player_leave_packet(player) - return words.join("leave",player.id) + return json.encode{t="leave",id=player.id} end local function player_move_packet(player,x,y) - return words.join("move",player.id,x,y) + return json.encode{t="move",id=player.id,x=x,y=y} end while true do @@ -87,12 +84,12 @@ while true do elseif ev.type == "receive" then local player = player_by_peer(ev.peer) if not player then error("sneezey "..ev.peer) end - local w = words.split(ev.data) - local op = w[1] + local j = json.decode(ev.data) + local op = j.t if op == "ppos" then - local _,x,y,a = unpack(w) - player.pos[1] = tonumber(x) - player.pos[2] = tonumber(y) + local x,y = j.x,j.y + player.pos[1] = x + player.pos[2] = y print(player.id,"-->",player.pos[1],player.pos[2]) for i,otherplayer in ipairs(playerlist) do if otherplayer ~= player then |