Skip to content

Commit

Permalink
Merge pull request #5 from B3none/team-management-updates
Browse files Browse the repository at this point in the history
Prevent players from being able to change team mid round, and continued updates to team management
  • Loading branch information
B3none authored Jan 2, 2024
2 parents 1d3e7b1 + c5fd02b commit 3af4900
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 165 deletions.
2 changes: 1 addition & 1 deletion Modules/Allocators/Equipment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static void Allocate(CCSPlayerController player)
player.GiveNamedItem(CsItem.AssaultSuit);

if (
player.TeamNum == (int)CsTeam.CounterTerrorist
(CsTeam)player.TeamNum == CsTeam.CounterTerrorist
&& player.PlayerPawn.IsValid
&& player.PlayerPawn.Value != null
&& player.PlayerPawn.Value.IsValid
Expand Down
2 changes: 1 addition & 1 deletion Modules/Allocators/Grenades.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void Allocate(CCSPlayerController player)
player.GiveNamedItem(CsItem.HEGrenade);
break;
case 3:
player.GiveNamedItem(player.TeamNum == (int)CsTeam.Terrorist ? CsItem.Molotov : CsItem.Incendiary);
player.GiveNamedItem((CsTeam)player.TeamNum == CsTeam.Terrorist ? CsItem.Molotov : CsItem.Incendiary);
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/Allocators/Weapons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public abstract class Weapons
{
public static void Allocate(CCSPlayerController player)
{
if (player.TeamNum == (int)CsTeam.Terrorist)
if ((CsTeam)player.TeamNum == CsTeam.Terrorist)
{
player.GiveNamedItem(CsItem.AK47);
// player.GiveNamedItem(CsItem.Glock);
player.GiveNamedItem(CsItem.Deagle);
}

if (player.TeamNum == (int)CsTeam.CounterTerrorist)
if ((CsTeam)player.TeamNum == CsTeam.CounterTerrorist)
{
player.GiveNamedItem(CsItem.M4A1S);
// player.GiveNamedItem(CsItem.USPS);
Expand Down
8 changes: 3 additions & 5 deletions Modules/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ public static T GetAndRemoveRandomItem<T>(List<T> list)

public static List<T> Shuffle<T>(List<T> list)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}Shuffle helper called.");
List<T> shuffledList = new List<T>(list); // Create a copy of the original list
var shuffledList = new List<T>(list); // Create a copy of the original list

int n = shuffledList.Count;
var n = shuffledList.Count;
while (n > 1)
{
n--;
Expand All @@ -66,7 +65,6 @@ public static List<T> Shuffle<T>(List<T> list)
shuffledList[k] = shuffledList[n];
shuffledList[n] = value;
}
Console.WriteLine($"{RetakesPlugin.LogPrefix}Shuffle helper complete.");

return shuffledList;
}
Expand All @@ -88,7 +86,7 @@ public static void RemoveAllWeaponsAndEntities(CCSPlayerController player)
{
return;
}

foreach(var weapon in player.PlayerPawn.Value.WeaponServices.MyWeapons)
{
if (weapon is not { IsValid: true, Value.IsValid: true })
Expand Down
65 changes: 28 additions & 37 deletions Modules/Managers/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ public void TerroristRoundWin()
{
_consecutiveRoundsWon = 0;
ScrambleTeams();
return;
}

BalanceTeams();
}

public void CounterTerroristRoundWin()
Expand All @@ -74,10 +71,7 @@ public void CounterTerroristRoundWin()

var targetNumTerrorists = 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 sortedCounterTerroristPlayers = GetSortedActivePlayers(CsTeam.CounterTerrorist);

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

Expand All @@ -94,28 +88,12 @@ public void CounterTerroristRoundWin()
newTerrorists.AddRange(playersLeft.Take(targetNumTerrorists - newTerrorists.Count));
}

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

Console.WriteLine($"{RetakesPlugin.LogPrefix}Got {sortedTerroristPlayers.Count} sortedTerroristPlayers.");

newTerrorists.AddRange(sortedCounterTerroristPlayers.Where(player => player.Score > 0).Take(targetNumTerrorists - newTerrorists.Count).ToList());

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

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

Console.WriteLine($"{RetakesPlugin.LogPrefix}There are currently {newTerrorists.Count} new terrorists.");
foreach (var player in Utilities.GetPlayers().Where(Helpers.IsValidPlayer))
{
if (player.TeamNum == (int)CsTeam.Terrorist)
if ((CsTeam)player.TeamNum == CsTeam.Terrorist)
{
player.SwitchTeam(CsTeam.CounterTerrorist);
continue;
Expand All @@ -138,16 +116,12 @@ public void BalanceTeams()
Console.WriteLine($"{RetakesPlugin.LogPrefix}Checking if they terrorists need a player. Queue.GetTargetNumTerrorists() = {Queue.GetTargetNumTerrorists()} | Helpers.GetCurrentNumPlayers(CsTeam.Terrorist) = {currentNumTerrorist} | numTerroristsNeeded {numTerroristsNeeded}");

List<CCSPlayerController> newTerrorists = new();
List<CCSPlayerController> newCounterTerrorists = new();

if (numTerroristsNeeded > 0)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}{numTerroristsNeeded} terrorists needed");

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

Console.WriteLine(
$"{RetakesPlugin.LogPrefix}Got {sortedCounterTerroristPlayers.Count} sortedCounterTerroristPlayers.");
Expand All @@ -162,11 +136,19 @@ public void BalanceTeams()

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

if (newTerrorists.Count < numTerroristsNeeded)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}Still not enough terrorists needed - starting to look at previous terrorists");
var sortedTerrorists = GetSortedActivePlayers(CsTeam.Terrorist);
var playersLeft = Helpers.Shuffle(sortedTerrorists.Except(newTerrorists).ToList());
newTerrorists.AddRange(playersLeft.Take(numTerroristsNeeded - newTerrorists.Count));
}

