summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-08-27 00:31:54 +0100
committerubq323 <ubq323@ubq323.website>2023-08-27 00:31:54 +0100
commitef0b7de7745c8932ff76fef2dd267c1d7a0d4b69 (patch)
treee5876c15da3bd013ce13bc41e8ff4514268f771a
parent00d6c715e8d71056274e22b3a7eae2e55d3840c1 (diff)
move chat formatting to serverside, and make chat messages include username
-rw-r--r--client/game.lua5
-rw-r--r--net.txt4
-rw-r--r--server/server.lua10
3 files changed, 10 insertions, 9 deletions
diff --git a/client/game.lua b/client/game.lua
index ede8d98..cf57c81 100644
--- a/client/game.lua
+++ b/client/game.lua
@@ -197,10 +197,8 @@ local function handle_net()
color=pl.color, id=pl.id,
username = pl.username,
}
- msgbox.add(pl.username.." joined")
elseif op == "leave" then
local id = j.id
- msgbox.add(remote_players[id].username.." left")
remote_players[id]=nil
elseif op == "move" then
local id,x,y = j.id,j.x,j.y
@@ -224,8 +222,7 @@ local function handle_net()
local h = coords.Hex:make(j.q,j.r)
map:set_at(h,j.tile)
elseif op == "chat" then
- local msg,from = j.msg,j.from
- msgbox.add("["..tostring(from).."] "..msg)
+ msgbox.add(j.msg)
end
end
until not ev
diff --git a/net.txt b/net.txt
index bec4003..d9d113f 100644
--- a/net.txt
+++ b/net.txt
@@ -35,8 +35,8 @@ chunk {tiles=[array of 128*128 tiles],u,v}
settile {q,r,tile}
tile at H(q,r) was set to tile
-chat {msg,from}
- recieve chat message msg from player with name from.
+chat {msg}
+ recieve chat message msg
give {tile}
gain 1x tile in your inventory
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