1
0
mirror of https://gitlab.com/rubenwardy/awards.git synced 2024-12-25 18:20:40 +01:00

Add serialization

This commit is contained in:
Rubenwardy 2013-02-22 19:57:24 +00:00
parent 2942ae602b
commit aef2e2ae91
2 changed files with 21 additions and 16 deletions

View File

@ -4,27 +4,17 @@
-- this is the api definition file for the awards mod -- this is the api definition file for the awards mod
------------------------------------------------------- -------------------------------------------------------
-- The global award namespace
awards={}
player_data={}
-- A table of award definitions
awards.def={}
-- Load files
dofile(minetest.get_modpath("awards").."/triggers.lua")
-- Table Save Load Functions -- Table Save Load Functions
local function save(table, file) local function save_playerD()
local file = io:open(file, "w") local file = io.open(minetest.get_worldpath().."/awards.txt", "w")
if file then if file then
file:write(minetest.serialize(table)) file:write(minetest.serialize(player_data))
file:close() file:close()
end end
end end
local function load(file) local function load_playerD()
local file = io:open(file, "r") local file = io.open(minetest.get_worldpath().."/awards.txt", "r")
if file then if file then
local table = minetest.deserialize(file:read("*all")) local table = minetest.deserialize(file:read("*all"))
if type(table) == "table" then if type(table) == "table" then
@ -34,6 +24,15 @@ local function load(file)
return {} return {}
end end
-- The global award namespace
awards={}
player_data=load_playerD()
-- A table of award definitions
awards.def={}
-- Load files
dofile(minetest.get_modpath("awards").."/triggers.lua")
-- API Functions -- API Functions
function awards.register_achievement(name,data_table) function awards.register_achievement(name,data_table)
@ -55,6 +54,8 @@ function awards.give_achievement(name,award)
if not data['unlocked'][award] or data['unlocked'][award]~=award then if not data['unlocked'][award] or data['unlocked'][award]~=award then
data['unlocked'][award]=award data['unlocked'][award]=award
minetest.chat_send_player(name, "Achievement Unlocked: "..award) minetest.chat_send_player(name, "Achievement Unlocked: "..award)
save_playerD()
end end
end end

View File

@ -68,3 +68,7 @@ minetest.register_on_newplayer(function(player)
--Table to contain achievement records --Table to contain achievement records
player_data[player:get_player_name()]['unlocked']={} player_data[player:get_player_name()]['unlocked']={}
end) end)
minetest.register_on_shutdown(function()
save_playerD()
end)