Skip to content

Commit

Permalink
LR / Miscellaneous Maintenance (#311)
Browse files Browse the repository at this point in the history
- RGB % is now clamped for customized rebel times.
- Bullet-For-Bullet LRs are now hardcoded to use deagles only.
- Credits are only rewarded (for LRs) when there are at least 5+ players
online.
  • Loading branch information
MSWS authored Oct 9, 2024
2 parents 889868f + d2521b1 commit acfb937
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 66 deletions.
39 changes: 24 additions & 15 deletions mod/Jailbreak.LastRequest/LastRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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 @@ -91,6 +95,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 @@ -105,25 +114,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 @@ -157,8 +166,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 @@ -13,11 +13,6 @@
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;
Expand All @@ -30,46 +25,13 @@ public BulletForBullet(BasePlugin plugin, IServiceProvider provider,
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 @@ -78,17 +40,30 @@ public override void Setup() {

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 @@ -118,11 +93,6 @@ public override void Execute() {
}, 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

0 comments on commit acfb937

Please sign in to comment.