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

@ -53,13 +53,6 @@ function ctf.register_on_load(func)
func(ctf._loaddata)
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 = {}
function ctf.register_on_init(func)
if ctf._mt_loaded then
@ -203,67 +196,8 @@ function ctf.setting(name)
end
function ctf.load()
ctf.log("io", "Loading CTF state")
local file = io.open(minetest.get_worldpath().."/ctf.txt", "r")
if file then
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!")
ctf._new_game = true
for i = 1, #ctf.registered_on_new_game do
ctf.registered_on_new_game[i]()
end
end

View file

@ -7,77 +7,6 @@ ctf.register_on_load(function(table)
ctf.diplo.diplo = table.diplo
end)
ctf.register_on_save(function()
return { diplo = ctf.diplo.diplo }
end)
function ctf.diplo.get(one,two)
if not ctf.diplo.diplo then
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
return "war"
end

View file

@ -131,73 +131,6 @@ ctf.gui.register_tab("news", "News", function(name, tname)
result)
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)
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
if fields.clear then
team.log = {}
ctf.needs_save = true
ctf.gui.show(name, "news")
return true
end
@ -272,82 +204,9 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
table.remove(team.log, id)
ctf.needs_save = true
ctf.gui.show(name, "news")
return true
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 = {}
-- 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
minetest.register_privilege("ctf_team_mgr", {
description = "Team manager",

View file

@ -40,8 +40,6 @@ function ctf.create_team(name, data)
ctf.registered_on_new_team[i](ctf.teams[name])
end
ctf.needs_save = true
return ctf.teams[name]
end
@ -55,7 +53,6 @@ function ctf.remove_team(name)
team.flags[i].team = nil
end
ctf.teams[name] = nil
ctf.needs_save = true
return true
else
return false
@ -99,7 +96,6 @@ function ctf.new_player(name)
ctf.players[name] = {
name = name
}
ctf.needs_save = true
else
ctf.error("team", "Can't create a blank player")
ctf.log("team", debug.traceback())
@ -137,7 +133,6 @@ function ctf.remove_player(name)
team.players[name] = nil
end
ctf.players[name] = nil
ctf.needs_save = true
return true
else
return false
@ -210,8 +205,6 @@ function ctf.join(name, team, force, by)
team_data.players[player.name] = player
ctf.player_last_team[name] = team
ctf.needs_save = true
minetest.log("action", name .. " 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)
end
end
ctf.needs_save = true
end
-- 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")
table.insert(ctf.team(team).log, 1, msg)
ctf.needs_save = true
return true
end