From d60c07731ef3a0a25ac66bf8e6f606a6e64f3e8d Mon Sep 17 00:00:00 2001 From: Kalle Date: Fri, 8 Nov 2024 20:05:52 +0100 Subject: [PATCH] draft: simple balancer --- src/TeamBalancer+Config.cs | 3 +- src/TeamBalancer+Utilities.cs | 51 +++++++++++++++++++++++++++++ src/TeamBalancer.cs | 61 +++++++++++++++++++++++++---------- src/lang/de.json | 10 ++++-- src/lang/en.json | 6 +++- 5 files changed, 108 insertions(+), 23 deletions(-) create mode 100644 src/TeamBalancer+Utilities.cs diff --git a/src/TeamBalancer+Config.cs b/src/TeamBalancer+Config.cs index d41810a..1f6299d 100644 --- a/src/TeamBalancer+Config.cs +++ b/src/TeamBalancer+Config.cs @@ -1,5 +1,4 @@ -using System.IO.Enumeration; -using System.Text.Json; +using System.Text.Json; using System.Text.Json.Serialization; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Config; diff --git a/src/TeamBalancer+Utilities.cs b/src/TeamBalancer+Utilities.cs new file mode 100644 index 0000000..400e0dc --- /dev/null +++ b/src/TeamBalancer+Utilities.cs @@ -0,0 +1,51 @@ +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Modules.Utils; + +namespace TeamBalancer +{ + public partial class TeamBalancer : BasePlugin + { + public void SendGlobalChatMessage(string message, float delay = 0) + { + foreach (CCSPlayerController player in Utilities.GetPlayers()) + { + if (player.IsBot) continue; + AddTimer(delay, () => player.PrintToChat(message)); + } + } + + public Tuple CountActivePlayers() + { + int count_t = 0; + int count_ct = 0; + foreach (CCSPlayerController player in Utilities.GetPlayers()) + { + if (player.Team == CsTeam.CounterTerrorist) + { + count_ct++; + } + else if (player.Team == CsTeam.Terrorist) + { + count_t++; + } + } + return Tuple.Create(count_t, count_ct); + } + + public int GetTeamScore(CsTeam team) + { + var teamManagers = Utilities.FindAllEntitiesByDesignerName("cs_team_manager"); + + foreach (var teamManager in teamManagers) + { + if ((int)team == teamManager.TeamNum) + { + return teamManager.Score; + } + } + + return 0; + } + } +} diff --git a/src/TeamBalancer.cs b/src/TeamBalancer.cs index b01c083..07c9a07 100644 --- a/src/TeamBalancer.cs +++ b/src/TeamBalancer.cs @@ -1,7 +1,5 @@ -using CounterStrikeSharp.API; -using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Utils; -using System.IO.Compression; namespace TeamBalancer { @@ -9,13 +7,15 @@ public partial class TeamBalancer : BasePlugin { public override string ModuleName => "Team Balancer"; public override string ModuleAuthor => "Jon-Mailes Graeffe / Kalle "; - public override string ModuleVersion => "0.0.1"; + public override string ModuleVersion => "0.0.2"; public override void Load(bool hotReload) { // initialize configuration LoadConfig(); SaveConfig(); + // create listeners + RegisterEventHandler(OnPlayerTeam); // print message if hot reload if (hotReload) { @@ -28,22 +28,49 @@ public override void Unload(bool hotReload) Console.WriteLine(Localizer["core.unload"]); } - public Tuple CountActivePlayers() + public HookResult OnPlayerTeam(EventPlayerTeam @event, GameEventInfo info) { - int count_t = 0; - int count_ct = 0; - foreach (CCSPlayerController player in Utilities.GetPlayers()) + var player = @event.Userid; + if (player == null + || @event.Team == (byte)CsTeam.Spectator + || @event.Team == (byte)CsTeam.None) return HookResult.Continue; + // get initial data + int score_t = GetTeamScore(CsTeam.Terrorist); + int score_ct = GetTeamScore(CsTeam.CounterTerrorist); + var (count_t, count_ct) = CountActivePlayers(); + // substract counter depending on team to match current players per team (because this is a Hook) + if (@event.Team == (byte)CsTeam.Terrorist) { - if (player.Team == CsTeam.CounterTerrorist) - { - count_ct++; - } - else if (player.Team == CsTeam.Terrorist) - { - count_t++; - } + count_t -= 1; } - return Tuple.Create(count_t, count_ct); + else if (@event.Team == (byte)CsTeam.CounterTerrorist) + { + count_ct -= 1; + } + // check if player should be switched to CT (if T) or vice versa + if (@event.Team == (byte)CsTeam.Terrorist + && count_ct <= count_t + && score_t - score_ct >= 2) + { + player.ChangeTeam(CsTeam.CounterTerrorist); + player.PrintToCenterAlert(Localizer["switch.to_ct_center"].Value + .Replace("{player}", player.PlayerName)); + SendGlobalChatMessage(Localizer["switch.to_ct_chat"].Value + .Replace("{player}", player.PlayerName)); + } + else if (@event.Team == (byte)CsTeam.CounterTerrorist + && count_t <= count_ct + && score_ct - score_t >= 2) + { + player.ChangeTeam(CsTeam.Terrorist); + player.PrintToCenterAlert(Localizer["switch.to_t_center"].Value + .Replace("{player}", player.PlayerName)); + SendGlobalChatMessage(Localizer["switch.to_t_chat"].Value + .Replace("{player}", player.PlayerName)); + } + return HookResult.Continue; } + + } } diff --git a/src/lang/de.json b/src/lang/de.json index ce83b91..bd0d281 100644 --- a/src/lang/de.json +++ b/src/lang/de.json @@ -1,5 +1,9 @@ { - "core.hotreload": "[TeamBalancer] Neu geladen!", - "core.unload": "[TeamBalancer] Plugin entladen!", - "config.loaded": "[TeamBalancer] Konfiguration geladen!" + "core.hotreload": "[Teamausgleich] Neu geladen!", + "core.unload": "[Teamausgleich] Plugin entladen!", + "config.loaded": "[Teamausgleich] Konfiguration geladen!", + "switch.to_t_center": "Du bist nun im Terroristen-Team!", + "switch.to_t_chat": "{magenta}[Teamausgleich] {default}bewegte {green}{player} {default}zu den {green}Terroristen{default}!", + "switch.to_ct_center": "Du bist nun im Antiterroristen-Team!", + "switch.to_ct_chat": "{magenta}[Teamausgleich] {default}bewegte {green}{player} {default}zu den {green}Antiterroristen{default}!" } \ No newline at end of file diff --git a/src/lang/en.json b/src/lang/en.json index 61f04c8..028c673 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -1,5 +1,9 @@ { "core.hotreload": "[TeamBalancer] Hot-Reloaded Plugin!", "core.unload": "[TeamBalancer] Unloaded Plugin!", - "config.loaded": "[TeamBalancer] Loaded configuration!" + "config.loaded": "[TeamBalancer] Loaded configuration!", + "switch.to_t_center": "You joined the Terrorist Team!", + "switch.to_t_chat": "{magenta}[TeamBalancer] {default}switched {green}{player} {default}to {green}Terrorists{default}!", + "switch.to_ct_center": "You joined the CounterTerrorist Team!", + "switch.to_ct_chat": "{magenta}[TeamBalancer] {default}switched {green}{player} {default}to {green}CounterTerrorists{default}!" } \ No newline at end of file