Skip to content

Commit

Permalink
Add rich log service, redo simpleview semantics again, convert existi…
Browse files Browse the repository at this point in the history
…ng logging

* Fully add rich logging service
* Redo Simpleview to use a specific "Newline" struct for explicit newlines
  • Loading branch information
Mooshua committed Feb 6, 2024
1 parent d10a262 commit 53fab1a
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 130 deletions.
8 changes: 4 additions & 4 deletions lang/Jailbreak.English/Logs/LogMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public class LogMessages : ILogMessages, ILanguage<Formatting.Languages.English>

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

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

Expand Down
4 changes: 2 additions & 2 deletions lang/Jailbreak.English/Teams/RatioNotifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class RatioNotifications : IRatioNotifications, ILanguage<Formatting.Lang
public IView YOU_WERE_AUTOBALANCED_PRISONER =>
new SimpleView
{
{ PREFIX, "You were autobalanced to the prisoner team!" },
{ PREFIX, "You were autobalanced to the prisoner team!" }, SimpleView.NEWLINE,
{ PREFIX, "Please use !guard to join the guard team." }
};

Expand All @@ -43,7 +43,7 @@ public class RatioNotifications : IRatioNotifications, ILanguage<Formatting.Lang
public IView LEFT_GUARD =>
new SimpleView
{
{ PREFIX, "You are no longer a guard." },
{ PREFIX, "You are no longer a guard." }, SimpleView.NEWLINE,
{ PREFIX, "Please use !guard if you want to re-join the guard team." }
};

Expand Down
2 changes: 1 addition & 1 deletion lang/Jailbreak.English/Warden/WardenNotifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class WardenNotifications : IWardenNotifications, ILanguage<Formatting.La
public IView PICKING_SHORTLY =>
new SimpleView
{
{ PREFIX, "Picking a warden shortly" },
{ PREFIX, "Picking a warden shortly" }, SimpleView.NEWLINE,
{ PREFIX, "To enter the warden queue, type !warden in chat." }
};

Expand Down
72 changes: 72 additions & 0 deletions mod/Jailbreak.Logs/Listeners/LogDamageListeners.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;

using Jailbreak.Formatting.Views;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Extensions;
using Jailbreak.Public.Mod.Logs;

namespace Jailbreak.Logs;

public class LogDamageListeners : IPluginBehavior
{
private readonly IRichLogService _logs;

public LogDamageListeners(IRichLogService logs)
{
_logs = logs;
}



[GameEventHandler]
public HookResult OnGrenadeThrown(EventGrenadeThrown @event, GameEventInfo info)
{
var player = @event.Userid;
if (!player.IsReal())
return HookResult.Continue;
var grenade = @event.Weapon;

_logs.Append(_logs.Player(player), $"threw a {grenade}");

return HookResult.Continue;
}

[GameEventHandler]
public HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
{
var player = @event.Userid;
if (!player.IsReal())
return HookResult.Continue;
var attacker = @event.Attacker;

bool isWorld = attacker == null || !attacker.IsReal();
int health = @event.DmgHealth;

if (isWorld)
{
if (health > 0)
{
_logs.Append($"The world hurt", _logs.Player(player), $"for {health} damage");
}
else
{
_logs.Append("The world killed", _logs.Player(player));
}
}
else
{
if (health > 0)
{
_logs.Append( _logs.Player(attacker), "hurt", _logs.Player(player), $"for {health} damage");

Check warning on line 62 in mod/Jailbreak.Logs/Listeners/LogDamageListeners.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'playerController' in 'FormatObject IRichLogService.Player(CCSPlayerController playerController)'.
}
else
{
_logs.Append(_logs.Player(attacker!), "killed", _logs.Player(player));
}
}

return HookResult.Continue;
}
}
38 changes: 38 additions & 0 deletions mod/Jailbreak.Logs/Listeners/LogEntityListeners.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;

using Jailbreak.Formatting.Views;
using Jailbreak.Public.Behaviors;

namespace Jailbreak.Logs;

