From 7eaec52291004854e4fd89be3dcf5ac19619d0d4 Mon Sep 17 00:00:00 2001 From: Mooshua <43320783+mooshua@users.noreply.github.com> Date: Thu, 4 Jan 2024 12:58:58 -0800 Subject: [PATCH] (Cherry-Pick) Switch inconsistent logging to use .NET logging conventions --- mod/Jailbreak.Teams/Queue/QueueBehavior.cs | 21 +++++++++++++------ mod/Jailbreak.Teams/Queue/QueueState.cs | 7 +++++++ mod/Jailbreak.Teams/Ratio/RatioBehavior.cs | 16 +++++++++----- mod/Jailbreak.Warden/Global/WardenBehavior.cs | 16 +++++++------- .../Selection/WardenSelectionBehavior.cs | 13 ++++++++---- .../Behaviors/RoundStateTracker.cs | 1 - 6 files changed, 51 insertions(+), 23 deletions(-) diff --git a/mod/Jailbreak.Teams/Queue/QueueBehavior.cs b/mod/Jailbreak.Teams/Queue/QueueBehavior.cs index fe550b97..3d2de08e 100644 --- a/mod/Jailbreak.Teams/Queue/QueueBehavior.cs +++ b/mod/Jailbreak.Teams/Queue/QueueBehavior.cs @@ -7,10 +7,15 @@ using Jailbreak.Formatting.Extensions; using Jailbreak.Formatting.Views; using Jailbreak.Public.Behaviors; +using Jailbreak.Formatting.Extensions; +using Jailbreak.Formatting.Views; using Jailbreak.Public.Extensions; using Jailbreak.Public.Generic; using Jailbreak.Public.Mod.Teams; +using Microsoft.Extensions.Logging; +using Microsoft.VisualBasic.CompilerServices; + using Serilog; namespace Jailbreak.Teams.Queue; @@ -19,11 +24,14 @@ public class QueueBehavior : IGuardQueue, IPluginBehavior { private int _counter; private IPlayerState _state; + private ILogger _logger; private IRatioNotifications _notifications; - public QueueBehavior(IPlayerStateFactory factory, IRatioNotifications notifications) + + public QueueBehavior(IPlayerStateFactory factory, IRatioNotifications notifications, ILogger logger) { + _logger = logger; _notifications = notifications; _counter = 0; _state = factory.Global(); @@ -60,10 +68,11 @@ public bool TryPop(int count) _notifications.JOIN_GUARD_QUEUE.ToAllChat().ToAllCenter(); } - 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] ); } @@ -77,12 +86,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; @@ -151,7 +160,7 @@ private void HandleLeaveRequest(CCSPlayerController player) player.PrintToCenter("An error occured removing you from the queue."); } - public int GetQueuePosition(CCSPlayerController player) + public int GetQueuePosition(CCSPlayerController player) { return Queue.ToList() .FindIndex(controller => controller.Slot == player.Slot); diff --git a/mod/Jailbreak.Teams/Queue/QueueState.cs b/mod/Jailbreak.Teams/Queue/QueueState.cs index 46e1e5c7..006087ea 100644 --- a/mod/Jailbreak.Teams/Queue/QueueState.cs +++ b/mod/Jailbreak.Teams/Queue/QueueState.cs @@ -13,7 +13,14 @@ public QueueState() /// public int Position { get; set; } + /// + /// True when this player is currently in the queue + /// public bool InQueue { get; set; } + /// + /// This player is allowed to be on the CT team. + /// If this is false, they will be swapped back to prisoner. + /// public bool IsGuard { get; set; } } diff --git a/mod/Jailbreak.Teams/Ratio/RatioBehavior.cs b/mod/Jailbreak.Teams/Ratio/RatioBehavior.cs index d6cfaec0..4a7297d8 100644 --- a/mod/Jailbreak.Teams/Ratio/RatioBehavior.cs +++ b/mod/Jailbreak.Teams/Ratio/RatioBehavior.cs @@ -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 _logger; public enum RatioActionType { @@ -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 logger) { _config = config; _guardQueue = guardQueue; + _logger = logger; } /// @@ -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(); } diff --git a/mod/Jailbreak.Warden/Global/WardenBehavior.cs b/mod/Jailbreak.Warden/Global/WardenBehavior.cs index a04b3cf5..0b198413 100644 --- a/mod/Jailbreak.Warden/Global/WardenBehavior.cs +++ b/mod/Jailbreak.Warden/Global/WardenBehavior.cs @@ -12,22 +12,24 @@ using Jailbreak.Formatting.Extensions; using Jailbreak.Formatting.Views; +using Microsoft.Extensions.Logging; + +using Serilog; + namespace Jailbreak.Warden.Global; public class WardenBehavior : IPluginBehavior, IWardenService { - - public void Dispose() - { - } + private ILogger _logger; private IWardenNotifications _notifications; private bool _hasWarden; private CCSPlayerController? _warden; - public WardenBehavior(IWardenNotifications notifications) + public WardenBehavior(ILogger logger, IWardenNotifications notifications) { + _logger = logger; _notifications = notifications; } @@ -85,7 +87,7 @@ public HookResult OnDeath(EventPlayerDeath ev, GameEventInfo info) if (ev.Userid.UserId == _warden.UserId) { if (!this.TryRemoveWarden()) - Server.PrintToConsole("[Warden] BUG: Problem removing current warden :^("); + _logger.LogWarning("[Warden] BUG: Problem removing current warden :^("); // Warden died! _notifications.WARDEN_DIED @@ -115,7 +117,7 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect ev, GameEventInfo inf if (ev.Userid.UserId == _warden.UserId) { if (!this.TryRemoveWarden()) - Server.PrintToConsole("[Warden] BUG: Problem removing current warden :^("); + _logger.LogWarning("[Warden] BUG: Problem removing current warden :^("); _notifications.WARDEN_LEFT diff --git a/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs b/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs index 2af5b012..150445ba 100644 --- a/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs +++ b/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs @@ -15,6 +15,8 @@ using Jailbreak.Public.Generic; using Jailbreak.Public.Mod.Warden; +using Microsoft.Extensions.Logging; + using Serilog; using Timer = CounterStrikeSharp.API.Modules.Timers.Timer; @@ -34,6 +36,8 @@ public class WardenSelectionBehavior : IPluginBehavior, IWardenSelectionService private IPlayerState _favor; + private ILogger _logger; + /// /// Whether or not to use the queue. /// When true, the queue should be skipped and turn to first-come-first-serve. @@ -44,10 +48,11 @@ public class WardenSelectionBehavior : IPluginBehavior, IWardenSelectionService private IWardenNotifications _notifications; - public WardenSelectionBehavior(IPlayerStateFactory factory, IWardenService warden, IWardenNotifications notifications) + public WardenSelectionBehavior(IPlayerStateFactory factory, IWardenService warden, IWardenNotifications notifications, ILogger logger) { _warden = warden; _notifications = notifications; + _logger = logger; _queue = factory.Round(); _favor = factory.Global(); @@ -95,7 +100,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) { @@ -110,13 +115,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)) diff --git a/src/Jailbreak.Generic/PlayerState/Behaviors/RoundStateTracker.cs b/src/Jailbreak.Generic/PlayerState/Behaviors/RoundStateTracker.cs index 344ee4d5..7c624fa2 100644 --- a/src/Jailbreak.Generic/PlayerState/Behaviors/RoundStateTracker.cs +++ b/src/Jailbreak.Generic/PlayerState/Behaviors/RoundStateTracker.cs @@ -17,7 +17,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;