summaryrefslogtreecommitdiff
path: root/server/server.lua
diff options
context:
space:
mode:
Diffstat (limited to 'server/server.lua')
-rw-r--r--server/server.lua29
1 files changed, 22 insertions, 7 deletions
diff --git a/server/server.lua b/server/server.lua
index 3ec467c..a3c8f60 100644
--- a/server/server.lua
+++ b/server/server.lua
@@ -48,8 +48,11 @@ end
local function chat_packet(fromplayer,msg)
return json.encode{t="chat",from=fromplayer.id,msg=msg}
end
-local function inv_packet(inv)
- return json.encode{t='inv',inv=inv}
+local function settile_packet(h,tile)
+ return json.encode{t='settile',q=h.q,r=h.r,tile=tile}
+end
+local function give_packet(tile)
+ return json.encode{t='give',tile=tile}
end
local map = MapS:make()
@@ -116,7 +119,6 @@ local function on_peer_disconnect(peer)
end
local function handle_player_packet(player,ev)
- print('player packet ',player,ev.data)
local j = json.decode(ev.data)
-- print(ev.channel,ev.data)
local op = j.t
@@ -136,11 +138,24 @@ local function handle_player_packet(player,ev)
local inv = player.inv
if old_tile == 0 then
- inv[new_tile] = (inv[new_tile] or 0) - 1
+ local nc = inv[new_tile] or 0
+ if nc <= 0 then
+ print("nooo")
+ player.peer:send(settile_packet(h,old_tile))
+ return
+ end
+ inv[new_tile] = nc - 1
elseif new_tile == 0 then
inv[old_tile] = (inv[old_tile] or 0) + 1
+ player.peer:send(give_packet(old_tile))
+ else
+ -- can't replace one tile with another
+ print("nooooo")
+ player.peer:send(settile_packet(h,old_tile))
+ return
end
- player.peer:send(inv_packet(inv))
+
+ print('settile', player.username, inv[old_tile], inv[new_tile])
map:set_at(h, new_tile)
@@ -172,7 +187,7 @@ local function handle_ev(ev)
elseif ev.type == 'disconnect' then
on_peer_disconnect(ev.peer)
elseif ev.type == 'receive' then
- print("recv:",ev.peer,ev.data)
+ -- print("recv:",ev.peer,ev.data)
if connecting_peers[ev.peer] then
local co = connecting_peers[ev.peer]
print("\t-> hshake",co,coroutine.status(co))
@@ -185,7 +200,7 @@ local function handle_ev(ev)
else
local player = player_by_peer(ev.peer)
if not player then error("packet from unknown peer ",ev.peer) end
- print("\t-> player",player)
+ -- print("\t-> player",player)
handle_player_packet(player, ev)
end
end