diff --git a/mod/Jailbreak.LastRequest/LastRequestFactory.cs b/mod/Jailbreak.LastRequest/LastRequestFactory.cs index 1e7313b8..f23b1b31 100644 --- a/mod/Jailbreak.LastRequest/LastRequestFactory.cs +++ b/mod/Jailbreak.LastRequest/LastRequestFactory.cs @@ -9,23 +9,25 @@ namespace Jailbreak.LastRequest; public class LastRequestFactory(ILastRequestManager manager, IServiceProvider services) : ILastRequestFactory { - private BasePlugin? plugin; + private BasePlugin plugin = null!; public void Start(BasePlugin basePlugin) { plugin = basePlugin; } public AbstractLastRequest CreateLastRequest(CCSPlayerController prisoner, CCSPlayerController guard, LRType type) { return type switch { - LRType.KNIFE_FIGHT => new KnifeFight(plugin!, manager, prisoner, guard), - LRType.GUN_TOSS => new GunToss(plugin!, manager, prisoner, guard), - LRType.NO_SCOPE => new NoScope(plugin!, manager, prisoner, guard), - LRType.SHOT_FOR_SHOT => new ShotForShot(plugin!, manager, prisoner, guard), - LRType.ROCK_PAPER_SCISSORS => new RockPaperScissors(plugin!, manager, + LRType.KNIFE_FIGHT => new KnifeFight(plugin, manager, prisoner, guard), + LRType.GUN_TOSS => new GunToss(plugin, manager, prisoner, guard), + LRType.NO_SCOPE => new NoScope(plugin, manager, prisoner, guard), + LRType.SHOT_FOR_SHOT => new BulletForBullet(plugin, manager, prisoner, + guard, false), + LRType.ROCK_PAPER_SCISSORS => new RockPaperScissors(plugin, manager, prisoner, guard), - LRType.COINFLIP => new Coinflip(plugin!, manager, prisoner, guard), - LRType.RACE => new Race(plugin!, manager, prisoner, guard, + LRType.COINFLIP => new Coinflip(plugin, manager, prisoner, guard), + LRType.RACE => new Race(plugin, manager, prisoner, guard, services.GetRequiredService()), - LRType.MAG_FOR_MAG => new MagForMag(plugin!, manager, prisoner, guard), + LRType.MAG_FOR_MAG => new BulletForBullet(plugin, manager, prisoner, + guard, true), _ => throw new ArgumentException("Invalid last request type: " + type, nameof(type)) }; diff --git a/mod/Jailbreak.LastRequest/LastRequests/MagForMag.cs b/mod/Jailbreak.LastRequest/LastRequests/BulletForBullet.cs similarity index 58% rename from mod/Jailbreak.LastRequest/LastRequests/MagForMag.cs rename to mod/Jailbreak.LastRequest/LastRequests/BulletForBullet.cs index 326b8f4c..774d798c 100644 --- a/mod/Jailbreak.LastRequest/LastRequests/MagForMag.cs +++ b/mod/Jailbreak.LastRequest/LastRequests/BulletForBullet.cs @@ -1,3 +1,5 @@ +using System.Runtime.InteropServices.JavaScript; +using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Modules.Menu; using CounterStrikeSharp.API.Modules.Timers; @@ -7,16 +9,25 @@ namespace Jailbreak.LastRequest.LastRequests; -public class MagForMag : WeaponizedRequest { +public class BulletForBullet : TeleportingRequest { private readonly ChatMenu chatMenu; private string? CHOSEN_PISTOL; - private int MAGAZINE_SIZE; - private CCSPlayerController? whosShot; - - public MagForMag(BasePlugin plugin, ILastRequestManager manager, - CCSPlayerController prisoner, CCSPlayerController guard) : base(plugin, - manager, prisoner, guard) { - chatMenu = new ChatMenu("Shot For Shot"); + private int? whosShot, magSize; + private bool magForMag = false; + + private static readonly string[] GUNS = new string[] { + "weapon_deagle", "weapon_glock", "weapon_hkp2000", "weapon_tec9", + "weapon_cz75a", "weapon_revolver" + }; + + private static readonly string[] GUN_NAMES = new string[] { + "Desert Eagle", "Glock 18", "USP-S", "Tec9", "CZ75", "Revolver" + }; + + public BulletForBullet(BasePlugin plugin, ILastRequestManager manager, + CCSPlayerController prisoner, CCSPlayerController guard, + bool magForMag) : base(plugin, manager, prisoner, guard) { + chatMenu = new ChatMenu(magForMag ? "Mag for Mag" : "Shot for Shot"); foreach (var pistol in Tag.PISTOLS) { chatMenu.AddMenuOption(pistol.GetFriendlyWeaponName(), OnSelect); } @@ -28,13 +39,9 @@ private void OnSelect(CCSPlayerController player, ChatMenuOption option) { if (player.Slot != Prisoner.Slot) return; MenuManager.CloseActiveMenu(player); - CHOSEN_PISTOL = Tag.PISTOLS.ElementAt(Array.IndexOf( - [ - "Desert Eagle", "Dualies", "Five Seven", "Glock 18", "HPK2000", "P250", - "USPS", "Tec9", "CZ75", "Revolver" - ], option.Text)); + CHOSEN_PISTOL = GUNS[Array.IndexOf(GUN_NAMES, option.Text)]; - PrintToParticipants(player.PlayerName + " has chosen to use the " + PrintToParticipants(player.PlayerName + " chose the " + CHOSEN_PISTOL.GetFriendlyWeaponName()); State = LRState.ACTIVE; @@ -44,24 +51,32 @@ private void OnSelect(CCSPlayerController player, ChatMenuOption option) { Guard.GetWeaponBase(CHOSEN_PISTOL).SetAmmo(0, 0); //steal the VData of the prisoners gun for mag size - MAGAZINE_SIZE = Prisoner.GetWeaponBase(CHOSEN_PISTOL)!.VData!.MaxClip1; + magSize = magForMag ? + Prisoner.GetWeaponBase(CHOSEN_PISTOL)!.VData!.MaxClip1 : + 1; - whosShot = new Random().Next(2) == 0 ? Prisoner : Guard; - PrintToParticipants(whosShot.PlayerName - + " has been chosen to shoot first"); + var shooter = new Random().Next(2) == 0 ? Prisoner : Guard; + whosShot = shooter.Slot; + PrintToParticipants(shooter.PlayerName + " gets to shoot first"); - whosShot.GetWeaponBase(CHOSEN_PISTOL).SetAmmo(MAGAZINE_SIZE, 0); + shooter.GetWeaponBase(CHOSEN_PISTOL).SetAmmo(magSize.Value, 0); } public override void Setup() { - Plugin.RegisterEventHandler(OnPlayerShoot); + Plugin.RegisterEventHandler(OnPlayerShoot); + + Prisoner.RemoveWeapons(); + Guard.RemoveWeapons(); + base.Setup(); + Execute(); - CHOSEN_PISTOL = String.Empty; + CHOSEN_PISTOL = string.Empty; chatMenu.Title = - $"Shot For Shot - {Prisoner.PlayerName} vs {Guard.PlayerName}"; + $"{chatMenu.Title} - {Prisoner.PlayerName} vs {Guard.PlayerName}"; } + public override void Execute() { State = LRState.PENDING; MenuManager.OpenChatMenu(Prisoner, chatMenu); @@ -80,9 +95,10 @@ public override void Execute() { LRResult.GUARD_WIN : LRResult.PRISONER_WIN; if (Guard.Health == Prisoner.Health) { - PrintToParticipants("Even health, since " + whosShot!.PlayerName - + " had the shot last, they lose."); - result = whosShot.Slot == Prisoner.Slot ? + var active = whosShot == Prisoner.Slot ? Guard : Prisoner; + PrintToParticipants("Even health, since " + active.PlayerName + + " had the shot, they lose."); + result = whosShot == Prisoner.Slot ? LRResult.GUARD_WIN : LRResult.PRISONER_WIN; } else { PrintToParticipants("Health was the deciding factor. "); } @@ -99,17 +115,18 @@ private void timeout() { Manager.EndLastRequest(this, LRResult.TIMED_OUT); } - private HookResult OnPlayerShoot(EventPlayerShoot @event, + private HookResult OnPlayerShoot(EventBulletImpact @event, GameEventInfo info) { if (State != LRState.ACTIVE) return HookResult.Continue; var player = @event.Userid; - if (player == null || whosShot == null || !player.IsReal()) + if (player == null || whosShot == null || !player.IsValid + || magSize == null) return HookResult.Continue; if (player.Slot != Prisoner.Slot && player.Slot != Guard.Slot) return HookResult.Continue; - if (player.Slot != whosShot.Slot) { + if (player.Slot != whosShot) { PrintToParticipants(player.PlayerName + " cheated."); player.Pawn.Value?.CommitSuicide(false, true); return HookResult.Handled; @@ -118,16 +135,14 @@ private HookResult OnPlayerShoot(EventPlayerShoot @event, if (player.GetWeaponBase(CHOSEN_PISTOL).Clip1 != 0) return HookResult.Continue; - PrintToParticipants(player.PlayerName + " has shot all their bullets."); var opponent = player.Slot == Prisoner.Slot ? Guard : Prisoner; - opponent.PrintToChat("Your shot"); - opponent.GetWeaponBase(CHOSEN_PISTOL).SetAmmo(MAGAZINE_SIZE, 0); - whosShot = opponent; + opponent.GetWeaponBase(CHOSEN_PISTOL).SetAmmo(magSize.Value, 0); + whosShot = opponent.Slot; return HookResult.Continue; } public override void OnEnd(LRResult result) { - Plugin.DeregisterEventHandler(OnPlayerShoot); + Plugin.DeregisterEventHandler(OnPlayerShoot); State = LRState.COMPLETED; } } \ No newline at end of file diff --git a/mod/Jailbreak.LastRequest/LastRequests/ShotForShot.cs b/mod/Jailbreak.LastRequest/LastRequests/ShotForShot.cs deleted file mode 100644 index 4f4be6f6..00000000 --- a/mod/Jailbreak.LastRequest/LastRequests/ShotForShot.cs +++ /dev/null @@ -1,126 +0,0 @@ -using CounterStrikeSharp.API.Core; -using CounterStrikeSharp.API.Modules.Menu; -using CounterStrikeSharp.API.Modules.Timers; -using Jailbreak.Public.Extensions; -using Jailbreak.Public.Mod.LastRequest; -using Jailbreak.Public.Mod.LastRequest.Enums; - -namespace Jailbreak.LastRequest.LastRequests; - -public class ShotForShot : WeaponizedRequest { - private readonly ChatMenu chatMenu; - private string? CHOSEN_PISTOL; - private CCSPlayerController? whosShot; - - public ShotForShot(BasePlugin plugin, ILastRequestManager manager, - CCSPlayerController prisoner, CCSPlayerController guard) : base(plugin, - manager, prisoner, guard) { - chatMenu = new ChatMenu("Shot For Shot"); - foreach (var pistol in Tag.PISTOLS) { - chatMenu.AddMenuOption(pistol.GetFriendlyWeaponName(), OnSelect); - } - } - - public override LRType Type => LRType.SHOT_FOR_SHOT; - - private void OnSelect(CCSPlayerController player, ChatMenuOption option) { - if (player.Slot != Prisoner.Slot) return; - MenuManager.CloseActiveMenu(player); - - CHOSEN_PISTOL = Tag.PISTOLS.ElementAt(Array.IndexOf( - [ - "Desert Eagle", "Dualies", "Five Seven", "Glock 18", "HPK2000", "P250", - "USPS", "Tec9", "CZ75", "Revolver" - ], option.Text)); - - PrintToParticipants(player.PlayerName + " has chosen to use the " - + CHOSEN_PISTOL.GetFriendlyWeaponName()); - State = LRState.ACTIVE; - - Prisoner.GiveNamedItem(CHOSEN_PISTOL); - Prisoner.GetWeaponBase(CHOSEN_PISTOL).SetAmmo(0, 0); - Guard.GiveNamedItem(CHOSEN_PISTOL); - Guard.GetWeaponBase(CHOSEN_PISTOL).SetAmmo(0, 0); - - whosShot = new Random().Next(2) == 0 ? Prisoner : Guard; - PrintToParticipants(whosShot.PlayerName - + " has been chosen to shoot first"); - - whosShot.GetWeaponBase(CHOSEN_PISTOL).SetAmmo(1, 0); - } - - public override void Setup() { - Plugin.RegisterEventHandler(OnPlayerShoot); - base.Setup(); - - CHOSEN_PISTOL = String.Empty; - chatMenu.Title = - $"Shot For Shot - {Prisoner.PlayerName} vs {Guard.PlayerName}"; - } - - public override void Execute() { - State = LRState.PENDING; - MenuManager.OpenChatMenu(Prisoner, chatMenu); - - Plugin.AddTimer(10, timeout, TimerFlags.STOP_ON_MAPCHANGE); - - Plugin.AddTimer(30, () => { - if (State != LRState.ACTIVE) return; - Prisoner.GiveNamedItem("weapon_knife"); - Guard.GiveNamedItem("weapon_knife"); - }); - Plugin.AddTimer(60, () => { - if (State != LRState.ACTIVE) return; - PrintToParticipants("Time's Up!"); - var result = Guard.Health > Prisoner.Health ? - LRResult.GUARD_WIN : - LRResult.PRISONER_WIN; - if (Guard.Health == Prisoner.Health) { - PrintToParticipants("Even health, since " + whosShot!.PlayerName - + " had the shot last, they lose."); - result = whosShot.Slot == Prisoner.Slot ? - LRResult.GUARD_WIN : - LRResult.PRISONER_WIN; - } else { PrintToParticipants("Health was the deciding factor. "); } - - if (result == LRResult.GUARD_WIN) - Prisoner.Pawn.Value?.CommitSuicide(false, true); - else - Guard.Pawn.Value?.CommitSuicide(false, true); - }, TimerFlags.STOP_ON_MAPCHANGE); - } - - private void timeout() { - if (CHOSEN_PISTOL == String.Empty) - Manager.EndLastRequest(this, LRResult.TIMED_OUT); - } - - private HookResult OnPlayerShoot(EventPlayerShoot @event, - GameEventInfo info) { - if (State != LRState.ACTIVE) return HookResult.Continue; - - var player = @event.Userid; - if (player == null || whosShot == null || !player.IsReal()) - return HookResult.Continue; - - if (player.Slot != Prisoner.Slot && player.Slot != Guard.Slot) - return HookResult.Continue; - if (player.Slot != whosShot.Slot) { - PrintToParticipants(player.PlayerName + " cheated."); - player.Pawn.Value?.CommitSuicide(false, true); - return HookResult.Handled; - } - - PrintToParticipants(player.PlayerName + " has shot."); - var opponent = player.Slot == Prisoner.Slot ? Guard : Prisoner; - opponent.PrintToChat("Your shot"); - opponent.GetWeaponBase(CHOSEN_PISTOL).SetAmmo(1, 0); - whosShot = opponent; - return HookResult.Continue; - } - - public override void OnEnd(LRResult result) { - Plugin.DeregisterEventHandler(OnPlayerShoot); - State = LRState.COMPLETED; - } -} \ No newline at end of file