Skip to content

Commit

Permalink
draft: simple balancer
Browse files Browse the repository at this point in the history
  • Loading branch information
derkalle4 committed Nov 8, 2024
1 parent 1ec7f42 commit d60c077
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 23 deletions.
3 changes: 1 addition & 2 deletions src/TeamBalancer+Config.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
51 changes: 51 additions & 0 deletions src/TeamBalancer+Utilities.cs
Original file line number Diff line number Diff line change
@@ -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<int, int> 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<CCSTeam>("cs_team_manager");

foreach (var teamManager in teamManagers)
{
if ((int)team == teamManager.TeamNum)
{
return teamManager.Score;
}
}

return 0;
}
}
}
61 changes: 44 additions & 17 deletions src/TeamBalancer.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using System.IO.Compression;

namespace TeamBalancer
{
public partial class TeamBalancer : BasePlugin
{
public override string ModuleName => "Team Balancer";
public override string ModuleAuthor => "Jon-Mailes Graeffe <[email protected]> / Kalle <[email protected]>";
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<EventPlayerTeam>(OnPlayerTeam);
// print message if hot reload
if (hotReload)
{
Expand All @@ -28,22 +28,49 @@ public override void Unload(bool hotReload)
Console.WriteLine(Localizer["core.unload"]);
}

public Tuple<int, int> 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;
}


}
}
10 changes: 7 additions & 3 deletions src/lang/de.json
Original file line number Diff line number Diff line change
@@ -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}!"
}
6 changes: 5 additions & 1 deletion src/lang/en.json
Original file line number Diff line number Diff line change
@@ -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}!"
}

0 comments on commit d60c077

Please sign in to comment.