From 0dae99bacd70a9911d6a8192de62f2165329d6ce Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Tue, 14 Jul 2015 09:37:40 +0100 Subject: [PATCH] Only save when needed --- mods/capturetheflag/ctf/core.lua | 8 ++++++++ mods/capturetheflag/ctf/gui.lua | 6 +++--- mods/capturetheflag/ctf/teams.lua | 6 +++--- mods/capturetheflag/ctf_chat/init.lua | 4 ++-- mods/capturetheflag/ctf_flag/flag_func.lua | 6 +++--- mods/capturetheflag/ctf_flag/init.lua | 4 ++-- 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/mods/capturetheflag/ctf/core.lua b/mods/capturetheflag/ctf/core.lua index e9a4a82..cc1ab6d 100644 --- a/mods/capturetheflag/ctf/core.lua +++ b/mods/capturetheflag/ctf/core.lua @@ -167,6 +167,14 @@ minetest.after(0, function() ctf._mt_loaded = true end) +function ctf.check_save() + if ctf.needs_save then + ctf.save() + end + minetest.after(10, ctf.check_save) +end +minetest.after(10, ctf.check_save) + function ctf.save() ctf.log("io", "Saving CTF state...") local file = io.open(minetest.get_worldpath().."/ctf.txt", "w") diff --git a/mods/capturetheflag/ctf/gui.lua b/mods/capturetheflag/ctf/gui.lua index 1533e9a..5e21fc7 100644 --- a/mods/capturetheflag/ctf/gui.lua +++ b/mods/capturetheflag/ctf/gui.lua @@ -232,7 +232,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) if fields.clear then if ctf and ctf.players and ctf.players[name] and ctf.players[name].team then ctf.team(ctf.players[name].team).log = {} - ctf.save() + ctf.needs_save = true ctf.gui.show(name, "news") end return true @@ -246,7 +246,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) if ctf and ctf.team(ctf.players[name].team) and ctf.team(ctf.players[name].team).data then if ctf.flag_colors[fields.color] then ctf.team(ctf.players[name].team).data.color = fields.color - ctf.save() + ctf.needs_save = true else local colors = "" for color, code in pairs(ctf.flag_colors) do @@ -278,7 +278,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) end table.remove(ctf.team(ctf.player(name).team).log,id) - ctf.save() + ctf.needs_save = true ctf.gui.show(name, "news") return true end diff --git a/mods/capturetheflag/ctf/teams.lua b/mods/capturetheflag/ctf/teams.lua index 3c48544..34db4db 100644 --- a/mods/capturetheflag/ctf/teams.lua +++ b/mods/capturetheflag/ctf/teams.lua @@ -18,7 +18,7 @@ function ctf.team(name) ctf.registered_on_new_team[i](ctf.teams[name.name]) end - ctf.save() + ctf.needs_save = true return ctf.teams[name.name] else @@ -145,7 +145,7 @@ function ctf.add_user(team, user) user.auth = false _team.players[user.name] = user ctf.players[user.name] = user - ctf.save() + ctf.needs_save = true return true else @@ -198,7 +198,7 @@ function ctf.post(team, msg) ctf.log("team", "message posted to team board") table.insert(ctf.team(team).log, 1, msg) - ctf.save() + ctf.needs_save = true return true end diff --git a/mods/capturetheflag/ctf_chat/init.lua b/mods/capturetheflag/ctf_chat/init.lua index 6af6004..5abdd12 100644 --- a/mods/capturetheflag/ctf_chat/init.lua +++ b/mods/capturetheflag/ctf_chat/init.lua @@ -140,7 +140,7 @@ minetest.register_chatcommand("ctf_reload", { description = "reload the ctf main frame and get settings", privs = {ctf_admin=true}, func = function(name, param) - ctf.save() + ctf.needs_save = true ctf.init() minetest.chat_send_player(name, "CTF core reloaded!") end @@ -159,7 +159,7 @@ minetest.register_chatcommand("team_owner", { ctf.player(param).auth = true minetest.chat_send_player(name, param.." was upgraded to an admin of "..ctf.player(name).team,false) end - ctf.save() + ctf.needs_save = true else minetest.chat_send_player(name, "Unable to do that :/ "..param.." does not exist, or is not part of a valid team.",false) end diff --git a/mods/capturetheflag/ctf_flag/flag_func.lua b/mods/capturetheflag/ctf_flag/flag_func.lua index 54e2d7c..d44f7c2 100644 --- a/mods/capturetheflag/ctf_flag/flag_func.lua +++ b/mods/capturetheflag/ctf_flag/flag_func.lua @@ -68,7 +68,7 @@ local function do_capture(attname, flag, returned) end ctf_flag.collect_claimed() - ctf.save() + ctf.needs_save = true end minetest.register_on_dieplayer(function(player) @@ -216,7 +216,7 @@ ctf_flag = { ctf.get_spawn(team) end - ctf.save() + ctf.needs_save = true local pos2 = { x = pos.x, @@ -226,7 +226,7 @@ ctf_flag = { if not ctf.team(team).data.color then ctf.team(team).data.color = "red" - ctf.save() + ctf.needs_save = true end minetest.env:set_node(pos2, {name="ctf_flag:flag_top_"..ctf.team(team).data.color}) diff --git a/mods/capturetheflag/ctf_flag/init.lua b/mods/capturetheflag/ctf_flag/init.lua index f114f0a..3ff9159 100644 --- a/mods/capturetheflag/ctf_flag/init.lua +++ b/mods/capturetheflag/ctf_flag/init.lua @@ -98,7 +98,7 @@ function ctf_flag.add(team, pos) pos.team = team table.insert(ctf.team(team).flags,pos) - ctf.save() + ctf.needs_save = true end -- get a flag from a team @@ -279,7 +279,7 @@ minetest.register_abm({ if not ctf.team(flag_team_data.team).data.color then ctf.team(flag_team_data.team).data.color = "red" - ctf.save() + ctf.needs_save = true end if flag_team_data.claimed then