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

Add Gang Stats #318

Merged
merged 17 commits into from
Nov 2, 2024
18 changes: 18 additions & 0 deletions mod/Gangs.BaseImpl/Stats/LGStat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Gangs.BaseImpl.Stats;

public class LGStat : BaseStat<LGData> {
public const string STAT_ID = "jb_lg_stat";
public override string StatId => STAT_ID;
public override string Name => "LGs";
public override string? Description => "LGs reached";
public override LGData? Value { get; set; }
}

public class LGData {
public int CtLgs { get; set; }
public int TLgs { get; set; }

public override string ToString() {
return $"CT/T Last Guards: {CtLgs}/{TLgs}";
}
}
22 changes: 22 additions & 0 deletions mod/Gangs.BaseImpl/Stats/LRStat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Gangs.BaseImpl.Stats;

public class LRStat : BaseStat<LRData> {
public const string STAT_ID = "jb_lr_stat";
public override string StatId => STAT_ID;
public override string Name => "LRs";
public override string? Description => "LRs reached";
public override LRData? Value { get; set; }
}

public class LRData {
public int LRsReachedAsCt { get; set; }
public int LRsReachedAsT { get; set; }
public int CtLrs { get; set; }
public int TLrs { get; set; }
public int CTLrsWon { get; set; }
public int TLrsWon { get; set; }

public override string ToString() {
return $"CT/T LRs, CT/T LR Wins: {CtLrs}/{TLrs} {CTLrsWon}/{TLrsWon}";
}
}
25 changes: 25 additions & 0 deletions mod/Gangs.BaseImpl/Stats/WardenStat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Gangs.BaseImpl.Stats;

public class WardenStat : BaseStat<WardenData> {
public const string STAT_ID = "jb_warden_stat";
public override string StatId => STAT_ID;
public override string Name => "Guard";
public override string? Description => "Stats revolving CT specific info";
public override WardenData? Value { get; set; }
}

public class WardenData {
public int TimesWardened { get; set; }
public int WardenDeaths { get; set; }
public int WardensKilled { get; set; }
public int GuardDeathsAsWarden { get; set; }
public int WardenDeathsAsGuard { get; set; }

public override string ToString() {
return $"WardenData:\n" + $" Times Wardened: {TimesWardened}\n"
+ $" Warden Deaths: {WardenDeaths}\n"
+ $" Wardens Killed: {WardensKilled}\n"
+ $" Guard Deaths as Warden: {GuardDeathsAsWarden}\n"
+ $" Warden Deaths as Guard: {WardenDeathsAsGuard}";
}
}
4 changes: 4 additions & 0 deletions mod/Jailbreak.LastGuard/Jailbreak.LastGuard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
<ProjectReference Include="..\..\public\Jailbreak.Formatting\Jailbreak.Formatting.csproj"/>
<ProjectReference Include="..\..\public\Jailbreak.Public\Jailbreak.Public.csproj"/>
<ProjectReference Include="..\..\public\Jailbreak.Validator\Jailbreak.Validator.csproj"/>
<ProjectReference Include="..\Gangs.BaseImpl\Gangs.BaseImpl.csproj"/>
</ItemGroup>

<ItemGroup>
<Reference Include="GangsAPI">
<HintPath>..\..\public\Jailbreak.Public\Mixin\GangsAPI.dll</HintPath>
</Reference>
<Reference Include="MStatsShared">
<HintPath>..\..\public\Jailbreak.Public\Mixin\MStatsShared.dll</HintPath>
</Reference>
Expand Down
57 changes: 46 additions & 11 deletions mod/Jailbreak.LastGuard/LastGuard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@
using CounterStrikeSharp.API.Modules.Cvars;
using CounterStrikeSharp.API.Modules.Cvars.Validators;
using CounterStrikeSharp.API.Modules.Utils;
using Gangs.BaseImpl.Stats;
using GangsAPI.Data;
using GangsAPI.Services;
using GangsAPI.Services.Player;
using Jailbreak.Formatting.Extensions;
using Jailbreak.Formatting.Views;
using Jailbreak.Public;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Mod.LastGuard;
using Jailbreak.Public.Mod.LastRequest;
using Jailbreak.Public.Mod.Rebel;
using Jailbreak.Public.Utils;
using Jailbreak.Validator;
using Microsoft.Extensions.DependencyInjection;
using MStatsShared;

namespace Jailbreak.LastGuard;

