Skip to content

Commit

Permalink
Switch inconsistent logging to use .NET logging conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mooshua committed Jan 4, 2024
1 parent 02205ff commit 0fdef8b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 22 deletions.
16 changes: 10 additions & 6 deletions mod/Jailbreak.Teams/Queue/QueueBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Jailbreak.Public.Generic;
using Jailbreak.Public.Mod.Teams;

using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic.CompilerServices;

using Serilog;
Expand All @@ -18,9 +19,11 @@ public class QueueBehavior : IGuardQueue, IPluginBehavior
{
private int _counter;
private IPlayerState<QueueState> _state;
private ILogger<QueueBehavior> _logger;

public QueueBehavior(IPlayerStateFactory factory)
public QueueBehavior(IPlayerStateFactory factory, ILogger<QueueBehavior> logger)
{
_logger = logger;
_counter = 0;
_state = factory.Global<QueueState>();
}
Expand Down Expand Up @@ -55,13 +58,14 @@ public bool TryPop(int count)
{
Server.PrintToChatAll("[Jail] Not enough guards are in the queue!");
Server.PrintToChatAll("[Jail] Type !guard in chat to join the queue");
ServerExtensions.PrintToCenterAll("Type !guard to become a guard!");
ServerExtensions.PrintToCenterAll("Not enough players in guard queue!\nType !guard to become a guard.");
}

Log.Information($"[Queue] {count}/{queue.Count}");
_logger.LogInformation("[Queue] Pop requested {@Count} out of {@InQueue}", count, queue.Count);

for (int i = 0; i < Math.Min(queue.Count, count); i++)
{
Log.Information($"[Queue] Popping { queue[i].PlayerName }");
_logger.LogInformation("[Queue] Popping player {@Name}", queue[i].PlayerName);

ForceGuard( queue[i] );
}
Expand All @@ -77,12 +81,12 @@ public bool TryPush(int count)
.Where(player => player.GetTeam() == CsTeam.CounterTerrorist)
.Shuffle(Random.Shared)
.ToList();
Log.Information($"[Queue] {count}/{players.Count}");
_logger.LogInformation("[Queue] Push requested {@Count} out of {@GuardCount}", count, players.Count);

for (int i = 0; i < Math.Min(count, players.Count); i++)
{
var toSwap = players[i];
Log.Information($"[Queue] Pushing {toSwap.PlayerName}");
_logger.LogInformation("[Queue] Pushing {@Name}", toSwap.PlayerName);
var state = _state.Get(toSwap);

state.IsGuard = false;
Expand Down
7 changes: 7 additions & 0 deletions mod/Jailbreak.Teams/Queue/QueueState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ public QueueState()
/// </summary>
public int Position { get; set; }

/// <summary>
/// True when this player is currently in the queue
/// </summary>
public bool InQueue { get; set; }

/// <summary>
/// This player is allowed to be on the CT team.
/// If this is false, they will be swapped back to prisoner.
/// </summary>
public bool IsGuard { get; set; }
}
16 changes: 11 additions & 5 deletions mod/Jailbreak.Teams/Ratio/RatioBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
using Jailbreak.Public.Extensions;
using Jailbreak.Public.Mod.Teams;

using Microsoft.Extensions.Logging;

using Serilog;

namespace Jailbreak.Teams.Ratio;

public class RatioBehavior : IPluginBehavior
{
private IGuardQueue _guardQueue;
private ILogger<RatioBehavior> _logger;

public enum RatioActionType
{
Expand All @@ -35,10 +40,11 @@ public RatioAction(int count = 0, RatioActionType type = RatioActionType.None)

private RatioConfig _config;

public RatioBehavior(RatioConfig config, IGuardQueue guardQueue)
public RatioBehavior(RatioConfig config, IGuardQueue guardQueue, ILogger<RatioBehavior> logger)
{
_config = config;
_guardQueue = guardQueue;
_logger = logger;
}

/// <summary>
Expand All @@ -56,24 +62,24 @@ public RatioAction Evaluate(int ct, int t)
double ts_per_ct = t / (double)normalized_ct;
int target = (int)( (ct + t) / _config.Target ) + 1;

Server.PrintToConsole($"[Ratio] Evaluating ratio of {ct}ct to {t}t: {ts_per_ct}t/ct ratio, {target} target.");
_logger.LogTrace("[Ratio] Evaluating ratio of {@Ct}ct to {@T}t: {@TsPerCt}t/ct ratio, {@Target} target.", ct ,t ,ts_per_ct, target);

if (_config.Maximum <= ts_per_ct)
{
// There are too many Ts per CT!
// Get more guards on the team
Server.PrintToConsole($"[Ratio] Decision: Not enough CTs: {_config.Maximum} <= {ts_per_ct}");
_logger.LogTrace("[Ratio] Decision: Not enough CTs: {@Maximum} <= {@TsPerCt}", _config.Maximum, ts_per_ct);
return new(target - ct, RatioActionType.Add);
}

if (ts_per_ct <= _config.Minimum)
{
// There are too many guards per T!
Server.PrintToConsole($"[Ratio] Decision: Too many CTs: {ts_per_ct} <= {_config.Minimum}");
_logger.LogTrace("[Ratio] Decision: Too many CTs: {@TsPerCt} <= {@Minimum}", ts_per_ct, _config.Minimum);
return new(ct - target, RatioActionType.Remove);
}

Server.PrintToConsole($"[Ratio] Decision: Goldilocks: {_config.Maximum} (max) <= {ts_per_ct} (t/ct) <= {_config.Minimum} (min)");
_logger.LogTrace("[Ratio] Decision: Goldilocks: {@Maximum} (max) <= {@TsPerCt} (t/ct) <= {@Minimum} (min)", _config.Maximum, ts_per_ct, _config.Minimum);
// Take no action
return new();
}
Expand Down
18 changes: 12 additions & 6 deletions mod/Jailbreak.Warden/Global/WardenBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@
using Jailbreak.Public.Extensions;
using Jailbreak.Public.Mod.Warden;

using Microsoft.Extensions.Logging;

using Serilog;

namespace Jailbreak.Warden.Global;

public class WardenBehavior : IPluginBehavior, IWardenService
{

public void Dispose()
{
}
private ILogger<WardenBehavior> _logger;

private bool _hasWarden;
private CCSPlayerController? _warden;

public WardenBehavior(ILogger<WardenBehavior> logger)
{
_logger = logger;
}

/// <summary>
/// Get the current warden, if there is one.
/// </summary>
Expand Down Expand Up @@ -73,7 +79,7 @@ public HookResult OnDeath(EventPlayerDeath ev, GameEventInfo info)
if (ev.Userid.UserId == _warden.UserId)

Check warning on line 79 in mod/Jailbreak.Warden/Global/WardenBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
if (!this.TryRemoveWarden())
Server.PrintToConsole("[Warden] BUG: Problem removing current warden :^(");
_logger.LogWarning("[Warden] BUG: Problem removing current warden :^(");

// Warden died!
Server.PrintToChatAll("[Warden] The current warden has died!");
Expand Down Expand Up @@ -101,7 +107,7 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect ev, GameEventInfo inf
if (ev.Userid.UserId == _warden.UserId)

Check warning on line 107 in mod/Jailbreak.Warden/Global/WardenBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
if (!this.TryRemoveWarden())
Server.PrintToConsole("[Warden] BUG: Problem removing current warden :^(");
_logger.LogWarning("[Warden] BUG: Problem removing current warden :^(");

// Warden died!
Server.PrintToChatAll("[Warden] The current warden has left the game!");
Expand Down
13 changes: 9 additions & 4 deletions mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using Jailbreak.Public.Generic;
using Jailbreak.Public.Mod.Warden;

using Microsoft.Extensions.Logging;

using Serilog;

using Timer = CounterStrikeSharp.API.Modules.Timers.Timer;
Expand All @@ -31,6 +33,8 @@ public class WardenSelectionBehavior : IPluginBehavior, IWardenSelectionService

private IPlayerState<QueueFavorState> _favor;

private ILogger<WardenSelectionBehavior> _logger;

/// <summary>
/// Whether or not to use the queue.
/// When true, the queue should be skipped and turn to first-come-first-serve.
Expand All @@ -39,9 +43,10 @@ public class WardenSelectionBehavior : IPluginBehavior, IWardenSelectionService

private IWardenService _warden;

public WardenSelectionBehavior(IPlayerStateFactory factory, IWardenService warden)
public WardenSelectionBehavior(IPlayerStateFactory factory, IWardenService warden, ILogger<WardenSelectionBehavior> logger)
{
_warden = warden;
_logger = logger;
_queue = factory.Round<QueueState>();
_favor = factory.Global<QueueFavorState>();

Expand Down Expand Up @@ -90,7 +95,7 @@ protected void OnChooseWarden()
.Where(player => _queue.Get(player).InQueue)
.ToList();

Log.Information("[WardenSelectionBehavior] Picking warden from {@Eligible}", eligible);
_logger.LogTrace("[WardenSelectionBehavior] Picking warden from {@Eligible}", eligible);

if (eligible.Count == 0)
{
Expand All @@ -105,13 +110,13 @@ protected void OnChooseWarden()
int tickets = favors.Sum(favor => favor.Value.GetTickets());
int chosen = Random.Shared.Next(tickets);

Server.PrintToConsole($"[Warden Raffle] Picking {chosen} out of {tickets}");
_logger.LogTrace("[Warden Raffle] Picking {@Chosen} out of {@Tickets}", chosen, tickets);

int pointer = 0;
foreach (var (player, favor) in favors)
{
int thisTickets = favor.GetTickets();
Server.PrintToConsole($"[Warden Raffle] {pointer} -> {pointer + thisTickets}: #{player.Slot} {player.PlayerName}");
_logger.LogTrace("[Warden Raffle] {@Pointer} -> {@End}: #{@Slot} {@Name}", pointer, pointer + thisTickets, player.Slot, player.PlayerName);

// If winning ticket belongs to this player, assign them as warden.
if (pointer <= chosen && chosen < (pointer + thisTickets))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public void Start(BasePlugin parent)
[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd ev, GameEventInfo info)
{
Log.Debug("[RoundStateTracker] Resetting all tracked states");
ResetAll();

return HookResult.Continue;
Expand Down

0 comments on commit 0fdef8b

Please sign in to comment.