-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
108 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
|
@@ -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; | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}!" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}!" | ||
} |