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

LR / Miscellaneous Maintenance #311

Merged
merged 5 commits into from
Oct 9, 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
39 changes: 24 additions & 15 deletions mod/Jailbreak.LastRequest/LastRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public class LastRequestManager(ILRLocale messages, IServiceProvider provider)
new("css_jb_lr_activate_lr_at", "Number of prisoners to activate LR at", 2,
ConVarFlags.FCVAR_NONE, new RangeValidator<int>(1, 32));

public static readonly FakeConVar<int> CV_MIN_PLAYERS_FOR_CREDITS =
new("css_jb_min_players_for_credits",
"Minimum number of players to start" + " giving credits out", 5);

private ILastRequestFactory? factory;
public bool IsLREnabledForRound { get; set; } = true;

Expand Down Expand Up @@ -87,6 +91,11 @@ public void DisableLRForRound() {
IsLREnabledForRound = false;
}

private static bool shouldGrantCredits() {
if (API.Gangs == null) return false;
return Utilities.GetPlayers().Count >= CV_MIN_PLAYERS_FOR_CREDITS.Value;
}

public void EnableLR(CCSPlayerController? died = null) {
messages.LastRequestEnabled().ToAllChat();
IsLREnabled = true;
Expand All @@ -101,25 +110,25 @@ public void EnableLR(CCSPlayerController? died = null) {

RoundUtil.AddTimeRemaining(CV_LR_GUARD_TIME.Value * cts);

foreach (var player in Utilities.GetPlayers()) {
var players = Utilities.GetPlayers();
foreach (var player in players) {
player.ExecuteClientCommand("play sounds/lr");
if (player.Team != CsTeam.Terrorist || !player.PawnIsAlive) continue;
if (died != null && player.SteamID == died.SteamID) continue;
player.ExecuteClientCommandFromServer("css_lr");
}

if (API.Gangs != null) {
var eco = API.Gangs.Services.GetService<IEcoManager>();
if (eco == null) return;
var survivors = Utilities.GetPlayers()
.Where(p => p is { IsBot: false, PawnIsAlive: true })
.Select(p => new PlayerWrapper(p))
.ToList();
Task.Run(async () => {
foreach (var survivor in survivors)
await eco.Grant(survivor, 100, reason: "LR Reached");
});
}
if (!shouldGrantCredits()) return;
var eco = API.Gangs?.Services.GetService<IEcoManager>();
if (eco == null) return;
var survivors = Utilities.GetPlayers()
.Where(p => p is { IsBot: false, PawnIsAlive: true })
.Select(p => new PlayerWrapper(p))
.ToList();
Task.Run(async () => {
foreach (var survivor in survivors)
await eco.Grant(survivor, 100, reason: "LR Reached");
});
}

public bool InitiateLastRequest(CCSPlayerController prisoner,
Expand Down Expand Up @@ -153,8 +162,8 @@ public bool EndLastRequest(AbstractLastRequest lr, LRResult result) {
RoundUtil.AddTimeRemaining(CV_LR_BONUS_TIME.Value);
messages.LastRequestDecided(lr, result).ToAllChat();

if (API.Gangs != null) {
var eco = API.Gangs.Services.GetService<IEcoManager>();
if (shouldGrantCredits()) {
var eco = API.Gangs?.Services.GetService<IEcoManager>();
if (eco == null) return false;
var wrapper =
new PlayerWrapper(result == LRResult.GUARD_WIN ?
Expand Down
64 changes: 17 additions & 47 deletions mod/Jailbreak.LastRequest/LastRequests/BulletForBullet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@
namespace Jailbreak.LastRequest.LastRequests;

public class BulletForBullet : TeleportingRequest {
private static readonly string[] GUNS = [
"weapon_deagle", "weapon_usp_silencer", "weapon_tec9", "weapon_ssg08",
"weapon_nova"
];

private readonly ChatMenu chatMenu;
private readonly bool magForMag;
private readonly ILRB4BLocale msg;
private string? designerName;

Check warning on line 18 in mod/Jailbreak.LastRequest/LastRequests/BulletForBullet.cs

View workflow job for this annotation

GitHub Actions / build

Field 'BulletForBullet.designerName' is never assigned to, and will always have its default value null

Check warning on line 18 in mod/Jailbreak.LastRequest/LastRequests/BulletForBullet.cs

View workflow job for this annotation

GitHub Actions / build

Field 'BulletForBullet.designerName' is never assigned to, and will always have its default value null
private int? whosShot, magSize;

Check warning on line 19 in mod/Jailbreak.LastRequest/LastRequests/BulletForBullet.cs

View workflow job for this annotation

GitHub Actions / build

Field 'BulletForBullet.designerName' is never assigned to, and will always have its default value null

Check warning on line 19 in mod/Jailbreak.LastRequest/LastRequests/BulletForBullet.cs

View workflow job for this annotation

GitHub Actions / build

Field 'BulletForBullet.designerName' is never assigned to, and will always have its default value null

public BulletForBullet(BasePlugin plugin, IServiceProvider provider,
CCSPlayerController prisoner, CCSPlayerController guard,
Expand All @@ -29,46 +24,13 @@
provider.GetRequiredService<ILastRequestManager>(), prisoner, guard) {
this.magForMag = magForMag;
chatMenu = new ChatMenu(magForMag ? "Mag for Mag" : "Shot for Shot");
foreach (var pistol in GUNS)
chatMenu.AddMenuOption(pistol.GetFriendlyWeaponName(),
(player, option) => OnSelect(player, option, pistol));

msg = provider.GetRequiredService<ILRB4BLocale>();
}

public override LRType Type
=> magForMag ? LRType.MAG_FOR_MAG : LRType.SHOT_FOR_SHOT;

private void OnSelect(CCSPlayerController player, ChatMenuOption _,
string designer) {
if (player.Slot != Prisoner.Slot) return;
designerName = designer;
MenuManager.CloseActiveMenu(player);

msg.WeaponSelected(player, designerName.GetFriendlyWeaponName())
.ToChat(Prisoner, Guard);

State = LRState.ACTIVE;

Prisoner.RemoveWeapons();
Guard.RemoveWeapons();
Prisoner.GiveNamedItem(designerName);
Guard.GiveNamedItem(designerName);

Plugin.AddTimer(0.5f, () => {
magSize = (magForMag ?
Prisoner.GetWeaponBase(designerName)!.VData?.MaxClip1 :
1) ?? 1;
Prisoner.GetWeaponBase(designerName).SetAmmo(0, 0);
Guard.GetWeaponBase(designerName).SetAmmo(0, 0);
var shooter = new Random().Next(2) == 0 ? Prisoner : Guard;
whosShot = shooter.Slot;
msg.PlayerGoesFirst(shooter).ToChat(Prisoner, Guard);

shooter.GetWeaponBase(designer).SetAmmo(magSize.Value, 0);
});
}

public override void Setup() {
Plugin.RegisterEventHandler<EventBulletImpact>(OnPlayerShoot);

Expand All @@ -77,17 +39,30 @@

base.Setup();
Execute();

chatMenu.Title =
$"{chatMenu.Title} - {Prisoner.PlayerName} vs {Guard.PlayerName}";
}

private const string weaponName = "weapon_deagle";

public override void Execute() {
State = LRState.PENDING;
MenuManager.OpenChatMenu(Prisoner, chatMenu);

Plugin.AddTimer(10, timeout, TimerFlags.STOP_ON_MAPCHANGE);
Plugin.AddTimer(5, () => {
if (State != LRState.PENDING) return;
Guard.GiveNamedItem(weaponName);
Prisoner.GiveNamedItem(weaponName);

magSize = (magForMag ?
Prisoner.GetWeaponBase(weaponName)!.VData?.MaxClip1 :
1) ?? 1;
Prisoner.GetWeaponBase(weaponName).SetAmmo(0, 0);
Guard.GetWeaponBase(weaponName).SetAmmo(0, 0);
var shooter = new Random().Next(2) == 0 ? Prisoner : Guard;
whosShot = shooter.Slot;
msg.PlayerGoesFirst(shooter).ToChat(Prisoner, Guard);
shooter.GetWeaponBase(weaponName).SetAmmo(magSize.Value, 0);
State = LRState.ACTIVE;
}, TimerFlags.STOP_ON_MAPCHANGE);

Plugin.AddTimer(30, () => {
if (State != LRState.ACTIVE) return;
Expand Down Expand Up @@ -117,11 +92,6 @@
}, TimerFlags.STOP_ON_MAPCHANGE);
}

private void timeout() {
if (string.IsNullOrEmpty(designerName))
Manager.EndLastRequest(this, LRResult.TIMED_OUT);
}

private HookResult OnPlayerShoot(EventBulletImpact @event,
GameEventInfo info) {
if (State != LRState.ACTIVE) return HookResult.Continue;
Expand Down
2 changes: 1 addition & 1 deletion mod/Jailbreak.RTD/Rewards/WeaponReward.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public WeaponReward(string weapon, CsTeam? requiredTeam = CsTeam.Terrorist) {

public virtual string Description
=> "You won a"
+ (weapon.GetFriendlyWeaponName()[0].IsVowel() ? "n" : "" + " ")
+ (weapon.GetFriendlyWeaponName()[0].IsVowel() ? "n" : "") + " "
+ weapon.GetFriendlyWeaponName() + " next round.";

public bool CanGrantReward(CCSPlayerController player) {
Expand Down
2 changes: 1 addition & 1 deletion mod/Jailbreak.Rebel/C4Bomb/C4Behavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private void detonate(CCSPlayerController player, CC4 bomb) {
}
}

if (API.Gangs != null) {
if (API.Gangs != null && killed > 0) {
var eco = API.Gangs.Services.GetService<IEcoManager>();
if (eco != null) {
var wrapper = new PlayerWrapper(player);
Expand Down
3 changes: 2 additions & 1 deletion mod/Jailbreak.Rebel/RebelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ private float getRebelTimePercentage(CCSPlayerController player) {
}

private Color getRebelColor(CCSPlayerController player) {
var percent = getRebelTimePercentage(player);
var percent = getRebelTimePercentage(player);
percent = Math.Clamp(percent, 0, 1);
var percentRgb = 255 - (int)Math.Round(percent * 255.0);
var color = Color.FromArgb(255, 255, percentRgb, percentRgb);
if (percent <= 0) color = Color.White;
Expand Down
2 changes: 1 addition & 1 deletion public/Jailbreak.Public/Jailbreak.Public.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.260"/>
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0"/>
<PackageReference Include="System.Text.Json" Version="8.0.4"/>
<PackageReference Include="System.Text.Json" Version="8.0.5"/>
<ProjectReference Include="..\Jailbreak.Tag\Jailbreak.Tag.csproj"/>
<Reference Include="GangsAPI">
<HintPath>Mixin\GangsAPI.dll</HintPath>
Expand Down
Loading