Begin Forming the Hidden System code-base

master
ExeVirus 2021-10-17 21:45:42 -04:00
parent 275f76e859
commit 012086903d
3 changed files with 137 additions and 26 deletions

View File

@ -62,24 +62,20 @@ game_doc.get_doc_data()
-- }
--
-- Note the hidden values are just the starting values for new players, the player hidden entry/category data
-- is held in this function:
game_doc.get_hidden_data()
game_doc.get_hidden_data
-- Returns a table
-- {
-- player_name = {
-- category_name { hidden=false/true, entries { entry1 { hidden=false/true }, entry2 {hidden=false/true } }
-- category_name { hidden=false/true, entries { entry1 { hidden=false/true }, entry2 {hidden=false/true } }
-- etc.
-- }
-- player_name = {
-- category_name { hidden=false/true, entries { entry1 { hidden=false/true }, entry2 {hidden=false/true } }
-- category_name { hidden=false/true, entries { entry1 { hidden=false/true }, entry2 {hidden=false/true } }
-- etc.
-- }
-- etc.
-- {
-- -- hidden_values actually stores a table of **shown** categories and entries
-- hidden_values { categories {cat1, cat3},
-- entries[cat1] = { entry2, entry3 },
-- entries[cat2] = { entry1, entry3 },
-- entries[cat3] = { entry1, entry2, entry3 }, --possibly entire table doesn't exist
-- selected_category = cat1 --possibly doesn't exist
-- selected_entry = ent1 --possibly doesn't exist
-- }
--
game_doc.get_doc_data()
```
## Settings

View File

@ -6,7 +6,7 @@
-- for accessing game_doc data. These are: --
-- --
-- <> game_doc.get_doc_data() --
-- <> game_doc.get_hidden_data() --
-- <> game_doc.get_hidden_data(player_name) --
-- --
-- Use the above functions to access and possibly make --
-- changes to the backend data in game_doc --
@ -16,8 +16,8 @@ game_doc.get_doc_data = function()
return game_doc.doc_data
end
game_doc.get_hidden_data = function()
return game_doc.player_data
game_doc.get_hidden_data = function(player_name)
return game_doc.player_data[player_name] --can be nil
end
-- Simple, right?

View File

@ -28,21 +28,136 @@
-- returned, the old player value will be remembered. --
--------------------------------------------------------------
--------------------------
--
-- load_player
--
-- Load the
--
-- Reused Variables
local world_path = minetest.get_worldpath()
--------------------------
--
-- on_joinplayer
-- load_from_json(file_name)
--
-- Loads a table from a .json file (if present) in the world
-- folder
--
game_doc.load_from_json = function(file_name)
local data = nil
local f = io.open(world_path .. "/game_doc/" .. file_name .. ".json", "rb")
if f ~= nil then
local file_contents = f:read("*all")
data = minetest.parse_json(file_contents)
f:close()
end
return data
end
--------------------------
--
-- save_to_json(file_name, table)
--
-- Save a provided table to a .json file in the world
-- folder
--
game_doc.save_to_json = function(file_name, table)
local f = io.open(world_path .. "/game_doc/" .. file_name .. ".json", "wb")
if f ~= nil and type(table) == "table" then
f:write(minetest.write_json(table))
f:close()
else
minetest.log("warning", "Unable to save json in world folder (game_doc.save_json() in player_management.lua")
end
end
-- At load time we have to do several steps
minetest.register_on_mods_loaded(
function()
--Generate the default hidden table
game_doc.build_hidden_defaults()
--Load the old defaults table if applicable
local old_table = game_doc.load_from_json("hidden.defaults.json")
if old_table then
--Get a diff between the old table and the current
local diff = game_doc.hidden_table_diff(old_table) --diff only contains a list of categories that are changed, and a list of entries that are changed
if diff then
-- Adjust *every* player file
game_doc.fix_all_players_defaults(diff)
end
end
--Save the new defaults
game_doc.save_to_json("hidden.defaults.json", game_doc.hidden_default)
end
)
--------------------------
--
-- build_hidden_defaults()
--
-- Generates the default hidden values for
-- all categories and entries
--
game_doc.build_hidden_defaults = function()
game_doc.hidden_default = {}
local hd = game_doc.hidden_default
hd.shown_categories = {}
hd.hidden_categories = {}
hd.categories = {}
for k,v in pairs(game_doc.doc_data) do
if v.hidden then
table.insert(hd.hidden_categories,k)
else
table.insert(hd.shown_categories,k)
end
hd.categories[k] = {}
local category = hd.categories[k]
category.hidden_entries = {}
category.shown_entries = {}
for _k, _v in pairs(v.entries) do
if _v.hidden then
table.insert(category.hidden_entries,_k)
else
table.insert(category.shown_entries,_k)
end
end
end
end
--------------------------
--
-- on_joinplayer()
--
-- handles creating and loading a player's stored data
--
minetest.register_on_joinplayer(
function(player, last_login)
local player_name = player:get_player_name()
game_doc.player_data[player_name] = {}
local loaded_data = game_doc.load_from_json(player_name..".json")
game_doc.setup_player_hidden_values(player_name, loaded_data) -- default + player effects (if not nil)
end)
--------------------------
--
-- on_leaveplayer()
--
-- handles setting player's data to nil
--
minetest.register_on_leaveplayer(
function(player, timed_out)
local player_name = player:get_player_name()
game_doc.player_data[player_name] = nil
end)
--------------------------
--
-- handle_modification(player_name,category,entry,show/hide) {true/false}
--
-- handles changes to hidden values for a player's categories or entry
--
game_doc.handle_modification = function (player_name, category, entry, show)
end