From 0fdef8bdeec1577d44fbcb392494cbca4fc9e261 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 1/2] Switch inconsistent logging to use .NET logging conventions --- mod/Jailbreak.Teams/Queue/QueueBehavior.cs | 16 ++++++++++------ mod/Jailbreak.Teams/Queue/QueueState.cs | 7 +++++++ mod/Jailbreak.Teams/Ratio/RatioBehavior.cs | 16 +++++++++++----- mod/Jailbreak.Warden/Global/WardenBehavior.cs | 18 ++++++++++++------ .../Selection/WardenSelectionBehavior.cs | 13 +++++++++---- .../PlayerState/Behaviors/RoundStateTracker.cs | 1 - 6 files changed, 49 insertions(+), 22 deletions(-) diff --git a/mod/Jailbreak.Teams/Queue/QueueBehavior.cs b/mod/Jailbreak.Teams/Queue/QueueBehavior.cs index 8e910ff4..4d2edf4e 100644 --- a/mod/Jailbreak.Teams/Queue/QueueBehavior.cs +++ b/mod/Jailbreak.Teams/Queue/QueueBehavior.cs @@ -8,6 +8,7 @@ using Jailbreak.Public.Generic; using Jailbreak.Public.Mod.Teams; +using Microsoft.Extensions.Logging; using Microsoft.VisualBasic.CompilerServices; using Serilog; @@ -18,9 +19,11 @@ public class QueueBehavior : IGuardQueue, IPluginBehavior { private int _counter; private IPlayerState _state; + private ILogger _logger; - public QueueBehavior(IPlayerStateFactory factory) + public QueueBehavior(IPlayerStateFactory factory, ILogger logger) { + _logger = logger; _counter = 0; _state = factory.Global(); } @@ -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] ); } @@ -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; 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 173c20c8..7a8da64e 100644 --- a/mod/Jailbreak.Warden/Global/WardenBehavior.cs +++ b/mod/Jailbreak.Warden/Global/WardenBehavior.cs @@ -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 _logger; private bool _hasWarden; private CCSPlayerController? _warden; + public WardenBehavior(ILogger logger) + { + _logger = logger; + } + /// /// Get the current warden, if there is one. /// @@ -73,7 +79,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! Server.PrintToChatAll("[Warden] The current warden has died!"); @@ -101,7 +107,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 :^("); // Warden died! Server.PrintToChatAll("[Warden] The current warden has left the game!"); diff --git a/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs b/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs index 4fd440eb..d373c1e6 100644 --- a/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs +++ b/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs @@ -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; @@ -31,6 +33,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. @@ -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 logger) { _warden = warden; + _logger = logger; _queue = factory.Round(); _favor = factory.Global(); @@ -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) { @@ -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)) diff --git a/src/Jailbreak.Generic/PlayerState/Behaviors/RoundStateTracker.cs b/src/Jailbreak.Generic/PlayerState/Behaviors/RoundStateTracker.cs index f10d2781..e50bd075 100644 --- a/src/Jailbreak.Generic/PlayerState/Behaviors/RoundStateTracker.cs +++ b/src/Jailbreak.Generic/PlayerState/Behaviors/RoundStateTracker.cs @@ -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; From f0fda6af5aa4ffe6f0c93c85f630687276a2a959 Mon Sep 17 00:00:00 2001 From: Mooshua <43320783+mooshua@users.noreply.github.com> Date: Thu, 4 Jan 2024 13:05:39 -0800 Subject: [PATCH 2/2] Full cleanup + reformat (automated) --- mod/Jailbreak.Teams/Jailbreak.Teams.csproj | 2 +- mod/Jailbreak.Teams/Queue/QueueBehavior.cs | 50 +++++------ mod/Jailbreak.Teams/Queue/QueueState.cs | 17 ++-- mod/Jailbreak.Teams/Ratio/RatioBehavior.cs | 58 ++++++------ mod/Jailbreak.Teams/Ratio/RatioConfig.cs | 14 +-- mod/Jailbreak.Teams/TeamsServiceExtension.cs | 2 +- .../Commands/WardenCommandsBehavior.cs | 12 +-- mod/Jailbreak.Warden/Global/WardenBehavior.cs | 55 +++++------- mod/Jailbreak.Warden/Jailbreak.Warden.csproj | 2 +- .../Selection/QueueFavorState.cs | 6 +- mod/Jailbreak.Warden/Selection/QueueState.cs | 8 +- .../Selection/WardenSelectionBehavior.cs | 90 +++++++++---------- .../WardenServiceExtension.cs | 8 +- .../Behaviors/IPluginBehavior.cs | 16 ++-- .../Configuration/IConfigService.cs | 11 +-- .../Extensions/EnumerableExtensions.cs | 12 ++- .../Extensions/PlayerExtensions.cs | 2 +- .../Extensions/ServerExtensions.cs | 5 +- .../Extensions/ServiceCollectionExtensions.cs | 14 +-- .../Extensions/StringExtensions.cs | 2 +- .../Jailbreak.Public/Generic/IPlayerState.cs | 10 +-- .../Generic/IPlayerStateFactory.cs | 10 +-- .../Jailbreak.Public/Jailbreak.Public.csproj | 6 +- .../Jailbreak.Public/Mod/Teams/IGuardQueue.cs | 23 +++-- .../Mod/Warden/IWardenSelectionService.cs | 18 ++-- .../Mod/Warden/IWardenService.cs | 6 +- .../GenericServiceExtension.cs | 5 +- .../Jailbreak.Generic.csproj | 2 +- .../Behaviors/AliveStateTracker.cs | 9 +- .../PlayerState/Behaviors/BaseStateTracker.cs | 18 ++-- .../Behaviors/GlobalStateTracker.cs | 7 +- .../Behaviors/RoundStateTracker.cs | 9 +- .../PlayerState/ITrackedPlayerState.cs | 6 +- .../PlayerState/PlayerStateFactory.cs | 13 +-- .../PlayerState/PlayerStateImpl.cs | 12 ++- src/Jailbreak/Config/ConfigService.cs | 32 ++++--- src/Jailbreak/Jailbreak.cs | 25 +++--- src/Jailbreak/Jailbreak.csproj | 8 +- src/Jailbreak/JailbreakServiceCollection.cs | 9 +- 39 files changed, 276 insertions(+), 338 deletions(-) diff --git a/mod/Jailbreak.Teams/Jailbreak.Teams.csproj b/mod/Jailbreak.Teams/Jailbreak.Teams.csproj index d809758e..29f7e134 100644 --- a/mod/Jailbreak.Teams/Jailbreak.Teams.csproj +++ b/mod/Jailbreak.Teams/Jailbreak.Teams.csproj @@ -7,7 +7,7 @@ - + diff --git a/mod/Jailbreak.Teams/Queue/QueueBehavior.cs b/mod/Jailbreak.Teams/Queue/QueueBehavior.cs index 4d2edf4e..6acdbeea 100644 --- a/mod/Jailbreak.Teams/Queue/QueueBehavior.cs +++ b/mod/Jailbreak.Teams/Queue/QueueBehavior.cs @@ -9,17 +9,14 @@ using Jailbreak.Public.Mod.Teams; using Microsoft.Extensions.Logging; -using Microsoft.VisualBasic.CompilerServices; - -using Serilog; namespace Jailbreak.Teams.Queue; public class QueueBehavior : IGuardQueue, IPluginBehavior { private int _counter; - private IPlayerState _state; - private ILogger _logger; + private readonly ILogger _logger; + private readonly IPlayerState _state; public QueueBehavior(IPlayerStateFactory factory, ILogger logger) { @@ -63,11 +60,11 @@ public bool TryPop(int count) _logger.LogInformation("[Queue] Pop requested {@Count} out of {@InQueue}", count, queue.Count); - for (int i = 0; i < Math.Min(queue.Count, count); i++) + for (var i = 0; i < Math.Min(queue.Count, count); i++) { _logger.LogInformation("[Queue] Popping player {@Name}", queue[i].PlayerName); - ForceGuard( queue[i] ); + ForceGuard(queue[i]); } return true; @@ -83,7 +80,7 @@ public bool TryPush(int count) .ToList(); _logger.LogInformation("[Queue] Push requested {@Count} out of {@GuardCount}", count, players.Count); - for (int i = 0; i < Math.Min(count, players.Count); i++) + for (var i = 0; i < Math.Min(count, players.Count); i++) { var toSwap = players[i]; _logger.LogInformation("[Queue] Pushing {@Name}", toSwap.PlayerName); @@ -109,6 +106,21 @@ public void ForceGuard(CCSPlayerController player) player.ChangeTeam(CsTeam.CounterTerrorist); } + + public int GetQueuePosition(CCSPlayerController player) + { + return Queue.ToList() + .FindIndex(controller => controller.Slot == player.Slot); + } + + public IEnumerable Queue + => Utilities.GetPlayers() + .Select(player => (Player: player, State: _state.Get(player))) + .Where(tuple => tuple.State.InQueue) // Exclude not in queue + .Where(tuple => !tuple.State.IsGuard) // Exclude current guards + .OrderBy(tuple => tuple.State.Position) // Order by counter value when joined queue + .Select(tuple => tuple.Player); + [GameEventHandler] public HookResult OnPlayerTeam(EventPlayerTeam ev, GameEventInfo info) { @@ -124,27 +136,9 @@ public HookResult OnPlayerTeam(EventPlayerTeam ev, GameEventInfo info) } if (player.GetTeam() == CsTeam.Terrorist && state.IsGuard) - { - if (this.TryExitQueue(player)) + if (TryExitQueue(player)) player.PrintToCenter("You were removed from the guard queue for switching to T.\nUse !guard to rejoin the queue!"); - } return HookResult.Continue; } - - - - public int GetQueuePosition(CCSPlayerController player) - { - return Queue.ToList() - .FindIndex(controller => controller.Slot == player.Slot); - } - - public IEnumerable Queue - => Utilities.GetPlayers() - .Select(player => (Player: player, State: _state.Get(player))) - .Where(tuple => tuple.State.InQueue) // Exclude not in queue - .Where(tuple => !tuple.State.IsGuard) // Exclude current guards - .OrderBy(tuple => tuple.State.Position) // Order by counter value when joined queue - .Select(tuple => tuple.Player); -} +} \ No newline at end of file diff --git a/mod/Jailbreak.Teams/Queue/QueueState.cs b/mod/Jailbreak.Teams/Queue/QueueState.cs index 006087ea..71f76235 100644 --- a/mod/Jailbreak.Teams/Queue/QueueState.cs +++ b/mod/Jailbreak.Teams/Queue/QueueState.cs @@ -2,25 +2,20 @@ public class QueueState { - - public QueueState() - { - } - /// - /// The counter value when the player entered the queue - /// Lower = join CT sooner + /// The counter value when the player entered the queue + /// Lower = join CT sooner /// public int Position { get; set; } /// - /// True when this player is currently in the queue + /// 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. + /// 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; } -} +} \ No newline at end of file diff --git a/mod/Jailbreak.Teams/Ratio/RatioBehavior.cs b/mod/Jailbreak.Teams/Ratio/RatioBehavior.cs index 4a7297d8..e223c80b 100644 --- a/mod/Jailbreak.Teams/Ratio/RatioBehavior.cs +++ b/mod/Jailbreak.Teams/Ratio/RatioBehavior.cs @@ -1,7 +1,6 @@ using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core.Attributes.Registration; -using CounterStrikeSharp.API.Modules.Cvars; using CounterStrikeSharp.API.Modules.Utils; using Jailbreak.Public.Behaviors; @@ -10,35 +9,20 @@ using Microsoft.Extensions.Logging; -using Serilog; - namespace Jailbreak.Teams.Ratio; public class RatioBehavior : IPluginBehavior { - private IGuardQueue _guardQueue; - private ILogger _logger; - public enum RatioActionType { Add, Remove, - None, - } - - public struct RatioAction - { - public RatioActionType Type { get; init; } - public int Count { get; init; } - - public RatioAction(int count = 0, RatioActionType type = RatioActionType.None) - { - Count = count; - Type = type; - } + None } - private RatioConfig _config; + private readonly RatioConfig _config; + private readonly IGuardQueue _guardQueue; + private readonly ILogger _logger; public RatioBehavior(RatioConfig config, IGuardQueue guardQueue, ILogger logger) { @@ -47,8 +31,12 @@ public RatioBehavior(RatioConfig config, IGuardQueue guardQueue, ILogger - /// Evaluate whether the provided CT/T ratio needs + /// Evaluate whether the provided CT/T ratio needs /// /// /// @@ -58,34 +46,34 @@ public RatioAction Evaluate(int ct, int t) // No divide by zero errors today... // Make value 0.01 so the decision will always be to add Ts // 1 / 0.01 = 100, etc... - var normalized_ct = (ct == 0 ? 0.01 : (double)ct); - double ts_per_ct = t / (double)normalized_ct; - int target = (int)( (ct + t) / _config.Target ) + 1; + var normalized_ct = ct == 0 ? 0.01 : ct; + var ts_per_ct = t / normalized_ct; + var target = (int)((ct + t) / _config.Target) + 1; - _logger.LogTrace("[Ratio] Evaluating ratio of {@Ct}ct to {@T}t: {@TsPerCt}t/ct ratio, {@Target} target.", ct ,t ,ts_per_ct, 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 _logger.LogTrace("[Ratio] Decision: Not enough CTs: {@Maximum} <= {@TsPerCt}", _config.Maximum, ts_per_ct); - return new(target - ct, RatioActionType.Add); + return new RatioAction(target - ct, RatioActionType.Add); } if (ts_per_ct <= _config.Minimum) { // There are too many guards per T! _logger.LogTrace("[Ratio] Decision: Too many CTs: {@TsPerCt} <= {@Minimum}", ts_per_ct, _config.Minimum); - return new(ct - target, RatioActionType.Remove); + return new RatioAction(ct - target, RatioActionType.Remove); } _logger.LogTrace("[Ratio] Decision: Goldilocks: {@Maximum} (max) <= {@TsPerCt} (t/ct) <= {@Minimum} (min)", _config.Maximum, ts_per_ct, _config.Minimum); // Take no action - return new(); + return new RatioAction(); } /// - /// When a round starts, balance out the teams + /// When a round starts, balance out the teams /// [GameEventHandler(HookMode.Pre)] public HookResult OnRoundStart(EventRoundStart ev, GameEventInfo info) @@ -108,8 +96,16 @@ public HookResult OnRoundStart(EventRoundStart ev, GameEventInfo info) return HookResult.Continue; } - public void Dispose() + public struct RatioAction { + public RatioActionType Type { get; init; } + public int Count { get; init; } + + public RatioAction(int count = 0, RatioActionType type = RatioActionType.None) + { + Count = count; + Type = type; + } } -} +} \ No newline at end of file diff --git a/mod/Jailbreak.Teams/Ratio/RatioConfig.cs b/mod/Jailbreak.Teams/Ratio/RatioConfig.cs index 23c01dbe..6bd6f942 100644 --- a/mod/Jailbreak.Teams/Ratio/RatioConfig.cs +++ b/mod/Jailbreak.Teams/Ratio/RatioConfig.cs @@ -3,20 +3,20 @@ public class RatioConfig { /// - /// The minimum amount of Ts per every CT. - /// When this is passed, CTs will be added until Target is reached. + /// The minimum amount of Ts per every CT. + /// When this is passed, CTs will be added until Target is reached. /// public double Minimum { get; set; } = 2.5; /// - /// The maximum amount of Ts per every CT. - /// When this is passed, CTs will be removed until Target is reached. + /// The maximum amount of Ts per every CT. + /// When this is passed, CTs will be removed until Target is reached. /// public double Maximum { get; set; } = 4; /// - /// When the ratio is autobalanced, the amount of guards - /// should be total_players / target. + /// When the ratio is autobalanced, the amount of guards + /// should be total_players / target. /// public double Target { get; set; } = 4; -} +} \ No newline at end of file diff --git a/mod/Jailbreak.Teams/TeamsServiceExtension.cs b/mod/Jailbreak.Teams/TeamsServiceExtension.cs index bbc83303..1e3ba1ae 100644 --- a/mod/Jailbreak.Teams/TeamsServiceExtension.cs +++ b/mod/Jailbreak.Teams/TeamsServiceExtension.cs @@ -16,4 +16,4 @@ public static void AddJailbreakTeams(this IServiceCollection collection) collection.AddPluginBehavior(); collection.AddPluginBehavior(); } -} +} \ No newline at end of file diff --git a/mod/Jailbreak.Warden/Commands/WardenCommandsBehavior.cs b/mod/Jailbreak.Warden/Commands/WardenCommandsBehavior.cs index 39e40390..2967a5f2 100644 --- a/mod/Jailbreak.Warden/Commands/WardenCommandsBehavior.cs +++ b/mod/Jailbreak.Warden/Commands/WardenCommandsBehavior.cs @@ -11,8 +11,8 @@ namespace Jailbreak.Warden.Commands; public class WardenCommandsBehavior : IPluginBehavior { - private IWardenSelectionService _queue; - private IWardenService _warden; + private readonly IWardenSelectionService _queue; + private readonly IWardenService _warden; public WardenCommandsBehavior(IWardenSelectionService queue, IWardenService warden) { @@ -20,6 +20,10 @@ public WardenCommandsBehavior(IWardenSelectionService queue, IWardenService ward _warden = warden; } + public void Dispose() + { + } + public HookResult HandleWarden(CCSPlayerController sender) { var isCt = sender.GetTeam() == CsTeam.CounterTerrorist; @@ -87,8 +91,4 @@ public HookResult OnPlayerChat(EventPlayerChat chat, GameEventInfo info) return HookResult.Continue; } - - public void Dispose() - { - } } diff --git a/mod/Jailbreak.Warden/Global/WardenBehavior.cs b/mod/Jailbreak.Warden/Global/WardenBehavior.cs index 7a8da64e..3034a719 100644 --- a/mod/Jailbreak.Warden/Global/WardenBehavior.cs +++ b/mod/Jailbreak.Warden/Global/WardenBehavior.cs @@ -1,6 +1,4 @@ -using System.Reflection; - -using CounterStrikeSharp.API; +using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core.Attributes.Registration; using CounterStrikeSharp.API.Modules.Utils; @@ -11,16 +9,11 @@ using Microsoft.Extensions.Logging; -using Serilog; - namespace Jailbreak.Warden.Global; public class WardenBehavior : IPluginBehavior, IWardenService { - private ILogger _logger; - - private bool _hasWarden; - private CCSPlayerController? _warden; + private readonly ILogger _logger; public WardenBehavior(ILogger logger) { @@ -28,44 +21,44 @@ public WardenBehavior(ILogger logger) } /// - /// Get the current warden, if there is one. + /// Get the current warden, if there is one. /// - public CCSPlayerController? Warden => _warden; + public CCSPlayerController? Warden { get; private set; } /// - /// Whether or not a warden is currently assigned + /// Whether or not a warden is currently assigned /// - public bool HasWarden => _hasWarden; + public bool HasWarden { get; private set; } public bool TrySetWarden(CCSPlayerController controller) { - if (_hasWarden) + if (HasWarden) return false; // Verify player is a CT if (controller.GetTeam() != CsTeam.CounterTerrorist) return false; - _hasWarden = true; - _warden = controller; + HasWarden = true; + Warden = controller; - Server.PrintToChatAll($"[Warden] {_warden.PlayerName.Sanitize()} is now the warden!"); - ServerExtensions.PrintToCenterAll($"{_warden.PlayerName.Sanitize()} is now the warden!"); - _warden.ClanName = "[WARDEN]"; + Server.PrintToChatAll($"[Warden] {Warden.PlayerName.Sanitize()} is now the warden!"); + ServerExtensions.PrintToCenterAll($"{Warden.PlayerName.Sanitize()} is now the warden!"); + Warden.ClanName = "[WARDEN]"; return true; } public bool TryRemoveWarden() { - if (!_hasWarden) + if (!HasWarden) return false; - if (_warden != null) - _warden.ClanName = ""; + if (Warden != null) + Warden.ClanName = ""; - _hasWarden = false; - _warden = null; + HasWarden = false; + Warden = null; return true; } @@ -73,12 +66,12 @@ public bool TryRemoveWarden() [GameEventHandler] public HookResult OnDeath(EventPlayerDeath ev, GameEventInfo info) { - if (!_hasWarden) + if (!HasWarden) return HookResult.Continue; - if (ev.Userid.UserId == _warden.UserId) + if (ev.Userid.UserId == Warden.UserId) { - if (!this.TryRemoveWarden()) + if (!TryRemoveWarden()) _logger.LogWarning("[Warden] BUG: Problem removing current warden :^("); // Warden died! @@ -93,7 +86,7 @@ public HookResult OnDeath(EventPlayerDeath ev, GameEventInfo info) [GameEventHandler] public HookResult OnRoundEnd(EventRoundEnd ev, GameEventInfo info) { - this.TryRemoveWarden(); + TryRemoveWarden(); return HookResult.Continue; } @@ -101,12 +94,12 @@ public HookResult OnRoundEnd(EventRoundEnd ev, GameEventInfo info) [GameEventHandler] public HookResult OnPlayerDisconnect(EventPlayerDisconnect ev, GameEventInfo info) { - if (!_hasWarden) + if (!HasWarden) return HookResult.Continue; - if (ev.Userid.UserId == _warden.UserId) + if (ev.Userid.UserId == Warden.UserId) { - if (!this.TryRemoveWarden()) + if (!TryRemoveWarden()) _logger.LogWarning("[Warden] BUG: Problem removing current warden :^("); // Warden died! diff --git a/mod/Jailbreak.Warden/Jailbreak.Warden.csproj b/mod/Jailbreak.Warden/Jailbreak.Warden.csproj index d809758e..29f7e134 100644 --- a/mod/Jailbreak.Warden/Jailbreak.Warden.csproj +++ b/mod/Jailbreak.Warden/Jailbreak.Warden.csproj @@ -7,7 +7,7 @@ - + diff --git a/mod/Jailbreak.Warden/Selection/QueueFavorState.cs b/mod/Jailbreak.Warden/Selection/QueueFavorState.cs index 36ccbd27..2e87186d 100644 --- a/mod/Jailbreak.Warden/Selection/QueueFavorState.cs +++ b/mod/Jailbreak.Warden/Selection/QueueFavorState.cs @@ -1,8 +1,7 @@ -namespace Jailbreak.Warden.Queue; +namespace Jailbreak.Warden.Selection; public class QueueFavorState { - public const int BASE_TICKETS = 2; public int RoundsWithoutWarden { get; set; } = 0; @@ -15,5 +14,4 @@ public int GetTickets() + Favor + RoundsWithoutWarden; } - -} +} \ No newline at end of file diff --git a/mod/Jailbreak.Warden/Selection/QueueState.cs b/mod/Jailbreak.Warden/Selection/QueueState.cs index 19f78507..e36f121c 100644 --- a/mod/Jailbreak.Warden/Selection/QueueState.cs +++ b/mod/Jailbreak.Warden/Selection/QueueState.cs @@ -1,11 +1,9 @@ -namespace Jailbreak.Warden.Queue; +namespace Jailbreak.Warden.Selection; public class QueueState { - /// - /// Whether or not this player is currently in the queue + /// Whether or not this player is currently in the queue /// public bool InQueue { get; set; } = false; - -} +} \ No newline at end of file diff --git a/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs b/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs index d373c1e6..3a2dbdf0 100644 --- a/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs +++ b/mod/Jailbreak.Warden/Selection/WardenSelectionBehavior.cs @@ -1,5 +1,4 @@ -using System.Reflection; -using System.Runtime.CompilerServices; +using System.Runtime.CompilerServices; using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; @@ -14,34 +13,31 @@ using Microsoft.Extensions.Logging; -using Serilog; - using Timer = CounterStrikeSharp.API.Modules.Timers.Timer; -namespace Jailbreak.Warden.Queue; +namespace Jailbreak.Warden.Selection; /// -/// Behavior responsible for choosing the next warden +/// Behavior responsible for choosing the next warden /// public class WardenSelectionBehavior : IPluginBehavior, IWardenSelectionService { + private readonly IPlayerState _favor; + + private readonly ILogger _logger; /// - /// A state dict that handles the player's current queue - /// enrollment and favor. Uses the Round reset mode. + /// A state dict that handles the player's current queue + /// enrollment and favor. Uses the Round reset mode. /// - private IPlayerState _queue; - - private IPlayerState _favor; - - private ILogger _logger; + private readonly IPlayerState _queue; /// - /// Whether or not to use the queue. - /// When true, the queue should be skipped and turn to first-come-first-serve. + /// Whether or not to use the queue. + /// When true, the queue should be skipped and turn to first-come-first-serve. /// private bool _queueInactive; - private IWardenService _warden; + private readonly IWardenService _warden; public WardenSelectionBehavior(IPlayerStateFactory factory, IWardenService warden, ILogger logger) { @@ -55,14 +51,37 @@ public WardenSelectionBehavior(IPlayerStateFactory factory, IWardenService warde public void Start(BasePlugin parent) { - } public void Dispose() { + } + + public bool TryEnter(CCSPlayerController player) + { + if (!CanEnterQueue(player)) + return false; + + _queue.Get(player).InQueue = true; + return true; + } + + public bool TryExit(CCSPlayerController player) + { + if (!CanEnterQueue(player)) + return false; + _queue.Get(player).InQueue = false; + return true; + } + + public bool InQueue(CCSPlayerController player) + { + return _queue.Get(player).InQueue; } + public bool Active => !_queueInactive; + [GameEventHandler] public HookResult OnRoundStart(EventRoundStart ev, GameEventInfo info) { @@ -85,7 +104,7 @@ public void ScheduleChooseWarden(float time = 5.0f) } /// - /// Timer callback that states it's time to choose the warden. + /// Timer callback that states it's time to choose the warden. /// protected void OnChooseWarden() { @@ -107,19 +126,19 @@ protected void OnChooseWarden() var favors = eligible .ToDictionary(player => player, player => _favor.Get(player)); - int tickets = favors.Sum(favor => favor.Value.GetTickets()); - int chosen = Random.Shared.Next(tickets); + var tickets = favors.Sum(favor => favor.Value.GetTickets()); + var chosen = Random.Shared.Next(tickets); _logger.LogTrace("[Warden Raffle] Picking {@Chosen} out of {@Tickets}", chosen, tickets); - int pointer = 0; + var pointer = 0; foreach (var (player, favor) in favors) { - int thisTickets = favor.GetTickets(); + var thisTickets = favor.GetTickets(); _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)) + if (pointer <= chosen && chosen < pointer + thisTickets) { _warden.TrySetWarden(player); favor.RoundsWithoutWarden = 0; @@ -155,29 +174,4 @@ private bool CanEnterQueue(CCSPlayerController player) return true; } - - public bool TryEnter(CCSPlayerController player) - { - if (!CanEnterQueue(player)) - return false; - - _queue.Get(player).InQueue = true; - return true; - } - - public bool TryExit(CCSPlayerController player) - { - if (!CanEnterQueue(player)) - return false; - - _queue.Get(player).InQueue = false; - return true; - } - - public bool InQueue(CCSPlayerController player) - => _queue.Get(player).InQueue; - - public bool Active => !_queueInactive; - - } diff --git a/mod/Jailbreak.Warden/WardenServiceExtension.cs b/mod/Jailbreak.Warden/WardenServiceExtension.cs index d6f17af1..b61412b1 100644 --- a/mod/Jailbreak.Warden/WardenServiceExtension.cs +++ b/mod/Jailbreak.Warden/WardenServiceExtension.cs @@ -1,10 +1,8 @@ -using CounterStrikeSharp.API.Core; - -using Jailbreak.Public.Extensions; +using Jailbreak.Public.Extensions; using Jailbreak.Public.Mod.Warden; using Jailbreak.Warden.Commands; using Jailbreak.Warden.Global; -using Jailbreak.Warden.Queue; +using Jailbreak.Warden.Selection; using Microsoft.Extensions.DependencyInjection; @@ -19,4 +17,4 @@ public static void AddJailbreakWarden(this IServiceCollection serviceCollection) serviceCollection.AddPluginBehavior(); } -} +} \ No newline at end of file diff --git a/public/Jailbreak.Public/Behaviors/IPluginBehavior.cs b/public/Jailbreak.Public/Behaviors/IPluginBehavior.cs index 137eb709..e645f759 100644 --- a/public/Jailbreak.Public/Behaviors/IPluginBehavior.cs +++ b/public/Jailbreak.Public/Behaviors/IPluginBehavior.cs @@ -3,22 +3,18 @@ namespace Jailbreak.Public.Behaviors; /// -/// A plugin extension class that is +/// A plugin extension class that is /// public interface IPluginBehavior : IDisposable { + void IDisposable.Dispose() + { + } /// - /// Tells the plugin that it will be starting imminently + /// Tells the plugin that it will be starting imminently /// void Start(BasePlugin parent) { - - } - - void IDisposable.Dispose() - { - } - -} +} \ No newline at end of file diff --git a/public/Jailbreak.Public/Configuration/IConfigService.cs b/public/Jailbreak.Public/Configuration/IConfigService.cs index 7b690ec9..01ce9e78 100644 --- a/public/Jailbreak.Public/Configuration/IConfigService.cs +++ b/public/Jailbreak.Public/Configuration/IConfigService.cs @@ -1,19 +1,16 @@ -using CounterStrikeSharp.API; -using CounterStrikeSharp.API.Core; - -namespace Jailbreak.Public.Configuration; +namespace Jailbreak.Public.Configuration; public interface IConfigService { public const string CONFIG_PATH = "jailbreak.json"; /// - /// Get the configuration object with the provided name + /// Get the configuration object with the provided name /// /// /// If the configuration service would return the default value, fail instead. Loudly. /// /// T Get(string path, bool failOnDefault = false) - where T: class, new(); -} + where T : class, new(); +} \ No newline at end of file diff --git a/public/Jailbreak.Public/Extensions/EnumerableExtensions.cs b/public/Jailbreak.Public/Extensions/EnumerableExtensions.cs index 1e109586..c1d662c6 100644 --- a/public/Jailbreak.Public/Extensions/EnumerableExtensions.cs +++ b/public/Jailbreak.Public/Extensions/EnumerableExtensions.cs @@ -7,18 +7,16 @@ public static IEnumerable Shuffle(this IEnumerable source, Random rng) // From https://stackoverflow.com/questions/1287567/is-using-random-and-orderby-a-good-shuffle-algorithm // Yay stackoverflow :D - T[] elements = source.ToArray(); + var elements = source.ToArray(); // Note i > 0 to avoid final pointless iteration - for (int i = elements.Length-1; i > 0; i--) + for (var i = elements.Length - 1; i > 0; i--) { // Swap element "i" with a random earlier element it (or itself) - int swapIndex = rng.Next(i + 1); + var swapIndex = rng.Next(i + 1); (elements[i], elements[swapIndex]) = (elements[swapIndex], elements[i]); } // Lazily yield (avoiding aliasing issues etc) - foreach (T element in elements) - { + foreach (var element in elements) yield return element; - } } -} +} \ No newline at end of file diff --git a/public/Jailbreak.Public/Extensions/PlayerExtensions.cs b/public/Jailbreak.Public/Extensions/PlayerExtensions.cs index 2b842a2d..2eef64a5 100644 --- a/public/Jailbreak.Public/Extensions/PlayerExtensions.cs +++ b/public/Jailbreak.Public/Extensions/PlayerExtensions.cs @@ -9,4 +9,4 @@ public static CsTeam GetTeam(this CCSPlayerController controller) { return (CsTeam)controller.TeamNum; } -} +} \ No newline at end of file diff --git a/public/Jailbreak.Public/Extensions/ServerExtensions.cs b/public/Jailbreak.Public/Extensions/ServerExtensions.cs index cd058274..b26a213d 100644 --- a/public/Jailbreak.Public/Extensions/ServerExtensions.cs +++ b/public/Jailbreak.Public/Extensions/ServerExtensions.cs @@ -1,5 +1,4 @@ -using CounterStrikeSharp.API; -using CounterStrikeSharp.API.Modules.Memory; +using CounterStrikeSharp.API.Modules.Memory; using CounterStrikeSharp.API.Modules.Utils; namespace Jailbreak.Public.Extensions; @@ -10,4 +9,4 @@ public static void PrintToCenterAll(string message) { VirtualFunctions.ClientPrintAll(HudDestination.Center, message, 0, 0, 0, 0); } -} +} \ No newline at end of file diff --git a/public/Jailbreak.Public/Extensions/ServiceCollectionExtensions.cs b/public/Jailbreak.Public/Extensions/ServiceCollectionExtensions.cs index ea3f949c..f758cce2 100644 --- a/public/Jailbreak.Public/Extensions/ServiceCollectionExtensions.cs +++ b/public/Jailbreak.Public/Extensions/ServiceCollectionExtensions.cs @@ -8,12 +8,12 @@ namespace Jailbreak.Public.Extensions; public static class ServiceCollectionExtensions { /// - /// Add a to the global service collection + /// Add a to the global service collection /// /// /// public static void AddPluginBehavior(this IServiceCollection collection) - where TExtension: class, IPluginBehavior + where TExtension : class, IPluginBehavior { // Add the root extension itself as a scoped service. // This means every time Load is called in the main Jailbreak loader, @@ -25,13 +25,13 @@ public static void AddPluginBehavior(this IServiceCollection collect } /// - /// Add a to the global service collection + /// Add a to the global service collection /// /// /// /// public static void AddPluginBehavior(this IServiceCollection collection) - where TExtension: class, IPluginBehavior, TInterface + where TExtension : class, IPluginBehavior, TInterface where TInterface : class { // Add the root extension itself as a scoped service. @@ -45,7 +45,7 @@ public static void AddPluginBehavior(this IServiceCollec } /// - /// Add an object to be loaded from the configuration file + /// Add an object to be loaded from the configuration file /// /// /// The section where the configuration object will be loaded from @@ -59,6 +59,6 @@ public static void AddConfig(this IServiceCollection collection, string // Not *really* important... but do we want to fail here or return default if section // isn't available? collection.AddTransient(provider => provider.GetRequiredService() - .Get(sectionName, /*failOnDefault*/ false)); + .Get(sectionName)); } -} +} \ No newline at end of file diff --git a/public/Jailbreak.Public/Extensions/StringExtensions.cs b/public/Jailbreak.Public/Extensions/StringExtensions.cs index cce35f8a..d29aef34 100644 --- a/public/Jailbreak.Public/Extensions/StringExtensions.cs +++ b/public/Jailbreak.Public/Extensions/StringExtensions.cs @@ -7,4 +7,4 @@ public static string Sanitize(this string unknown) return unknown .Replace("<", "<"); } -} +} \ No newline at end of file diff --git a/public/Jailbreak.Public/Generic/IPlayerState.cs b/public/Jailbreak.Public/Generic/IPlayerState.cs index fa903c76..c93903db 100644 --- a/public/Jailbreak.Public/Generic/IPlayerState.cs +++ b/public/Jailbreak.Public/Generic/IPlayerState.cs @@ -3,14 +3,12 @@ namespace Jailbreak.Public.Generic; /// -/// A player state dictionary that automatically deletes stale states -/// and translates between different client formats. +/// A player state dictionary that automatically deletes stale states +/// and translates between different client formats. /// /// public interface IPlayerState - where TState: class, new() + where TState : class, new() { - TState Get(CCSPlayerController controller); - -} +} \ No newline at end of file diff --git a/public/Jailbreak.Public/Generic/IPlayerStateFactory.cs b/public/Jailbreak.Public/Generic/IPlayerStateFactory.cs index 7e9872fc..1dbc9b15 100644 --- a/public/Jailbreak.Public/Generic/IPlayerStateFactory.cs +++ b/public/Jailbreak.Public/Generic/IPlayerStateFactory.cs @@ -2,9 +2,8 @@ public interface IPlayerStateFactory { - /// - /// This state lasts from when the player connect to until the player disconnects. + /// This state lasts from when the player connect to until the player disconnects. /// /// /// @@ -12,7 +11,7 @@ IPlayerState Global() where T : class, new(); /// - /// This state resets when the player dies + /// This state resets when the player dies /// /// /// @@ -20,11 +19,10 @@ IPlayerState Alive() where T : class, new(); /// - /// This state resets when the round ends or begins. + /// This state resets when the round ends or begins. /// /// /// IPlayerState Round() where T : class, new(); - -} +} \ No newline at end of file diff --git a/public/Jailbreak.Public/Jailbreak.Public.csproj b/public/Jailbreak.Public/Jailbreak.Public.csproj index 2dd2d587..83079e55 100644 --- a/public/Jailbreak.Public/Jailbreak.Public.csproj +++ b/public/Jailbreak.Public/Jailbreak.Public.csproj @@ -7,12 +7,12 @@ - - + + - + diff --git a/public/Jailbreak.Public/Mod/Teams/IGuardQueue.cs b/public/Jailbreak.Public/Mod/Teams/IGuardQueue.cs index cb374beb..2abc6639 100644 --- a/public/Jailbreak.Public/Mod/Teams/IGuardQueue.cs +++ b/public/Jailbreak.Public/Mod/Teams/IGuardQueue.cs @@ -4,6 +4,10 @@ namespace Jailbreak.Public.Mod.Teams; public interface IGuardQueue { + /// + /// Get all players in the queue + /// + IEnumerable Queue { get; } bool TryEnterQueue(CCSPlayerController player); @@ -11,36 +15,31 @@ public interface IGuardQueue bool TryExitQueue(CCSPlayerController player); /// - /// Pop the provided amount of players from the queue - /// and put them on the CT team + /// Pop the provided amount of players from the queue + /// and put them on the CT team /// /// /// bool TryPop(int count); /// - /// Pull the provided amount of players from the CT team - /// and put them back in the queue. + /// Pull the provided amount of players from the CT team + /// and put them back in the queue. /// /// /// bool TryPush(int count); /// - /// Force the player to join CT, without being pushed back to T. + /// Force the player to join CT, without being pushed back to T. /// /// void ForceGuard(CCSPlayerController player); /// - /// Get this player's position in the queue + /// Get this player's position in the queue /// /// /// int GetQueuePosition(CCSPlayerController player); - - /// - /// Get all players in the queue - /// - IEnumerable Queue { get; } -} +} \ No newline at end of file diff --git a/public/Jailbreak.Public/Mod/Warden/IWardenSelectionService.cs b/public/Jailbreak.Public/Mod/Warden/IWardenSelectionService.cs index 4f5a88a4..b25730f7 100644 --- a/public/Jailbreak.Public/Mod/Warden/IWardenSelectionService.cs +++ b/public/Jailbreak.Public/Mod/Warden/IWardenSelectionService.cs @@ -5,26 +5,26 @@ namespace Jailbreak.Public.Mod.Warden; public interface IWardenSelectionService { /// - /// Enter this player into the warden queue + /// Whether or not the warden queue is currently in use + /// + bool Active { get; } + + /// + /// Enter this player into the warden queue /// /// bool TryEnter(CCSPlayerController player); /// - /// Remove this player from the warden queue + /// Remove this player from the warden queue /// /// bool TryExit(CCSPlayerController player); /// - /// Determine whether this player is in the queue or not + /// Determine whether this player is in the queue or not /// /// /// bool InQueue(CCSPlayerController player); - - /// - /// Whether or not the warden queue is currently in use - /// - bool Active {get;} -} +} \ No newline at end of file diff --git a/public/Jailbreak.Public/Mod/Warden/IWardenService.cs b/public/Jailbreak.Public/Mod/Warden/IWardenService.cs index 097efd3c..abd7b897 100644 --- a/public/Jailbreak.Public/Mod/Warden/IWardenService.cs +++ b/public/Jailbreak.Public/Mod/Warden/IWardenService.cs @@ -4,11 +4,10 @@ namespace Jailbreak.Public.Mod.Warden; public interface IWardenService { - CCSPlayerController? Warden { get; } /// - /// Whether or not a warden is currently assigned + /// Whether or not a warden is currently assigned /// bool HasWarden { get; } @@ -16,5 +15,4 @@ public interface IWardenService bool TrySetWarden(CCSPlayerController warden); bool TryRemoveWarden(); - -} +} \ No newline at end of file diff --git a/src/Jailbreak.Generic/GenericServiceExtension.cs b/src/Jailbreak.Generic/GenericServiceExtension.cs index 0dd96e1f..850207e3 100644 --- a/src/Jailbreak.Generic/GenericServiceExtension.cs +++ b/src/Jailbreak.Generic/GenericServiceExtension.cs @@ -1,6 +1,5 @@ -using CounterStrikeSharp.API.Core; - -using Jailbreak.Generic.Behaviors; +using Jailbreak.Generic.PlayerState; +using Jailbreak.Generic.PlayerState.Behaviors; using Jailbreak.Public.Extensions; using Jailbreak.Public.Generic; diff --git a/src/Jailbreak.Generic/Jailbreak.Generic.csproj b/src/Jailbreak.Generic/Jailbreak.Generic.csproj index d809758e..29f7e134 100644 --- a/src/Jailbreak.Generic/Jailbreak.Generic.csproj +++ b/src/Jailbreak.Generic/Jailbreak.Generic.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/Jailbreak.Generic/PlayerState/Behaviors/AliveStateTracker.cs b/src/Jailbreak.Generic/PlayerState/Behaviors/AliveStateTracker.cs index 1ff3c8d6..1400a989 100644 --- a/src/Jailbreak.Generic/PlayerState/Behaviors/AliveStateTracker.cs +++ b/src/Jailbreak.Generic/PlayerState/Behaviors/AliveStateTracker.cs @@ -1,17 +1,14 @@ -using System.Reflection; - -using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core.Attributes.Registration; using Jailbreak.Public.Behaviors; -namespace Jailbreak.Generic.Behaviors; +namespace Jailbreak.Generic.PlayerState.Behaviors; public class AliveStateTracker : BaseStateTracker, IPluginBehavior { public void Start(BasePlugin parent) { - } [GameEventHandler] @@ -21,4 +18,4 @@ public HookResult OnDeath(EventPlayerDeath ev, GameEventInfo info) return HookResult.Continue; } -} +} \ No newline at end of file diff --git a/src/Jailbreak.Generic/PlayerState/Behaviors/BaseStateTracker.cs b/src/Jailbreak.Generic/PlayerState/Behaviors/BaseStateTracker.cs index ef846748..b2cfa420 100644 --- a/src/Jailbreak.Generic/PlayerState/Behaviors/BaseStateTracker.cs +++ b/src/Jailbreak.Generic/PlayerState/Behaviors/BaseStateTracker.cs @@ -1,20 +1,25 @@ using CounterStrikeSharp.API.Core; -namespace Jailbreak.Generic.Behaviors; +namespace Jailbreak.Generic.PlayerState.Behaviors; public class BaseStateTracker : IDisposable { - private List _trackedPlayerStates = new(); + private readonly List _trackedPlayerStates = new(); + + public void Dispose() + { + ResetAll(); + } protected void Reset(CCSPlayerController controller) { - foreach (ITrackedPlayerState trackedPlayerState in _trackedPlayerStates) + foreach (var trackedPlayerState in _trackedPlayerStates) trackedPlayerState.Reset(controller); } protected void ResetAll() { - foreach (ITrackedPlayerState trackedPlayerState in _trackedPlayerStates) + foreach (var trackedPlayerState in _trackedPlayerStates) trackedPlayerState.Drop(); } @@ -22,7 +27,4 @@ public void Track(ITrackedPlayerState state) { _trackedPlayerStates.Add(state); } - - public void Dispose() - => ResetAll(); -} +} \ No newline at end of file diff --git a/src/Jailbreak.Generic/PlayerState/Behaviors/GlobalStateTracker.cs b/src/Jailbreak.Generic/PlayerState/Behaviors/GlobalStateTracker.cs index b15ca770..e6d459b2 100644 --- a/src/Jailbreak.Generic/PlayerState/Behaviors/GlobalStateTracker.cs +++ b/src/Jailbreak.Generic/PlayerState/Behaviors/GlobalStateTracker.cs @@ -3,17 +3,16 @@ using Jailbreak.Public.Behaviors; -namespace Jailbreak.Generic.Behaviors; +namespace Jailbreak.Generic.PlayerState.Behaviors; public class GlobalStateTracker : BaseStateTracker, IPluginBehavior { public void Start(BasePlugin parent) { - } /// - /// Disconnect handler to reset states on user leave + /// Disconnect handler to reset states on user leave /// /// /// @@ -23,4 +22,4 @@ public HookResult OnDisconnect(EventPlayerDisconnect ev, GameEventInfo info) Reset(ev.Userid); return HookResult.Continue; } -} +} \ No newline at end of file diff --git a/src/Jailbreak.Generic/PlayerState/Behaviors/RoundStateTracker.cs b/src/Jailbreak.Generic/PlayerState/Behaviors/RoundStateTracker.cs index e50bd075..1c4aa376 100644 --- a/src/Jailbreak.Generic/PlayerState/Behaviors/RoundStateTracker.cs +++ b/src/Jailbreak.Generic/PlayerState/Behaviors/RoundStateTracker.cs @@ -1,19 +1,14 @@ -using System.Reflection; - -using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core.Attributes.Registration; using Jailbreak.Public.Behaviors; -using Serilog; - -namespace Jailbreak.Generic.Behaviors; +namespace Jailbreak.Generic.PlayerState.Behaviors; public class RoundStateTracker : BaseStateTracker, IPluginBehavior { public void Start(BasePlugin parent) { - } [GameEventHandler] diff --git a/src/Jailbreak.Generic/PlayerState/ITrackedPlayerState.cs b/src/Jailbreak.Generic/PlayerState/ITrackedPlayerState.cs index 73a0af5d..b0638454 100644 --- a/src/Jailbreak.Generic/PlayerState/ITrackedPlayerState.cs +++ b/src/Jailbreak.Generic/PlayerState/ITrackedPlayerState.cs @@ -1,17 +1,17 @@ using CounterStrikeSharp.API.Core; -namespace Jailbreak.Generic; +namespace Jailbreak.Generic.PlayerState; public interface ITrackedPlayerState { /// - /// Reset a state for a specific player + /// Reset a state for a specific player /// /// void Reset(CCSPlayerController controller); /// - /// Reset states for all players + /// Reset states for all players /// void Drop(); } diff --git a/src/Jailbreak.Generic/PlayerState/PlayerStateFactory.cs b/src/Jailbreak.Generic/PlayerState/PlayerStateFactory.cs index fdd4c730..ea8a4cde 100644 --- a/src/Jailbreak.Generic/PlayerState/PlayerStateFactory.cs +++ b/src/Jailbreak.Generic/PlayerState/PlayerStateFactory.cs @@ -1,13 +1,13 @@ -using Jailbreak.Generic.Behaviors; +using Jailbreak.Generic.PlayerState.Behaviors; using Jailbreak.Public.Generic; -namespace Jailbreak.Generic; +namespace Jailbreak.Generic.PlayerState; public class PlayerStateFactory : IPlayerStateFactory { - private GlobalStateTracker _global; - private AliveStateTracker _alive; - private RoundStateTracker _round; + private readonly AliveStateTracker _alive; + private readonly GlobalStateTracker _global; + private readonly RoundStateTracker _round; public PlayerStateFactory(GlobalStateTracker global, AliveStateTracker alive, RoundStateTracker round) { @@ -32,7 +32,8 @@ public PlayerStateFactory(GlobalStateTracker global, AliveStateTracker alive, Ro _global.Track(state); _alive.Track(state); - return state; } + return state; + } public IPlayerState Round() where T : class, new() { diff --git a/src/Jailbreak.Generic/PlayerState/PlayerStateImpl.cs b/src/Jailbreak.Generic/PlayerState/PlayerStateImpl.cs index 1e9b4424..d5a3a615 100644 --- a/src/Jailbreak.Generic/PlayerState/PlayerStateImpl.cs +++ b/src/Jailbreak.Generic/PlayerState/PlayerStateImpl.cs @@ -2,12 +2,12 @@ using Jailbreak.Public.Generic; -namespace Jailbreak.Generic; +namespace Jailbreak.Generic.PlayerState; public class PlayerStateImpl : IPlayerState, ITrackedPlayerState where TState : class, new() { - private Dictionary _states = new(); + private readonly Dictionary _states = new(); public TState Get(CCSPlayerController controller) { @@ -18,8 +18,12 @@ public TState Get(CCSPlayerController controller) } public void Reset(CCSPlayerController controller) - => _states.Remove(controller.Slot); + { + _states.Remove(controller.Slot); + } public void Drop() - => _states.Clear(); + { + _states.Clear(); + } } diff --git a/src/Jailbreak/Config/ConfigService.cs b/src/Jailbreak/Config/ConfigService.cs index 446531d9..c7d52286 100644 --- a/src/Jailbreak/Config/ConfigService.cs +++ b/src/Jailbreak/Config/ConfigService.cs @@ -7,33 +7,18 @@ using Microsoft.Extensions.Logging; - namespace Jailbreak.Config; public class ConfigService : IConfigService { - private ILogger _logger; + private readonly ILogger _logger; public ConfigService(ILogger logger) { _logger = logger; } - private T Fail(bool fail, string message) - where T: class, new() - { - // We would be returning default. - // Check if caller wants us to cry and scream instead. - if (fail) - throw new InvalidOperationException(message); - - _logger.LogWarning("[Config] Tripped load fail state with message: {@Message}", message); - - return new T(); - } - /// - /// /// /// /// @@ -49,7 +34,7 @@ public T Get(string path, bool fail = false) var jsonText = File.ReadAllText(jsonPath); - var jsonObject = JsonObject.Parse(jsonText); + var jsonObject = JsonNode.Parse(jsonText); if (jsonObject == null) return Fail(fail, $"Unable to parse configuration file at {jsonPath}"); @@ -63,4 +48,17 @@ public T Get(string path, bool fail = false) return config; } + + private T Fail(bool fail, string message) + where T : class, new() + { + // We would be returning default. + // Check if caller wants us to cry and scream instead. + if (fail) + throw new InvalidOperationException(message); + + _logger.LogWarning("[Config] Tripped load fail state with message: {@Message}", message); + + return new T(); + } } diff --git a/src/Jailbreak/Jailbreak.cs b/src/Jailbreak/Jailbreak.cs index de0352d9..34ab659d 100644 --- a/src/Jailbreak/Jailbreak.cs +++ b/src/Jailbreak/Jailbreak.cs @@ -1,7 +1,5 @@ using System.Collections.Immutable; -using System.Reflection; -using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using Jailbreak.Public.Behaviors; @@ -9,25 +7,26 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using Serilog; - namespace Jailbreak; public class Jailbreak : BasePlugin { - public override string ModuleName => "Jailbreak"; - public override string ModuleVersion => "0.1.0"; - public override string ModuleAuthor => "EdgeGamers Development"; + private IReadOnlyList _extensions; - private IServiceProvider _provider; + private readonly IServiceProvider _provider; private IServiceScope _scope; - private IReadOnlyList _extensions; public Jailbreak(IServiceProvider provider) { _provider = provider; } + public override string ModuleName => "Jailbreak"; + + public override string ModuleVersion => "0.1.0"; + + public override string ModuleAuthor => "EdgeGamers Development"; + public override void Load(bool hotReload) { Logger.LogInformation("[Jailbreak] Loading..."); @@ -38,7 +37,7 @@ public override void Load(bool hotReload) Logger.LogInformation("[Jailbreak] Found {@BehaviorCount} behaviors.", _extensions.Count); - foreach (IPluginBehavior extension in _extensions) + foreach (var extension in _extensions) { // Register all event handlers on the extension object RegisterAllAttributes(extension); @@ -56,10 +55,8 @@ public override void Unload(bool hotReload) { Logger.LogInformation("[Jailbreak] Shutting down..."); - foreach (IPluginBehavior extension in _extensions) - { + foreach (var extension in _extensions) extension.Dispose(); - } // Dispose of original extensions scope // When loading again we will get a new scope to avoid leaking state. @@ -67,4 +64,4 @@ public override void Unload(bool hotReload) base.Unload(hotReload); } -} +} \ No newline at end of file diff --git a/src/Jailbreak/Jailbreak.csproj b/src/Jailbreak/Jailbreak.csproj index 19088432..50d6651b 100644 --- a/src/Jailbreak/Jailbreak.csproj +++ b/src/Jailbreak/Jailbreak.csproj @@ -32,10 +32,10 @@ - - - - + + + + diff --git a/src/Jailbreak/JailbreakServiceCollection.cs b/src/Jailbreak/JailbreakServiceCollection.cs index 1c7c7756..7bd7b26a 100644 --- a/src/Jailbreak/JailbreakServiceCollection.cs +++ b/src/Jailbreak/JailbreakServiceCollection.cs @@ -1,6 +1,4 @@ -using System.Reflection; - -using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Core; using Jailbreak.Config; using Jailbreak.Generic; @@ -9,12 +7,11 @@ using Jailbreak.Warden; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; namespace Jailbreak; /// -/// Class that auto-registers all jailbreak services and classes. +/// Class that auto-registers all jailbreak services and classes. /// public class JailbreakServiceCollection : IPluginServiceCollection { @@ -29,4 +26,4 @@ public void ConfigureServices(IServiceCollection serviceCollection) serviceCollection.AddJailbreakWarden(); serviceCollection.AddJailbreakTeams(); } -} +} \ No newline at end of file