Console.WriteLine($"{RetakesPlugin.LogPrefix}Swapping player(s) to terrorist...");
foreach (var player in newTerrorists)
{
Expand All @@ -180,11 +162,11 @@ public void BalanceTeams()
var numCounterTerroristsNeeded = Queue.GetTargetNumCounterTerrorists() - currentNumCounterTerroristAfterBalance;
Console.WriteLine($"{RetakesPlugin.LogPrefix}checking if CT need a player. Queue.GetTargetNumCounterTerrorists() = {Queue.GetTargetNumCounterTerrorists()} | Helpers.GetCurrentNumPlayers(CsTeam.CounterTerrorist) = {currentNumTerroristAfterBalance} | numCounterTerroristsNeeded {numCounterTerroristsNeeded}");

if (currentNumTerroristAfterBalance > 1 && numCounterTerroristsNeeded > 0)
if (numCounterTerroristsNeeded > 0)
{
var terroristsWithZeroScore = /*Queue.ActivePlayers*/Utilities.GetPlayers()
var terroristsWithZeroScore = Queue.ActivePlayers
.Where(player =>
player.TeamNum == (int)CsTeam.Terrorist
(CsTeam)player.TeamNum == CsTeam.Terrorist
&& Helpers.IsValidPlayer(player)
&& _playerRoundScores.GetValueOrDefault((int)player.UserId!, 0) == 0
)
Expand All @@ -195,16 +177,16 @@ public void BalanceTeams()
Console.WriteLine($"{RetakesPlugin.LogPrefix}Moving terrorists with 0 score to CT.");

// Shuffle to avoid repetitive swapping of the same players
newCounterTerrorists = Helpers.Shuffle(terroristsWithZeroScore).Take(numCounterTerroristsNeeded).ToList();
var newCounterTerrorists = Helpers.Shuffle(terroristsWithZeroScore).Take(numCounterTerroristsNeeded).ToList();

if (numCounterTerroristsNeeded > newCounterTerrorists.Count)
{
// For remaining excess terrorists, move the ones with the lowest score to CT
newCounterTerrorists.AddRange(
/*Queue.ActivePlayers*/Utilities.GetPlayers()
Queue.ActivePlayers
.Except(newCounterTerrorists)
.Except(newTerrorists)
.Where(player => player.TeamNum == (int)CsTeam.Terrorist && Helpers.IsValidPlayer(player))
.Where(player => (CsTeam)player.TeamNum == CsTeam.Terrorist && Helpers.IsValidPlayer(player))
.OrderBy(player => _playerRoundScores.GetValueOrDefault((int)player.UserId!, 0))
.Take(numTerroristsNeeded - newCounterTerrorists.Count)
.ToList()
Expand Down Expand Up @@ -236,4 +218,13 @@ public void SetupActivePlayers()
Queue.ActivePlayers = Queue.QueuePlayers;
Queue.QueuePlayers = new List<CCSPlayerController>();
}

private List<CCSPlayerController> GetSortedActivePlayers(CsTeam? team = null)
{
return Queue.ActivePlayers
.Where(Helpers.IsValidPlayer)
.Where(player => team == null || (CsTeam)player.TeamNum == team)
.OrderByDescending(player => _playerRoundScores.GetValueOrDefault((int)player.UserId!, 0))
.ToList();
}
}
102 changes: 62 additions & 40 deletions Modules/Managers/Queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class Queue
public List<CCSPlayerController> QueuePlayers = new();
public List<CCSPlayerController> ActivePlayers = new();

public List<CCSPlayerController> RoundTerrorists = new();
public List<CCSPlayerController> RoundCounterTerrorists = new();

public Queue(int? retakesMaxPlayers, float? retakesTerroristRatio)
{
_maxRetakesPlayers = retakesMaxPlayers ?? 9;
Expand All @@ -29,44 +32,76 @@ public int GetTargetNumTerrorists()

public int GetTargetNumCounterTerrorists()
{
var targetPlayers = ActivePlayers.Count - GetTargetNumTerrorists();
return targetPlayers > 0 ? targetPlayers : 1;
return ActivePlayers.Count - GetTargetNumTerrorists();
}

public void PlayerTriedToJoinTeam(CCSPlayerController player, bool switchToSpectator, bool isWarmup)
public void PlayerTriedToJoinTeam(CCSPlayerController player, CsTeam fromTeam, CsTeam toTeam)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] PlayerTriedToJoinTeam called.");

if (fromTeam == CsTeam.None && toTeam == CsTeam.Spectator)
{
// This is called when a player first joins.
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] None -> Spectator.");
return;
}

Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Checking ActivePlayers.");
if (ActivePlayers.Contains(player))
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Already an active player.");
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Player is an active player.");

if (toTeam == CsTeam.Spectator)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Switching to spectator.");
ActivePlayers.Remove(player);
return;
}

