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 example program #8

Merged
merged 1 commit into from
Feb 7, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CounterStrike2GSI Example Program", "CounterStrike2GSI Example Program\CounterStrike2GSI Example Program.csproj", "{5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CounterStrike2GSI", "..\build\CounterStrike2GSI\CounterStrike2GSI.csproj", "{FD7228AA-698A-3716-8CFF-373A33438C95}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5ABA3AEF-9C06-4353-A906-FFE8FA879DA5}.Release|Any CPU.Build.0 = Release|Any CPU
{FD7228AA-698A-3716-8CFF-373A33438C95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD7228AA-698A-3716-8CFF-373A33438C95}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD7228AA-698A-3716-8CFF-373A33438C95}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD7228AA-698A-3716-8CFF-373A33438C95}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9DCC2CC9-9B14-47CC-82B9-3A5573C884F6}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>CounterStrike2GSI_Example_Program</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\build\CounterStrike2GSI\CounterStrike2GSI.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using CounterStrike2GSI;
using CounterStrike2GSI.EventMessages;

namespace CounterStrike2GSI_Example_program
{
class Program
{
static GameStateListener? _gsl;

static void Main(string[] args)
{
_gsl = new GameStateListener(4000);

if (!_gsl.GenerateGSIConfigFile("Example"))
{
Console.WriteLine("Could not generate GSI configuration file.");
}

// There are many callbacks that can be subscribed.
// This example shows a few.
_gsl.NewGameState += OnNewGameState;
_gsl.GameEvent += OnGameEvent;
_gsl.BombStateUpdated += OnBombStateUpdated;
_gsl.PlayerGotKill += OnPlayerGotKill;
_gsl.PlayerDied += OnPlayerDied;
_gsl.KillFeed += OnKillFeed;
_gsl.PlayerWeaponsPickedUp += OnPlayerWeaponsPickedUp;
_gsl.PlayerWeaponsDropped += OnPlayerWeaponsDropped;
_gsl.RoundStarted += OnRoundStarted;
_gsl.RoundConcluded += OnRoundConcluded;

if (!_gsl.Start())
{
Console.WriteLine("GameStateListener could not start. Try running this program as Administrator. Exiting.");
Console.ReadLine();
Environment.Exit(0);
}
Console.WriteLine("Listening for game integration calls...");

Console.WriteLine("Press ESC to quit");
do
{
while (!Console.KeyAvailable)
{
Thread.Sleep(1000);
}
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);
}

private static void OnNewGameState(GameState gamestate)
{
// Guaranteed to fire before CS2GameEvent events.
}

private static void OnGameEvent(CS2GameEvent game_event)
{
if (game_event is PlayerTookDamage player_took_damage)
{
Console.WriteLine($"The player {player_took_damage.Player.Name} took {player_took_damage.Previous - player_took_damage.New} damage!");
}
else if (game_event is PlayerActiveWeaponChanged active_weapon_changed)
{
Console.WriteLine($"The player {active_weapon_changed.Player.Name} changed their active weapon to {active_weapon_changed.New.Name} from {active_weapon_changed.Previous.Name}!");
}
}

private static void OnBombStateUpdated(BombStateUpdated game_event)
{
Console.WriteLine($"The bomb is now {game_event.New}.");
}

private static void OnPlayerGotKill(PlayerGotKill game_event)
{
Console.WriteLine($"The player {game_event.Player.Name} earned a {(game_event.IsHeadshot ? "headshot " : "")}kill with {game_event.Weapon.Name}!" + (game_event.IsAce ? " And it was an ACE!" : ""));
}

private static void OnPlayerDied(PlayerDied game_event)
{
Console.WriteLine($"The player {game_event.Player.Name} died.");
}

private static void OnKillFeed(KillFeed game_event)
{
Console.WriteLine($"{game_event.Killer.Name} killed {game_event.Victim.Name} with {game_event.Weapon.Name}{(game_event.IsHeadshot ? " as a headshot." : ".")}");
}

private static void OnPlayerWeaponsPickedUp(PlayerWeaponsPickedUp game_event)
{
Console.WriteLine($"The player {game_event.Player.Name} picked up the following weapons:");
foreach (var weapon in game_event.Weapons)
{
Console.WriteLine($"\t{weapon.Name}");
}
}

private static void OnPlayerWeaponsDropped(PlayerWeaponsDropped game_event)
{
Console.WriteLine($"The player {game_event.Player.Name} dropped the following weapons:");
foreach (var weapon in game_event.Weapons)
{
Console.WriteLine($"\t{weapon.Name}");
}
}

private static void OnRoundStarted(RoundStarted game_event)
{
if (game_event.IsFirstRound)
{
Console.WriteLine($"First round {game_event.Round} started.");
}
else if (game_event.IsLastRound)
{
Console.WriteLine($"Last round {game_event.Round} started.");
}
else
{
Console.WriteLine($"A new round {game_event.Round} started.");
}
}

private static void OnRoundConcluded(RoundConcluded game_event)
{
Console.WriteLine($"Round {game_event.Round} concluded by {game_event.WinningTeam} for reason: {game_event.RoundConclusionReason}");
}
}
}