From bb0dbf3dbe2d8053d3870697f9f43aaba6397d8f Mon Sep 17 00:00:00 2001 From: MSWS Date: Thu, 29 Aug 2024 21:03:47 -0700 Subject: [PATCH] Dont broadcast opening if already opened --- .../Warden/WardenCmdOpenLocale.cs | 20 +++++++ mod/Jailbreak.Warden/Global/WardenBehavior.cs | 55 +++++++++++++++++-- mod/Jailbreak.Warden/Jailbreak.Warden.csproj | 1 + .../Views/Warden/IWardenCmdOpenLocale.cs | 14 +++++ 4 files changed, 85 insertions(+), 5 deletions(-) diff --git a/lang/Jailbreak.English/Warden/WardenCmdOpenLocale.cs b/lang/Jailbreak.English/Warden/WardenCmdOpenLocale.cs index 0c220712..6fcef3a8 100644 --- a/lang/Jailbreak.English/Warden/WardenCmdOpenLocale.cs +++ b/lang/Jailbreak.English/Warden/WardenCmdOpenLocale.cs @@ -34,6 +34,26 @@ public IView CellsOpened WardenLocale.PREFIX, ChatColors.Grey + "Cells were auto-opened." }; + public IView CellsOpenedWithPrisoners(int prisoners) { + return new SimpleView { + WardenLocale.PREFIX, + "Detected", + prisoners, + ChatColors.Green + "prisoner" + (prisoners == 1 ? "" : "s") + + " still in cells, opening..." + }; + } + + public IView CellsOpenedSnitchPrisoners(int prisoners) { + return new SimpleView { + WardenLocale.PREFIX, + "Detected", + prisoners, + ChatColors.Green + "prisoner" + (prisoners == 1 ? "" : "s") + + " still in cells..." + }; + } + public IView OpeningFailed => new SimpleView { WardenLocale.PREFIX, "Failed to open the cells." }; } \ No newline at end of file diff --git a/mod/Jailbreak.Warden/Global/WardenBehavior.cs b/mod/Jailbreak.Warden/Global/WardenBehavior.cs index aa162fcf..e039c767 100644 --- a/mod/Jailbreak.Warden/Global/WardenBehavior.cs +++ b/mod/Jailbreak.Warden/Global/WardenBehavior.cs @@ -16,6 +16,7 @@ using Jailbreak.Public.Mod.Warden; using Jailbreak.Public.Mod.Zones; using Jailbreak.Public.Utils; +using Jailbreak.Zones; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using MStatsShared; @@ -58,6 +59,11 @@ public class WardenBehavior(ILogger logger, new("css_jb_warden_opencells_delay", "Delay in seconds to auto-open cells at, -1 to disable", 60); + public static readonly FakeConVar CV_WARDEN_AUTO_SNITCH = + new("css_jb_warden_auto_snitch", + "True to broadcast how many prisoners were in cells when they auto-open", + true); + public static readonly FakeConVar CV_WARDEN_HEALTH = new("css_jb_warden_hp", "HP for the warden", 125, ConVarFlags.FCVAR_NONE, new RangeValidator(1, 200)); @@ -409,18 +415,57 @@ public HookResult OnRoundStart(EventRoundStart ev, GameEventInfo info) { openCellsTimer?.Kill(); openCellsTimer = parent.AddTimer(CV_WARDEN_AUTO_OPEN_CELLS.Value, () => { - if (openCmd.OpenedCells) return; - var zone = provider.GetService(); - if (zone != null) - MapUtil.OpenCells(zone); + var cellZone = getCellZone(); + + var prisoners = PlayerUtil.FromTeam(CsTeam.Terrorist) + .Count(p => p.Pawn.Value != null && p.Pawn.Value.AbsOrigin != null + && cellZone.IsInsideZone(p.Pawn.Value?.AbsOrigin!)); + + if (openCmd.OpenedCells) { + if (CV_WARDEN_AUTO_SNITCH.Value && prisoners > 0) + cmdLocale.CellsOpenedSnitchPrisoners(prisoners); + return; + } + + var zoneMgr = provider.GetService(); + // Regardless of if we actually _detect_ prisoners in cells, + // we should still open them (for convenience) + if (zoneMgr != null) + MapUtil.OpenCells(zoneMgr); else MapUtil.OpenCells(); - cmdLocale.CellsOpened.ToAllChat(); + + // Only if we detect prisoners in cells (i.e. presumably + // cells haven't been opened yet) should we send the message + + if (prisoners == 0) return; + + if (CV_WARDEN_AUTO_SNITCH.Value) { + cmdLocale.CellsOpenedWithPrisoners(prisoners); + } else { cmdLocale.CellsOpened.ToAllChat(); } }); return HookResult.Continue; } + private IZone getCellZone() { + var manager = provider.GetService(); + if (manager != null) { + var zones = manager.GetZones(Server.MapName, ZoneType.CELL) + .GetAwaiter() + .GetResult(); + if (zones.Count > 0) return new MultiZoneWrapper(zones); + } + + var bounds = new DistanceZone( + Utilities + .FindAllEntitiesByDesignerName("info_player_terrorist") + .Where(s => s.AbsOrigin != null) + .Select(s => s.AbsOrigin!) + .ToList(), DistanceZone.WIDTH_CELL); + return bounds; + } + [GameEventHandler] public HookResult OnPlayerDisconnect(EventPlayerDisconnect ev, GameEventInfo info) { diff --git a/mod/Jailbreak.Warden/Jailbreak.Warden.csproj b/mod/Jailbreak.Warden/Jailbreak.Warden.csproj index 56894def..ca3670c3 100644 --- a/mod/Jailbreak.Warden/Jailbreak.Warden.csproj +++ b/mod/Jailbreak.Warden/Jailbreak.Warden.csproj @@ -9,6 +9,7 @@ + diff --git a/public/Jailbreak.Formatting/Views/Warden/IWardenCmdOpenLocale.cs b/public/Jailbreak.Formatting/Views/Warden/IWardenCmdOpenLocale.cs index daf76bcd..ad0ac482 100644 --- a/public/Jailbreak.Formatting/Views/Warden/IWardenCmdOpenLocale.cs +++ b/public/Jailbreak.Formatting/Views/Warden/IWardenCmdOpenLocale.cs @@ -20,5 +20,19 @@ public interface IWardenCmdOpenLocale { /// public IView CellsOpenedBy(CCSPlayerController? player); + /// + /// Cells were opened with prisoners still inside. + /// + /// + /// + public IView CellsOpenedWithPrisoners(int prisoners); + + /// + /// Cells were already opened, but there are still prisoners inside. + /// + /// + /// + public IView CellsOpenedSnitchPrisoners(int prisoners); + public IView CannotOpenYet(int seconds); } \ No newline at end of file