if (
RoundTerrorists.Count > 0
&& RoundCounterTerrorists.Count > 0
&& (
(toTeam == CsTeam.CounterTerrorist && !RoundCounterTerrorists.Contains(player))
|| (toTeam == CsTeam.Terrorist && !RoundTerrorists.Contains(player))
)
)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] player is not in round list for {toTeam}, switching to spectator.");
ActivePlayers.Remove(player);
QueuePlayers.Add(player);
player.CommitSuicide(false, true);
player.ChangeTeam(CsTeam.Spectator);
return;
}

Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Do nothing.");
return;
}

Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Checking QueuePlayers.");
if (!QueuePlayers.Contains(player))
{
if (isWarmup && ActivePlayers.Count < _maxRetakesPlayers)
if (RetakesPlugin.GetGameRules().WarmupPeriod && ActivePlayers.Count < _maxRetakesPlayers)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Not found, adding to ActivePlayers (because in warmup).");
ActivePlayers.Add(player);
return;
}
else
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Not found, adding to QueuePlayers.");
QueuePlayers.Add(player);
}

Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Not found, adding to QueuePlayers.");
QueuePlayers.Add(player);
}
else
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Found in QueuePlayers, do nothing.");
}

Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Should switch to spectator? {(switchToSpectator ? "yes" : "no")}");
if (switchToSpectator)
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Should switch to spectator? {(toTeam != CsTeam.Spectator ? "yes" : "no")}");
if (toTeam != CsTeam.Spectator)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Changing to spectator.");
player.CommitSuicide(false, true);
player.ChangeTeam(CsTeam.Spectator);
}
}
Expand All @@ -89,25 +124,10 @@ private void RemoveDisconnectedPlayers()
QueuePlayers.RemoveAll(player => disconnectedQueuePlayers.Contains(player));
}
}

private void AddConnectedPlayers()
{
var connectedPlayers = Utilities.GetPlayers().Where(player => Helpers.IsValidPlayer(player) && Helpers.IsPlayerConnected(player)).ToList();

foreach (var connectedPlayer in connectedPlayers)
{
if (!ActivePlayers.Contains(connectedPlayer) && !QueuePlayers.Contains(connectedPlayer))
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}Adding {connectedPlayer.PlayerName} to QueuePlayers.");
QueuePlayers.Add(connectedPlayer);
}
}
}

public void Update()
{
RemoveDisconnectedPlayers();
AddConnectedPlayers();

var playersToAdd = _maxRetakesPlayers - ActivePlayers.Count;

Expand All @@ -122,25 +142,15 @@ public void Update()
// loop players to add, and set their team to CT
foreach (var player in playersToAddList)
{
if (player.TeamNum == (int)CsTeam.Spectator)
{
player.SwitchTeam(CsTeam.CounterTerrorist);
}
player.SwitchTeam(CsTeam.CounterTerrorist);
}
}
}

public void PlayerDisconnected(CCSPlayerController player)
{
if (ActivePlayers.Contains(player))
{
ActivePlayers.Remove(player);
}

if (QueuePlayers.Contains(player))
{
QueuePlayers.Remove(player);
}
ActivePlayers.Remove(player);
QueuePlayers.Remove(player);
}

public void DebugQueues(bool isBefore)
Expand All @@ -163,4 +173,16 @@ public void DebugQueues(bool isBefore)
Console.WriteLine($"{RetakesPlugin.LogPrefix}QueuePlayers ({(isBefore ? "BEFORE" : "AFTER")}): {string.Join(", ", QueuePlayers.Where(Helpers.IsValidPlayer).Select(player => player.PlayerName))}");
}
}

public void ClearRoundTeams()
{
RoundTerrorists.Clear();
RoundCounterTerrorists.Clear();
}

public void SetRoundTeams()
{
RoundTerrorists = Utilities.GetPlayers().Where(player => Helpers.IsValidPlayer(player) && (CsTeam)player.TeamNum == CsTeam.Terrorist).ToList();
RoundCounterTerrorists = Utilities.GetPlayers().Where(player => Helpers.IsValidPlayer(player) && (CsTeam)player.TeamNum == CsTeam.CounterTerrorist).ToList();
}
}
Loading

0 comments on commit 3af4900

Please sign in to comment.