public class LogEntityListeners : IPluginBehavior
{
private readonly IRichLogService _logs;

public LogEntityListeners(IRichLogService logs)
{
_logs = logs;
}

[EntityOutputHook("func_button", "OnPressed")]
public HookResult OnButtonPressed(CEntityIOOutput output, string name, CEntityInstance activator,
CEntityInstance caller, CVariant value, float delay)
{
if (!activator.IsValid)
return HookResult.Continue;
int index = (int)activator.Index;
CCSPlayerPawn? pawn = Utilities.GetEntityFromIndex<CCSPlayerPawn>(index);
if (!pawn.IsValid)
return HookResult.Continue;
if (!pawn.OriginalController.IsValid)
return HookResult.Continue;
CBaseEntity? ent = Utilities.GetEntityFromIndex<CBaseEntity>((int)caller.Index);
if (!ent.IsValid)
return HookResult.Continue;
_logs.Append(
$"{_logs.Player(pawn.OriginalController.Value!)} pressed a button {ent.Entity?.Name ?? "Unlabeled"} -> {output?.Connections?.TargetDesc ?? "None"}");
return HookResult.Continue;
}
}
92 changes: 0 additions & 92 deletions mod/Jailbreak.Logs/LogsListeners.cs

This file was deleted.

21 changes: 16 additions & 5 deletions mod/Jailbreak.Logs/LogsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@

namespace Jailbreak.Logs;

public class LogsManager : IPluginBehavior, ILogService
public class LogsManager : IPluginBehavior, ILogService, IRichLogService
{
private readonly List<IView> _logMessages = new();

private IRichPlayerTag _richPlayerTag;
private ILogMessages _messages;

public LogsManager(IServiceProvider serviceProvider, ILogMessages messages)
public LogsManager(IServiceProvider serviceProvider, ILogMessages messages, IRichPlayerTag richPlayerTag)
{
_messages = messages;

_richPlayerTag = richPlayerTag;
}

[GameEventHandler]
private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
{
_messages.BEGIN_JAILBREAK_LOGS
.ToServerConsole()
Expand All @@ -49,7 +50,7 @@ private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
}

[GameEventHandler]
private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
{
Clear();
return HookResult.Continue;
Expand All @@ -60,6 +61,16 @@ public void Append(params FormatObject[] objects)
_logMessages.Add(_messages.CREATE_LOG(objects));
}

public FormatObject Player(CCSPlayerController playerController)
{
return new TreeFormatObject()
{
playerController,
$"[{playerController.UserId}]",
_richPlayerTag.Rich(playerController)
};
}

public void Append(string message)
{
_logMessages.Add(_messages.CREATE_LOG(message));
Expand Down
7 changes: 5 additions & 2 deletions mod/Jailbreak.Logs/LogsServiceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ public static class LogsServiceExtension
{
public static void AddJailbreakLogs(this IServiceCollection services)
{
services.AddPluginBehavior<ILogService, LogsManager>();
services.AddPluginBehavior<IRichLogService, LogsManager>();
services.AddTransient<ILogService>(provider => provider.GetRequiredService<IRichLogService>());

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

services.AddPluginBehavior<LogEntityListeners>();
services.AddPluginBehavior<LogDamageListeners>();

// PlayerTagHelper is a lower-level class that avoids dependency loops.
services.AddTransient<IRichPlayerTag, PlayerTagHelper>();
Expand Down
15 changes: 8 additions & 7 deletions mod/Jailbreak.Logs/Tags/PlayerTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Jailbreak.Formatting.Core;
using Jailbreak.Formatting.Objects;
using Jailbreak.Formatting.Views;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Extensions;
using Jailbreak.Public.Mod.Logs;
using Jailbreak.Public.Mod.Rebel;
Expand All @@ -15,30 +16,30 @@ namespace Jailbreak.Logs.Tags;

public class PlayerTagHelper : IRichPlayerTag, IPlayerTag
{
private IWardenService _wardenService;
private IRebelService _rebelService;
private Lazy<IWardenService> _wardenService;
private Lazy<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>();
_wardenService = new ( () => provider.GetRequiredService<IWardenService>() );
_rebelService = new ( () => provider.GetRequiredService<IRebelService>() );
}

public FormatObject Rich(CCSPlayerController player)
{
if (_wardenService.IsWarden(player))
if (_wardenService.Value.IsWarden(player))
return new StringFormatObject("(WARDEN)", ChatColors.DarkBlue);
if (player.GetTeam() == CsTeam.CounterTerrorist)
return new StringFormatObject("(CT)", ChatColors.BlueGrey);
if (_rebelService.IsRebel(player))
if (_rebelService.Value.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()}";
return Rich(playerController).ToPlain();
}
}
Loading

0 comments on commit 53fab1a

Please sign in to comment.