diff options
author | RealBadAngel <maciej.kasatkin@o2.pl> | 2014-05-05 09:39:03 +0200 |
---|---|---|
committer | RealBadAngel <maciej.kasatkin@o2.pl> | 2014-05-05 09:39:03 +0200 |
commit | b9b2892f28a56f6d1e5d58058d7845f4002cd434 (patch) | |
tree | 3bdade05ce4923e61b413ba426b52026ce433b5f /datastorage.lua | |
parent | 79320c109c637378d89d2ea79d5e32d0db5a67d2 (diff) |
Add waypoints support (requires current minetest from git)
Diffstat (limited to 'datastorage.lua')
-rw-r--r-- | datastorage.lua | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/datastorage.lua b/datastorage.lua new file mode 100644 index 0000000..096e35e --- /dev/null +++ b/datastorage.lua @@ -0,0 +1,83 @@ +datastorage={} +datastorage["!registered_players"]={} + +datastorage.save_data = function(table_pointer) + local data = minetest.serialize( datastorage[table_pointer] ) + local path = minetest.get_worldpath().."/datastorage_"..table_pointer..".data" + local file = io.open( path, "w" ) + if( file ) then + file:write( data ) + file:close() + return true + else return nil + end +end + +datastorage.load_data = function(table_pointer) + local path = minetest.get_worldpath().."/datastorage_"..table_pointer..".data" + local file = io.open( path, "r" ) + if( file ) then + local data = file:read("*all") + datastorage[table_pointer] = minetest.deserialize( data ) + file:close() + return true + else return nil + end +end + +datastorage.get_container = function (player, key) + local player_name = player:get_player_name() + local container = datastorage[player_name] + if container[key] == nil then + container[key] = {} + end + datastorage.save_data(player_name) + return container[key] +end + +-- forced save of all player's data +datastorage.save_container = function (player) + local player_name = player:get_player_name() + datastorage.save_data(player_name) +end + + +-- Init +if datastorage.load_data("!registered_players") == nil then + datastorage["!registered_players"]={} + datastorage.save_data("!registered_players") +end + +minetest.register_on_joinplayer(function(player) + local player_name = player:get_player_name() + local registered = nil + for __,tab in ipairs(datastorage["!registered_players"]) do + if tab["player_name"] == player_name then registered = true break end + end + if registered == nil then + local new={} + new["player_name"]=player_name + table.insert(datastorage["!registered_players"],new) + datastorage[player_name]={} + datastorage.save_data("!registered_players") + datastorage.save_data(player_name) + else + datastorage.load_data(player_name) + end +end +) + +minetest.register_on_leaveplayer(function(player) + local player_name = player:get_player_name() + datastorage.save_data(player_name) + datastorage[player_name] = nil +end +) + +minetest.register_on_shutdown(function() + for __,tab in ipairs(datastorage["!registered_players"]) do + if datastorage[tab["player_name"]] == nil then break end + datastorage.save_data(tab["player_name"]) + end +end +) |