Skip to content
This repository was archived by the owner on Jan 25, 2025. It is now read-only.

Commit 8342355

Browse files
committed
fix: team-balance freeze/crash the server
1 parent 31c7dc1 commit 8342355

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- fix: death statistics counter from bots even if disabled
55
- fix: remove unused code part
66
- fix: disconnect message not shown even if enabled
7+
- fix: team-balance freeze/crash the server
78
- chore: update README.md to use new team standards and fix broken links
89

910
-- 2024.06.03 - V4.4.0

K4-System/src/Module/Rank/RankEvents.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,15 @@ public void Initialize_Events()
167167
var team1Players = players.Where(p => p?.Controller?.Team == CsTeam.Terrorist).ToList();
168168
var team2Players = players.Where(p => p?.Controller?.Team == CsTeam.CounterTerrorist).ToList();
169169

170-
var team1RankPoints = team1Players.Select(p => p?.rankData?.Points ?? 0).Sum();
171-
var team2RankPoints = team2Players.Select(p => p?.rankData?.Points ?? 0).Sum();
170+
var team1RankPoints = team1Players.Sum(p => p?.rankData?.Points ?? 0);
171+
var team2RankPoints = team2Players.Sum(p => p?.rankData?.Points ?? 0);
172172

173-
while (Math.Abs(team1RankPoints - team2RankPoints) > Config.RankSettings.RankBasedTeamBalanceMaxDifference)
173+
int maxSwitches = 10; // Safety break for the loop
174+
175+
while (Math.Abs(team1RankPoints - team2RankPoints) > Config.RankSettings.RankBasedTeamBalanceMaxDifference && maxSwitches > 0)
174176
{
177+
maxSwitches--;
178+
175179
if (team1RankPoints > team2RankPoints)
176180
{
177181
var playerToSwitch = team1Players.OrderByDescending(p => p?.rankData?.Points ?? 0).FirstOrDefault();
@@ -180,6 +184,8 @@ public void Initialize_Events()
180184
team1Players.Remove(playerToSwitch);
181185
team2Players.Add(playerToSwitch);
182186
playerToSwitch.Controller?.ChangeTeam(CsTeam.CounterTerrorist);
187+
team1RankPoints -= playerToSwitch?.rankData?.Points ?? 0;
188+
team2RankPoints += playerToSwitch?.rankData?.Points ?? 0;
183189
}
184190
}
185191
else
@@ -190,14 +196,20 @@ public void Initialize_Events()
190196
team2Players.Remove(playerToSwitch);
191197
team1Players.Add(playerToSwitch);
192198
playerToSwitch.Controller?.ChangeTeam(CsTeam.Terrorist);
199+
team2RankPoints -= playerToSwitch?.rankData?.Points ?? 0;
200+
team1RankPoints += playerToSwitch?.rankData?.Points ?? 0;
193201
}
194202
}
195-
196-
team1RankPoints = team1Players.Select(p => p?.rankData?.Points ?? 0).Sum();
197-
team2RankPoints = team2Players.Select(p => p?.rankData?.Points ?? 0).Sum();
198203
}
199204

200-
Server.PrintToChatAll($" {plugin.Localizer["k4.general.prefix"]} {plugin.Localizer["k4.ranks.rank.teamsbalanced"]}");
205+
if (maxSwitches > 0)
206+
{
207+
Server.PrintToChatAll($" {plugin.Localizer["k4.general.prefix"]} {plugin.Localizer["k4.ranks.rank.teamsbalanced"]}");
208+
}
209+
else
210+
{
211+
plugin.Logger.LogWarning("Max team switch attempts reached, team balance may not be perfect.");
212+
}
201213
}
202214
}
203215
}
@@ -209,7 +221,6 @@ public void Initialize_Events()
209221
return HookResult.Continue;
210222
}, HookMode.Post);
211223

212-
213224
plugin.RegisterEventHandler((EventBombPlanted @event, GameEventInfo info) =>
214225
{
215226
ModifyPlayerPointsConnector(@event.Userid, Config.PointSettings.BombPlant, "k4.phrases.bombplanted");

0 commit comments

Comments
 (0)