Skip to content

Commit

Permalink
v2.1.1 - Hotfix for crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mesharsky committed Aug 26, 2024
1 parent a8393c6 commit 918e07f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
67 changes: 48 additions & 19 deletions Class/BalanceStats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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.");
}
}
}
1 change: 1 addition & 0 deletions Events/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>();
var delay = endTime == null ? 1 : (endTime - 1);

Expand Down
4 changes: 2 additions & 2 deletions Mesharsky_TeamBalance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down

0 comments on commit 918e07f

Please sign in to comment.