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 rebel module #9

Merged
merged 1 commit into from
Feb 3, 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
7 changes: 7 additions & 0 deletions Jailbreak.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "lang", "lang", "{CDCDE44E-0
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jailbreak.English", "lang\Jailbreak.English\Jailbreak.English.csproj", "{FC2D6F50-BCFF-41E6-A965-6C73CC01C3BF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Jailbreak.Rebel", "mod\Jailbreak.Rebel\Jailbreak.Rebel.csproj", "{CB2391A1-6CDD-496F-B8D6-674FD6268038}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -56,6 +58,10 @@ Global
{FC2D6F50-BCFF-41E6-A965-6C73CC01C3BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC2D6F50-BCFF-41E6-A965-6C73CC01C3BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC2D6F50-BCFF-41E6-A965-6C73CC01C3BF}.Release|Any CPU.Build.0 = Release|Any CPU
{CB2391A1-6CDD-496F-B8D6-674FD6268038}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CB2391A1-6CDD-496F-B8D6-674FD6268038}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB2391A1-6CDD-496F-B8D6-674FD6268038}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB2391A1-6CDD-496F-B8D6-674FD6268038}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{9135CCC9-66C5-4A9C-AE3C-91475B5F0437} = {177DA48D-8306-4102-918D-992569878581}
Expand All @@ -65,5 +71,6 @@ Global
{28EE05E4-8FE3-4CC6-AA03-0C533EFBFBF2} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7}
{446E0B6F-E4FE-45E6-BD9B-BD943698327A} = {59311734-3648-43C2-B43C-385718B0D103}
{FC2D6F50-BCFF-41E6-A965-6C73CC01C3BF} = {CDCDE44E-01D2-4B76-99DA-A57E1E956038}
{CB2391A1-6CDD-496F-B8D6-674FD6268038} = {36BA84C0-291C-4930-A7C6-97CDF8F7F0D7}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions mod/Jailbreak.Rebel/Jailbreak.Rebel.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\public\Jailbreak.Formatting\Jailbreak.Formatting.csproj" />
<ProjectReference Include="..\..\public\Jailbreak.Public\Jailbreak.Public.csproj" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions mod/Jailbreak.Rebel/RebelListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Extensions;
using Jailbreak.Public.Mod.Rebel;

namespace Jailbreak.Teams;

public class RebelListener : IPluginBehavior
{
private IRebelService _rebelService;

public RebelListener(IRebelService rebelService)
{
_rebelService = rebelService;
}

public void Start(BasePlugin parent)
{
parent.RegisterEventHandler<EventPlayerHurt>(OnPlayerHurt);
}

HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
{
var player = @event.Userid;
if (!player.IsValid)
return HookResult.Continue;
if (player.GetTeam() != CsTeam.CounterTerrorist)
return HookResult.Continue;

var attacker = @event.Attacker;
if (!attacker.IsValid)
return HookResult.Continue;

if (attacker.GetTeam() != CsTeam.Terrorist)
return HookResult.Continue;

_rebelService.MarkRebel(attacker, 120);
return HookResult.Continue;
}
}
100 changes: 100 additions & 0 deletions mod/Jailbreak.Rebel/RebelManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System.Drawing;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Mod.Rebel;

namespace Jailbreak.Teams;

public class RebelManager : IPluginBehavior, IRebelService
{
private Dictionary<CCSPlayerController, float> rebelTimes = new();

public void Start(BasePlugin parent)
{
parent.RegisterEventHandler<EventPlayerDisconnect>(OnPlayerDisconnect);

parent.AddTimer(1f, () =>
{
foreach (var player in GetActiveRebels())
{
if (!player.IsValid)
continue;

if (GetRebelTimeLeft(player) <= 0)
{
UnmarkRebel(player);
continue;
}

ApplyRebelColor(player);
}
});
}

HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo info)
{
if (rebelTimes.ContainsKey(@event.Userid))
{
rebelTimes.Remove(@event.Userid);
}

return HookResult.Continue;
}

