Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to localizations #51

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions lang/Jailbreak.English/LastRequest/LastRequestMessages.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
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;

Expand All @@ -15,4 +19,51 @@ 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
};
}

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
};
}
}
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 @@

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 @@
}

// 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;
}
}
}
41 changes: 36 additions & 5 deletions mod/Jailbreak.LastRequest/LastRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
private ILastRequestMessages messages;
private ILastRequestFactory factory;

public LastRequestManager(LastRequestConfig config, ILastRequestMessages messages, ILastRequestFactory factory)

Check warning on line 21 in mod/Jailbreak.LastRequest/LastRequestManager.cs

View workflow job for this annotation

GitHub Actions / build

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

Check warning on line 21 in mod/Jailbreak.LastRequest/LastRequestManager.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_parent' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
this.config = config;
this.messages = messages;
Expand Down Expand Up @@ -55,10 +55,20 @@
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,31 @@
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);

messages.InformLastRequest(lr).ToPlayerChat(prisoner);
messages.InformLastRequest(lr).ToPlayerChat(guard);
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);
if (result == LRResult.GuardWin || result == LRResult.PrisonerWin)
messages.LastRequestDecided(lr, result).ToAllChat();
lr.End(result);
ActiveLRs.Remove(lr);
return true;
}
}
23 changes: 9 additions & 14 deletions mod/Jailbreak.LastRequest/LastRequests/KnifeFight.cs
Original file line number Diff line number Diff line change
@@ -1,5 +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;

Expand All @@ -10,33 +12,26 @@ public class KnifeFight : AbstractLastRequest
public KnifeFight(BasePlugin plugin, CCSPlayerController prisoner, CCSPlayerController guard) : base(plugin,
prisoner, guard)
{
plugin.RegisterEventHandler<EventPlayerDeath>(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()
{
// 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);
}

public override void Execute()
{
prisoner.PrintToChat("Begin!");
guard.PrintToChat("Begin!");
prisoner.GiveNamedItem("weapon_knife");
guard.GiveNamedItem("weapon_knife");
this.state = LRState.Active;
Expand Down
8 changes: 8 additions & 0 deletions public/Jailbreak.Formatting/Views/ILastRequestMessages.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
using CounterStrikeSharp.API.Core;
using Jailbreak.Formatting.Base;
using Jailbreak.Public.Mod.LastRequest;
using Jailbreak.Public.Mod.LastRequest.Enums;

namespace Jailbreak.Formatting.Views;

public interface ILastRequestMessages
{
public IView LastRequestEnabled();
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);
}
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);
}

}
Loading