Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Team balance config #139

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RetakesPlugin/Modules/Configs/MapConfigData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public class MapConfigData
{
public List<Spawn> Spawns { get; set; } = new();
public List<Spawn> Spawns { get; set; } = [];
}
4 changes: 3 additions & 1 deletion RetakesPlugin/Modules/Configs/RetakesConfigData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class RetakesConfigData
{
public static int CurrentVersion = 9;
public static int CurrentVersion = 11;

public int Version { get; set; } = CurrentVersion;
public int MaxPlayers { get; set; } = 9;
Expand All @@ -20,4 +20,6 @@ public class RetakesConfigData
public bool ShouldForceEvenTeamsWhenPlayerCountIsMultipleOf10 { get; set; } = true;
public bool EnableFallbackBombsiteAnnouncement { get; set; } = true;
public bool ShouldRemoveSpectators { get; set; } = true;
public bool IsBalanceEnabled { get; set; } = true;
public bool ShouldPreventTeamChangesMidRound { get; set; } = true;
}
12 changes: 4 additions & 8 deletions RetakesPlugin/Modules/Managers/BreakerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,15 @@ namespace RetakesPlugin.Modules.Managers;

public class BreakerManager
{
private readonly List<(string designerName, string action)> _entityActions = new();
private readonly List<(string designerName, string action)> _entityActions = [];

private static readonly HashSet<string> MapsWithPropDynamic = new()
{
private static readonly HashSet<string> MapsWithPropDynamic = [
"de_vertigo",
"de_nuke",
"de_mirage"
};
];

private static readonly HashSet<string> MapsWithFuncButton = new()
{
"de_nuke"
};
private static readonly HashSet<string> MapsWithFuncButton = ["de_nuke"];

public BreakerManager(bool? shouldBreakBreakables, bool? shouldOpenDoors)
{
Expand Down
39 changes: 23 additions & 16 deletions RetakesPlugin/Modules/Managers/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ public class GameManager
private readonly int _consecutiveRoundWinsToScramble;
private readonly bool _isScrambleEnabled;
private readonly bool _removeSpectatorsEnabled;
private readonly bool _isBalanceEnabled;
public const int ScoreForKill = 50;
public const int ScoreForAssist = 25;
public const int ScoreForDefuse = 50;

public GameManager(Translator translator, QueueManager queueManager, int? roundsToScramble, bool? isScrambleEnabled, bool? RemoveSpectatorsEnabled)
public GameManager(Translator translator, QueueManager queueManager, int? roundsToScramble, bool? isScrambleEnabled, bool? removeSpectatorsEnabled, bool? isBalanceEnabled)
{
_translator = translator;
QueueManager = queueManager;
_consecutiveRoundWinsToScramble = roundsToScramble ?? 5;
_isScrambleEnabled = isScrambleEnabled ?? true;
_removeSpectatorsEnabled = RemoveSpectatorsEnabled ?? false;
_removeSpectatorsEnabled = removeSpectatorsEnabled ?? false;
_isBalanceEnabled = isBalanceEnabled ?? true;
}

private bool _scrambleNextRound;
Expand Down Expand Up @@ -141,8 +143,8 @@ private void CounterTerroristRoundWin()

private void BalanceTeams()
{
List<CCSPlayerController> newTerrorists = new();
List<CCSPlayerController> newCounterTerrorists = new();
List<CCSPlayerController> newTerrorists = [];
List<CCSPlayerController> newCounterTerrorists = [];

var currentNumTerrorist = Helpers.GetCurrentNumPlayers(CsTeam.Terrorist);
var numTerroristsNeeded = QueueManager.GetTargetNumTerrorists() - currentNumTerrorist;
Expand Down Expand Up @@ -199,25 +201,30 @@ private void BalanceTeams()

public void OnRoundPreStart(CsTeam winningTeam)
{
// Handle team swaps during round pre-start.
switch (winningTeam)
if (_isBalanceEnabled)
{
case CsTeam.CounterTerrorist:
CounterTerroristRoundWin();
break;
// Handle team swaps during round pre-start.
switch (winningTeam)
{
case CsTeam.CounterTerrorist:
CounterTerroristRoundWin();
break;

case CsTeam.Terrorist:
TerroristRoundWin();
break;
case CsTeam.Terrorist:
TerroristRoundWin();
break;
}
}


if (_scrambleNextRound)
{
ScrambleTeams();
}

BalanceTeams();
if (_isBalanceEnabled)
{
BalanceTeams();
}
}

private List<CCSPlayerController> GetSortedActivePlayers(CsTeam? team = null)
Expand All @@ -231,8 +238,8 @@ private List<CCSPlayerController> GetSortedActivePlayers(CsTeam? team = null)

private void SetTeams(List<CCSPlayerController>? terrorists, List<CCSPlayerController>? counterTerrorists)
{
terrorists ??= new List<CCSPlayerController>();
counterTerrorists ??= new List<CCSPlayerController>();
terrorists ??= [];
counterTerrorists ??= [];

foreach (var player in QueueManager.ActivePlayers.Where(Helpers.IsValidPlayer))
{
Expand Down
24 changes: 19 additions & 5 deletions RetakesPlugin/Modules/Managers/QueueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@ public class QueueManager
private readonly float _terroristRatio;
private readonly string _queuePriorityFlag;
private readonly bool _shouldForceEvenTeamsWhenPlayerCountIsMultipleOf10;
private readonly bool _shouldPreventTeamChangesMidRound;

public HashSet<CCSPlayerController> QueuePlayers = new();
public HashSet<CCSPlayerController> ActivePlayers = new();
public HashSet<CCSPlayerController> QueuePlayers = [];
public HashSet<CCSPlayerController> ActivePlayers = [];

public QueueManager(
Translator translator,
int? retakesMaxPlayers,
float? retakesTerroristRatio,
string? queuePriorityFlag,
bool? shouldForceEvenTeamsWhenPlayerCountIsMultipleOf10
bool? shouldForceEvenTeamsWhenPlayerCountIsMultipleOf10,
bool? shouldPreventTeamChangesMidRound
)
{
_translator = translator;
_maxRetakesPlayers = retakesMaxPlayers ?? 9;
_terroristRatio = retakesTerroristRatio ?? 0.45f;
_queuePriorityFlag = queuePriorityFlag ?? "@css/vip";
_shouldForceEvenTeamsWhenPlayerCountIsMultipleOf10 = shouldForceEvenTeamsWhenPlayerCountIsMultipleOf10 ?? true;
_shouldPreventTeamChangesMidRound = shouldPreventTeamChangesMidRound ?? true;
}

public int GetTargetNumTerrorists()
Expand Down Expand Up @@ -69,6 +72,12 @@ public HookResult PlayerJoinedTeam(CCSPlayerController player, CsTeam fromTeam,
Helpers.CheckRoundDone();
return HookResult.Continue;
}

if (!_shouldPreventTeamChangesMidRound)
{
Helpers.Debug($"[{player.PlayerName}] Preventing team changes mid round is disabled, allowing team change.");
return HookResult.Continue;
}

if (
_roundTerrorists.Count > 0
Expand Down Expand Up @@ -309,8 +318,8 @@ public void DebugQueues(bool isBefore)
}
}

private List<CCSPlayerController> _roundTerrorists = new();
private List<CCSPlayerController> _roundCounterTerrorists = new();
private List<CCSPlayerController> _roundTerrorists = [];
private List<CCSPlayerController> _roundCounterTerrorists = [];

public void ClearRoundTeams()
{
Expand All @@ -320,6 +329,11 @@ public void ClearRoundTeams()

public void SetRoundTeams()
{
if (!_shouldPreventTeamChangesMidRound)
{
return;
}

_roundTerrorists = ActivePlayers
.Where(player => Helpers.IsValidPlayer(player) && player.Team == CsTeam.Terrorist).ToList();
_roundCounterTerrorists = ActivePlayers
Expand Down
10 changes: 5 additions & 5 deletions RetakesPlugin/Modules/Managers/SpawnManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public void CalculateMapSpawns()

_spawns.Add(Bombsite.A, new Dictionary<CsTeam, List<Spawn>>()
{
{ CsTeam.Terrorist, new List<Spawn>()},
{ CsTeam.CounterTerrorist, new List<Spawn>()}
{ CsTeam.Terrorist, [] },
{ CsTeam.CounterTerrorist, [] }
});
_spawns.Add(Bombsite.B, new Dictionary<CsTeam, List<Spawn>>()
{
{ CsTeam.Terrorist, new List<Spawn>()},
{ CsTeam.CounterTerrorist, new List<Spawn>()}
{ CsTeam.Terrorist, [] },
{ CsTeam.CounterTerrorist, [] }
});

foreach (var spawn in _mapConfig.GetSpawnsClone())
Expand All @@ -42,7 +42,7 @@ public List<Spawn> GetSpawns(Bombsite bombsite, CsTeam? team = null)
{
if (_spawns[bombsite][CsTeam.Terrorist].Count == 0 && _spawns[bombsite][CsTeam.CounterTerrorist].Count == 0)
{
return new List<Spawn>();
return [];
}

if (team == null)
Expand Down
14 changes: 8 additions & 6 deletions RetakesPlugin/RetakesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace RetakesPlugin;
[MinimumApiVersion(220)]
public class RetakesPlugin : BasePlugin
{
private const string Version = "2.0.9";
private const string Version = "2.0.10";

#region Plugin info
public override string ModuleName => "Retakes Plugin";
Expand Down Expand Up @@ -59,7 +59,7 @@ public class RetakesPlugin : BasePlugin
private Bombsite? _forcedBombsite;

// TODO: We should really store this in SQLite, but for now we'll just store it in memory.
private readonly HashSet<CCSPlayerController> _hasMutedVoices = new();
private readonly HashSet<CCSPlayerController> _hasMutedVoices = [];

private void ResetState()
{
Expand Down Expand Up @@ -598,11 +598,13 @@ private void OnMapStart(string mapName, string? customMapConfig = null)
_retakesConfig?.RetakesConfigData?.MaxPlayers,
_retakesConfig?.RetakesConfigData?.TerroristRatio,
_retakesConfig?.RetakesConfigData?.QueuePriorityFlag,
_retakesConfig?.RetakesConfigData?.ShouldForceEvenTeamsWhenPlayerCountIsMultipleOf10
_retakesConfig?.RetakesConfigData?.ShouldForceEvenTeamsWhenPlayerCountIsMultipleOf10,
_retakesConfig?.RetakesConfigData?.ShouldPreventTeamChangesMidRound
),
_retakesConfig?.RetakesConfigData?.RoundsToScramble,
_retakesConfig?.RetakesConfigData?.IsScrambleEnabled,
_retakesConfig?.RetakesConfigData?.ShouldRemoveSpectators
_retakesConfig?.RetakesConfigData?.ShouldRemoveSpectators,
_retakesConfig?.RetakesConfigData?.IsBalanceEnabled
);

_breakerManager = new BreakerManager(
Expand Down Expand Up @@ -1010,15 +1012,15 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo
private void AnnounceBombsite(Bombsite bombsite, bool onlyCenter = false)
{
string[] bombsiteAnnouncers =
{
[
"balkan_epic",
"leet_epic",
"professional_epic",
"professional_fem",
"seal_epic",
"swat_epic",
"swat_fem"
};
];

// Get translation message
var numTerrorist = Helpers.GetCurrentNumPlayers(CsTeam.Terrorist);
Expand Down
Loading