From e95fc908f3952737e41efce88c83ce7414e130b0 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 26 Aug 2023 08:22:06 +0100 Subject: add player inventories --- server/chunk.lua | 4 ++-- server/map.lua | 4 ++-- server/player.lua | 2 ++ server/server.lua | 18 ++++++++++++++++-- 4 files changed, 22 insertions(+), 6 deletions(-) (limited to 'server') diff --git a/server/chunk.lua b/server/chunk.lua index 20db704..da5b5b1 100644 --- a/server/chunk.lua +++ b/server/chunk.lua @@ -18,7 +18,7 @@ function ChunkS.apply_migrations(self) end end -function ChunkS.load_from_disk(cls,cp) +function ChunkS.invigorate(cls,cp) -- tries to load from database. returns nil if not there. local txn,dbi = db.get_db('chunks') local d = dbi[tostring(cp)] @@ -33,7 +33,7 @@ function ChunkS.load_from_disk(cls,cp) return self end -function ChunkS.save_if_dirty(self,txn) +function ChunkS.persist(self,txn) if self.dirty then print("saving chunk",self.cp) local dbi = txn:open'chunks' diff --git a/server/map.lua b/server/map.lua index e8be2ff..2775f7c 100644 --- a/server/map.lua +++ b/server/map.lua @@ -18,7 +18,7 @@ function MapS.obtain(self,cp) if ch then return ch else - ch = ChunkS:load_from_disk(cp) + ch = ChunkS:invigorate(cp) if not ch then ch = worldgen.gen_chunk(cp) end @@ -34,7 +34,7 @@ function MapS.save_chunk(self,cp,txn) local ch = self:chunk(cp) if not ch then return end - ch:save_if_dirty(txn) + ch:persist(txn) end return {MapS=MapS} diff --git a/server/player.lua b/server/player.lua index bd7b52a..105efff 100644 --- a/server/player.lua +++ b/server/player.lua @@ -15,6 +15,7 @@ function Player.make(cls,peer,username) peer = peer, id = nextid, username = username, + inv = {}, } nextid = nextid + 1 return setmetatable(self,cls) @@ -27,6 +28,7 @@ function Player.info_part(self) y=self.pos.y, color=self.color, username=self.username, + inv = self.inv, } end diff --git a/server/server.lua b/server/server.lua index 8a725af..3ec467c 100644 --- a/server/server.lua +++ b/server/server.lua @@ -48,6 +48,9 @@ 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} +end local map = MapS:make() @@ -128,8 +131,19 @@ local function handle_player_packet(player,ev) end elseif op == "settile" then local h = coords.Hex:make(j.q,j.r) - map:set_at(h,j.tile) - -- print(player.id,"settile",h,j.tile) + local old_tile = map:at(h) + local new_tile = j.tile + local inv = player.inv + + if old_tile == 0 then + inv[new_tile] = (inv[new_tile] or 0) - 1 + elseif new_tile == 0 then + inv[old_tile] = (inv[old_tile] or 0) + 1 + end + player.peer:send(inv_packet(inv)) + + map:set_at(h, new_tile) + for i,otherplayer in ipairs(playerlist) do if otherplayer ~= player then -- same packet structure s2c as c2s -- cgit v1.2.3