From 0b3485d79684c1fcf45ac48e2857aaa4cca0338d Mon Sep 17 00:00:00 2001 From: MSWS Date: Mon, 19 Feb 2024 21:57:26 -0800 Subject: [PATCH 1/2] Move to localizations --- .../LastRequest/LastRequestMessages.cs | 22 ++++++++++++ .../LastRequestCommand.cs | 28 +++++++++------ .../LastRequestManager.cs | 36 ++++++++++++++++--- .../LastRequests/KnifeFight.cs | 5 +++ .../Views/ILastRequestMessages.cs | 3 ++ .../Mod/LastRequest/ILastRequestManager.cs | 13 ++++--- 6 files changed, 88 insertions(+), 19 deletions(-) diff --git a/lang/Jailbreak.English/LastRequest/LastRequestMessages.cs b/lang/Jailbreak.English/LastRequest/LastRequestMessages.cs index 52d95ebd..9c2e9c10 100644 --- a/lang/Jailbreak.English/LastRequest/LastRequestMessages.cs +++ b/lang/Jailbreak.English/LastRequest/LastRequestMessages.cs @@ -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; @@ -15,4 +17,24 @@ public class LastRequestMessages : ILastRequestMessages, ILanguage - public LastRequestCommand(ILastRequestManager manager, ILastRequestFactory factory) + public LastRequestCommand(ILastRequestManager manager, ILastRequestMessages messages, + IGenericCommandNotifications generic) { _lrManager = manager; + _messages = messages; + _generic = generic; } public void Start(BasePlugin plugin) @@ -65,12 +70,12 @@ public void Command_LastRequest(CCSPlayerController? executor, CommandInfo info) } // Validate LR - var type = LRTypeExtensions.FromString(info.GetArg(1)); - if (type == null) + if (!Enum.TryParse(info.GetArg(1), true, out var type)) { info.ReplyToCommand("Invalid LR"); return; } + if (info.ArgCount == 2) { MenuManager.OpenCenterHtmlMenu(plugin, executor, playerSelector.CreateMenu(executor, (LRType)type)); @@ -78,32 +83,35 @@ public void Command_LastRequest(CCSPlayerController? executor, CommandInfo info) } var target = info.GetArgTargetResult(2); - new Target(info.GetArg(2)).GetTarget(executor); if (!target.Players.Any()) { - info.ReplyToCommand($"Could not find valid player using {info.GetArg(2)}"); + _generic.PlayerNotFound(info.GetArg(2)); return; } if (target.Players.Count > 1) { - info.ReplyToCommand("Too many players"); + _generic.PlayerFoundMultiple(info.GetArg(2)); return; } var player = target.Players.First(); if (player.Team != CsTeam.CounterTerrorist) { - info.ReplyToCommand("They're not on CT!"); + _messages.InvalidPlayerChoice(player, "They're not on CT!"); return; } if (!player.PawnIsAlive) { - info.ReplyToCommand("They're not alive!"); + _messages.InvalidPlayerChoice(player, "They're not alive!"); return; } - _lrManager.InitiateLastRequest(executor, player, (LRType)type); + if (!_lrManager.InitiateLastRequest(executor, player, (LRType)type)) + { + info.ReplyToCommand("An error occurred while initiating the last request. Please try again later."); + return; + } } } \ No newline at end of file diff --git a/mod/Jailbreak.LastRequest/LastRequestManager.cs b/mod/Jailbreak.LastRequest/LastRequestManager.cs index 4540da22..1b8c70b7 100644 --- a/mod/Jailbreak.LastRequest/LastRequestManager.cs +++ b/mod/Jailbreak.LastRequest/LastRequestManager.cs @@ -55,10 +55,20 @@ public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info) if (!player.IsReal()) return HookResult.Continue; + if (IsLREnabled) + { + var activeLr = ((ILastRequestManager)this).GetActiveLR(player); + if (activeLr != null) + { + var isPrisoner = activeLr.prisoner.Slot == player.Slot; + EndLastRequest(activeLr, isPrisoner ? LRResult.PrisonerWin : LRResult.GuardWin); + } + } + if (player.GetTeam() != CsTeam.Terrorist) return HookResult.Continue; - if (CountAlivePrisoners() - 1> config.PrisonersToActiveLR) + if (CountAlivePrisoners() - 1 > config.PrisonersToActiveLR) return HookResult.Continue; IsLREnabled = true; @@ -83,10 +93,26 @@ private bool CountsToLR(CCSPlayerController player) public bool IsLREnabled { get; set; } public IList ActiveLRs { get; } = new List(); - public void InitiateLastRequest(CCSPlayerController prisoner, CCSPlayerController guard, LRType type) + public bool InitiateLastRequest(CCSPlayerController prisoner, CCSPlayerController guard, LRType type) + { + try + { + var lr = factory.CreateLastRequest(prisoner, guard, type); + lr.Setup(); + ActiveLRs.Add(lr); + return true; + } + catch (ArgumentException e) + { + Console.WriteLine(e); + return false; + } + } + + public bool EndLastRequest(AbstractLastRequest lr, LRResult result) { - AbstractLastRequest lr = factory.CreateLastRequest(prisoner, guard, type); - lr.Setup(); - ActiveLRs.Add(lr); + lr.End(result); + ActiveLRs.Remove(lr); + return true; } } \ No newline at end of file diff --git a/mod/Jailbreak.LastRequest/LastRequests/KnifeFight.cs b/mod/Jailbreak.LastRequest/LastRequests/KnifeFight.cs index 0726e929..ff9cc9c7 100644 --- a/mod/Jailbreak.LastRequest/LastRequests/KnifeFight.cs +++ b/mod/Jailbreak.LastRequest/LastRequests/KnifeFight.cs @@ -1,3 +1,4 @@ +using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core.Attributes.Registration; using Jailbreak.Public.Mod.LastRequest; @@ -27,6 +28,7 @@ public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info) public override void Setup() { + Server.PrintToChatAll($"{prisoner.PlayerName} is knife fighting {guard.PlayerName}"); // Strip weapons, teleport T to CT prisoner.RemoveWeapons(); guard.RemoveWeapons(); @@ -37,6 +39,8 @@ public override void Setup() public override void Execute() { + prisoner.PrintToChat("Begin!"); + guard.PrintToChat("Begin!"); prisoner.GiveNamedItem("weapon_knife"); guard.GiveNamedItem("weapon_knife"); this.state = LRState.Active; @@ -44,6 +48,7 @@ public override void Execute() public override void End(LRResult result) { + Server.PrintToChatAll($"The knife fight ended!"); this.state = LRState.Completed; } } \ No newline at end of file diff --git a/public/Jailbreak.Formatting/Views/ILastRequestMessages.cs b/public/Jailbreak.Formatting/Views/ILastRequestMessages.cs index 10235732..37c6768a 100644 --- a/public/Jailbreak.Formatting/Views/ILastRequestMessages.cs +++ b/public/Jailbreak.Formatting/Views/ILastRequestMessages.cs @@ -1,3 +1,4 @@ +using CounterStrikeSharp.API.Core; using Jailbreak.Formatting.Base; namespace Jailbreak.Formatting.Views; @@ -6,4 +7,6 @@ public interface ILastRequestMessages { public IView LastRequestEnabled(); public IView LastRequestDisabled(); + public IView InvalidLastRequest(string query); + public IView InvalidPlayerChoice(CCSPlayerController player, string reason); } \ No newline at end of file diff --git a/public/Jailbreak.Public/Mod/LastRequest/ILastRequestManager.cs b/public/Jailbreak.Public/Mod/LastRequest/ILastRequestManager.cs index e709d66a..caf33a93 100644 --- a/public/Jailbreak.Public/Mod/LastRequest/ILastRequestManager.cs +++ b/public/Jailbreak.Public/Mod/LastRequest/ILastRequestManager.cs @@ -8,12 +8,17 @@ public interface ILastRequestManager : IPluginBehavior { public bool IsLREnabled { get; set; } public IList ActiveLRs { get; } - - void InitiateLastRequest(CCSPlayerController prisoner, CCSPlayerController guard, LRType lrType); + + bool InitiateLastRequest(CCSPlayerController prisoner, CCSPlayerController guard, LRType lrType); + bool EndLastRequest(AbstractLastRequest lr, LRResult result); public bool IsInLR(CCSPlayerController player) { - return ActiveLRs.Any(lr => lr.guard.Slot == player.Slot || lr.prisoner.Slot == player.Slot); + return GetActiveLR(player) != null; + } + + public AbstractLastRequest? GetActiveLR(CCSPlayerController player) + { + return ActiveLRs.FirstOrDefault(lr => lr.guard.Slot == player.Slot || lr.prisoner.Slot == player.Slot); } - } \ No newline at end of file From 1df1bee1a55adf06ead98d14bc038dcd3cec3dce Mon Sep 17 00:00:00 2001 From: MSWS Date: Mon, 19 Feb 2024 22:48:40 -0800 Subject: [PATCH 2/2] Update lifecycle and messages --- .../LastRequest/LastRequestMessages.cs | 29 +++++++++++++++++++ .../LastRequestManager.cs | 5 ++++ .../LastRequests/KnifeFight.cs | 22 ++++---------- .../Views/ILastRequestMessages.cs | 5 ++++ 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/lang/Jailbreak.English/LastRequest/LastRequestMessages.cs b/lang/Jailbreak.English/LastRequest/LastRequestMessages.cs index 9c2e9c10..067f2466 100644 --- a/lang/Jailbreak.English/LastRequest/LastRequestMessages.cs +++ b/lang/Jailbreak.English/LastRequest/LastRequestMessages.cs @@ -3,6 +3,8 @@ using Jailbreak.Formatting.Base; using Jailbreak.Formatting.Logistics; using Jailbreak.Formatting.Views; +using Jailbreak.Public.Mod.LastRequest; +using Jailbreak.Public.Mod.LastRequest.Enums; namespace Jailbreak.English.LastRequest; @@ -37,4 +39,31 @@ public IView InvalidPlayerChoice(CCSPlayerController player, string reason) reason }; } + + public IView InformLastRequest(AbstractLastRequest lr) + { + return new SimpleView() + { + lr.prisoner, "is", lr.type.ToFriendlyString(), + "against", lr.guard + }; + } + + public IView AnnounceLastRequest(AbstractLastRequest lr) + { + return new SimpleView() + { + lr.prisoner, "is", lr.type.ToFriendlyString(), + "against", lr.guard + }; + } + + public IView LastRequestDecided(AbstractLastRequest lr, LRResult result) + { + return new SimpleView() + { + lr.prisoner, "'s LR has been decided: ", + result == LRResult.PrisonerWin ? lr.prisoner : lr.guard + }; + } } \ No newline at end of file diff --git a/mod/Jailbreak.LastRequest/LastRequestManager.cs b/mod/Jailbreak.LastRequest/LastRequestManager.cs index 1b8c70b7..4e720dd7 100644 --- a/mod/Jailbreak.LastRequest/LastRequestManager.cs +++ b/mod/Jailbreak.LastRequest/LastRequestManager.cs @@ -100,6 +100,9 @@ public bool InitiateLastRequest(CCSPlayerController prisoner, CCSPlayerControlle var lr = factory.CreateLastRequest(prisoner, guard, type); lr.Setup(); ActiveLRs.Add(lr); + + messages.InformLastRequest(lr).ToPlayerChat(prisoner); + messages.InformLastRequest(lr).ToPlayerChat(guard); return true; } catch (ArgumentException e) @@ -111,6 +114,8 @@ public bool InitiateLastRequest(CCSPlayerController prisoner, CCSPlayerControlle public bool EndLastRequest(AbstractLastRequest lr, LRResult result) { + if (result == LRResult.GuardWin || result == LRResult.PrisonerWin) + messages.LastRequestDecided(lr, result).ToAllChat(); lr.End(result); ActiveLRs.Remove(lr); return true; diff --git a/mod/Jailbreak.LastRequest/LastRequests/KnifeFight.cs b/mod/Jailbreak.LastRequest/LastRequests/KnifeFight.cs index ff9cc9c7..242bb6f6 100644 --- a/mod/Jailbreak.LastRequest/LastRequests/KnifeFight.cs +++ b/mod/Jailbreak.LastRequest/LastRequests/KnifeFight.cs @@ -1,6 +1,7 @@ using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core.Attributes.Registration; +using CounterStrikeSharp.API.Modules.Utils; using Jailbreak.Public.Mod.LastRequest; using Jailbreak.Public.Mod.LastRequest.Enums; @@ -11,29 +12,19 @@ public class KnifeFight : AbstractLastRequest public KnifeFight(BasePlugin plugin, CCSPlayerController prisoner, CCSPlayerController guard) : base(plugin, prisoner, guard) { - plugin.RegisterEventHandler(OnPlayerDeath); - } - - public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info) - { - if (@event.Userid.Slot != prisoner.Slot && @event.Userid.Slot != guard.Slot) - return HookResult.Continue; - - if (@event.Userid.Slot == prisoner.Slot) - End(LRResult.GuardWin); - else - End(LRResult.PrisonerWin); - return HookResult.Continue; } public override void Setup() { - Server.PrintToChatAll($"{prisoner.PlayerName} is knife fighting {guard.PlayerName}"); // Strip weapons, teleport T to CT prisoner.RemoveWeapons(); guard.RemoveWeapons(); + guard.Teleport(prisoner.Pawn.Value!.AbsOrigin!, prisoner.Pawn.Value.AbsRotation!, new Vector()); + prisoner.Pawn.Value.MoveType = MoveType_t.MOVETYPE_NONE; + guard.Pawn.Value!.MoveType = MoveType_t.MOVETYPE_NONE; this.state = LRState.Pending; - + plugin.AddTimer(0.5f, () => guard.Pawn.Value.MoveType = MoveType_t.MOVETYPE_WALK); + plugin.AddTimer(0.8f, () => guard.Pawn.Value.MoveType = MoveType_t.MOVETYPE_WALK); plugin.AddTimer(3, Execute); } @@ -48,7 +39,6 @@ public override void Execute() public override void End(LRResult result) { - Server.PrintToChatAll($"The knife fight ended!"); this.state = LRState.Completed; } } \ No newline at end of file diff --git a/public/Jailbreak.Formatting/Views/ILastRequestMessages.cs b/public/Jailbreak.Formatting/Views/ILastRequestMessages.cs index 37c6768a..611794bb 100644 --- a/public/Jailbreak.Formatting/Views/ILastRequestMessages.cs +++ b/public/Jailbreak.Formatting/Views/ILastRequestMessages.cs @@ -1,5 +1,7 @@ using CounterStrikeSharp.API.Core; using Jailbreak.Formatting.Base; +using Jailbreak.Public.Mod.LastRequest; +using Jailbreak.Public.Mod.LastRequest.Enums; namespace Jailbreak.Formatting.Views; @@ -9,4 +11,7 @@ public interface ILastRequestMessages public IView LastRequestDisabled(); public IView InvalidLastRequest(string query); public IView InvalidPlayerChoice(CCSPlayerController player, string reason); + public IView InformLastRequest(AbstractLastRequest lr); + public IView AnnounceLastRequest(AbstractLastRequest lr); + public IView LastRequestDecided(AbstractLastRequest lr, LRResult result); } \ No newline at end of file