Remove saving from ctf_pvp_engine

This commit is contained in:
rubenwardy 2019-08-28 17:05:59 +01:00
parent a735266eb3
commit e53732f41e
16 changed files with 31 additions and 349 deletions

View file

@ -95,7 +95,6 @@ ctf.register_on_killedplayer(function(victim, killer)
match.score = match.score + bounty_score match.score = match.score + bounty_score
main.bounty_kills = main.bounty_kills + 1 main.bounty_kills = main.bounty_kills + 1
match.bounty_kills = match.bounty_kills + 1 match.bounty_kills = match.bounty_kills + 1
ctf.needs_save = true
end end
bountied_player = nil bountied_player = nil

View file

@ -1 +0,0 @@
ctf

View file

@ -1,5 +0,0 @@
ctf.save = function()
for i = 1, #ctf.registered_on_save do
ctf.registered_on_save[i]()
end
end

View file

@ -359,7 +359,6 @@ minetest.register_chatcommand("reset_rankings", {
ctf_stats.players[reset_name] = nil ctf_stats.players[reset_name] = nil
ctf_stats.player(reset_name) ctf_stats.player(reset_name)
ctf.needs_save = true
return true, "Successfully reset the stats and ranking of " .. reset_name return true, "Successfully reset the stats and ranking of " .. reset_name
end end
}) })
@ -386,7 +385,6 @@ minetest.register_chatcommand("transfer_rankings", {
ctf_stats.players[dest] = ctf_stats.players[src] ctf_stats.players[dest] = ctf_stats.players[src]
ctf_stats.players[src] = nil ctf_stats.players[src] = nil
ctf.needs_save = true
return true, "Stats of '" .. src .. "' have been transferred to '" .. dest .. "'." return true, "Stats of '" .. src .. "' have been transferred to '" .. dest .. "'."
end end

View file

@ -1,5 +1,6 @@
ctf_stats = {} ctf_stats = {}
local _needs_save = false
local storage = minetest.get_mod_storage() local storage = minetest.get_mod_storage()
local prev_match_summary = storage:get_string("prev_match_summary") local prev_match_summary = storage:get_string("prev_match_summary")
local data_to_persist = { "matches", "players" } local data_to_persist = { "matches", "players" }
@ -53,7 +54,7 @@ function ctf_stats.load_legacy()
blue = ctf_stats.matches.blue_wins or 0, blue = ctf_stats.matches.blue_wins or 0,
} }
ctf.needs_save = true _needs_save = true
os.remove(minetest.get_worldpath() .. "/ctf_stats.txt") os.remove(minetest.get_worldpath() .. "/ctf_stats.txt")
return true return true
@ -64,7 +65,7 @@ function ctf_stats.load()
for _, key in pairs(data_to_persist) do for _, key in pairs(data_to_persist) do
ctf_stats[key] = minetest.parse_json(storage:get_string(key)) ctf_stats[key] = minetest.parse_json(storage:get_string(key))
end end
ctf.needs_save = true _needs_save = true
end end
-- Make sure all tables are present -- Make sure all tables are present
@ -87,20 +88,27 @@ function ctf_stats.load()
for name, player_stats in pairs(ctf_stats.players) do for name, player_stats in pairs(ctf_stats.players) do
if not player_stats.score or player_stats.score <= 0 then if not player_stats.score or player_stats.score <= 0 then
ctf_stats.players[name] = nil ctf_stats.players[name] = nil
ctf.needs_save = true _needs_save = true
else else
player_stats.bounty_kills = player_stats.bounty_kills or 0 player_stats.bounty_kills = player_stats.bounty_kills or 0
end end
end end
end end
ctf.register_on_save(function() function ctf_stats.save()
if not _needs_save then
return
end
_needs_save = false
for _, key in pairs(data_to_persist) do for _, key in pairs(data_to_persist) do
storage:set_string(key, minetest.write_json(ctf_stats[key])) storage:set_string(key, minetest.write_json(ctf_stats[key]))
end end
return nil minetest.after(13, ctf_stats.save)
end) end
minetest.after(13, ctf_stats.save)
function ctf_stats.player_or_nil(name) function ctf_stats.player_or_nil(name)
return ctf_stats.players[name], ctf_stats.current.red[name] or ctf_stats.current.blue[name] return ctf_stats.players[name], ctf_stats.current.red[name] or ctf_stats.current.blue[name]
@ -154,7 +162,7 @@ table.insert(ctf_flag.registered_on_capture, 1, function(name, flag)
main.score = main.score + 25 main.score = main.score + 25
match.captures = match.captures + 1 match.captures = match.captures + 1
match.score = match.score + 25 match.score = match.score + 25
ctf.needs_save = true _needs_save = true
end end
winner_player = name winner_player = name
@ -166,7 +174,7 @@ table.insert(ctf_flag.registered_on_capture, 1, function(name, flag)
end) end)
ctf_match.register_on_winner(function(winner) ctf_match.register_on_winner(function(winner)
ctf.needs_save = true _needs_save = true
ctf_stats.matches.wins[winner] = ctf_stats.matches.wins[winner] + 1 ctf_stats.matches.wins[winner] = ctf_stats.matches.wins[winner] + 1
winner_team = winner winner_team = winner
@ -183,7 +191,7 @@ ctf_match.register_on_winner(function(winner)
end) end)
ctf_match.register_on_skip_map(function() ctf_match.register_on_skip_map(function()
ctf.needs_save = true _needs_save = true
ctf_stats.matches.skipped = ctf_stats.matches.skipped + 1 ctf_stats.matches.skipped = ctf_stats.matches.skipped + 1
-- Show match summary -- Show match summary
@ -206,7 +214,7 @@ ctf_match.register_on_new_match(function()
winner_team = "-" winner_team = "-"
winner_player = "-" winner_player = "-"
ctf_stats.start = os.time() ctf_stats.start = os.time()
ctf.needs_save = true _needs_save = true
end) end)
ctf_flag.register_on_pick_up(function(name, flag) ctf_flag.register_on_pick_up(function(name, flag)
@ -216,7 +224,7 @@ ctf_flag.register_on_pick_up(function(name, flag)
main.score = main.score + 10 main.score = main.score + 10
match.attempts = match.attempts + 1 match.attempts = match.attempts + 1
match.score = match.score + 10 match.score = match.score + 10
ctf.needs_save = true _needs_save = true
end end
hud_score.new(name, { hud_score.new(name, {
@ -231,7 +239,7 @@ ctf_flag.register_on_precapture(function(name, flag)
local main, _ = ctf_stats.player(name) local main, _ = ctf_stats.player(name)
if main then if main then
main.wins[tplayer.team] = main.wins[tplayer.team] + 1 main.wins[tplayer.team] = main.wins[tplayer.team] + 1
ctf.needs_save = true _needs_save = true
end end
return true return true
end) end)
@ -313,7 +321,7 @@ ctf.register_on_killedplayer(function(victim, killer)
match.kills = match.kills + 1 match.kills = match.kills + 1
match.score = match.score + reward match.score = match.score + reward
match.kills_since_death = match.kills_since_death + 1 match.kills_since_death = match.kills_since_death + 1
ctf.needs_save = true _needs_save = true
reward = math.floor(reward * 100) / 100 reward = math.floor(reward * 100) / 100
@ -331,7 +339,7 @@ minetest.register_on_dieplayer(function(player)
main.deaths = main.deaths + 1 main.deaths = main.deaths + 1
match.deaths = match.deaths + 1 match.deaths = match.deaths + 1
match.kills_since_death = 0 match.kills_since_death = 0
ctf.needs_save = true _needs_save = true
end end
end) end)

View file

@ -53,13 +53,6 @@ function ctf.register_on_load(func)
func(ctf._loaddata) func(ctf._loaddata)
end end
end end
ctf.registered_on_save = {}
function ctf.register_on_save(func)
if ctf._mt_loaded then
error("You can't register callbacks at game time!")
end
table.insert(ctf.registered_on_save, func)
end
ctf.registered_on_init = {} ctf.registered_on_init = {}
function ctf.register_on_init(func) function ctf.register_on_init(func)
if ctf._mt_loaded then if ctf._mt_loaded then
@ -203,67 +196,8 @@ function ctf.setting(name)
end end
function ctf.load() function ctf.load()
ctf.log("io", "Loading CTF state") ctf._new_game = true
local file = io.open(minetest.get_worldpath().."/ctf.txt", "r") for i = 1, #ctf.registered_on_new_game do
if file then ctf.registered_on_new_game[i]()
local table = minetest.deserialize(file:read("*all"))
if type(table) == "table" then
ctf.teams = table.teams
ctf.players = table.players
for i = 1, #ctf.registered_on_load do
ctf.registered_on_load[i](table)
end
return
end
ctf._loaddata = table
else
ctf.log("io", "ctf.txt is not present in the world folder")
ctf._new_game = true
for i = 1, #ctf.registered_on_new_game do
ctf.registered_on_new_game[i]()
end
end
end
minetest.after(0, function()
ctf._loaddata = nil
ctf._mt_loaded = true
end)
function ctf.check_save()
if ctf_flag and ctf_flag.assert_flags then
ctf_flag.assert_flags()
end
if ctf.needs_save then
ctf.save()
end
minetest.after(10, ctf.check_save)
end
minetest.after(10, ctf.check_save)
function ctf.save()
local file = io.open(minetest.get_worldpath().."/ctf.txt", "w")
if file then
local out = {
teams = ctf.teams,
players = ctf.players
}
for i = 1, #ctf.registered_on_save do
local res = ctf.registered_on_save[i]()
if res then
for key, value in pairs(res) do
out[key] = value
end
end
end
file:write(minetest.serialize(out))
file:close()
ctf.needs_save = false
else
ctf.error("io", "CTF file failed to save!")
end end
end end

View file

@ -7,77 +7,6 @@ ctf.register_on_load(function(table)
ctf.diplo.diplo = table.diplo ctf.diplo.diplo = table.diplo
end) end)
ctf.register_on_save(function()
return { diplo = ctf.diplo.diplo }
end)
function ctf.diplo.get(one,two) function ctf.diplo.get(one,two)
if not ctf.diplo.diplo then return "war"
return ctf.setting("default_diplo_state")
end
for i = 1, #ctf.diplo.diplo do
local dip = ctf.diplo.diplo[i]
if (dip.one == one and dip.two == two) or
(dip.one == two and dip.two == one) then
return dip.state
end
end
return ctf.setting("default_diplo_state")
end
function ctf.diplo.set(one, two, state)
if ctf.diplo.diplo then
-- Check the table for an existing diplo state
for i = 1, #ctf.diplo.diplo do
local dip = ctf.diplo.diplo[i]
if (dip.one == one and dip.two == two) or
(dip.one == two and dip.two == one) then
dip.state = state
return
end
end
else
ctf.diplo.diplo = {}
end
table.insert(ctf.diplo.diplo,{one=one,two=two,state=state})
end
function ctf.diplo.check_requests(one, two)
local team = ctf.team(two)
if not team.log then
return nil
end
for i=1,#team.log do
if team.log[i].team == one and
team.log[i].type == "request" and
team.log[i].mode == "diplo" then
return team.log[i].msg
end
end
return nil
end
function ctf.diplo.cancel_requests(one, two)
local team = ctf.team(two)
if not team.log then
return
end
for i=1,#team.log do
if team.log[i].team == one and
team.log[i].type == "request" and
team.log[i].mode == "diplo" then
table.remove(team.log,i)
return
end
end
return
end end

View file

@ -131,73 +131,6 @@ ctf.gui.register_tab("news", "News", function(name, tname)
result) result)
end) end)
-- Team interface
ctf.gui.register_tab("diplo", "Diplomacy", function(name, tname)
local result = ""
local data = {}
local amount = 0
for key, value in pairs(ctf.teams) do
if key ~= tname then
table.insert(data,{
team = key,
state = ctf.diplo.get(tname, key),
to = ctf.diplo.check_requests(tname, key),
from = ctf.diplo.check_requests(key, tname)
})
end
end
result = result .. "label[1,1;Diplomacy from the perspective of " .. tname .. "]"
for i = 1, #data do
amount = i
local height = (i*1)+0.5
if height > 5 then
break
end
result = result .. "background[1," .. height .. ";8.2,1;diplo_" ..
data[i].state .. ".png]"
result = result .. "button[1.25," .. height .. ";2,1;team_" ..
data[i].team .. ";" .. data[i].team .. "]"
result = result .. "label[3.75," .. height .. ";" .. data[i].state
.. "]"
if ctf.can_mod(name, tname) and ctf.player(name).team == tname then
if not data[i].from and not data[i].to then
if data[i].state == "war" then
result = result .. "button[7.5," .. height ..
";1.5,1;peace_" .. data[i].team .. ";Peace]"
elseif data[i].state == "peace" then
result = result .. "button[6," .. height ..
";1.5,1;war_" .. data[i].team .. ";War]"
result = result .. "button[7.5," .. height ..
";1.5,1;alli_" .. data[i].team .. ";Alliance]"
elseif data[i].state == "alliance" then
result = result .. "button[6," .. height ..
";1.5,1;peace_" .. data[i].team .. ";Peace]"
end
elseif data[i].from ~= nil then
result = result .. "label[6," .. height ..
";request recieved]"
elseif data[i].to ~= nil then
result = result .. "label[5.5," .. height ..
";request sent]"
result = result .. "button[7.5," .. height ..
";1.5,1;cancel_" .. data[i].team .. ";Cancel]"
end
end
end
minetest.show_formspec(name, "ctf:diplo",
"size[10,7]" ..
ctf.gui.get_tabs(name, tname) ..
result
)
end)
local function formspec_is_ctf_tab(fsname) local function formspec_is_ctf_tab(fsname)
for name, tab in pairs(ctf.gui.tabs) do for name, tab in pairs(ctf.gui.tabs) do
@ -234,7 +167,6 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
-- News page -- News page
if fields.clear then if fields.clear then
team.log = {} team.log = {}
ctf.needs_save = true
ctf.gui.show(name, "news") ctf.gui.show(name, "news")
return true return true
end end
@ -272,82 +204,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end end
table.remove(team.log, id) table.remove(team.log, id)
ctf.needs_save = true
ctf.gui.show(name, "news") ctf.gui.show(name, "news")
return true return true
end end
end end
end end
end) end)
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
local tplayer = ctf.player(name)
local tname = tplayer.team
local team = ctf.team(tname)
if not team or formname ~= "ctf:diplo" then
return false
end
for key, field in pairs(fields) do
local tname2 = string.match(key, "team_(.+)")
if tname2 and ctf.team(tname2) then
ctf.gui.show(name, "diplo", tname2)
return true
end
if ctf.can_mod(name, tname) then
tname2 = string.match(key, "peace_(.+)")
if tname2 then
if ctf.diplo.get(tname, tname2) == "war" then
ctf.post(tname2, {
type = "request",
msg = "peace",
team = tname,
mode = "diplo" })
else
ctf.diplo.set(tname, tname2, "peace")
ctf.post(tname, {
msg = "You have cancelled the alliance treaty with " .. tname2 })
ctf.post(tname2, {
msg = tname .. " has cancelled the alliance treaty" })
end
ctf.gui.show(name, "diplo")
return true
end
tname2 = string.match(key, "war_(.+)")
if tname2 then
ctf.diplo.set(tname, tname2, "war")
ctf.post(tname, {
msg = "You have declared war on " .. tname2 })
ctf.post(tname2, {
msg = tname .. " has declared war on you" })
ctf.gui.show(name, "diplo")
return true
end
tname2 = string.match(key, "alli_(.+)")
if tname2 then
ctf.post(tname2, {
type = "request",
msg = "alliance",
team = tname,
mode = "diplo" })
ctf.gui.show(name, "diplo")
return true
end
tname2 = string.match(key, "cancel_(.+)")
if tname2 then
ctf.diplo.cancel_requests(tname, tname2)
ctf.gui.show(name, "diplo")
return true
end
end -- end if can mod
end -- end for each field
end)

View file

@ -4,14 +4,6 @@
ctf = {} ctf = {}
-- Fix for https://github.com/minetest/minetest/issues/2383
local csa = minetest.chat_send_all
function minetest.chat_send_all(msg)
minetest.after(0, function()
csa(msg)
end)
end
-- Privs -- Privs
minetest.register_privilege("ctf_team_mgr", { minetest.register_privilege("ctf_team_mgr", {
description = "Team manager", description = "Team manager",

View file

@ -40,8 +40,6 @@ function ctf.create_team(name, data)
ctf.registered_on_new_team[i](ctf.teams[name]) ctf.registered_on_new_team[i](ctf.teams[name])
end end
ctf.needs_save = true
return ctf.teams[name] return ctf.teams[name]
end end
@ -55,7 +53,6 @@ function ctf.remove_team(name)
team.flags[i].team = nil team.flags[i].team = nil
end end
ctf.teams[name] = nil ctf.teams[name] = nil
ctf.needs_save = true
return true return true
else else
return false return false
@ -99,7 +96,6 @@ function ctf.new_player(name)
ctf.players[name] = { ctf.players[name] = {
name = name name = name
} }
ctf.needs_save = true
else else
ctf.error("team", "Can't create a blank player") ctf.error("team", "Can't create a blank player")
ctf.log("team", debug.traceback()) ctf.log("team", debug.traceback())
@ -137,7 +133,6 @@ function ctf.remove_player(name)
team.players[name] = nil team.players[name] = nil
end end
ctf.players[name] = nil ctf.players[name] = nil
ctf.needs_save = true
return true return true
else else
return false return false
@ -210,8 +205,6 @@ function ctf.join(name, team, force, by)
team_data.players[player.name] = player team_data.players[player.name] = player
ctf.player_last_team[name] = team ctf.player_last_team[name] = team
ctf.needs_save = true
minetest.log("action", name .. " joined team " .. team) minetest.log("action", name .. " joined team " .. team)
minetest.chat_send_all(name.." has joined team "..team) minetest.chat_send_all(name.." has joined team "..team)
@ -232,8 +225,6 @@ function ctf.clean_player_lists()
ctf.log("utils", " - Skipping player "..str.name) ctf.log("utils", " - Skipping player "..str.name)
end end
end end
ctf.needs_save = true
end end
-- Sees if the player can change stuff in a team -- Sees if the player can change stuff in a team
@ -268,7 +259,6 @@ function ctf.post(team, msg)
ctf.log("team", "message posted to team board") ctf.log("team", "message posted to team board")
table.insert(ctf.team(team).log, 1, msg) table.insert(ctf.team(team).log, 1, msg)
ctf.needs_save = true
return true return true
end end

View file

@ -242,7 +242,6 @@ minetest.register_chatcommand("ctf_reload", {
description = "reload the ctf main frame and get settings", description = "reload the ctf main frame and get settings",
privs = {ctf_admin=true}, privs = {ctf_admin=true},
func = function(name, param) func = function(name, param)
ctf.needs_save = true
ctf.init() ctf.init()
return true, "CTF core reloaded!" return true, "CTF core reloaded!"
end end
@ -274,7 +273,6 @@ minetest.register_chatcommand("team_owner", {
ctf.player(param).auth = true ctf.player(param).auth = true
return true, param.." was upgraded to an admin of "..ctf.player(name).team return true, param.." was upgraded to an admin of "..ctf.player(name).team
end end
ctf.needs_save = true
else else
return false, "Unable to do that :/ "..param.." does not exist, or is not part of a valid team." return false, "Unable to do that :/ "..param.." does not exist, or is not part of a valid team."
end end

View file

@ -35,7 +35,6 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if team and ctf.can_mod(name, pdata.team) then if team and ctf.can_mod(name, pdata.team) then
if ctf.flag_colors[fields.color] then if ctf.flag_colors[fields.color] then
team.data.color = fields.color team.data.color = fields.color
ctf.needs_save = true
minetest.chat_send_player(name, "Team color set to " .. fields.color) minetest.chat_send_player(name, "Team color set to " .. fields.color)
else else

View file

@ -103,7 +103,6 @@ function ctf_flag.add(team, pos)
pos.team = team pos.team = team
table.insert(ctf.team(team).flags,pos) table.insert(ctf.team(team).flags,pos)
ctf.needs_save = true
end end
function ctf_flag.update(pos) function ctf_flag.update(pos)
@ -135,7 +134,6 @@ function ctf_flag.update(pos)
if not ctf.team(flag_team_data.team).data.color then if not ctf.team(flag_team_data.team).data.color then
ctf.team(flag_team_data.team).data.color = "red" ctf.team(flag_team_data.team).data.color = "red"
ctf.needs_save = true
end end
if flag_team_data.claimed then if flag_team_data.claimed then

View file

@ -79,8 +79,6 @@ local function do_capture(attname, flag, returned)
ctf_flag.registered_on_capture[i](attname, flag) ctf_flag.registered_on_capture[i](attname, flag)
end end
end end
ctf.needs_save = true
end end
local function player_drop_flag(player) local function player_drop_flag(player)
@ -227,8 +225,6 @@ ctf_flag = {
team.spawn = pos team.spawn = pos
end end
ctf.needs_save = true
local pos2 = { local pos2 = {
x = pos.x, x = pos.x,
y = pos.y + 1, y = pos.y + 1,
@ -237,7 +233,6 @@ ctf_flag = {
if not team.data.color then if not team.data.color then
team.data.color = "red" team.data.color = "red"
ctf.needs_save = true
end end
minetest.set_node(pos2, {name="ctf_flag:flag_top_"..team.data.color}) minetest.set_node(pos2, {name="ctf_flag:flag_top_"..team.data.color})

View file

@ -141,6 +141,9 @@ local function update_flag_drops()
" seconds to capture the flag before it returns.") " seconds to capture the flag before it returns.")
end end
end end
ctf_flag.assert_flags()
minetest.after(5, update_flag_drops) minetest.after(5, update_flag_drops)
end end
minetest.after(5, update_flag_drops) minetest.after(5, update_flag_drops)

View file

@ -1,20 +1,6 @@
# Data Formats
This file documents the contents of ctf.txt
Values are added to the file using ctf.register_on_save and ctf.register_on_load.
Here are the default values:
```lua
{
players = ctf.players,
teams = ctf.teams,
diplo = ctf.diplo.diplo
}
```
## Players ## Players
Commonly called tplayer (may be called data or player in old code). Commonly called tplayer (may be called data or player in old code).
Player name is commonly called name (but may be called other things in older code). Player name is commonly called name (but may be called other things in older code).
```lua ```lua
@ -34,7 +20,7 @@ ctf.players = {
## Teams ## Teams
Commonly called team. Commonly called team.
Team name is commonly called tname (but may be called team in old code). Team name is commonly called tname (but may be called team in old code).
```lua ```lua