diff --git a/lang/Jailbreak.English/Logs/LogMessages.cs b/lang/Jailbreak.English/Logs/LogMessages.cs index c5eda4da..3b446565 100644 --- a/lang/Jailbreak.English/Logs/LogMessages.cs +++ b/lang/Jailbreak.English/Logs/LogMessages.cs @@ -1,8 +1,34 @@ -using Jailbreak.Formatting.Logistics; +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Modules.Utils; + +using Jailbreak.Formatting.Base; +using Jailbreak.Formatting.Core; +using Jailbreak.Formatting.Logistics; +using Jailbreak.Formatting.Objects; +using Jailbreak.Formatting.Views; +using Jailbreak.Public.Extensions; namespace Jailbreak.English.Logs; -public class LogMessages : ILanguage +public class LogMessages : ILogMessages, ILanguage { + + + public IView BEGIN_JAILBREAK_LOGS => new SimpleView() + { + { "********************************" }, + { "***** BEGIN JAILBREAK LOGS *****" }, + { "********************************" } + }; + + public IView END_JAILBREAK_LOGS => new SimpleView() + { + { "********************************" }, + { "****** END JAILBREAK LOGS ******" }, + { "********************************" } + }; + + + } diff --git a/mod/Jailbreak.Logs/LogsListeners.cs b/mod/Jailbreak.Logs/LogsListeners.cs index 50fc0f5e..12957f6b 100644 --- a/mod/Jailbreak.Logs/LogsListeners.cs +++ b/mod/Jailbreak.Logs/LogsListeners.cs @@ -36,7 +36,7 @@ private HookResult OnButtonPressed(CEntityIOOutput output, string name, CEntityI CBaseEntity? ent = Utilities.GetEntityFromIndex((int)caller.Index); if (!ent.IsValid) return HookResult.Continue; - logs.AddLogMessage( + logs.Append( $"{logs.FormatPlayer(pawn.OriginalController.Value!)} pressed a button {ent.Entity?.Name ?? "Unlabeled"} -> {output?.Connections?.TargetDesc ?? "None"}"); return HookResult.Continue; } @@ -48,7 +48,7 @@ private HookResult OnGrenadeThrown(EventGrenadeThrown @event, GameEventInfo info return HookResult.Continue; var grenade = @event.Weapon; - logs.AddLogMessage($"{logs.FormatPlayer(player)} threw a {grenade}"); + logs.Append($"{logs.FormatPlayer(player)} threw a {grenade}"); return HookResult.Continue; } @@ -67,26 +67,26 @@ private HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info) { if (health > 0) { - logs.AddLogMessage($"The world hurt {logs.FormatPlayer(player)} for {health} damage"); + logs.Append($"The world hurt {logs.FormatPlayer(player)} for {health} damage"); } else { - logs.AddLogMessage($"The world killed {logs.FormatPlayer(player)}"); + logs.Append($"The world killed {logs.FormatPlayer(player)}"); } } else { if (health > 0) { - logs.AddLogMessage( + logs.Append( $"{logs.FormatPlayer(attacker!)} hurt {logs.FormatPlayer(player)} for {health} damage"); } else { - logs.AddLogMessage($"{logs.FormatPlayer(attacker!)} killed {logs.FormatPlayer(player)}"); + logs.Append($"{logs.FormatPlayer(attacker!)} killed {logs.FormatPlayer(player)}"); } } return HookResult.Continue; } -} \ No newline at end of file +} diff --git a/mod/Jailbreak.Logs/LogsManager.cs b/mod/Jailbreak.Logs/LogsManager.cs index 29e1d3c5..b4294f12 100644 --- a/mod/Jailbreak.Logs/LogsManager.cs +++ b/mod/Jailbreak.Logs/LogsManager.cs @@ -1,10 +1,13 @@ using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Core.Attributes.Registration; using CounterStrikeSharp.API.Modules.Utils; using Jailbreak.Formatting.Base; using Jailbreak.Formatting.Core; using Jailbreak.Formatting.Extensions; +using Jailbreak.Formatting.Objects; +using Jailbreak.Formatting.Views; using Jailbreak.Public.Behaviors; using Jailbreak.Public.Extensions; using Jailbreak.Public.Mod.Logs; @@ -17,101 +20,81 @@ namespace Jailbreak.Logs; public class LogsManager : IPluginBehavior, ILogService { private readonly List _logMessages = new(); - private long startTime; - private IWardenService _wardenService; - private IRebelService _rebelService; - private IServiceProvider _serviceProvider; + private ILogMessages _messages; - public LogsManager(IServiceProvider serviceProvider) + public LogsManager(IServiceProvider serviceProvider, ILogMessages messages) { - _serviceProvider = serviceProvider; - } + _messages = messages; - public void Start(BasePlugin parent) - { - parent.RegisterEventHandler(OnRoundStart); - parent.RegisterEventHandler(OnRoundEnd); - _wardenService = _serviceProvider.GetRequiredService(); - _rebelService = _serviceProvider.GetRequiredService(); } + [GameEventHandler] private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info) { + _messages.BEGIN_JAILBREAK_LOGS + .ToServerConsole() + .ToAllConsole(); + // By default, print all logs to player consoles at the end of the round. foreach (var log in _logMessages) - log.ToAllConsole(); + log.ToServerConsole() + .ToAllConsole(); + + _messages.END_JAILBREAK_LOGS + .ToServerConsole() + .ToAllConsole(); return HookResult.Continue; } + [GameEventHandler] private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info) { - startTime = DateTimeOffset.Now.ToUnixTimeSeconds(); - ClearLogMessages(); + Clear(); return HookResult.Continue; } - public void AddLogMessage(string message) + public void Append(params FormatObject[] objects) { - - // format to [MM:SS] message - string prefix = $"[{TimeSpan.FromSeconds(DateTimeOffset.Now.ToUnixTimeSeconds() - startTime):mm\\:ss}] "; - _logMessages.Add(prefix + message); + _logMessages.Add(_messages.CREATE_LOG(objects)); } - public ICollection GetLogMessages() + public void Append(string message) { - return _logMessages; + _logMessages.Add(_messages.CREATE_LOG(message)); } - public void ClearLogMessages() + public IEnumerable GetMessages() { - _logMessages.Clear(); + return _logMessages.SelectMany(view => view.ToWriter().Plain); } - public string FormatPlayer(CCSPlayerController player) + public void Clear() { - if (_wardenService.IsWarden(player)) - return $"{player.PlayerName} (WARDEN)"; - if (player.GetTeam() == CsTeam.CounterTerrorist) - return $"{player.PlayerName} (CT)"; - if (_rebelService.IsRebel(player)) - return $"{player.PlayerName} (REBEL)"; - return $"{player.PlayerName} (Prisoner)"; + _logMessages.Clear(); } - public void PrintLogs(CCSPlayerController? player) { - if (player == null) + if (player == null || !player.IsReal()) { - printLogs(Server.PrintToConsole); - } - else if (player.IsReal()) - { - printLogs(player.PrintToConsole); - } - } + _messages.BEGIN_JAILBREAK_LOGS + .ToServerConsole(); + foreach (var log in _logMessages) + log.ToServerConsole(); + _messages.END_JAILBREAK_LOGS + .ToServerConsole(); - private void printLogs(Delegate printFunction) - { - if (!GetLogMessages().Any()) - { - printFunction.DynamicInvoke("No logs to display."); return; } - printFunction.DynamicInvoke("********************************"); - printFunction.DynamicInvoke("***** BEGIN JAILBREAK LOGS *****"); - printFunction.DynamicInvoke("********************************"); - foreach (string log in GetLogMessages()) - { - printFunction.DynamicInvoke(log); - } - printFunction.DynamicInvoke("********************************"); - printFunction.DynamicInvoke("****** END JAILBREAK LOGS ******"); - printFunction.DynamicInvoke("********************************"); + _messages.BEGIN_JAILBREAK_LOGS + .ToPlayerConsole(player); + foreach (var log in _logMessages) + log.ToPlayerConsole(player); + _messages.END_JAILBREAK_LOGS + .ToPlayerConsole(player); } } diff --git a/mod/Jailbreak.Logs/LogsServiceExtension.cs b/mod/Jailbreak.Logs/LogsServiceExtension.cs index 4c626273..122f6af5 100644 --- a/mod/Jailbreak.Logs/LogsServiceExtension.cs +++ b/mod/Jailbreak.Logs/LogsServiceExtension.cs @@ -1,4 +1,6 @@ -using Jailbreak.Public.Extensions; +using Jailbreak.Formatting.Views; +using Jailbreak.Logs.Tags; +using Jailbreak.Public.Extensions; using Jailbreak.Public.Mod.Logs; using Microsoft.Extensions.DependencyInjection; @@ -8,9 +10,13 @@ public static class LogsServiceExtension { public static void AddJailbreakLogs(this IServiceCollection services) { - services.AddPluginBehavior(); - - services.AddPluginBehavior(); - services.AddPluginBehavior(); - } -} \ No newline at end of file + services.AddPluginBehavior(); + + services.AddPluginBehavior(); + services.AddPluginBehavior(); + + // PlayerTagHelper is a lower-level class that avoids dependency loops. + services.AddTransient(); + services.AddTransient(); + } +} diff --git a/mod/Jailbreak.Logs/Tags/PlayerTagHelper.cs b/mod/Jailbreak.Logs/Tags/PlayerTagHelper.cs new file mode 100644 index 00000000..62ef7d07 --- /dev/null +++ b/mod/Jailbreak.Logs/Tags/PlayerTagHelper.cs @@ -0,0 +1,44 @@ +using CounterStrikeSharp.API.Core; +using CounterStrikeSharp.API.Modules.Utils; + +using Jailbreak.Formatting.Core; +using Jailbreak.Formatting.Objects; +using Jailbreak.Formatting.Views; +using Jailbreak.Public.Extensions; +using Jailbreak.Public.Mod.Logs; +using Jailbreak.Public.Mod.Rebel; +using Jailbreak.Public.Mod.Warden; + +using Microsoft.Extensions.DependencyInjection; + +namespace Jailbreak.Logs.Tags; + +public class PlayerTagHelper : IRichPlayerTag, IPlayerTag +{ + private IWardenService _wardenService; + private IRebelService _rebelService; + + public PlayerTagHelper(IServiceProvider provider) + { + // Lazy-load dependencies to avoid loops, since we are a lower-level class. + _wardenService = provider.GetRequiredService(); + _rebelService = provider.GetRequiredService(); + } + + public FormatObject Rich(CCSPlayerController player) + { + if (_wardenService.IsWarden(player)) + return new StringFormatObject("(WARDEN)", ChatColors.DarkBlue); + if (player.GetTeam() == CsTeam.CounterTerrorist) + return new StringFormatObject("(CT)", ChatColors.BlueGrey); + if (_rebelService.IsRebel(player)) + return new StringFormatObject("(REBEL)", ChatColors.Darkred); + + return new StringFormatObject("(T)", ChatColors.Yellow); + } + + public string Plain(CCSPlayerController playerController) + { + return $"{playerController.PlayerName} [#{playerController.UserId}] {Rich(playerController).ToPlain()}"; + } +} diff --git a/mod/Jailbreak.Rebel/RebelManager.cs b/mod/Jailbreak.Rebel/RebelManager.cs index 61d6203d..d2909a58 100644 --- a/mod/Jailbreak.Rebel/RebelManager.cs +++ b/mod/Jailbreak.Rebel/RebelManager.cs @@ -117,7 +117,7 @@ public bool MarkRebel(CCSPlayerController player, long time = 120) { if (!rebelTimes.ContainsKey(player)) { - logs.AddLogMessage(player.PlayerName + " is now a rebel."); + logs.Append(player.PlayerName + " is now a rebel."); } rebelTimes[player] = DateTimeOffset.Now.ToUnixTimeSeconds() + time; @@ -128,7 +128,7 @@ public bool MarkRebel(CCSPlayerController player, long time = 120) public void UnmarkRebel(CCSPlayerController player) { notifs.NO_LONGER_REBEL.ToPlayerChat(player); - logs.AddLogMessage(player.PlayerName + " is no longer a rebel."); + logs.Append(player.PlayerName + " is no longer a rebel."); rebelTimes.Remove(player); ApplyRebelColor(player); diff --git a/mod/Jailbreak.Warden/Global/WardenBehavior.cs b/mod/Jailbreak.Warden/Global/WardenBehavior.cs index ad5cc0bf..05c05dbc 100644 --- a/mod/Jailbreak.Warden/Global/WardenBehavior.cs +++ b/mod/Jailbreak.Warden/Global/WardenBehavior.cs @@ -71,7 +71,7 @@ public bool TrySetWarden(CCSPlayerController controller) .ToAllChat() .ToAllCenter(); - logs.AddLogMessage($"{_warden.PlayerName} is now the warden."); + logs.Append($"{_warden.PlayerName} is now the warden."); return true; } @@ -87,7 +87,7 @@ public bool TryRemoveWarden() _warden.Pawn.Value.RenderMode = RenderMode_t.kRenderTransColor; _warden.Pawn.Value.Render = Color.FromArgb(254, 255, 255, 255); Utilities.SetStateChanged(_warden.Pawn.Value, "CBaseModelEntity", "m_clrRender"); - logs.AddLogMessage($"{_warden.PlayerName} is no longer the warden."); + logs.Append($"{_warden.PlayerName} is no longer the warden."); } _warden = null; diff --git a/public/Jailbreak.Formatting/Extensions/ViewExtensions.cs b/public/Jailbreak.Formatting/Extensions/ViewExtensions.cs index 981f483a..4d0663b6 100644 --- a/public/Jailbreak.Formatting/Extensions/ViewExtensions.cs +++ b/public/Jailbreak.Formatting/Extensions/ViewExtensions.cs @@ -18,6 +18,18 @@ public static FormatWriter ToWriter(this IView view) return writer; } + public static IView ToServerConsole(this IView view) + { + var writer = view.ToWriter(); + + foreach (string s in writer.Plain) + { + Server.PrintToConsole(s); + } + + return view; + } + #region Individual public static IView ToPlayerConsole(this IView view, CCSPlayerController player) diff --git a/public/Jailbreak.Formatting/Logistics/LanguageConfig.cs b/public/Jailbreak.Formatting/Logistics/LanguageConfig.cs index 3848bfc8..43a22a42 100644 --- a/public/Jailbreak.Formatting/Logistics/LanguageConfig.cs +++ b/public/Jailbreak.Formatting/Logistics/LanguageConfig.cs @@ -18,7 +18,7 @@ public LanguageConfig(IServiceCollection collection) public void WithGenericCommand() where TGenericCommand : class, ILanguage, IGenericCommandNotifications => _collection.AddSingleton(); - + public void WithRatio() where TRatio : class, ILanguage, IRatioNotifications => _collection.AddSingleton(); @@ -26,8 +26,12 @@ public void WithRatio() public void WithWarden() where TWarden : class, ILanguage, IWardenNotifications => _collection.AddSingleton(); - + public void WithRebel() where TRebel : class, ILanguage, IRebelNotifications => _collection.AddSingleton(); + + public void WithLogging() + where TLogging : class, ILanguage, ILogMessages + => _collection.AddSingleton(); } diff --git a/public/Jailbreak.Formatting/Views/ILogMessages.cs b/public/Jailbreak.Formatting/Views/ILogMessages.cs new file mode 100644 index 00000000..759cae8e --- /dev/null +++ b/public/Jailbreak.Formatting/Views/ILogMessages.cs @@ -0,0 +1,39 @@ +using CounterStrikeSharp.API; +using CounterStrikeSharp.API.Modules.Utils; + +using Jailbreak.Formatting.Base; +using Jailbreak.Formatting.Core; +using Jailbreak.Formatting.Objects; +using Jailbreak.Public.Extensions; + +namespace Jailbreak.Formatting.Views; + +public interface ILogMessages +{ + + public FormatObject TIME() + { + var gamerules = ServerExtensions.GetGameRules(); + var start = gamerules.RoundStartTime; + var current = Server.CurrentTime; + var elapsed = current - start; + + var minutes = Math.Floor(elapsed / 60f).ToString("00"); + var seconds = Math.Floor(elapsed % 60).ToString("00"); + + return new StringFormatObject($"[{minutes}:{seconds}]", ChatColors.Gold); + } + + public IView CREATE_LOG(params FormatObject[] objects) + { + return new SimpleView() + { + TIME(), + objects + }; + } + + public IView BEGIN_JAILBREAK_LOGS { get; } + + public IView END_JAILBREAK_LOGS { get; } +} diff --git a/public/Jailbreak.Formatting/Views/IRichPlayerTag.cs b/public/Jailbreak.Formatting/Views/IRichPlayerTag.cs new file mode 100644 index 00000000..0c14809a --- /dev/null +++ b/public/Jailbreak.Formatting/Views/IRichPlayerTag.cs @@ -0,0 +1,15 @@ +using CounterStrikeSharp.API.Core; + +using Jailbreak.Formatting.Core; + +namespace Jailbreak.Formatting.Views; + +public interface IRichPlayerTag +{ + /// + /// Get a tag for this player, which contains context about the player's current actions + /// + /// + /// + FormatObject Rich(CCSPlayerController player); +} diff --git a/public/Jailbreak.Public/Mod/Logs/ILogService.cs b/public/Jailbreak.Public/Mod/Logs/ILogService.cs index 8e528f62..4be4b511 100644 --- a/public/Jailbreak.Public/Mod/Logs/ILogService.cs +++ b/public/Jailbreak.Public/Mod/Logs/ILogService.cs @@ -4,9 +4,8 @@ namespace Jailbreak.Public.Mod.Logs; public interface ILogService { - void AddLogMessage(string message); - ICollection GetLogMessages(); - void ClearLogMessages(); - string FormatPlayer(CCSPlayerController player); + void Append(string message); + IEnumerable GetMessages(); + void Clear(); void PrintLogs(CCSPlayerController? player); -} \ No newline at end of file +} diff --git a/public/Jailbreak.Public/Mod/Logs/IPlayerTag.cs b/public/Jailbreak.Public/Mod/Logs/IPlayerTag.cs new file mode 100644 index 00000000..dbdc79ff --- /dev/null +++ b/public/Jailbreak.Public/Mod/Logs/IPlayerTag.cs @@ -0,0 +1,13 @@ +using CounterStrikeSharp.API.Core; + +namespace Jailbreak.Public.Mod.Logs; + +public interface IPlayerTag +{ + /// + /// Get a tag that contains context about the player. + /// + /// + /// + string Plain(CCSPlayerController playerController); +} diff --git a/src/Jailbreak/JailbreakServiceCollection.cs b/src/Jailbreak/JailbreakServiceCollection.cs index d1046809..58fbd4ab 100644 --- a/src/Jailbreak/JailbreakServiceCollection.cs +++ b/src/Jailbreak/JailbreakServiceCollection.cs @@ -5,6 +5,7 @@ using Jailbreak.Config; using Jailbreak.Debug; using Jailbreak.English.Generic; +using Jailbreak.English.Logs; using Jailbreak.English.Rebel; using Jailbreak.English.Teams; using Jailbreak.English.Warden; @@ -48,6 +49,7 @@ public void ConfigureServices(IServiceCollection serviceCollection) config.WithRatio(); config.WithWarden(); config.WithRebel(); + config.WithLogging(); }); } }