diff --git a/Class/BalanceStats.cs b/Class/BalanceStats.cs index 45e6092..bb6bb93 100644 --- a/Class/BalanceStats.cs +++ b/Class/BalanceStats.cs @@ -41,26 +41,39 @@ public bool ShouldMoveLowestScorers() public void MoveLowestScorersFromBiggerTeam() { - while (CT.Stats.Count != T.Stats.Count) + int ctPlayerCount = CT.Stats.Count; + int tPlayerCount = T.Stats.Count; + + // Determine which team has more players and how many need to be moved + int playersToMove = Math.Abs(ctPlayerCount - tPlayerCount) / 2; + + if (playersToMove == 0) { - if (CT.Stats.Count > T.Stats.Count) - { - var playerToMove = CT.Stats.OrderBy(p => p.PerformanceScore).FirstOrDefault(); - if (playerToMove != null) - { - T.AddPlayer(playerToMove); - CT.RemovePlayer(playerToMove); - } - } - else if (T.Stats.Count > CT.Stats.Count) - { - var playerToMove = T.Stats.OrderBy(p => p.PerformanceScore).FirstOrDefault(); - if (playerToMove != null) - { - CT.AddPlayer(playerToMove); - T.RemovePlayer(playerToMove); - } - } + PrintDebugMessage("Teams are already balanced by size. No need to move players."); + return; + } + + // Move players from the bigger team to the smaller team + if (ctPlayerCount > tPlayerCount) + { + MovePlayers(CT, T, playersToMove); + } + else if (tPlayerCount > ctPlayerCount) + { + MovePlayers(T, CT, playersToMove); + } + + PrintDebugMessage($"Players moved to balance team sizes. CT Players: {CT.Stats.Count}, T Players: {T.Stats.Count}"); + } + + private static void MovePlayers(TeamStats fromTeam, TeamStats toTeam, int playersToMove) + { + var playersToMoveList = fromTeam.Stats.OrderBy(p => p.PerformanceScore).Take(playersToMove).ToList(); + + foreach (var player in playersToMoveList) + { + fromTeam.RemovePlayer(player); + toTeam.AddPlayer(player); } } @@ -127,21 +140,37 @@ public void PerformSwap(PlayerStats ctPlayer, PlayerStats tPlayer) CT.AddPlayer(tPlayer); T.AddPlayer(ctPlayer); + + PrintDebugMessage($"Swapped CT player {ctPlayer.PlayerName} with T player {tPlayer.PlayerName}"); } public void AssignPlayerTeams() { foreach (var player in CT.Stats) { + if (player == null) + { + PrintDebugMessage("Found null player in CT stats, skipping."); + continue; + } + if (player.Team != (int)CsTeam.CounterTerrorist) ChangePlayerTeam(player.PlayerSteamID, CsTeam.CounterTerrorist); } foreach (var player in T.Stats) { + if (player == null) + { + PrintDebugMessage("Found null player in T stats, skipping."); + continue; + } + if (player.Team != (int)CsTeam.Terrorist) ChangePlayerTeam(player.PlayerSteamID, CsTeam.Terrorist); } + + PrintDebugMessage("Player team assignments completed."); } } } diff --git a/Events/Events.cs b/Events/Events.cs index b835e09..af10d60 100644 --- a/Events/Events.cs +++ b/Events/Events.cs @@ -48,6 +48,7 @@ public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info) public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info) { UpdatePlayerStatsInCache(); + var endTime = ConVar.Find("mp_round_restart_delay")?.GetPrimitiveValue(); var delay = endTime == null ? 1 : (endTime - 1); diff --git a/Mesharsky_TeamBalance.cs b/Mesharsky_TeamBalance.cs index c456c84..b92ae03 100644 --- a/Mesharsky_TeamBalance.cs +++ b/Mesharsky_TeamBalance.cs @@ -6,7 +6,7 @@ namespace Mesharsky_TeamBalance; public partial class Mesharsky_TeamBalance : BasePlugin { public override string ModuleName => "Mesharsky Team Balance"; - public override string ModuleVersion => "2.1"; + public override string ModuleVersion => "2.1.1"; public override string ModuleAuthor => "Mesharsky"; public override void Load(bool hotReload) @@ -15,7 +15,7 @@ public override void Load(bool hotReload) Initialize_Events(); AddCommandListener("jointeam", Command_JoinTeam); - AddTimer(7.0f, () => + AddTimer(5.0f, () => { string conVarName = "mp_autoteambalance"; ConVar? cvar = ConVar.Find(conVarName);