Skip to content

Commit

Permalink
Fixed team balancing
Browse files Browse the repository at this point in the history
  • Loading branch information
B3none committed Dec 22, 2023
1 parent 79090c4 commit c6abc99
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 10 deletions.
24 changes: 24 additions & 0 deletions Modules/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,28 @@ public static void ExecuteRetakesConfiguration()
{
Server.ExecuteCommand("execifexists cs2-retakes/retakes.cfg");
}

public static int GetCurrentNumTerrorists()
{
Console.WriteLine($"{RetakesPlugin.MessagePrefix} GetCurrentNumTerrorists called");
// var gameRules = GetGameRules();
//
// if (gameRules != null)
// {
// return gameRules.NumTerrorist;
// }

var numTerrorists = 0;

foreach (var player in Utilities.GetPlayers().Where(player => IsValidPlayer(player) && IsPlayerConnected(player)))
{
if (player.TeamNum == (int)CsTeam.Terrorist)
{
Console.WriteLine($"{RetakesPlugin.MessagePrefix} Found terrorist! {player.PlayerName}");
numTerrorists++;
}
}

return numTerrorists;
}
}
51 changes: 46 additions & 5 deletions Modules/Managers/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private void ScrambleTeams()

foreach (var player in Helpers.Shuffle(Queue.ActivePlayers))
{
player.SwitchTeam(numAssigned < Queue.GetNumTerrorists() ? CsTeam.Terrorist : CsTeam.CounterTerrorist);
player.SwitchTeam(numAssigned < Queue.GetTargetNumTerrorists() ? CsTeam.Terrorist : CsTeam.CounterTerrorist);
numAssigned++;
}
}
Expand Down Expand Up @@ -56,24 +56,65 @@ public void TerroristRoundWin()
{
_consecutiveRoundsWon = 0;
ScrambleTeams();
return;
}

var numTerroristsNeeded = Queue.GetTargetNumTerrorists() - Helpers.GetCurrentNumTerrorists();
Console.WriteLine($"{RetakesPlugin.MessagePrefix}Terrorists won, checking if they need a player. Queue.GetTargetNumTerrorists() = {Queue.GetTargetNumTerrorists()} | Helpers.GetCurrentNumTerrorists() = {Helpers.GetCurrentNumTerrorists()} | numTerroristsNeeded {numTerroristsNeeded}");

if (!(numTerroristsNeeded > 0))
{
Console.WriteLine($"{RetakesPlugin.MessagePrefix}No terrorists needed");
return;
}

Console.WriteLine($"{RetakesPlugin.MessagePrefix}{numTerroristsNeeded} terrorists needed");

var sortedCounterTerroristPlayers = Queue.ActivePlayers
.Where(player => Helpers.IsValidPlayer(player) && player.TeamNum == (int)CsTeam.CounterTerrorist)
.OrderByDescending(player => _playerRoundScores.GetValueOrDefault((int)player.UserId!, 0))
.ToList();

Console.WriteLine($"{RetakesPlugin.MessagePrefix}Got {sortedCounterTerroristPlayers.Count} sortedCounterTerroristPlayers.");

var newTerrorists = sortedCounterTerroristPlayers.Where(player => player.Score > 0).Take(numTerroristsNeeded).ToList();

Console.WriteLine($"{RetakesPlugin.MessagePrefix}{sortedCounterTerroristPlayers.Where(player => player.Score > 0).ToList().Count} sortedCounterTerroristPlayers with more than 0 score found.");
Console.WriteLine($"{RetakesPlugin.MessagePrefix}There are currently {newTerrorists.Count} new terrorists.");

if (newTerrorists.Count < numTerroristsNeeded)
{
Console.WriteLine($"{RetakesPlugin.MessagePrefix}Still not enough terrorists needed.");
var playersLeft = Helpers.Shuffle(sortedCounterTerroristPlayers.Except(newTerrorists).ToList());
newTerrorists.AddRange(playersLeft.Take(numTerroristsNeeded - newTerrorists.Count));
}

Console.WriteLine($"{RetakesPlugin.MessagePrefix}Swapping players to terrorist.");
foreach (var player in newTerrorists)
{
Console.WriteLine($"{RetakesPlugin.MessagePrefix}Swapping player {player.PlayerName} to terrorist.");
player.SwitchTeam(CsTeam.Terrorist);
}
}

public void CounterTerroristRoundWin()
{
_consecutiveRoundsWon = 0;

var numTerrorists = Queue.GetNumTerrorists();
var numTerrorists = Queue.GetTargetNumTerrorists();

var sortedCounterTerroristPlayers = Queue.ActivePlayers
.Where(player => player.TeamNum == (int)CsTeam.CounterTerrorist && Helpers.IsValidPlayer(player))
.OrderByDescending(player => _playerRoundScores.GetValueOrDefault((int)player.UserId!, 0))
.ToList();

var newTerrorists = sortedCounterTerroristPlayers.Where(player => player.Score > 0).Take(numTerrorists).ToList();

var playersLeft = Helpers.Shuffle(sortedCounterTerroristPlayers.Except(newTerrorists).ToList());
newTerrorists.AddRange(playersLeft.Take(numTerrorists - newTerrorists.Count));

if (newTerrorists.Count < numTerrorists)
{
var playersLeft = Helpers.Shuffle(sortedCounterTerroristPlayers.Except(newTerrorists).ToList());
newTerrorists.AddRange(playersLeft.Take(numTerrorists - newTerrorists.Count));
}

foreach (var player in Utilities.GetPlayers().Where(Helpers.IsValidPlayer))
{
Expand Down
10 changes: 5 additions & 5 deletions Modules/Managers/Queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace RetakesPlugin.Modules.Managers;

public class Queue
{
public const int MaxRetakesPlayers = 9;
public const float TerroristRatio = 0.45f;
private const int MaxRetakesPlayers = 9;
private const float TerroristRatio = 0.45f;

public List<CCSPlayerController> QueuePlayers = new();
public List<CCSPlayerController> ActivePlayers = new();

public int GetNumTerrorists()
public int GetTargetNumTerrorists()
{
var ratio = TerroristRatio * ActivePlayers.Count;
var numTerrorists = (int)Math.Round(ratio);
Expand All @@ -21,9 +21,9 @@ public int GetNumTerrorists()
return numTerrorists > 0 ? numTerrorists : 1;
}

public int GetNumCounterTerrorists()
public int GetTargetNumCounterTerrorists()
{
return ActivePlayers.Count - GetNumTerrorists();
return ActivePlayers.Count - GetTargetNumTerrorists();
}

public void PlayerTriedToJoinTeam(CCSPlayerController player, bool switchToSpectator)
Expand Down

0 comments on commit c6abc99

Please sign in to comment.