Skip to content

Commit 71483fe

Browse files
authored
Refactor spectator kick logic and error handling
1 parent 905607b commit 71483fe

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

ServerStats.cs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -984,24 +984,32 @@ private void OnNoHumansRestartTimer()
984984
private void KickSpectatorsAndRestart()
985985
{
986986
_spectatorKickTimer = null;
987-
var allPlayers = Utilities.GetPlayers();
988-
bool kicked = false;
989987

990-
foreach (var p in allPlayers)
988+
try
991989
{
992-
if (p != null && p.IsValid && !p.IsBot && !p.IsHLTV && p.TeamNum == 1)
990+
var allPlayers = Utilities.GetPlayers();
991+
bool kicked = false;
992+
993+
foreach (var p in allPlayers)
993994
{
994-
Server.ExecuteCommand($"kickid {p.UserId} \"AFK Spectator\"");
995-
kicked = true;
995+
if (p != null && p.IsValid && !p.IsBot && !p.IsHLTV && p.TeamNum == 1)
996+
{
997+
Server.ExecuteCommand($"kickid {p.UserId} \"AFK Spectator\"");
998+
kicked = true;
999+
}
9961000
}
997-
}
9981001

999-
if (kicked)
1002+
if (kicked)
1003+
{
1004+
Console.WriteLine("[ServerStats] Kicked spectators due to inactivity.");
1005+
}
1006+
1007+
Server.ExecuteCommand("mp_restartgame 1");
1008+
}
1009+
catch (Exception ex)
10001010
{
1001-
Console.WriteLine("[ServerStats] Kicked spectators due to inactivity.");
1011+
Console.WriteLine($"[ServerStats] KickSpectatorsAndRestart error (possibly mid-map-transition): {ex.Message}");
10021012
}
1003-
1004-
Server.ExecuteCommand("mp_restartgame 1");
10051013
}
10061014

10071015
private PlayerMatchData GetOrAddPlayer(CCSPlayerController player)
@@ -1113,7 +1121,8 @@ private HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
11131121
Server.PrintToChatAll($" {ChatColors.Yellow}{data.Name} just got an Ace!");
11141122
}
11151123

1116-
if (weaponName.Contains("taser", StringComparison.OrdinalIgnoreCase))
1124+
bool victimIsBot = victim != null && victim.IsValid && victim.IsBot;
1125+
if (weaponName.Contains("taser", StringComparison.OrdinalIgnoreCase) && !IsWarmup() && !victimIsBot)
11171126
{
11181127
data.CurrentZeusKills++;
11191128
if (data.CurrentZeusKills > _highestZeusKills)
@@ -1259,8 +1268,8 @@ private void SnapshotRoundStats()
12591268
}
12601269
}
12611270

1262-
// Update stats for all tracked players
1263-
foreach (var data in _matchData.Players)
1271+
// Update stats for all tracked players (snapshot to avoid collection-modified errors)
1272+
foreach (var data in _matchData.Players.ToList())
12641273
{
12651274
var playerEntity = Utilities.GetPlayers().FirstOrDefault(p =>
12661275
{

0 commit comments

Comments
 (0)