mirror of
https://repo.or.cz/minetest_playereffects.git
synced 2024-11-15 21:00:18 +01:00
Add autosave functionality
This commit is contained in:
parent
3f2a23889f
commit
a91e4c79ce
101
init.lua
101
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)
|
||||
|
||||
|
|
|
@ -4,3 +4,9 @@
|
|||
|
||||
-- Wheather to use the HUD to expose the active effects to players (true or false)
|
||||
playereffects.use_hud = true
|
||||
|
||||
-- Wheather to use autosave (true or false)
|
||||
playereffects.use_autosave = true
|
||||
|
||||
-- The time interval between autosaves, in seconds (only used when use_autosave is true)
|
||||
playereffects.autosave_time = 10
|
||||
|
|
Loading…
Reference in New Issue
Block a user