Allocate players into teams on the basis of cumulative team scores (#319)

Implement `ctf.custom_alloc`, which recalculates cumulative team scores and returns the name of the team with the lowest cumulative score.
This commit is contained in:
ANAND 2019-10-31 20:00:50 +05:30 committed by GitHub
parent 680dd4c87c
commit 2653a09329
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 3 deletions

View file

@ -24,7 +24,7 @@ ctf.flag.nobuild_radius = 3
ctf.flag.alerts = true
ctf.flag.drop_time = 300
ctf.allocate_mode = 3
ctf.allocate_mode = 4
ctf.players_can_change_team = false
ctf.friendly_fire = false
ctf.autoalloc_on_joinplayer = true

View file

@ -1,6 +1,7 @@
local storage = minetest.get_mod_storage()
local data = minetest.parse_json(storage:get_string("locktoteam")) or {}
-- Override autoalloc function to implement team-locking
local ctf_autoalloc = ctf.autoalloc
function ctf.autoalloc(name, alloc_mode)
if data[name] then
@ -10,7 +11,7 @@ function ctf.autoalloc(name, alloc_mode)
return ctf_autoalloc(name, alloc_mode)
end
ChatCmdBuilder.new("ctf_lockpt", function(cmd)
ChatCmdBuilder.new("ctf_lock_to_team", function(cmd)
cmd:sub(":name :team", function(name, pname, team)
if team == "!" then
data[pname] = nil
@ -29,6 +30,59 @@ end, {
}
})
-- Struct containing the name and score of the team with lowest cumulative score
local lowest = {}
--[[
lowest = {
team =,
score =
}
]]
-- List of cumulative team scores indexed by team name
local scores = {}
--[[
scores = {
red = ,
blue
}
]]
local function update_lowest()
-- Update lowest.score and lowest.team
lowest = {}
for tname, score in pairs(scores) do
if not lowest.score or score <= lowest.score then
lowest.score = score
lowest.team = tname
end
end
print("\nlowest = " .. dump(lowest) .. "\n")
end
local function calc_scores()
-- Update the cumulative score of all teams
print("\n[calc_scores]")
for tname, team in pairs(ctf.teams) do
local score = 0
for pname, _ in pairs(team.players) do
score = score + ctf_stats.player(pname).score
end
scores[tname] = score
print("\t" .. tname .. " = " .. score)
end
update_lowest()
print("[calc_scores] ********\n")
end
-- Override team-allocation logic
-- Allocate player into the team with the lowest cumulative score
function ctf.custom_alloc(name)
calc_scores()
return lowest.team
end
function table.map_inplace(t, f) -- luacheck: ignore
for key, value in pairs(t) do
t[key] = f(value)
@ -67,7 +121,6 @@ function ctf_alloc.set_all()
ctf.log("autoalloc", name .. " was allocated to " .. team)
ctf.join(name, team)
end
ctf.move_to_spawn(name)
if ctf.setting("match.clear_inv") then