Skip to content

Commit

Permalink
Move to localizations
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Feb 20, 2024
1 parent 67b6ba8 commit 0b3485d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 19 deletions.
22 changes: 22 additions & 0 deletions lang/Jailbreak.English/LastRequest/LastRequestMessages.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;
Expand All @@ -15,4 +17,24 @@ public class LastRequestMessages : ILastRequestMessages, ILanguage<Formatting.La
{
{ "Last Request has been disabled." }
};

public IView InvalidLastRequest(string query)
{
return new SimpleView()
{
"Invalid Last Request: ",
query
};
}

public IView InvalidPlayerChoice(CCSPlayerController player, string reason)
{
return new SimpleView()
{
"Invalid player choice: ",
player,
" Reason: ",
reason
};
}
}
28 changes: 18 additions & 10 deletions mod/Jailbreak.LastRequest/LastRequestCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CounterStrikeSharp.API.Modules.Commands.Targeting;
using CounterStrikeSharp.API.Modules.Menu;
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Formatting.Views;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Extensions;
using Jailbreak.Public.Mod.LastRequest;
Expand All @@ -14,16 +15,20 @@ namespace Jailbreak.LastRequest;

public class LastRequestCommand : IPluginBehavior
{

private ILastRequestManager _lrManager;
private LastRequestMenuSelector menuSelector;
private LastRequestPlayerSelector playerSelector;
private BasePlugin plugin;
private ILastRequestMessages _messages;
private IGenericCommandNotifications _generic;

// css_lr <player> <LRType>
public LastRequestCommand(ILastRequestManager manager, ILastRequestFactory factory)
public LastRequestCommand(ILastRequestManager manager, ILastRequestMessages messages,

Check warning on line 26 in mod/Jailbreak.LastRequest/LastRequestCommand.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'menuSelector' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 26 in mod/Jailbreak.LastRequest/LastRequestCommand.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'playerSelector' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 26 in mod/Jailbreak.LastRequest/LastRequestCommand.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'plugin' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 26 in mod/Jailbreak.LastRequest/LastRequestCommand.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'menuSelector' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 26 in mod/Jailbreak.LastRequest/LastRequestCommand.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'playerSelector' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 26 in mod/Jailbreak.LastRequest/LastRequestCommand.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'plugin' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
IGenericCommandNotifications generic)
{
_lrManager = manager;
_messages = messages;
_generic = generic;
}

public void Start(BasePlugin plugin)
Expand Down Expand Up @@ -65,45 +70,48 @@ public void Command_LastRequest(CCSPlayerController? executor, CommandInfo info)
}

// Validate LR
var type = LRTypeExtensions.FromString(info.GetArg(1));
if (type == null)
if (!Enum.TryParse<LRType>(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));
return;
}

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;
}
}
}
36 changes: 31 additions & 5 deletions mod/Jailbreak.LastRequest/LastRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -83,10 +93,26 @@ private bool CountsToLR(CCSPlayerController player)
public bool IsLREnabled { get; set; }
public IList<AbstractLastRequest> ActiveLRs { get; } = new List<AbstractLastRequest>();

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;
}
}
5 changes: 5 additions & 0 deletions mod/Jailbreak.LastRequest/LastRequests/KnifeFight.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using Jailbreak.Public.Mod.LastRequest;
Expand Down Expand Up @@ -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();
Expand All @@ -37,13 +39,16 @@ 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;
}

public override void End(LRResult result)
{
Server.PrintToChatAll($"The knife fight ended!");
this.state = LRState.Completed;
}
}
3 changes: 3 additions & 0 deletions public/Jailbreak.Formatting/Views/ILastRequestMessages.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CounterStrikeSharp.API.Core;
using Jailbreak.Formatting.Base;

namespace Jailbreak.Formatting.Views;
Expand All @@ -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);
}
13 changes: 9 additions & 4 deletions public/Jailbreak.Public/Mod/LastRequest/ILastRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ public interface ILastRequestManager : IPluginBehavior
{
public bool IsLREnabled { get; set; }
public IList<AbstractLastRequest> 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);
}

}

0 comments on commit 0b3485d

Please sign in to comment.