Skip to content

Commit

Permalink
Add naive team sorting and shuffling
Browse files Browse the repository at this point in the history
Will probably be shit. Fixes #37
  • Loading branch information
rubenwardy committed Apr 6, 2018
1 parent 0abd12b commit c21b192
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 21 deletions.
63 changes: 63 additions & 0 deletions mods/ctf_alloc/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,66 @@ end, {
ctf_admin = true,
}
})

function table.map_inplace(t, f)
for key, value in pairs(t) do
t[key] = f(value)
end
return t
end

ctf_alloc = {}
function ctf_alloc.set_all()
local players = minetest.get_connected_players()
table.map_inplace(players, function(a)
local stats, _ = ctf_stats.player(a:get_player_name())
return {
player = a,
score = stats.score,
}
end)
table.sort(players, function(a, b)
return a.score > b.score
end)

for k=1, math.random(#players / 4) do
local i = math.random(#players)
local j = math.random(#players)

local tmp = players[i]
players[i] = players[j]
players[j] = tmp
end

minetest.log("warning", dump(players))

local to_red = true
for _, spair in pairs(players) do
local player = spair.player
local name = player:get_player_name()
local alloc_mode = tonumber(ctf.setting("allocate_mode"))
local team
if to_red then
team = "red"
else
team = "blue"
end
to_red = not to_red

if alloc_mode ~= 0 and team then
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
local inv = player:get_inventory()
inv:set_list("main", {})
inv:set_list("craft", {})
give_initial_stuff(player)
end

player:set_hp(20)
end
end
1 change: 1 addition & 0 deletions mods/ctf_match/depends.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ctf
ctf_flag
ctf_inventory
ctf_alloc
vote
hudkit
irc?
22 changes: 1 addition & 21 deletions mods/ctf_match/matches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,7 @@ function ctf_match.next()

ctf_match.create_teams()

for i, player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local alloc_mode = tonumber(ctf.setting("allocate_mode"))
local team = ctf.autoalloc(name, alloc_mode)

if alloc_mode ~= 0 and team then
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
local inv = player:get_inventory()
inv:set_list("main", {})
inv:set_list("craft", {})
give_initial_stuff(player)
end

player:set_hp(20)
end
ctf_alloc.set_all()

minetest.set_timeofday(0.4)

Expand Down

0 comments on commit c21b192

Please sign in to comment.