Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Commit

Permalink
fix: team-balance freeze/crash the server
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed Jul 24, 2024
1 parent 31c7dc1 commit 8342355
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- fix: death statistics counter from bots even if disabled
- fix: remove unused code part
- fix: disconnect message not shown even if enabled
- fix: team-balance freeze/crash the server
- chore: update README.md to use new team standards and fix broken links

-- 2024.06.03 - V4.4.0
Expand Down
27 changes: 19 additions & 8 deletions K4-System/src/Module/Rank/RankEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ public void Initialize_Events()
var team1Players = players.Where(p => p?.Controller?.Team == CsTeam.Terrorist).ToList();
var team2Players = players.Where(p => p?.Controller?.Team == CsTeam.CounterTerrorist).ToList();
var team1RankPoints = team1Players.Select(p => p?.rankData?.Points ?? 0).Sum();
var team2RankPoints = team2Players.Select(p => p?.rankData?.Points ?? 0).Sum();
var team1RankPoints = team1Players.Sum(p => p?.rankData?.Points ?? 0);
var team2RankPoints = team2Players.Sum(p => p?.rankData?.Points ?? 0);
while (Math.Abs(team1RankPoints - team2RankPoints) > Config.RankSettings.RankBasedTeamBalanceMaxDifference)
int maxSwitches = 10; // Safety break for the loop
while (Math.Abs(team1RankPoints - team2RankPoints) > Config.RankSettings.RankBasedTeamBalanceMaxDifference && maxSwitches > 0)
{
maxSwitches--;
if (team1RankPoints > team2RankPoints)
{
var playerToSwitch = team1Players.OrderByDescending(p => p?.rankData?.Points ?? 0).FirstOrDefault();
Expand All @@ -180,6 +184,8 @@ public void Initialize_Events()
team1Players.Remove(playerToSwitch);
team2Players.Add(playerToSwitch);
playerToSwitch.Controller?.ChangeTeam(CsTeam.CounterTerrorist);
team1RankPoints -= playerToSwitch?.rankData?.Points ?? 0;
team2RankPoints += playerToSwitch?.rankData?.Points ?? 0;
}
}
else
Expand All @@ -190,14 +196,20 @@ public void Initialize_Events()
team2Players.Remove(playerToSwitch);
team1Players.Add(playerToSwitch);
playerToSwitch.Controller?.ChangeTeam(CsTeam.Terrorist);
team2RankPoints -= playerToSwitch?.rankData?.Points ?? 0;
team1RankPoints += playerToSwitch?.rankData?.Points ?? 0;
}
}
team1RankPoints = team1Players.Select(p => p?.rankData?.Points ?? 0).Sum();
team2RankPoints = team2Players.Select(p => p?.rankData?.Points ?? 0).Sum();
}
Server.PrintToChatAll($" {plugin.Localizer["k4.general.prefix"]} {plugin.Localizer["k4.ranks.rank.teamsbalanced"]}");
if (maxSwitches > 0)
{
Server.PrintToChatAll($" {plugin.Localizer["k4.general.prefix"]} {plugin.Localizer["k4.ranks.rank.teamsbalanced"]}");
}
else
{
plugin.Logger.LogWarning("Max team switch attempts reached, team balance may not be perfect.");
}
}
}
}
Expand All @@ -209,7 +221,6 @@ public void Initialize_Events()
return HookResult.Continue;
}, HookMode.Post);


plugin.RegisterEventHandler((EventBombPlanted @event, GameEventInfo info) =>
{
ModifyPlayerPointsConnector(@event.Userid, Config.PointSettings.BombPlant, "k4.phrases.bombplanted");
Expand Down

0 comments on commit 8342355

Please sign in to comment.