public ISet<CCSPlayerController> GetActiveRebels()
{
return rebelTimes.Keys.ToHashSet();
}

public float GetRebelTimeLeft(CCSPlayerController player)
{
if (rebelTimes.TryGetValue(player, out float time))
{
return time - DateTime.Now.Ticks / 1000f;
}

return 0;
}

public bool MarkRebel(CCSPlayerController player, float time)
{
rebelTimes.Add(player, DateTime.Now.Ticks / 1000f + time);
ApplyRebelColor(player);
return true;
}

public void UnmarkRebel(CCSPlayerController player)
{
player.PrintToChat("You are no longer a rebel");
rebelTimes.Remove(player);
ApplyRebelColor(player);
}

// https://www.desmos.com/calculator/g2v6vvg7ax
private float GetRebelTimePercentage(CCSPlayerController player)
{
float x = GetRebelTimeLeft(player);
if (x > 120)
return 1;
if (x <= 0)
return 0;
return (float)(100 - (120 - x) * (Math.Sqrt(120 - x)) / 13f) / 100;
}

private void ApplyRebelColor(CCSPlayerController player)
{
if (!player.IsValid || player.Pawn.Value == null)
return;
var percentage = GetRebelTimePercentage(player);
var inverse = 1 - percentage;
var inverseInt = (int)(inverse * 255);
var color = Color.FromArgb(254, (int)percentage * 255, inverseInt, inverseInt);
if (percentage <= 0)
{
color = Color.FromArgb(254, 255, 255, 255);
}

player.Pawn.Value.Render = color;
}
}
16 changes: 16 additions & 0 deletions mod/Jailbreak.Rebel/RebelServiceExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Jailbreak.Public.Extensions;
using Jailbreak.Public.Mod.Rebel;
using Jailbreak.Public.Mod.Teams;

using Microsoft.Extensions.DependencyInjection;

namespace Jailbreak.Teams;

public static class RebelServiceExtension
{
public static void AddJailbreakRebel(this IServiceCollection collection)
{
collection.AddPluginBehavior<IRebelService, RebelManager>();
collection.AddPluginBehavior<RebelListener>();
}
}
19 changes: 19 additions & 0 deletions public/Jailbreak.Public/Mod/Rebel/IRebelService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using CounterStrikeSharp.API.Core;

namespace Jailbreak.Public.Mod.Rebel;

public interface IRebelService
{
ISet<CCSPlayerController> GetActiveRebels();

bool IsRebel(CCSPlayerController player)
{
return GetRebelTimeLeft(player) > 0;
}

float GetRebelTimeLeft(CCSPlayerController player);

bool MarkRebel(CCSPlayerController player, float time);

void UnmarkRebel(CCSPlayerController player);
}
1 change: 1 addition & 0 deletions src/Jailbreak/Jailbreak.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<ProjectReference Include="..\..\lang\Jailbreak.English\Jailbreak.English.csproj" />
<ProjectReference Include="..\..\mod\Jailbreak.Teams\Jailbreak.Teams.csproj" />
<ProjectReference Include="..\..\mod\Jailbreak.Warden\Jailbreak.Warden.csproj" />
<ProjectReference Include="..\..\mod\Jailbreak.Rebel\Jailbreak.Rebel.csproj" />
<ProjectReference Include="..\..\public\Jailbreak.Public\Jailbreak.Public.csproj" />
<ProjectReference Include="..\Jailbreak.Generic\Jailbreak.Generic.csproj" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions src/Jailbreak/JailbreakServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void ConfigureServices(IServiceCollection serviceCollection)
serviceCollection.AddJailbreakGeneric();
serviceCollection.AddJailbreakWarden();
serviceCollection.AddJailbreakTeams();
serviceCollection.AddJailbreakRebel();

// Add in english localization
serviceCollection.AddLanguage<Formatting.Languages.English>(config =>
Expand Down
Loading