Skip to content

Commit

Permalink
Added the initial ClutchAnnouncePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
B3none committed Jan 2, 2024
1 parent eeeb83b commit 81a7f7a
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 40 deletions.
114 changes: 114 additions & 0 deletions ClutchAnnouncePlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Utils;

namespace ClutchAnnouncePlugin;

[MinimumApiVersion(129)]
public class ClutchAnnouncePlugin : BasePlugin
{
private const string Version = "1.0.0";

public override string ModuleName => "Clutch Announce Plugin";
public override string ModuleVersion => Version;
public override string ModuleAuthor => "B3none";
public override string ModuleDescription => "Announce when someone wins a clutch scenario";

private const string LogPrefix = $"[Clutch Announce {Version}] ";
private static readonly string MessagePrefix = $"[{ChatColors.Yellow}Clutch{ChatColors.White}] ";

// Constants
private const int MinPlayers = 3;

// State
private int _opponents;
private CsTeam? _clutchTeam;
private CCSPlayerController? _clutchPlayer;

// Listeners
[GameEventHandler]
public HookResult OnClientDisconnect(EventClientDisconnect @event, GameEventInfo info)
{
CheckAlive();

return HookResult.Continue;
}

[GameEventHandler]
public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
{
CheckAlive();

return HookResult.Continue;
}

[GameEventHandler]
public HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
{
Console.WriteLine($"{LogPrefix}OnRoundEnd event fired!");

if (_clutchPlayer != null && (CsTeam)@event.Winner == _clutchTeam)
{
Server.PrintToChatAll(
$"{MessagePrefix}Player {ChatColors.Green}{_clutchPlayer.PlayerName}{ChatColors.White} has clutched a 1v{_opponents}!");
}

_clutchPlayer = null;
_clutchTeam = null;
_opponents = 0;

return HookResult.Continue;
}

// Utilities
private void CheckAlive()
{
if (_clutchPlayer != null)
{
return;
}

List<CCSPlayerController> aliveCts = new();
List<CCSPlayerController> aliveTs = new();

var players = Utilities.GetPlayers().Where(IsValidPlayer).ToList();

foreach (var player in players)
{
if ((CsTeam)player.TeamNum == CsTeam.CounterTerrorist && player.PlayerPawn.Value!.Health > 0)
{
aliveCts.Add(player);
}
else if ((CsTeam)player.TeamNum == CsTeam.Terrorist && player.PlayerPawn.Value!.Health > 0)
{
aliveTs.Add(player);
}
}

if (aliveTs.Count == 1 && aliveCts.Count >= MinPlayers)
{
_clutchTeam = CsTeam.Terrorist;
_opponents = aliveCts.Count;

_clutchPlayer = aliveTs.First();
}
else if (aliveCts.Count == 1 && aliveTs.Count >= MinPlayers)
{
_clutchTeam = CsTeam.CounterTerrorist;
_opponents = aliveTs.Count;

_clutchPlayer = aliveCts.First();
}
}

// Helpers
private static bool IsValidPlayer(CCSPlayerController player)
{
return player.IsValid
&& player.PlayerPawn.IsValid
&& player.PlayerPawn.Value != null
&& player.PlayerPawn.Value.IsValid;
}
}
2 changes: 1 addition & 1 deletion TemplatePlugin.csproj → ClutchAnnouncePlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>TemplatePlugin</RootNamespace>
<RootNamespace>ClutchAnnouncePlugin</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# CS2 {PLUGIN_NAME}
{PLUGIN_NAME} plugin written in C# for CounterStrikeSharp

## Progress
- [x] These will
- [x] Help you
- [ ] Keep people
- [x] Up to date
- [ ] With your progress
# CS2 Clutch Announce
Clutch Announce plugin written in C# for CounterStrikeSharp

## Installation
1. Download the zip file from the latest release, and extract the contents into your `counterstrikesharp/plugins` directory.

## Setup for local development
Download the latest release of CounterStrikeSharp, and put the contents into the `CounterStrikeSharp` directory.

## Credits
Franc1sco Franug - For the original plugin
30 changes: 0 additions & 30 deletions TemplatePlugin.cs

This file was deleted.

0 comments on commit 81a7f7a

Please sign in to comment.