From ef0b7de7745c8932ff76fef2dd267c1d7a0d4b69 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sun, 27 Aug 2023 00:31:54 +0100 Subject: move chat formatting to serverside, and make chat messages include username --- server/server.lua | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'server') diff --git a/server/server.lua b/server/server.lua index a3c8f60..1c637bc 100644 --- a/server/server.lua +++ b/server/server.lua @@ -45,8 +45,8 @@ end local function player_move_packet(player,x,y) return json.encode{t="move",id=player.id,x=x,y=y} end -local function chat_packet(fromplayer,msg) - return json.encode{t="chat",from=fromplayer.id,msg=msg} +local function chat_packet(msg) + return json.encode{t="chat",msg=msg} end local function settile_packet(h,tile) return json.encode{t='settile',q=h.q,r=h.r,tile=tile} @@ -77,6 +77,8 @@ local function create_player(...) if otherplayer ~= player then player.peer:send(player_join_packet(otherplayer)) otherplayer.peer:send(player_join_packet(player)) + otherplayer.peer:send( + chat_packet(player.username .. " joined")) end end @@ -113,6 +115,7 @@ local function on_peer_disconnect(peer) table.remove(playerlist, idx) for i,otherplayer in ipairs(playerlist) do otherplayer.peer:send(player_leave_packet(player)) + otherplayer.peer:send(chat_packet(player.username..' left')) end print("disconnect", player.id, player.peer) end @@ -176,7 +179,8 @@ local function handle_player_packet(player,ev) elseif op == "chat" then print("chat ["..player.id.."] "..j.msg) for i,otherplayer in ipairs(playerlist) do - otherplayer.peer:send(chat_packet(player,j.msg)) + otherplayer.peer:send( + chat_packet('['..player.username..'] '..j.msg)) end end end -- cgit v1.2.3