Skip to content

Commit

Permalink
Fix scrambling bugs B3none#104
Browse files Browse the repository at this point in the history
- Ensures ScrambleTeams() is not automatically called if IsScrambleEnabled is False
- Fix almost scamble message to take roundsToScramble configuration into account.
- Fix admin team scramble only done on T-win and not if more than 3
  rounds in a row have been won.
  • Loading branch information
kraigher committed Apr 6, 2024
1 parent 9e9cc8a commit be9895b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
61 changes: 41 additions & 20 deletions RetakesPlugin/Modules/Managers/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public void ScrambleNextRound(CCSPlayerController? admin = null)

private void ScrambleTeams()
{
_scrambleNextRound = false;
_consecutiveRoundsWon = 0;

var shuffledActivePlayers = Helpers.Shuffle(QueueManager.ActivePlayers);

var newTerrorists = shuffledActivePlayers.Take(QueueManager.GetTargetNumTerrorists()).ToList();
Expand Down Expand Up @@ -66,40 +69,35 @@ public void AddScore(CCSPlayerController player, int score)

private int _consecutiveRoundsWon;

public void TerroristRoundWin()
private void TerroristRoundWin()
{
_consecutiveRoundsWon++;

if (_consecutiveRoundsWon == _consecutiveRoundWinsToScramble)
var shouldScrambleNow = _isScrambleEnabled && _consecutiveRoundsWon == _consecutiveRoundWinsToScramble;
var roundsLeftToScramble = _consecutiveRoundWinsToScramble - _consecutiveRoundsWon;
// Almost scramble if 1-2 rounds left to automatic scramble
var shouldAlmostScramble = _isScrambleEnabled && roundsLeftToScramble > 0 && roundsLeftToScramble <= 2;

if (shouldScrambleNow)
{
Server.PrintToChatAll(
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.scramble", _consecutiveRoundWinsToScramble]}");

_consecutiveRoundsWon = 0;
ScrambleTeams();
}
else if (_consecutiveRoundsWon >= 3)
else if (shouldAlmostScramble)
{
if (_isScrambleEnabled)
{
Server.PrintToChatAll(
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.almost_scramble", _consecutiveRoundsWon, _consecutiveRoundWinsToScramble - _consecutiveRoundsWon]}");
}
else
{
Server.PrintToChatAll(
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.win_streak", _consecutiveRoundsWon]}");
}
Server.PrintToChatAll(
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.almost_scramble", _consecutiveRoundsWon, roundsLeftToScramble]}");
}
else if (_scrambleNextRound)
else if (_consecutiveRoundsWon >= 3)
{
_scrambleNextRound = false;
_consecutiveRoundsWon = 0;
ScrambleTeams();
Server.PrintToChatAll(
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.win_streak", _consecutiveRoundsWon]}");
}
}

public void CounterTerroristRoundWin()
private void CounterTerroristRoundWin()
{
if (_consecutiveRoundsWon >= 3)
{
Expand Down Expand Up @@ -140,7 +138,7 @@ public void CounterTerroristRoundWin()
SetTeams(newTerrorists, newCounterTerrorists);
}

public void BalanceTeams()
private void BalanceTeams()
{
List<CCSPlayerController> newTerrorists = new();
List<CCSPlayerController> newCounterTerrorists = new();
Expand Down Expand Up @@ -198,6 +196,29 @@ public void BalanceTeams()
SetTeams(newTerrorists, newCounterTerrorists);
}

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

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


if (_scrambleNextRound)
{
ScrambleTeams();
}

BalanceTeams();
}

private List<CCSPlayerController> GetSortedActivePlayers(CsTeam? team = null)
{
return QueueManager.ActivePlayers
Expand Down
20 changes: 3 additions & 17 deletions RetakesPlugin/RetakesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,23 +557,9 @@ public HookResult OnRoundPreStart(EventRoundPrestart @event, GameEventInfo info)
_gameManager.QueueManager.DebugQueues(false);
Helpers.Debug($"Updated queues.");

// Handle team swaps during round pre-start.
switch (_lastRoundWinner)
{
case CsTeam.CounterTerrorist:
Helpers.Debug($"Calling CounterTerroristRoundWin()");
_gameManager.CounterTerroristRoundWin();
Helpers.Debug($"CounterTerroristRoundWin call complete");
break;

case CsTeam.Terrorist:
Helpers.Debug($"Calling TerroristRoundWin()");
_gameManager.TerroristRoundWin();
Helpers.Debug($"TerroristRoundWin call complete");
break;
}

_gameManager.BalanceTeams();
Helpers.Debug($"Calling GameManager.OnRoundPreStart({_lastRoundWinner})");
_gameManager.OnRoundPreStart(_lastRoundWinner);
Helpers.Debug($"GameManager.OnRoundPreStart call complete");

// Set round teams to prevent team changes mid round
_gameManager.QueueManager.SetRoundTeams();
Expand Down

0 comments on commit be9895b

Please sign in to comment.