Skip to content

Commit a56b400

Browse files
authored
Merge pull request #105 from kraigher/fix_scrambling_bugs
Fix scrambling bugs #104
2 parents 9e9cc8a + 7174b17 commit a56b400

File tree

2 files changed

+45
-38
lines changed

2 files changed

+45
-38
lines changed

RetakesPlugin/Modules/Managers/GameManager.cs

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public void ScrambleNextRound(CCSPlayerController? admin = null)
3535

3636
private void ScrambleTeams()
3737
{
38+
_scrambleNextRound = false;
39+
_consecutiveRoundsWon = 0;
40+
3841
var shuffledActivePlayers = Helpers.Shuffle(QueueManager.ActivePlayers);
3942

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

6770
private int _consecutiveRoundsWon;
6871

69-
public void TerroristRoundWin()
72+
private void TerroristRoundWin()
7073
{
7174
_consecutiveRoundsWon++;
7275

73-
if (_consecutiveRoundsWon == _consecutiveRoundWinsToScramble)
76+
var shouldScrambleNow = _isScrambleEnabled && _consecutiveRoundsWon == _consecutiveRoundWinsToScramble;
77+
var roundsLeftToScramble = _consecutiveRoundWinsToScramble - _consecutiveRoundsWon;
78+
// Almost scramble if 1-2 rounds left to automatic scramble
79+
var shouldAlmostScramble = _isScrambleEnabled && roundsLeftToScramble > 0 && roundsLeftToScramble <= 2;
80+
81+
if (shouldScrambleNow)
7482
{
7583
Server.PrintToChatAll(
7684
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.scramble", _consecutiveRoundWinsToScramble]}");
7785

78-
_consecutiveRoundsWon = 0;
7986
ScrambleTeams();
8087
}
81-
else if (_consecutiveRoundsWon >= 3)
88+
else if (shouldAlmostScramble)
8289
{
83-
if (_isScrambleEnabled)
84-
{
85-
Server.PrintToChatAll(
86-
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.almost_scramble", _consecutiveRoundsWon, _consecutiveRoundWinsToScramble - _consecutiveRoundsWon]}");
87-
}
88-
else
89-
{
90-
Server.PrintToChatAll(
91-
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.win_streak", _consecutiveRoundsWon]}");
92-
}
90+
Server.PrintToChatAll(
91+
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.almost_scramble", _consecutiveRoundsWon, roundsLeftToScramble]}");
9392
}
94-
else if (_scrambleNextRound)
93+
else if (_consecutiveRoundsWon >= 3)
9594
{
96-
_scrambleNextRound = false;
97-
_consecutiveRoundsWon = 0;
98-
ScrambleTeams();
95+
Server.PrintToChatAll(
96+
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.win_streak", _consecutiveRoundsWon]}");
9997
}
10098
}
10199

102-
public void CounterTerroristRoundWin()
100+
private void CounterTerroristRoundWin()
103101
{
104102
if (_consecutiveRoundsWon >= 3)
105103
{
@@ -140,7 +138,7 @@ public void CounterTerroristRoundWin()
140138
SetTeams(newTerrorists, newCounterTerrorists);
141139
}
142140

143-
public void BalanceTeams()
141+
private void BalanceTeams()
144142
{
145143
List<CCSPlayerController> newTerrorists = new();
146144
List<CCSPlayerController> newCounterTerrorists = new();
@@ -198,6 +196,29 @@ public void BalanceTeams()
198196
SetTeams(newTerrorists, newCounterTerrorists);
199197
}
200198

199+
public void OnRoundPreStart(CsTeam winningTeam)
200+
{
201+
// Handle team swaps during round pre-start.
202+
switch (winningTeam)
203+
{
204+
case CsTeam.CounterTerrorist:
205+
CounterTerroristRoundWin();
206+
break;
207+
208+
case CsTeam.Terrorist:
209+
TerroristRoundWin();
210+
break;
211+
}
212+
213+
214+
if (_scrambleNextRound)
215+
{
216+
ScrambleTeams();
217+
}
218+
219+
BalanceTeams();
220+
}
221+
201222
private List<CCSPlayerController> GetSortedActivePlayers(CsTeam? team = null)
202223
{
203224
return QueueManager.ActivePlayers

RetakesPlugin/RetakesPlugin.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace RetakesPlugin;
2020
[MinimumApiVersion(201)]
2121
public class RetakesPlugin : BasePlugin
2222
{
23-
private const string Version = "2.0.1";
23+
private const string Version = "2.0.2";
2424

2525
#region Plugin info
2626
public override string ModuleName => "Retakes Plugin";
@@ -557,23 +557,9 @@ public HookResult OnRoundPreStart(EventRoundPrestart @event, GameEventInfo info)
557557
_gameManager.QueueManager.DebugQueues(false);
558558
Helpers.Debug($"Updated queues.");
559559

560-
// Handle team swaps during round pre-start.
561-
switch (_lastRoundWinner)
562-
{
563-
case CsTeam.CounterTerrorist:
564-
Helpers.Debug($"Calling CounterTerroristRoundWin()");
565-
_gameManager.CounterTerroristRoundWin();
566-
Helpers.Debug($"CounterTerroristRoundWin call complete");
567-
break;
568-
569-
case CsTeam.Terrorist:
570-
Helpers.Debug($"Calling TerroristRoundWin()");
571-
_gameManager.TerroristRoundWin();
572-
Helpers.Debug($"TerroristRoundWin call complete");
573-
break;
574-
}
575-
576-
_gameManager.BalanceTeams();
560+
Helpers.Debug($"Calling GameManager.OnRoundPreStart({_lastRoundWinner})");
561+
_gameManager.OnRoundPreStart(_lastRoundWinner);
562+
Helpers.Debug($"GameManager.OnRoundPreStart call complete");
577563

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

0 commit comments

Comments
 (0)