Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Special Treatment #82

Merged
merged 2 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lang/Jailbreak.English/Warden/SpecialTreatmentNotifications.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using CounterStrikeSharp.API.Core;
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;

namespace Jailbreak.English.Warden;

public class SpecialTreatmentNotifications : ISpecialTreatmentNotifications, ILanguage<Formatting.Languages.English>
{
public static FormatObject PREFIX = new HiddenFormatObject( $" {ChatColors.Lime}[{ChatColors.Green}ST{ChatColors.Lime}]" )
{
// Hide in panorama and center text
Plain = false,
Panorama = false,
Chat = true,
};

public IView GRANTED =>
new SimpleView { PREFIX, "You now have special treatment!" };

public IView REVOKED =>
new SimpleView { PREFIX, "Your special treatment was removed" };

public IView GRANTED_TO(CCSPlayerController player)
{
return new SimpleView { PREFIX, player, "now has Special Treatment!" };
}

public IView REVOKED_FROM(CCSPlayerController player)
{
return new SimpleView { PREFIX, player, "no longer has Special Treatment." };
}

}
78 changes: 78 additions & 0 deletions mod/Jailbreak.Warden/Commands/SpecialTreatmentCommandsBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Utils;

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

namespace Jailbreak.Warden.Commands;

public class SpecialTreatmentCommandsBehavior : IPluginBehavior
{

private IWardenService _warden;
private ISpecialTreatmentService _specialTreatment;

private IGenericCommandNotifications _generic;
private ISpecialTreatmentNotifications _notifications;

public SpecialTreatmentCommandsBehavior(IWardenService warden, ISpecialTreatmentService specialTreatment, IGenericCommandNotifications generic, ISpecialTreatmentNotifications notifications)
{
_warden = warden;
_specialTreatment = specialTreatment;
_generic = generic;
_notifications = notifications;
}

[ConsoleCommand("css_treat", "Grant or revoke special treatment from a player")]
[ConsoleCommand("css_st", "Grant or revoke special treatment from a player")]
[CommandHelper(1, "[target]", CommandUsage.CLIENT_ONLY)]
public void Command_Toggle(CCSPlayerController? player, CommandInfo command)
{
if (player == null)
return;

if (!_warden.IsWarden(player))
// You're not that warden, blud
return;

// Since we have min_args, don't need to check for validity here.
// just only get targets that are T's.
var targets = command.GetArgTargetResult(1);
var eligible = targets
.Where(player => player.Team == CsTeam.Terrorist)
.ToList();

if (eligible.Count == 0)
{
_generic.PlayerNotFound(command.GetArg(1))
.ToPlayerChat(player)
.ToPlayerConsole(player);
return;
}
else if (eligible.Count != 1)
{
_generic.PlayerFoundMultiple(command.GetArg(1))
.ToPlayerChat(player)
.ToPlayerConsole(player);
return;
}

// One target, mark as ST.
var special = eligible.First();

if (_specialTreatment.IsSpecialTreatment(special))
{
// Revoke
_specialTreatment.Revoke(special);
}
else
{
// Player does not have ST, grant
_specialTreatment.Grant(special);
}
}
}
90 changes: 90 additions & 0 deletions mod/Jailbreak.Warden/SpecialTreatment/SpecialTreatmentBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System.Drawing;

using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;

using Jailbreak.Formatting.Extensions;
using Jailbreak.Formatting.Views;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Generic;
using Jailbreak.Public.Mod.Rebel;
using Jailbreak.Public.Mod.Warden;

namespace Jailbreak.Warden.SpecialTreatment;

public class SpecialTreatmentBehavior : IPluginBehavior, ISpecialTreatmentService
{
private IPlayerState<SpecialTreatmentState> _sts;
private IRebelService _rebel;
private ISpecialTreatmentNotifications _notifications;

public SpecialTreatmentBehavior(IPlayerStateFactory factory, IRebelService rebel, ISpecialTreatmentNotifications notifications)
{
_sts = factory.Round<SpecialTreatmentState>();

_rebel = rebel;
_notifications = notifications;
}

private class SpecialTreatmentState
{
public bool HasSpecialTreatment { get; set; } = false;
}

public bool IsSpecialTreatment(CCSPlayerController player)
{
return _sts.Get(player)
.HasSpecialTreatment;
}

public void Grant(CCSPlayerController player)
{
// Player is already granted ST
if (IsSpecialTreatment(player))
return;

_sts.Get(player).HasSpecialTreatment = true;

_rebel.UnmarkRebel(player);
this.SetPlayerColor(player, /* hasSt */ true);

_notifications.GRANTED
.ToPlayerChat(player)
.ToPlayerCenter(player);

_notifications.GRANTED_TO(player)
.ToAllChat();
}

public void Revoke(CCSPlayerController player)
{
// Player is already revoked
if (!IsSpecialTreatment(player))
return;

_sts.Get(player).HasSpecialTreatment = false;

this.SetPlayerColor(player, /* hasSt */ false);

_notifications.REVOKED
.ToPlayerChat(player)
.ToPlayerCenter(player);

_notifications.REVOKED_FROM(player)
.ToAllChat();
}

private void SetPlayerColor(CCSPlayerController player, bool hasSt)
{
if (!player.IsValid || player.Pawn.Value == null)
return;

var color = hasSt
? Color.FromArgb(254, 150, 255, 150)
: Color.FromArgb(254, 255, 255, 255);

player.Pawn.Value.RenderMode = RenderMode_t.kRenderTransColor;
player.Pawn.Value.Render = color;
Utilities.SetStateChanged(player.Pawn.Value, "CBaseModelEntity", "m_clrRender");
}
}
7 changes: 6 additions & 1 deletion mod/Jailbreak.Warden/WardenServiceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Jailbreak.Warden.Markers;
using Jailbreak.Warden.Paint;
using Jailbreak.Warden.Selection;
using Jailbreak.Warden.SpecialTreatment;

using Microsoft.Extensions.DependencyInjection;

namespace Jailbreak.Warden;
Expand All @@ -15,9 +17,12 @@ public static void AddJailbreakWarden(this IServiceCollection serviceCollection)
{
serviceCollection.AddPluginBehavior<IWardenService, WardenBehavior>();
serviceCollection.AddPluginBehavior<IWardenSelectionService, WardenSelectionBehavior>();
serviceCollection.AddPluginBehavior<ISpecialTreatmentService, SpecialTreatmentBehavior>();

serviceCollection.AddPluginBehavior<SpecialTreatmentCommandsBehavior>();
serviceCollection.AddPluginBehavior<WardenCommandsBehavior>();

serviceCollection.AddPluginBehavior<WardenMarkerBehavior>();
serviceCollection.AddPluginBehavior<WardenPaintBehavior>();
}
}
}
6 changes: 5 additions & 1 deletion public/Jailbreak.Formatting/Logistics/LanguageConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ public void WithRebel<TRebel>()
public void WithLogging<TLogging>()
where TLogging : class, ILanguage<TDialect>, ILogMessages
=> _collection.AddSingleton<ILogMessages, TLogging>();

public void WithLastRequest<TLastRequest>()
where TLastRequest : class, ILanguage<TDialect>, ILastRequestMessages
=> _collection.AddSingleton<ILastRequestMessages, TLastRequest>();

public void WithSpecialTreatment<TSpecialTreatment>()
where TSpecialTreatment : class, ILanguage<TDialect>, ISpecialTreatmentNotifications
=> _collection.AddSingleton<ISpecialTreatmentNotifications, TSpecialTreatment>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using CounterStrikeSharp.API.Core;

using Jailbreak.Formatting.Base;

namespace Jailbreak.Formatting.Views;

public interface ISpecialTreatmentNotifications
{
public IView GRANTED { get; }

public IView REVOKED { get; }

public IView GRANTED_TO(CCSPlayerController player);

public IView REVOKED_FROM(CCSPlayerController player);

}
21 changes: 21 additions & 0 deletions public/Jailbreak.Public/Mod/Warden/ISpecialTreatmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using CounterStrikeSharp.API.Core;

namespace Jailbreak.Public.Mod.Warden;

public interface ISpecialTreatmentService
{
public bool IsSpecialTreatment(CCSPlayerController player);

/// <summary>
/// Give this player ST for the rest of the round
/// </summary>
/// <param name="player"></param>
public void Grant(CCSPlayerController player);

/// <summary>
/// Revoke the player's special treatment for the current round
/// Does nothing if not ST.
/// </summary>
/// <param name="player"></param>
public void Revoke(CCSPlayerController player);
}
1 change: 1 addition & 0 deletions src/Jailbreak/JailbreakServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void ConfigureServices(IServiceCollection serviceCollection)
config.WithRebel<RebelNotifications>();
config.WithLogging<LogMessages>();
config.WithLastRequest<LastRequestMessages>();
config.WithSpecialTreatment<SpecialTreatmentNotifications>();
});
}
}
Loading