summaryrefslogtreecommitdiff
path: root/init.lua
diff options
context:
space:
mode:
authorWuzzy <almikes@aol.com>2014-07-14 00:43:16 +0200
committerWuzzy <almikes@aol.com>2014-07-14 00:43:16 +0200
commita91e4c79ce2db34f8bfcfd0717b02b54e10b9363 (patch)
tree95640e0a3d822b46947d2cf0cca717aca121a8c4 /init.lua
parent3f2a23889f5dbcb46bae7734bc51e8514a571feb (diff)
Add autosave functionality
Diffstat (limited to 'init.lua')
-rw-r--r--init.lua101
1 files changed, 62 insertions, 39 deletions
diff --git a/init.lua b/init.lua
index 4f113e7..421098b 100644
--- a/init.lua
+++ b/init.lua
@@ -25,13 +25,12 @@ dofile(minetest.get_modpath("playereffects").."/settings.lua")
do
local filepath = minetest.get_worldpath().."/playereffects.mt"
local file = io.open(filepath, "r")
- local string
if file then
minetest.log("action", "[playereffects] playereffects.mt opened.")
local string = file:read()
io.close(file)
if(string ~= nil) then
- savetable = minetest.deserialize(string)
+ local savetable = minetest.deserialize(string)
playereffects.inactive_effects = savetable.inactive_effects
minetest.debug("[playereffects] playereffects.mt successfully read.")
minetest.debug("[playereffects] inactive_effects = "..dump(playereffects.inactive_effects))
@@ -181,6 +180,51 @@ function playereffects.get_player_effects(playername)
end
end
+--[=[ Saving all data to file ]=]
+function playereffects.save_to_file()
+ local save_time = os.time()
+ local savetable = {}
+ local inactive_effects = {}
+ for id,effecttable in pairs(playereffects.inactive_effects) do
+ local playername = id
+ if(inactive_effects[playername] == nil) then
+ inactive_effects[playername] = {}
+ end
+ for i=1,#effecttable do
+ table.insert(inactive_effects[playername], effecttable[i])
+ end
+ end
+ for id,effect in pairs(playereffects.effects) do
+ local new_duration = effect.time_left - os.difftime(save_time, effect.start_time)
+ local new_effect = {
+ effect_id = effect.effect_id,
+ effect_type_id = effect.effect_type_id,
+ time_left = new_duration,
+ start_time = effect.start_time,
+ playername = effect.playername,
+ }
+ if(inactive_effects[effect.playername] == nil) then
+ inactive_effects[effect.playername] = {}
+ end
+ table.insert(inactive_effects[effect.playername], new_effect)
+ end
+
+ savetable.inactive_effects = inactive_effects
+ savetable.last_effect_id = playereffects.last_effect_id
+
+ local savestring = minetest.serialize(savetable)
+
+ local filepath = minetest.get_worldpath().."/playereffects.mt"
+ local file = io.open(filepath, "w")
+ if file then
+ file:write(savestring)
+ io.close(file)
+ minetest.log("action", "[playereffects] Wrote playereffects data into "..filepath..".")
+ else
+ minetest.log("error", "[playereffects] Failed to write playereffects data into "..filepath..".")
+ end
+end
+
--[=[ Callbacks ]=]
--[[ Cancel all effects on player death ]]
minetest.register_on_dieplayer(function(player)
@@ -212,36 +256,7 @@ end)
minetest.register_on_shutdown(function()
minetest.log("action", "[playereffects] Server shuts down. Rescuing data into playereffects.mt")
- local shutdown_time = os.time()
- local savetable = {}
- local effects = playereffects.effects
- local inactive_effects = playereffects.inactive_effects
- for id,effect in pairs(effects) do
- local new_duration = effect.time_left - os.difftime(shutdown_time, effect.start_time)
- local new_effect = effect
- new_effect.time_left = new_duration
- if(inactive_effects[effect.playername] == nil) then
- inactive_effects[effect.playername] = {}
- end
- table.insert(inactive_effects[effect.playername], new_effect)
- playereffects.cancel_effect(effect.effect_id)
- end
-
- savetable.inactive_effects = inactive_effects
- savetable.last_effect_id = playereffects.last_effect_id
-
- savestring = minetest.serialize(savetable)
-
- local filepath = minetest.get_worldpath().."/playereffects.mt"
- local file = io.open(filepath, "w")
- if file then
- file:write(savestring)
- io.close(file)
- minetest.log("action", "[playereffects] Wrote playereffects data into "..filepath..".")
- else
- minetest.log("error", "[playereffects] Failed to write playereffects data into "..filepath..".")
- end
-
+ playereffects.save_to_file()
end)
minetest.register_on_joinplayer(function(player)
@@ -258,16 +273,24 @@ minetest.register_on_joinplayer(function(player)
end)
playereffects.globalstep_timer = 0
+playereffects.autosave_timer = 0
minetest.register_globalstep(function(dtime)
playereffects.globalstep_timer = playereffects.globalstep_timer + dtime
- if(playereffects.globalstep_timer < 1) then
- return
+ playereffects.autosave_timer = playereffects.autosave_timer + dtime
+ -- Update HUDs of all players
+ if(playereffects.globalstep_timer >= 1) then
+ playereffects.globalstep_timer = 0
+
+ local players = minetest.get_connected_players()
+ for p=1,#players do
+ playereffects.hud_update(players[p])
+ end
end
- playereffects.globalstep_timer = 0
-
- local players = minetest.get_connected_players()
- for p=1,#players do
- playereffects.hud_update(players[p])
+ -- Autosave into file
+ if(playereffects.use_autosave == true and playereffects.autosave_timer >= playereffects.autosave_time) then
+ playereffects.autosave_timer = 0
+ minetest.log("action", "[playereffects] Autosaving mod data to playereffects.mt ...")
+ playereffects.save_to_file()
end
end)