Skip to content

Commit

Permalink
Merge pull request #105 from kraigher/fix_scrambling_bugs
Browse files Browse the repository at this point in the history
Fix scrambling bugs #104
  • Loading branch information
B3none authored Apr 6, 2024
2 parents 9e9cc8a + 7174b17 commit a56b400
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 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
22 changes: 4 additions & 18 deletions RetakesPlugin/RetakesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace RetakesPlugin;
[MinimumApiVersion(201)]
public class RetakesPlugin : BasePlugin
{
private const string Version = "2.0.1";
private const string Version = "2.0.2";

#region Plugin info
public override string ModuleName => "Retakes Plugin";
Expand Down 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 a56b400

Please sign in to comment.