Skip to content

Commit

Permalink
Work on translations, boil down LogManager
Browse files Browse the repository at this point in the history
* Remove most stuff from LogManager, into their own classes & services
* Add PlayerTagHelper as an experimental service to avoid dependency loop shenanigans in main classes
* Add LogMessages as an english translation
* Add ToServerConsole IView extension
  • Loading branch information
Mooshua committed Feb 6, 2024
1 parent 0060dd1 commit d10a262
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 84 deletions.
30 changes: 28 additions & 2 deletions lang/Jailbreak.English/Logs/LogMessages.cs
Original file line number Diff line number Diff line change
@@ -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<Formatting.Languages.English>
public class LogMessages : ILogMessages, ILanguage<Formatting.Languages.English>
{



public IView BEGIN_JAILBREAK_LOGS => new SimpleView()
{
{ "********************************" },
{ "***** BEGIN JAILBREAK LOGS *****" },
{ "********************************" }
};

public IView END_JAILBREAK_LOGS => new SimpleView()
{
{ "********************************" },
{ "****** END JAILBREAK LOGS ******" },
{ "********************************" }
};



}
14 changes: 7 additions & 7 deletions mod/Jailbreak.Logs/LogsListeners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private HookResult OnButtonPressed(CEntityIOOutput output, string name, CEntityI
CBaseEntity? ent = Utilities.GetEntityFromIndex<CBaseEntity>((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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
}
}
97 changes: 40 additions & 57 deletions mod/Jailbreak.Logs/LogsManager.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,101 +20,81 @@ namespace Jailbreak.Logs;
public class LogsManager : IPluginBehavior, ILogService
{
private readonly List<IView> _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<EventRoundStart>(OnRoundStart);
parent.RegisterEventHandler<EventRoundEnd>(OnRoundEnd);
_wardenService = _serviceProvider.GetRequiredService<IWardenService>();
_rebelService = _serviceProvider.GetRequiredService<IRebelService>();
}

[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<string> GetLogMessages()
public void Append(string message)
{
return _logMessages;
_logMessages.Add(_messages.CREATE_LOG(message));
}

public void ClearLogMessages()
public IEnumerable<string> 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);
}
}
20 changes: 13 additions & 7 deletions mod/Jailbreak.Logs/LogsServiceExtension.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -8,9 +10,13 @@ public static class LogsServiceExtension
{
public static void AddJailbreakLogs(this IServiceCollection services)
{
services.AddPluginBehavior<ILogService, LogsManager>();

services.AddPluginBehavior<LogsCommand>();
services.AddPluginBehavior<LogsListeners>();
}
}
services.AddPluginBehavior<ILogService, LogsManager>();

services.AddPluginBehavior<LogsCommand>();
services.AddPluginBehavior<LogsListeners>();

// PlayerTagHelper is a lower-level class that avoids dependency loops.
services.AddTransient<IRichPlayerTag, PlayerTagHelper>();
services.AddTransient<IPlayerTag, PlayerTagHelper>();
}
}
44 changes: 44 additions & 0 deletions mod/Jailbreak.Logs/Tags/PlayerTagHelper.cs
Original file line number Diff line number Diff line change
@@ -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<IWardenService>();
_rebelService = provider.GetRequiredService<IRebelService>();
}

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()}";
}
}
4 changes: 2 additions & 2 deletions mod/Jailbreak.Rebel/RebelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions mod/Jailbreak.Warden/Global/WardenBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions public/Jailbreak.Formatting/Extensions/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions public/Jailbreak.Formatting/Logistics/LanguageConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ public LanguageConfig(IServiceCollection collection)
public void WithGenericCommand<TGenericCommand>()
where TGenericCommand : class, ILanguage<TDialect>, IGenericCommandNotifications
=> _collection.AddSingleton<IGenericCommandNotifications, TGenericCommand>();

public void WithRatio<TRatio>()
where TRatio : class, ILanguage<TDialect>, IRatioNotifications
=> _collection.AddSingleton<IRatioNotifications, TRatio>();

public void WithWarden<TWarden>()
where TWarden : class, ILanguage<TDialect>, IWardenNotifications
=> _collection.AddSingleton<IWardenNotifications, TWarden>();

public void WithRebel<TRebel>()
where TRebel : class, ILanguage<TDialect>, IRebelNotifications
=> _collection.AddSingleton<IRebelNotifications, TRebel>();

public void WithLogging<TLogging>()
where TLogging : class, ILanguage<TDialect>, ILogMessages
=> _collection.AddSingleton<ILogMessages, TLogging>();
}
Loading

0 comments on commit d10a262

Please sign in to comment.