From c21b192f82244be38058d2c3e60578dcae12f164 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 6 Apr 2018 13:34:58 +0100 Subject: [PATCH] Add naive team sorting and shuffling Will probably be shit. Fixes #37 --- mods/ctf_alloc/init.lua | 63 ++++++++++++++++++++++++++++++++++++++ mods/ctf_match/depends.txt | 1 + mods/ctf_match/matches.lua | 22 +------------ 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/mods/ctf_alloc/init.lua b/mods/ctf_alloc/init.lua index 8f6b057..ad8cc42 100644 --- a/mods/ctf_alloc/init.lua +++ b/mods/ctf_alloc/init.lua @@ -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 diff --git a/mods/ctf_match/depends.txt b/mods/ctf_match/depends.txt index 7547079..b7749df 100644 --- a/mods/ctf_match/depends.txt +++ b/mods/ctf_match/depends.txt @@ -1,6 +1,7 @@ ctf ctf_flag ctf_inventory +ctf_alloc vote hudkit irc? diff --git a/mods/ctf_match/matches.lua b/mods/ctf_match/matches.lua index 28aba10..9bebc80 100644 --- a/mods/ctf_match/matches.lua +++ b/mods/ctf_match/matches.lua @@ -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)