summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client/game.lua12
-rw-r--r--client/util.lua2
-rw-r--r--net.txt4
-rw-r--r--server/server.lua29
4 files changed, 34 insertions, 13 deletions
diff --git a/client/game.lua b/client/game.lua
index 43e2352..ede8d98 100644
--- a/client/game.lua
+++ b/client/game.lua
@@ -214,11 +214,12 @@ local function handle_net()
username=pl.username,
inv = {},
}
- elseif op == "inv" then
- local_player.inv = j.inv
elseif op == "chunk" then
local ch = ChunkC:from_packet_data(j)
map:add_chunk(ch)
+ elseif op == "give" then
+ local old_count = local_player.inv[j.tile] or 0
+ local_player.inv[j.tile] = old_count + 1
elseif op == "settile" then
local h = coords.Hex:make(j.q,j.r)
map:set_at(h,j.tile)
@@ -241,10 +242,13 @@ local function update(dt)
local msx,msy = love.mouse.getPosition()
local mh =
camera:screen_to_world(Pos:make(msx,msy)):to_hex():round()
+ local inv = local_player.inv
if map:at(mh) == 0 and love.mouse.isDown(1) then
- if (local_player.inv[selected_tile] or 0) > 0 then
+ local old_count = inv[selected_tile] or 0
+ if old_count > 0 then
map:set_at(mh,selected_tile)
send_settile(mh,selected_tile)
+ inv[selected_tile] = old_count - 1
end
elseif map:at(mh) ~= 0 and love.mouse.isDown(2) then
map:set_at(mh,0)
@@ -350,7 +354,7 @@ local function draw()
end
table.insert(inv_rows, s)
end
- util.print_good(table.concat(inv_rows,'\n'),0,0)
+ util.print_good(table.concat(inv_rows,'\n'),"end",0)
if show_controls then
diff --git a/client/util.lua b/client/util.lua
index a1c4b35..ebfad6c 100644
--- a/client/util.lua
+++ b/client/util.lua
@@ -7,6 +7,8 @@ local function print_good(str,x,y)
local W,H = love.graphics.getDimensions()
if x == "center" then x = (W/2)-(w/2) end
if y == "center" then y = (H/2)-(h/2) end
+ if x == "end" then x = W-w end
+ if y == "end" then y = H-h end
love.graphics.setColor(0,0,0,0.8)
love.graphics.rectangle("fill",x,y,w,h)
love.graphics.setColor(1,1,1)
diff --git a/net.txt b/net.txt
index 21e46d0..bec4003 100644
--- a/net.txt
+++ b/net.txt
@@ -38,5 +38,5 @@ settile {q,r,tile}
chat {msg,from}
recieve chat message msg from player with name from.
-inv {inv={tile:count}}
- tell client about inventory of its player
+give {tile}
+ gain 1x tile in your inventory
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