Skip to content

Commit

Permalink
Merge pull request #44 from B3none/player-join-improvements
Browse files Browse the repository at this point in the history
Player join improvements and breaker manager stability improvements
  • Loading branch information
B3none authored Jan 21, 2024
2 parents e48a46e + 573cf04 commit 903945b
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 93 deletions.
24 changes: 4 additions & 20 deletions Modules/Managers/BreakerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,17 @@ public void Handle()
{
case "func_breakable":
case "func_breakable_surf":
case "prop_dynamic":
case "prop.breakable.01":
case "prop.breakable.02":
var breakableEntity = new PointerTo<CBreakable>(pEntity.Handle).Value;

if (breakableEntity.IsValid)
{
breakableEntity.AcceptInput(action);
}
break;

case "prop.breakable.01":
case "prop.breakable.02":
var breakableProp = new PointerTo<CBreakableProp>(pEntity.Handle).Value;

if (breakableProp.IsValid)
{
breakableProp.AcceptInput(action);
}
break;

case "prop_dynamic":
var dynamicProp = new PointerTo<CDynamicProp>(pEntity.Handle).Value;

if (dynamicProp.IsValid)
{
dynamicProp.AcceptInput(action);
}
break;


case "func_button":
var button = new PointerTo<CBaseButton>(pEntity.Handle).Value;

Expand Down
132 changes: 69 additions & 63 deletions Modules/Managers/QueueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,40 @@ public int GetTargetNumTerrorists()
// Ensure at least one terrorist if the calculated number is zero
return numTerrorists > 0 ? numTerrorists : 1;
}

public int GetTargetNumCounterTerrorists()
{
return ActivePlayers.Count - GetTargetNumTerrorists();
}

public void PlayerJoinedTeam(CCSPlayerController player, CsTeam fromTeam, CsTeam toTeam)
public HookResult PlayerJoinedTeam(CCSPlayerController player, CsTeam fromTeam, CsTeam toTeam)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] PlayerTriedToJoinTeam called.");

if (
fromTeam == CsTeam.None && toTeam == CsTeam.Spectator
fromTeam == CsTeam.None && toTeam == CsTeam.Spectator
|| fromTeam == CsTeam.Spectator && toTeam == CsTeam.None
|| fromTeam == toTeam && toTeam == CsTeam.None
)
{
// This is called when a player first joins.
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] {fromTeam.ToString()} -> {toTeam.ToString()}.");
return;
Console.WriteLine(
$"{RetakesPlugin.LogPrefix}[{player.PlayerName}] {fromTeam.ToString()} -> {toTeam.ToString()}.");
return HookResult.Continue;
}

Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Checking ActivePlayers.");
if (ActivePlayers.Contains(player))
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Player is an active player.");

if (toTeam == CsTeam.Spectator)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Switching to spectator.");
RemovePlayerFromQueues(player);
return;
return HookResult.Continue;
}

if (
_roundTerrorists.Count > 0
&& _roundCounterTerrorists.Count > 0
Expand All @@ -80,13 +81,13 @@ public void PlayerJoinedTeam(CCSPlayerController player, CsTeam fromTeam, CsTeam
{
player.CommitSuicide(false, true);
}

player.ChangeTeam(CsTeam.Spectator);
return;
return HookResult.Handled;
}
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Do nothing.");
return;

Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] The player tried joining the team they're already on, or, there were not enough players so we don't care. Do nothing.");
return HookResult.Handled;
}

Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Checking QueuePlayers.");
Expand All @@ -96,63 +97,58 @@ public void PlayerJoinedTeam(CCSPlayerController player, CsTeam fromTeam, CsTeam
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Not found, adding to ActivePlayers (because in warmup).");
ActivePlayers.Add(player);
return;
return HookResult.Continue;
}

Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Not found, adding to QueuePlayers.");
player.PrintToChat($"{RetakesPlugin.MessagePrefix}{_translator["queue.joined"]}");
QueuePlayers.Add(player);
return HookResult.Handled;
}
else
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Found in QueuePlayers, do nothing.");
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Already in Queue, do nothing.");
}

if (toTeam != CsTeam.Spectator)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}[{player.PlayerName}] Changing to spectator.");
if (player.PawnIsAlive)
{
player.CommitSuicide(false, true);
}

player.ChangeTeam(CsTeam.Spectator);
return;
}

Helpers.CheckRoundDone();
return HookResult.Handled;
}

private void RemoveDisconnectedPlayers()
{
var disconnectedActivePlayers = ActivePlayers.Where(player => !Helpers.IsValidPlayer(player) || !Helpers.IsPlayerConnected(player)).ToList();
var disconnectedActivePlayers = ActivePlayers
.Where(player => !Helpers.IsValidPlayer(player) || !Helpers.IsPlayerConnected(player)).ToList();

if (disconnectedActivePlayers.Count > 0)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}Removing {disconnectedActivePlayers.Count} disconnected players from ActivePlayers.");
Console.WriteLine(
$"{RetakesPlugin.LogPrefix}Removing {disconnectedActivePlayers.Count} disconnected players from ActivePlayers.");
ActivePlayers.RemoveWhere(player => disconnectedActivePlayers.Contains(player));
}

