summaryrefslogtreecommitdiff
path: root/init.lua
diff options
context:
space:
mode:
authorWuzzy <almikes@aol.com>2016-08-31 22:36:06 +0200
committerWuzzy <almikes@aol.com>2016-08-31 22:36:06 +0200
commitc55539578c14660164d55fa8c409ab4b298ecd28 (patch)
treefb240ce6e2567fd718938c2f7af1414b46855ff0 /init.lua
parentfa998da3c9dcfc9f3eba2ce4af96623a74503314 (diff)
Update viewed and revealed counters on join
Diffstat (limited to 'init.lua')
-rw-r--r--init.lua37
1 files changed, 31 insertions, 6 deletions
diff --git a/init.lua b/init.lua
index 84972f3..9ff9c2e 100644
--- a/init.lua
+++ b/init.lua
@@ -682,19 +682,44 @@ minetest.register_chatcommand("doc", {
minetest.register_on_joinplayer(function(player)
local playername = player:get_player_name()
- if doc.data.players[playername] == nil then
+ local playerdata = doc.data.players[playername]
+ if playerdata == nil then
-- Initialize player data
doc.data.players[playername] = {}
+ playerdata = doc.data.players[playername]
-- Table for persistant data
- doc.data.players[playername].stored_data = {}
+ playerdata.stored_data = {}
-- Contains viewed entries
- doc.data.players[playername].stored_data.viewed = {}
+ playerdata.stored_data.viewed = {}
-- Count viewed entries
- doc.data.players[playername].stored_data.viewed_count = {}
+ playerdata.stored_data.viewed_count = {}
-- Contains revealed/unhidden entries
- doc.data.players[playername].stored_data.revealed = {}
+ playerdata.stored_data.revealed = {}
-- Count revealed entries
- doc.data.players[playername].stored_data.revealed_count = {}
+ playerdata.stored_data.revealed_count = {}
+ else
+ -- Completely rebuild viewed and revealed counts from scratch
+ for cid, cat in pairs(doc.data.categories) do
+ if playerdata.stored_data.viewed[cid] == nil then
+ playerdata.stored_data.viewed[cid] = {}
+ end
+ if playerdata.stored_data.revealed[cid] == nil then
+ playerdata.stored_data.revealed[cid] = {}
+ end
+ local vc = 0
+ local rc = doc.get_entry_count(cid) - doc.data.categories[cid].hidden_count
+ for eid, entry in pairs(cat.entries) do
+ if playerdata.stored_data.viewed[cid][eid] then
+ vc = vc + 1
+ playerdata.stored_data.revealed[cid][eid] = true
+ end
+ if playerdata.stored_data.revealed[cid][eid] and entry.hidden then
+ rc = rc + 1
+ end
+ end
+ playerdata.stored_data.viewed_count[cid] = vc
+ playerdata.stored_data.revealed_count[cid] = rc
+ end
end
end)