public class LastGuard(ILGLocale notifications, ILastRequestManager lrManager)
: ILastGuardService, IPluginBehavior {
public class LastGuard(ILGLocale notifications, ILastRequestManager lrManager,
IRebelService rebel) : ILastGuardService, IPluginBehavior {
public static readonly FakeConVar<bool> CV_ALWAYS_OVERRIDE_CT = new(
"css_jb_lg_apply_lower_hp",
"If true, the LG will be forced lower health if calculated");
Expand Down Expand Up @@ -61,15 +67,31 @@ public class LastGuard(ILGLocale notifications, ILastRequestManager lrManager)

private readonly Random rng = new();
private bool canStart;
private bool isLastGuard;
private List<CCSPlayerController> lastGuardPrisoners = [];
public bool IsLastGuardActive { get; private set; }

public void StartLastGuard(CCSPlayerController lastGuard) {
var guardPlayerPawn = lastGuard.PlayerPawn.Value;

if (guardPlayerPawn == null || !guardPlayerPawn.IsValid) return;

isLastGuard = true;
IsLastGuardActive = true;

var stats = API.Gangs?.Services.GetService<IPlayerStatManager>();
if (stats != null)
foreach (var player in PlayerUtil.GetAlive()) {
var wrapper = new PlayerWrapper(player);
Task.Run(async () => {
var (success, stat) =
await stats.GetForPlayer<LGData>(wrapper, LGStat.STAT_ID);
if (!success || stat == null) stat = new LGData();
if (wrapper.Team == CsTeam.CounterTerrorist)
stat.CtLgs++;
else
stat.TLgs++;
await stats.SetForPlayer(wrapper, LGStat.STAT_ID, stat);
});
}

API.Stats?.PushStat(new ServerStat("JB_LASTGUARD",
lastGuard.SteamID.ToString()));
Expand Down Expand Up @@ -104,6 +126,8 @@ public void StartLastGuard(CCSPlayerController lastGuard) {
.ToAllCenter()
.ToAllChat();

foreach (var player in lastGuardPrisoners) rebel.MarkRebel(player);

if (string.IsNullOrEmpty(CV_LG_WEAPON.Value)) return;

foreach (var player in lastGuardPrisoners)
Expand All @@ -112,6 +136,14 @@ public void StartLastGuard(CCSPlayerController lastGuard) {

public void DisableLastGuardForRound() { canStart = false; }

public void Start(BasePlugin basePlugin, bool hotreload) {
if (API.Gangs == null) return;

var stats = API.Gangs.Services.GetService<IStatManager>();
if (stats == null) return;
stats.Stats.Add(new LGStat());
}

private int calculateHealth() {
var aliveTerrorists = Utilities.GetPlayers()
.Where(plr => plr is { PawnIsAlive: true, Team: CsTeam.Terrorist })
Expand All @@ -129,9 +161,10 @@ public HookResult OnPlayerDeathEvent(EventPlayerDeath @event,
GameEventInfo info) {
var player = @event.Userid;
if (player == null) return HookResult.Continue;

checkLastGuard(@event.Userid);

if (!isLastGuard) return HookResult.Continue;
if (!IsLastGuardActive) return HookResult.Continue;

if (player.Team != CsTeam.Terrorist) return HookResult.Continue;

Expand Down Expand Up @@ -174,7 +207,7 @@ private bool playerHasGun(CCSPlayerController player) {

private void checkLastGuard(CCSPlayerController? poi) {
if (poi == null) return;
if (isLastGuard) return;
if (IsLastGuardActive) return;
lastGuardPrisoners.Remove(poi);
if (poi.Team != CsTeam.CounterTerrorist) return;
var aliveCts = Utilities.GetPlayers()
Expand All @@ -193,17 +226,19 @@ private void checkLastGuard(CCSPlayerController? poi) {

[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info) {
isLastGuard = false;
IsLastGuardActive = false;
return HookResult.Continue;
}

[GameEventHandler]
public HookResult OnRoundStartEvent(EventRoundStart @event,
GameEventInfo info) {
canStart = Utilities.GetPlayers()
.Count(plr
=> plr is { PawnIsAlive: true, Team: CsTeam.CounterTerrorist })
>= CV_MINIMUM_CTS.Value;
Server.NextFrame(() => {
canStart = Utilities.GetPlayers()
.Count(plr
=> plr is { PawnIsAlive: true, Team: CsTeam.CounterTerrorist })
>= CV_MINIMUM_CTS.Value;
});
return HookResult.Continue;
}

Expand Down
1 change: 1 addition & 0 deletions mod/Jailbreak.LastRequest/Jailbreak.LastRequest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<ProjectReference Include="..\..\public\Jailbreak.Formatting\Jailbreak.Formatting.csproj"/>
<ProjectReference Include="..\..\public\Jailbreak.Public\Jailbreak.Public.csproj"/>
<ProjectReference Include="..\Gangs.BaseImpl\Gangs.BaseImpl.csproj"/>
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading