Skip to content

Commit

Permalink
Add auto-cells opening
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Aug 24, 2024
1 parent 5931549 commit ba5f0da
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 15 deletions.
15 changes: 13 additions & 2 deletions lang/Jailbreak.English/Warden/WardenCmdOpenLocale.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Logistics;
using Jailbreak.Formatting.Views.Warden;
Expand All @@ -16,10 +18,19 @@ public IView CannotOpenYet(int seconds) {
}

public IView AlreadyOpened
=> new SimpleView { WardenLocale.PREFIX, "You already opened cells." };
=> new SimpleView { WardenLocale.PREFIX, "Cells are already opened." };

public IView CellsOpenedBy(CCSPlayerController? player) {
return player == null ?
new SimpleView {
WardenLocale.PREFIX,
$"{ChatColors.Blue}The warden {ChatColors.Default}opened the cells."
} :
new SimpleView { WardenLocale.PREFIX, player, "opened the cells." };
}

public IView CellsOpened
=> new SimpleView { WardenLocale.PREFIX, "The warden opened cells." };
=> new SimpleView { WardenLocale.PREFIX, "Cells were auto-opened." };

public IView OpeningFailed
=> new SimpleView { WardenLocale.PREFIX, "Failed to open the cells." };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Cvars.Validators;
using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Extensions;
using Jailbreak.Formatting.Views.Warden;
using Jailbreak.Public.Behaviors;
Expand All @@ -13,19 +14,19 @@

namespace Jailbreak.Warden.Commands;

public class OpenCommandsBehavior(IWardenService warden, IWardenLocale msg,
public class WardenOpenCommandsBehavior(IWardenService warden, IWardenLocale msg,
IWardenCmdOpenLocale wardenCmdOpenMsg, IZoneManager zoneManager)
: IPluginBehavior {
: IPluginBehavior, IWardenOpenCommand {
public static readonly FakeConVar<int> CvOpenCommandCooldown = new(
"css_jb_warden_open_cooldown",
"Minimum seconds warden must wait before being able to open the cells.", 30,
customValidators: new RangeValidator<int>(0, 300));

private bool openedCells;
public bool OpenedCells { get; set; }

[GameEventHandler]
public HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info) {
openedCells = false;
OpenedCells = false;
return HookResult.Continue;
}

Expand All @@ -45,15 +46,21 @@ public void Command_Open(CCSPlayerController? executor, CommandInfo info) {
return;
}

if (openedCells) {
if (OpenedCells) {
wardenCmdOpenMsg.AlreadyOpened.ToChat(executor);
return;
}
}

openedCells = true;
var result = MapUtil.OpenCells(zoneManager);
(result ? wardenCmdOpenMsg.CellsOpened : wardenCmdOpenMsg.OpeningFailed)
.ToAllChat();
OpenedCells = true;
var result = MapUtil.OpenCells(zoneManager);
IView message;
if (result) {
if (executor != null || !warden.IsWarden(executor)) {
message = wardenCmdOpenMsg.CellsOpenedBy(executor);
} else { message = wardenCmdOpenMsg.CellsOpenedBy(null); }
} else { message = wardenCmdOpenMsg.OpeningFailed; }

message.ToAllChat();
}
}
29 changes: 26 additions & 3 deletions mod/Jailbreak.Warden/Global/WardenBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
using Jailbreak.Public.Mod.Mute;
using Jailbreak.Public.Mod.Rebel;
using Jailbreak.Public.Mod.Warden;
using Jailbreak.Public.Mod.Zones;
using Jailbreak.Public.Utils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MStatsShared;
using Timer = CounterStrikeSharp.API.Modules.Timers.Timer;

namespace Jailbreak.Warden.Global;

// By making it a struct we ensure values from the CCSPlayerPawn are passed by VALUE.
internal struct PreWardenStats(int armorValue, int health, int maxHealth,
public struct PreWardenStats(int armorValue, int health, int maxHealth,
bool headHealthShot, bool hadHelmetArmor) {
public readonly int ArmorValue = armorValue;
public readonly int Health = health;
Expand All @@ -33,7 +36,8 @@ internal struct PreWardenStats(int armorValue, int health, int maxHealth,
public class WardenBehavior(ILogger<WardenBehavior> logger,
IWardenLocale locale, IRichLogService logs,
ISpecialTreatmentService specialTreatment, IRebelService rebels,
WardenConfig config, IMuteService mute) : IPluginBehavior, IWardenService {
WardenConfig config, IMuteService mute, IServiceProvider provider)
: IPluginBehavior, IWardenService {
private readonly ISet<CCSPlayerController> bluePrisoners =
new HashSet<CCSPlayerController>();

Expand Down Expand Up @@ -61,11 +65,15 @@ public class WardenBehavior(ILogger<WardenBehavior> logger,
"Max HP for the warden", 100, ConVarFlags.FCVAR_NONE,
new RangeValidator<int>(1, 200));

public readonly FakeConVar<int> CvWardenAutoOpenCells =
new("css_jb_warden_opencells_delay",
"Delay in seconds to auto-open cells at, -1 to disable", 60);

private bool firstWarden;
private string? oldTag;
private char? oldTagColor;

private BasePlugin? parent;
private BasePlugin parent = null!;
private PreWardenStats? preWardenStats;
private Timer? unblueTimer;

Expand Down Expand Up @@ -394,6 +402,21 @@ public HookResult OnRoundStart(EventRoundStart ev, GameEventInfo info) {
firstWarden = true;
preWardenStats = null;

if (CvWardenAutoOpenCells.Value < 0) return HookResult.Continue;
var openCmd = provider.GetService<IWardenOpenCommand>();
if (openCmd == null) return HookResult.Continue;
var cmdLocale = provider.GetRequiredService<IWardenCmdOpenLocale>();

parent.AddTimer(CvWardenAutoOpenCells.Value, () => {
if (openCmd.OpenedCells) return;
var zone = provider.GetService<IZoneManager>();
if (zone != null)
MapUtil.OpenCells(zone);
else
MapUtil.OpenCells();
cmdLocale.CellsOpened.ToAllChat();
});

return HookResult.Continue;
}

Expand Down
2 changes: 1 addition & 1 deletion mod/Jailbreak.Warden/WardenServiceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public static void AddJailbreakWarden(
.AddPluginBehavior<IWardenSelectionService, WardenSelectionBehavior>();
serviceCollection
.AddPluginBehavior<ISpecialTreatmentService, SpecialTreatmentBehavior>();
serviceCollection.AddPluginBehavior<IWardenOpenCommand, WardenOpenCommandsBehavior>();

serviceCollection.AddPluginBehavior<SpecialTreatmentCommandsBehavior>();
serviceCollection.AddPluginBehavior<PeaceCommandsBehavior>();
serviceCollection.AddPluginBehavior<WardenCommandsBehavior>();
serviceCollection.AddPluginBehavior<RollCommandBehavior>();
serviceCollection.AddPluginBehavior<OpenCommandsBehavior>();

serviceCollection.AddPluginBehavior<WardenMarkerBehavior>();
serviceCollection.AddPluginBehavior<WardenPaintBehavior>();
Expand Down
13 changes: 13 additions & 0 deletions public/Jailbreak.Formatting/Views/Warden/IWardenCmdOpenLocale.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
using CounterStrikeSharp.API.Core;
using Jailbreak.Formatting.Base;

namespace Jailbreak.Formatting.Views.Warden;

public interface IWardenCmdOpenLocale {
/// <summary>
/// The cells were auto-opened.
/// </summary>
public IView CellsOpened { get; }

/// <summary>
/// The cells were opened by the specified player.
/// If the player is null, the cells were opened by the warden.
/// </summary>
/// <param name="player"></param>
/// <returns></returns>
public IView CellsOpenedBy(CCSPlayerController? player);

public IView OpeningFailed { get; }
public IView AlreadyOpened { get; }
public IView CannotOpenYet(int seconds);
Expand Down
3 changes: 3 additions & 0 deletions public/Jailbreak.Public/Mod/SpecialDay/AbstractSpecialDay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Jailbreak.Public.Mod.LastRequest;
using Jailbreak.Public.Mod.Rebel;
using Jailbreak.Public.Mod.SpecialDay.Enums;
using Jailbreak.Public.Mod.Warden;
using Jailbreak.Public.Mod.Zones;
using Jailbreak.Public.Utils;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -94,6 +95,8 @@ public virtual void Setup() {
MapUtil.OpenCells();
else
MapUtil.OpenCells(zones);
var openCmd = Provider.GetService<IWardenOpenCommand>();
if (openCmd != null) openCmd.OpenedCells = true;
}

doTeleports();
Expand Down
5 changes: 5 additions & 0 deletions public/Jailbreak.Public/Mod/Warden/IWardenOpenCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Jailbreak.Public.Mod.Warden;

public interface IWardenOpenCommand {
bool OpenedCells { get; set; }
}

0 comments on commit ba5f0da

Please sign in to comment.