summaryrefslogtreecommitdiff
path: root/server/chunk.lua
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-03-24 21:11:10 +0000
committerubq323 <ubq323@ubq323.website>2023-03-24 21:11:10 +0000
commite77609c5bc8b44aa22ef88063246fd05add5e705 (patch)
tree1829e2098e40f7fad82af8707bd8a359edd2be1d /server/chunk.lua
parent0927f9297c06525a453b4aad44fa4c2916c75906 (diff)
use lmdb for world storage; plus other small things
support numpad 8456 for movement in addition to wasd refactor server and add player module update outdated documentation slightly
Diffstat (limited to 'server/chunk.lua')
-rw-r--r--server/chunk.lua37
1 files changed, 24 insertions, 13 deletions
diff --git a/server/chunk.lua b/server/chunk.lua
index 8e71327..852f1fe 100644
--- a/server/chunk.lua
+++ b/server/chunk.lua
@@ -1,6 +1,7 @@
local class = require"common.class"
local Chunk = require"common.chunk".Chunk
local json = require"common.dkjson"
+local db = require'db'
local ChunkS = class.extend(Chunk)
function ChunkS.make(cls,...)
@@ -8,31 +9,41 @@ function ChunkS.make(cls,...)
self.dirty = false
return self
end
-function ChunkS.load_from_disk(cls,cp)
- -- returns nil if not there
- local filename = cp:filename()
- local f = io.open(filename,"r")
- if not f then return nil end
- local j = json.decode(f:read("a"))
- self = cls:from_packet_data(j)
+
+function ChunkS.apply_migrations(self)
+ -- if tile format has changed and format in db isn't up to date any more
+ -- then perform updates here
for i,t in ipairs(self.tiles) do
if t == false then self.tiles[i] = 0
elseif t == true then self.tiles[i] = 9 end
end
- f:close()
+end
+
+function ChunkS.load_from_disk(cls,cp)
+ -- tries to load from database. returns nil if not there.
+ local txn,dbi = db.get_db('chunks')
+ local d = dbi[tostring(cp)]
+ txn:commit()
+
+ if not d then return nil end
+ local j = json.decode(d)
+
+ local self = cls:from_packet_data(j)
+ self:apply_migrations()
+
return self
end
+
function ChunkS.save_if_dirty(self)
if self.dirty then
print("saving chunk",self.cp)
- local filename = self.cp:filename()
- local f = io.open(filename,"w")
- f:write(self:data_packet())
- f:flush()
- f:close()
+ local txn,dbi = db.get_db('chunks',true)
+ dbi[tostring(self.cp)] = self:data_packet()
+ txn:commit()
self.dirty = false
end
end
+
function ChunkS.set_at(self,...)
Chunk.set_at(self,...)
self.dirty = true