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

Dev #10

Merged
merged 16 commits into from
Feb 4, 2024
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
22 changes: 22 additions & 0 deletions lang/Jailbreak.English/Rebel/RebelNotifications.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CounterStrikeSharp.API.Modules.Utils;
using Jailbreak.Formatting.Base;
using Jailbreak.Formatting.Core;
using Jailbreak.Formatting.Logistics;
using Jailbreak.Formatting.Objects;
using Jailbreak.Formatting.Views;

namespace Jailbreak.English.Rebel;

public class RebelNotifications : IRebelNotifications, ILanguage<Formatting.Languages.English>
{
public static FormatObject PREFIX = new HiddenFormatObject( $" {ChatColors.Darkred}[{ChatColors.LightRed}Rebel{ChatColors.Darkred}]" )
{
// Hide in panorama and center text
Plain = false,
Panorama = false,
Chat = true,
};
public IView NO_LONGER_REBEL => new SimpleView(writer =>
writer
.Line(PREFIX, "You are no longer a rebel."));
}
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;
}
}
127 changes: 127 additions & 0 deletions mod/Jailbreak.Rebel/RebelManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System.Drawing;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Timers;
using Jailbreak.Formatting.Extensions;
using Jailbreak.Formatting.Views;
using Jailbreak.Public.Behaviors;
using Jailbreak.Public.Mod.Rebel;

namespace Jailbreak.Teams;

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

public RebelManager(IRebelNotifications notifs)
{
this.notifs = notifs;
}

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

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

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

ApplyRebelColor(player);
}
}, TimerFlags.REPEAT);
}

private HookResult OnRoundStart(EventRoundStart @event, GameEventInfo info)
{
rebelTimes.Clear();
foreach (var player in Utilities.GetPlayers())
{
if (!player.IsValid)
continue;
ApplyRebelColor(player);
}

return HookResult.Continue;
}

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 long GetRebelTimeLeft(CCSPlayerController player)
{
if (rebelTimes.TryGetValue(player, out long time))
{
return time - DateTimeOffset.Now.ToUnixTimeSeconds();
}

return 0;
}

public bool MarkRebel(CCSPlayerController player, long time)
{
rebelTimes[player] = DateTimeOffset.Now.ToUnixTimeSeconds() + time;
ApplyRebelColor(player);
return true;
}

public void UnmarkRebel(CCSPlayerController player)
{
notifs.NO_LONGER_REBEL.ToPlayerChat(player);

rebelTimes.Remove(player);
ApplyRebelColor(player);
}

// https://www.desmos.com/calculator/g2v6vvg7ax
private float GetRebelTimePercentage(CCSPlayerController player)
{
long 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 percent = GetRebelTimePercentage(player);
var percentRGB = 255 - (int)Math.Round(percent * 255.0);
var color = Color.FromArgb(254, 255, percentRGB, percentRGB);
if (percent <= 0)
{
color = Color.FromArgb(254, 255, 255, 255);
}

player.PrintToConsole("Color: " + color);
player.Pawn.Value.RenderMode = RenderMode_t.kRenderTransColor;
player.Pawn.Value.Render = color;
Utilities.SetStateChanged(player.Pawn.Value, "CBaseModelEntity", "m_clrRender");
}
}
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>();
}
}
6 changes: 5 additions & 1 deletion mod/Jailbreak.Warden/Global/WardenBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
{
_warden.Pawn.Value.RenderMode = RenderMode_t.kRenderTransColor;
_warden.Pawn.Value.Render = Color.Blue;
Utilities.SetStateChanged(_warden.Pawn.Value, "CBaseModelEntity", "m_clrRender");
}

_notifications.NEW_WARDEN(_warden)
Expand All @@ -77,9 +78,12 @@
return false;

_hasWarden = false;

if (_warden != null && _warden.Pawn.Value != null)
{
_warden.Pawn.Value.Render = Color.FromArgb(254, 255, 255, 255);
Utilities.SetStateChanged(_warden.Pawn.Value, "CBaseModelEntity", "m_clrRender");
}

_warden = null;

Expand All @@ -92,7 +96,7 @@
if (!_hasWarden)
return HookResult.Continue;

if (ev.Userid.UserId == _warden.UserId)

Check warning on line 99 in mod/Jailbreak.Warden/Global/WardenBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 99 in mod/Jailbreak.Warden/Global/WardenBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
if (!this.TryRemoveWarden())
_logger.LogWarning("[Warden] BUG: Problem removing current warden :^(");
Expand Down Expand Up @@ -122,7 +126,7 @@
if (!_hasWarden)
return HookResult.Continue;

if (ev.Userid.UserId == _warden.UserId)

Check warning on line 129 in mod/Jailbreak.Warden/Global/WardenBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 129 in mod/Jailbreak.Warden/Global/WardenBehavior.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
if (!this.TryRemoveWarden())
_logger.LogWarning("[Warden] BUG: Problem removing current warden :^(");
Expand Down
4 changes: 4 additions & 0 deletions public/Jailbreak.Formatting/Logistics/LanguageConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ public void WithRatio<TRatio>()
public void WithWarden<TWarden>()
where TWarden : class, ILanguage<TDialect>, IWardenNotifications
=> _collection.AddSingleton<IWardenNotifications, TWarden>();

public void WithRebel<TRebel>()
where TRebel : class, ILanguage<TDialect>, IRebelNotifications
=> _collection.AddSingleton<IRebelNotifications, TRebel>();
}
8 changes: 8 additions & 0 deletions public/Jailbreak.Formatting/Views/IRebelNotifications.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Jailbreak.Formatting.Base;

namespace Jailbreak.Formatting.Views;

public interface IRebelNotifications
{
public IView NO_LONGER_REBEL { get; }
}
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;
}

long GetRebelTimeLeft(CCSPlayerController player);

bool MarkRebel(CCSPlayerController player, long 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
3 changes: 3 additions & 0 deletions src/Jailbreak/JailbreakServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CounterStrikeSharp.API.Core;

using Jailbreak.Config;
using Jailbreak.English.Rebel;
using Jailbreak.English.Teams;
using Jailbreak.English.Warden;
using Jailbreak.Formatting.Languages;
Expand Down Expand Up @@ -32,12 +33,14 @@ public void ConfigureServices(IServiceCollection serviceCollection)
serviceCollection.AddJailbreakGeneric();
serviceCollection.AddJailbreakWarden();
serviceCollection.AddJailbreakTeams();
serviceCollection.AddJailbreakRebel();

// Add in english localization
serviceCollection.AddLanguage<Formatting.Languages.English>(config =>
{
config.WithRatio<RatioNotifications>();
config.WithWarden<WardenNotifications>();
config.WithRebel<RebelNotifications>();
});
}
}
Loading