var disconnectedQueuePlayers = QueuePlayers.Where(player => !Helpers.IsValidPlayer(player) || !Helpers.IsPlayerConnected(player)).ToList();


var disconnectedQueuePlayers = QueuePlayers
.Where(player => !Helpers.IsValidPlayer(player) || !Helpers.IsPlayerConnected(player)).ToList();

if (disconnectedQueuePlayers.Count > 0)
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}Removing {disconnectedQueuePlayers.Count} disconnected players from QueuePlayers.");
Console.WriteLine(
$"{RetakesPlugin.LogPrefix}Removing {disconnectedQueuePlayers.Count} disconnected players from QueuePlayers.");
QueuePlayers.RemoveWhere(player => disconnectedQueuePlayers.Contains(player));
}
}

private void HandleQueuePriority()
{
var vipsInQueue = QueuePlayers.Where(player => AdminManager.PlayerHasPermissions(player, "@css/vip")).ToList().Count;
var vipsInQueue = QueuePlayers.Where(player => AdminManager.PlayerHasPermissions(player, "@css/vip")).ToList()
.Count;

if (vipsInQueue > 0)
{
Helpers.Shuffle(
ActivePlayers
.Where(player => !AdminManager.PlayerHasPermissions(player, "@css/vip"))
.ToList()
)
ActivePlayers
.Where(player => !AdminManager.PlayerHasPermissions(player, "@css/vip"))
.ToList()
)
.ForEach(player =>
{
if (vipsInQueue <= 0)
Expand All @@ -166,12 +162,12 @@ private void HandleQueuePriority()
});
}
}

public void Update()
{
RemoveDisconnectedPlayers();
HandleQueuePriority();

var playersToAdd = _maxRetakesPlayers - ActivePlayers.Count;

if (playersToAdd > 0 && QueuePlayers.Count > 0)
Expand All @@ -183,9 +179,9 @@ public void Update()
.OrderBy(player => AdminManager.PlayerHasPermissions(player, "@css/vip"))
.Take(playersToAdd)
.ToList();

QueuePlayers.RemoveWhere(playersToAddList.Contains);

// loop players to add, and set their team to CT
foreach (var player in playersToAddList)
{
Expand All @@ -194,9 +190,9 @@ public void Update()
{
continue;
}

ActivePlayers.Add(player);

if (player.Team != CsTeam.CounterTerrorist)
{
player.SwitchTeam(CsTeam.CounterTerrorist);
Expand All @@ -207,7 +203,7 @@ public void Update()
if (ActivePlayers.Count == _maxRetakesPlayers && QueuePlayers.Count > 0)
{
var waitingMessage = _translator["queue.waiting", ActivePlayers.Count];

foreach (var player in QueuePlayers)
{
player.PrintToChat($"{RetakesPlugin.MessagePrefix}{waitingMessage}");
Expand All @@ -221,61 +217,71 @@ public void RemovePlayerFromQueues(CCSPlayerController player)
QueuePlayers.Remove(player);
_roundTerrorists.Remove(player);
_roundCounterTerrorists.Remove(player);

Helpers.CheckRoundDone();
}

public void DebugQueues(bool isBefore)
{
if (!ActivePlayers.Any())
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}ActivePlayers ({(isBefore ? "BEFORE" : "AFTER")}): No active players.");
Console.WriteLine(
$"{RetakesPlugin.LogPrefix}ActivePlayers ({(isBefore ? "BEFORE" : "AFTER")}): No active players.");
}
else
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}ActivePlayers ({(isBefore ? "BEFORE" : "AFTER")}): {string.Join(", ", ActivePlayers.Where(Helpers.IsValidPlayer).Select(player => player.PlayerName))}");
Console.WriteLine(
$"{RetakesPlugin.LogPrefix}ActivePlayers ({(isBefore ? "BEFORE" : "AFTER")}): {string.Join(", ", ActivePlayers.Where(Helpers.IsValidPlayer).Select(player => player.PlayerName))}");
}

if (!QueuePlayers.Any())
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}QueuePlayers ({(isBefore ? "BEFORE" : "AFTER")}): No players in the queue.");
Console.WriteLine(
$"{RetakesPlugin.LogPrefix}QueuePlayers ({(isBefore ? "BEFORE" : "AFTER")}): No players in the queue.");
}
else
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}QueuePlayers ({(isBefore ? "BEFORE" : "AFTER")}): {string.Join(", ", QueuePlayers.Where(Helpers.IsValidPlayer).Select(player => player.PlayerName))}");
Console.WriteLine(
$"{RetakesPlugin.LogPrefix}QueuePlayers ({(isBefore ? "BEFORE" : "AFTER")}): {string.Join(", ", QueuePlayers.Where(Helpers.IsValidPlayer).Select(player => player.PlayerName))}");
}

if (!_roundTerrorists.Any())
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}_roundTerrorists ({(isBefore ? "BEFORE" : "AFTER")}): No players in the queue.");
Console.WriteLine(
$"{RetakesPlugin.LogPrefix}_roundTerrorists ({(isBefore ? "BEFORE" : "AFTER")}): No players in the queue.");
}
else
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}_roundTerrorists ({(isBefore ? "BEFORE" : "AFTER")}): {string.Join(", ", _roundTerrorists.Where(Helpers.IsValidPlayer).Select(player => player.PlayerName))}");
Console.WriteLine(
$"{RetakesPlugin.LogPrefix}_roundTerrorists ({(isBefore ? "BEFORE" : "AFTER")}): {string.Join(", ", _roundTerrorists.Where(Helpers.IsValidPlayer).Select(player => player.PlayerName))}");
}

if (!_roundCounterTerrorists.Any())
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}_roundCounterTerrorists ({(isBefore ? "BEFORE" : "AFTER")}): No players in the queue.");
Console.WriteLine(
$"{RetakesPlugin.LogPrefix}_roundCounterTerrorists ({(isBefore ? "BEFORE" : "AFTER")}): No players in the queue.");
}
else
{
Console.WriteLine($"{RetakesPlugin.LogPrefix}_roundCounterTerrorists ({(isBefore ? "BEFORE" : "AFTER")}): {string.Join(", ", _roundCounterTerrorists.Where(Helpers.IsValidPlayer).Select(player => player.PlayerName))}");
Console.WriteLine(
$"{RetakesPlugin.LogPrefix}_roundCounterTerrorists ({(isBefore ? "BEFORE" : "AFTER")}): {string.Join(", ", _roundCounterTerrorists.Where(Helpers.IsValidPlayer).Select(player => player.PlayerName))}");
}
}

private List<CCSPlayerController> _roundTerrorists = new();
private List<CCSPlayerController> _roundCounterTerrorists = new();

public void ClearRoundTeams()
{
_roundTerrorists.Clear();
_roundCounterTerrorists.Clear();
}

public void SetRoundTeams()
{
_roundTerrorists = Utilities.GetPlayers().Where(player => Helpers.IsValidPlayer(player) && player.Team == CsTeam.Terrorist).ToList();
_roundCounterTerrorists = Utilities.GetPlayers().Where(player => Helpers.IsValidPlayer(player) && player.Team == CsTeam.CounterTerrorist).ToList();
_roundTerrorists = Utilities.GetPlayers()
.Where(player => Helpers.IsValidPlayer(player) && player.Team == CsTeam.Terrorist).ToList();
_roundCounterTerrorists = Utilities.GetPlayers()
.Where(player => Helpers.IsValidPlayer(player) && player.Team == CsTeam.CounterTerrorist).ToList();
}
}
}
29 changes: 19 additions & 10 deletions RetakesPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public override void Load(bool hotReload)
Console.WriteLine($"{LogPrefix}Plugin loaded!");

RegisterListener<Listeners.OnMapStart>(OnMapStart);

AddCommandListener("jointeam", OnCommandJoinTeam);

if (hotReload)
{
Expand Down Expand Up @@ -629,28 +631,35 @@ public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
[GameEventHandler(HookMode.Pre)]
public HookResult OnPlayerTeam(EventPlayerTeam @event, GameEventInfo info)
{
Console.WriteLine($"{LogPrefix}OnPlayerTeam event fired.");

// Ensure all team join events are silent.
@event.Silent = true;

return HookResult.Continue;
}

private HookResult OnCommandJoinTeam(CCSPlayerController? player, CommandInfo commandInfo)
{
if (_gameManager == null)
{
Console.WriteLine($"{LogPrefix}Game manager not loaded.");
return HookResult.Continue;
}

var player = @event.Userid;

if (!Helpers.IsValidPlayer(player))
if (
!Helpers.IsValidPlayer(player)
|| commandInfo.ArgCount < 2
|| !Enum.TryParse<CsTeam>(commandInfo.GetArg(1), out var toTeam)
)
{
return HookResult.Continue;
return HookResult.Handled;
}

var fromTeam = player!.Team;

Console.WriteLine($"{LogPrefix}[{player.PlayerName}] {(CsTeam)@event.Oldteam} -> {(CsTeam)@event.Team}");
Console.WriteLine($"{LogPrefix}[{player.PlayerName}] {fromTeam} -> {toTeam}");

_gameManager.QueueManager.DebugQueues(true);
_gameManager.QueueManager.PlayerJoinedTeam(player, (CsTeam)@event.Oldteam, (CsTeam)@event.Team);
var response = _gameManager.QueueManager.PlayerJoinedTeam(player, fromTeam, toTeam);
_gameManager.QueueManager.DebugQueues(false);

Console.WriteLine($"{LogPrefix}[{player.PlayerName}] checking to ensure we have active players");
Expand All @@ -667,8 +676,8 @@ public HookResult OnPlayerTeam(EventPlayerTeam @event, GameEventInfo info)

Helpers.RestartGame();
}

return HookResult.Continue;
return response;
}

[GameEventHandler(HookMode.Pre)]
Expand Down

0 comments on commit 903945b

Please sign in to comment.