Skip to content

Commit

Permalink
Prevent some crashes when touching disconnected clients (#22)
Browse files Browse the repository at this point in the history
* Make global state eviction slightly more verbose
* All format operations will check for a valid client before continuing
  • Loading branch information
Mooshua authored Feb 5, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 2974998 commit 211ca83
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
14 changes: 12 additions & 2 deletions mod/Jailbreak.Teams/Queue/QueueBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public void Start(BasePlugin parent)

public bool TryEnterQueue(CCSPlayerController player)
{
if (!player.IsReal())
return false;

if (player.GetTeam() == CsTeam.CounterTerrorist)
return false;

Expand All @@ -59,6 +62,9 @@ public bool TryEnterQueue(CCSPlayerController player)

public bool TryExitQueue(CCSPlayerController player)
{
if (!player.IsReal())
return false;

var state = _state.Get(player);
state.InQueue = false;
state.IsGuard = false;
Expand All @@ -68,7 +74,9 @@ public bool TryExitQueue(CCSPlayerController player)

public bool TryPop(int count)
{
var queue = Queue.ToList();
var queue = Queue
.Where(player => player.IsReal())
.ToList();

if (queue.Count <= count)
{
Expand All @@ -91,9 +99,11 @@ public bool TryPop(int count)
public bool TryPush(int count)
{
var players = Utilities.GetPlayers()
.Where(player => player.IsReal())
.Where(player => player.GetTeam() == CsTeam.CounterTerrorist)
.Shuffle(Random.Shared)
.ToList();

_logger.LogInformation("[Queue] Push requested {@Count} out of {@GuardCount}", count, players.Count);

for (int i = 0; i < Math.Min(count, players.Count); i++)
Expand Down Expand Up @@ -176,7 +186,7 @@ public HookResult OnPlayerSpawn(EventPlayerSpawn ev, GameEventInfo info)
var player = ev.Userid;
if (!player.IsReal())
return HookResult.Continue;

var state = _state.Get(ev.Userid);

if (player.GetTeam() == CsTeam.CounterTerrorist && !state.IsGuard)
Expand Down
37 changes: 25 additions & 12 deletions public/Jailbreak.Formatting/Extensions/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Core;
using Jailbreak.Public.Extensions;

namespace Jailbreak.Formatting.Extensions;

Expand All @@ -22,40 +23,52 @@ public static FormatWriter ToWriter(this IView view)

public static IView ToPlayerConsole(this IView view, CCSPlayerController player)
{
var writer = view.ToWriter();
if (player.IsReal())
{
var writer = view.ToWriter();

foreach (string writerLine in writer.Plain)
player.PrintToConsole(writerLine);
foreach (string writerLine in writer.Plain)
player.PrintToConsole(writerLine);
}

return view;
}

public static IView ToPlayerChat(this IView view, CCSPlayerController player)
{
var writer = view.ToWriter();
if (player.IsReal())
{
var writer = view.ToWriter();

foreach (string writerLine in writer.Chat)
player.PrintToChat(writerLine);
foreach (string writerLine in writer.Chat)
player.PrintToChat(writerLine);
}

return view;
}

public static IView ToPlayerCenter(this IView view, CCSPlayerController player)
{
var writer = view.ToWriter();
var merged = string.Join('\n', writer.Plain);
if (player.IsReal())
{
var writer = view.ToWriter();
var merged = string.Join('\n', writer.Plain);

player.PrintToCenter(merged);
player.PrintToCenter(merged);
}

return view;
}

public static IView ToPlayerCenterHtml(this IView view, CCSPlayerController player)
{
var writer = view.ToWriter();
var merged = string.Join('\n', writer.Panorama);
if (player.IsReal())
{
var writer = view.ToWriter();
var merged = string.Join('\n', writer.Panorama);

player.PrintToCenterHtml(merged);
player.PrintToCenterHtml(merged);
}

return view;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Jailbreak.Generic/PlayerState/Behaviors/GlobalStateTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ public HookResult OnDisconnect(EventPlayerDisconnect ev, GameEventInfo info)
Reset(ev.Userid);
return HookResult.Continue;
}

/// <summary>
/// Reset all global states when a new game starts
/// </summary>
/// <param name="ev"></param>
/// <param name="info"></param>
/// <returns></returns>
public HookResult OnGameEnd(EventGameEnd ev, GameEventInfo info)
{
ResetAll();
return HookResult.Continue;
}
}

0 comments on commit 211ca83

Please sign in to comment.