-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathGameManager.cs
281 lines (234 loc) · 10.1 KB
/
GameManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
namespace RetakesPlugin.Modules.Managers;
public class GameManager
{
private readonly Translator _translator;
private Dictionary<int, int> _playerRoundScores = new();
public readonly QueueManager QueueManager;
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, bool? isBalanceEnabled)
{
_translator = translator;
QueueManager = queueManager;
_consecutiveRoundWinsToScramble = roundsToScramble ?? 5;
_isScrambleEnabled = isScrambleEnabled ?? true;
_removeSpectatorsEnabled = removeSpectatorsEnabled ?? false;
_isBalanceEnabled = isBalanceEnabled ?? true;
}
private bool _scrambleNextRound;
public void ScrambleNextRound(CCSPlayerController? admin = null)
{
_scrambleNextRound = true;
Server.PrintToChatAll(
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.admin_scramble", admin?.PlayerName ?? "The server owner"]}");
}
private void ScrambleTeams()
{
_scrambleNextRound = false;
_consecutiveRoundsWon = 0;
var shuffledActivePlayers = Helpers.Shuffle(QueueManager.ActivePlayers);
var newTerrorists = shuffledActivePlayers.Take(QueueManager.GetTargetNumTerrorists()).ToList();
var newCounterTerrorists = shuffledActivePlayers.Except(newTerrorists).ToList();
SetTeams(newTerrorists, newCounterTerrorists);
}
public void ResetPlayerScores()
{
_playerRoundScores = new Dictionary<int, int>();
}
public void AddScore(CCSPlayerController player, int score)
{
if (!Helpers.IsValidPlayer(player) || player.UserId == null)
{
return;
}
var playerId = (int)player.UserId;
if (!_playerRoundScores.TryAdd(playerId, score))
{
// Add to the player's existing score
_playerRoundScores[playerId] += score;
}
}
private int _consecutiveRoundsWon;
private void TerroristRoundWin()
{
_consecutiveRoundsWon++;
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]}");
ScrambleTeams();
}
else if (shouldAlmostScramble)
{
Server.PrintToChatAll(
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.almost_scramble", _consecutiveRoundsWon, roundsLeftToScramble]}");
}
else if (_consecutiveRoundsWon >= 3)
{
Server.PrintToChatAll(
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.win_streak", _consecutiveRoundsWon]}");
}
}
private void CounterTerroristRoundWin()
{
if (_consecutiveRoundsWon >= 3)
{
Server.PrintToChatAll(
$"{RetakesPlugin.MessagePrefix}{_translator["retakes.teams.win_streak_over", _consecutiveRoundsWon]}");
}
_consecutiveRoundsWon = 0;
var targetNumTerrorists = QueueManager.GetTargetNumTerrorists();
var sortedCounterTerroristPlayers = GetSortedActivePlayers(CsTeam.CounterTerrorist);
// Ensure that the players with the scores are set as new terrorists first.
var newTerrorists = sortedCounterTerroristPlayers.Where(player => player.Score > 0).Take(targetNumTerrorists)
.ToList();
if (newTerrorists.Count < targetNumTerrorists)
{
// Shuffle the other players with 0 score to ensure it's random who is swapped
var playersLeft = Helpers.Shuffle(sortedCounterTerroristPlayers.Except(newTerrorists).ToList());
newTerrorists.AddRange(playersLeft.Take(targetNumTerrorists - newTerrorists.Count));
}
if (newTerrorists.Count < targetNumTerrorists)
{
// If we still don't have enough terrorists
newTerrorists.AddRange(
GetSortedActivePlayers(CsTeam.Terrorist)
.Take(targetNumTerrorists - newTerrorists.Count)
);
}
newTerrorists.AddRange(sortedCounterTerroristPlayers.Where(player => player.Score > 0)
.Take(targetNumTerrorists - newTerrorists.Count).ToList());
var newCounterTerrorists = QueueManager.ActivePlayers.Except(newTerrorists).ToList();
SetTeams(newTerrorists, newCounterTerrorists);
}
private void BalanceTeams()
{
List<CCSPlayerController> newTerrorists = [];
List<CCSPlayerController> newCounterTerrorists = [];
var currentNumTerrorist = Helpers.GetCurrentNumPlayers(CsTeam.Terrorist);
var numTerroristsNeeded = QueueManager.GetTargetNumTerrorists() - currentNumTerrorist;
if (numTerroristsNeeded > 0)
{
var sortedCounterTerroristPlayers = GetSortedActivePlayers(CsTeam.CounterTerrorist);
newTerrorists = sortedCounterTerroristPlayers.Where(player => player.Score > 0)
.Take(numTerroristsNeeded).ToList();
if (newTerrorists.Count < numTerroristsNeeded)
{
var playersLeft = Helpers.Shuffle(sortedCounterTerroristPlayers.Except(newTerrorists).ToList());
newTerrorists.AddRange(playersLeft.Take(numTerroristsNeeded - newTerrorists.Count));
}
}
var currentNumCounterTerroristAfterBalance = Helpers.GetCurrentNumPlayers(CsTeam.CounterTerrorist);
var numCounterTerroristsNeeded =
QueueManager.GetTargetNumCounterTerrorists() - currentNumCounterTerroristAfterBalance;
if (numCounterTerroristsNeeded > 0)
{
var terroristsWithZeroScore = QueueManager.ActivePlayers
.Where(player =>
Helpers.IsValidPlayer(player)
&& player.Team == CsTeam.Terrorist
&& _playerRoundScores.GetValueOrDefault((int)player.UserId!, 0) == 0
)
.Except(newTerrorists)
.ToList();
// Shuffle to avoid repetitive swapping of the same players
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(
QueueManager.ActivePlayers
.Except(newCounterTerrorists)
.Except(newTerrorists)
.Where(player => Helpers.IsValidPlayer(player) && player.Team == CsTeam.Terrorist)
.OrderBy(player => _playerRoundScores.GetValueOrDefault((int)player.UserId!, 0))
.Take(numTerroristsNeeded - newCounterTerrorists.Count)
.ToList()
);
}
}
SetTeams(newTerrorists, newCounterTerrorists);
}
public void OnRoundPreStart(CsTeam winningTeam)
{
// Handle team swaps during round pre-start.
switch (winningTeam)
{
case CsTeam.CounterTerrorist:
if (_isBalanceEnabled)
{
CounterTerroristRoundWin();
}
break;
case CsTeam.Terrorist:
TerroristRoundWin();
break;
}
if (_scrambleNextRound)
{
ScrambleTeams();
}
if (_isBalanceEnabled)
{
BalanceTeams();
}
}
private List<CCSPlayerController> GetSortedActivePlayers(CsTeam? team = null)
{
return QueueManager.ActivePlayers
.Where(Helpers.IsValidPlayer)
.Where(player => team == null || player.Team == team)
.OrderByDescending(player => _playerRoundScores.GetValueOrDefault((int)player.UserId!, 0))
.ToList();
}
private void SetTeams(List<CCSPlayerController>? terrorists, List<CCSPlayerController>? counterTerrorists)
{
terrorists ??= [];
counterTerrorists ??= [];
foreach (var player in QueueManager.ActivePlayers.Where(Helpers.IsValidPlayer))
{
if (terrorists.Contains(player))
{
player.SwitchTeam(CsTeam.Terrorist);
}
else if (counterTerrorists.Contains(player))
{
player.SwitchTeam(CsTeam.CounterTerrorist);
}
}
}
public HookResult RemoveSpectators(EventPlayerTeam @event, HashSet<CCSPlayerController> _hasMutedVoices)
{
if (_removeSpectatorsEnabled)
{
CCSPlayerController? player = @event.Userid;
if (!Helpers.IsValidPlayer(player))
{
return HookResult.Continue;
}
int team = @event.Team;
if (team == (int)CsTeam.Spectator)
{
// Ensure player is active ingame.
if (QueueManager.ActivePlayers.Contains(player))
{
QueueManager.RemovePlayerFromQueues(player);
_hasMutedVoices.Remove(player);
}
}
}
return HookResult.Continue;
}
}