From ce586383154258f18850f301592852c8084241df Mon Sep 17 00:00:00 2001 From: oscar <29248751+oscar-wos@users.noreply.github.com> Date: Sun, 2 Jun 2024 10:04:32 +0100 Subject: [PATCH] feat: CapabilityServer (server_id/map_id) --- .github/workflows/dotnet.yml | 8 ++++++++ .gitignore | 2 +- Sessions.API/ISessionsServer.cs | 6 ++++++ TestPlugin/TestPlugin.cs | 27 ++++++++++++++++++++++++--- src/Globals.cs | 4 ++-- src/Sessions.cs | 8 ++++---- src/SessionsAPI.cs | 7 +++++++ 7 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 Sessions.API/ISessionsServer.cs diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 2bff4bf..77aecdd 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -53,10 +53,18 @@ jobs: - name: Create Directories run: | mkdir -p plugin/plugins/Sessions + mkdir -p plugin/plugins/SessionsTest mkdir -p plugin/shared/Sessions.API + + mv ./TestPlugin/bin/Release/net8.0/TestPlugin.deps.json ./TestPlugin/bin/Release/net8.0/SessionsTest.deps.json + mv ./TestPlugin/bin/Release/net8.0/TestPlugin.dll ./TestPlugin/bin/Release/net8.0/SessionsTest.dll + mv ./TestPlugin/bin/Release/net8.0/TestPlugin.pdb ./TestPlugin/bin/Release/net8.0/SessionsTest.pdb + mv ./TestPlugin/bin/Release/net8.0/* ./plugin/plugins/SessionsTest mv ./src/bin/Release/net8.0/* ./plugin/plugins/Sessions mv ./Sessions.API/bin/Release/net8.0/* ./plugin/shared/Sessions.API + rm ./plugin/plugins/Sessions/Sessions.API.* + rm ./plugin/plugins/SessionsTest/Sessions.API.* - name: Zip run: | diff --git a/.gitignore b/.gitignore index 857ac3d..c213ef1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,5 @@ src/bin/ src/obj/ Sessions.API/bin/ Sessions.API/obj/ -TestPlugin/bin/ +TestPlugin/bin TestPlugin/obj/ \ No newline at end of file diff --git a/Sessions.API/ISessionsServer.cs b/Sessions.API/ISessionsServer.cs new file mode 100644 index 0000000..5aaaff3 --- /dev/null +++ b/Sessions.API/ISessionsServer.cs @@ -0,0 +1,6 @@ +namespace Sessions.API; + +public interface ISessionsServer +{ + Server? Server { get; } +} \ No newline at end of file diff --git a/TestPlugin/TestPlugin.cs b/TestPlugin/TestPlugin.cs index e1a4ac8..cba5fa4 100644 --- a/TestPlugin/TestPlugin.cs +++ b/TestPlugin/TestPlugin.cs @@ -1,6 +1,7 @@ using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core.Capabilities; +using Microsoft.Extensions.Logging; using Sessions.API; namespace TestPlugin; @@ -11,12 +12,32 @@ public class TestPlugin : BasePlugin public override string ModuleVersion => "1.0.0"; private static PlayerCapability CapabilityPlayer { get; } = new("sessions:player"); + private static PluginCapability CapabilityServer { get; } = new("sessions:server"); public override void Load(bool isReload) { - foreach (var session in Utilities.GetPlayers().Select(player => CapabilityPlayer.Get(player)!.Session)) + AddCommand("css_test", "test", (controller, _) => { - Console.WriteLine(session!.Id); - } + if (controller == null) + return; + + var server = CapabilityServer.Get()!.Server; + var player = CapabilityPlayer.Get(controller)!.Player; + var session = CapabilityPlayer.Get(controller)!.Session; + + Logger.LogInformation($"Server: {server!.Id} - Map: ${server!.Map!.Id} - Player: {player!.Id} - Session: {session!.Id}"); + }); + + RegisterEventHandler((@event, _) => + { + if (@event.Userid == null) + return HookResult.Continue; + + var controller = @event.Userid; + var player = CapabilityPlayer.Get(controller)!.Player; + + Logger.LogInformation($"Player {player!.Id} connected"); + return HookResult.Continue; + }, HookMode.Pre); } } \ No newline at end of file diff --git a/src/Globals.cs b/src/Globals.cs index 229943b..f25c389 100644 --- a/src/Globals.cs +++ b/src/Globals.cs @@ -7,9 +7,9 @@ public partial class Sessions public SessionsConfig Config { get; set; } = new(); public override string ModuleName => "Sessions"; public override string ModuleAuthor => "github.com/oscar-wos/Sessions"; - public override string ModuleVersion => "1.3.1"; + public override string ModuleVersion => "1.3.2"; - private Server? _server; + public Server? Server; private readonly Ip _ip = new(); public required IDatabase Database; public readonly Dictionary Players = []; diff --git a/src/Sessions.cs b/src/Sessions.cs index bb672f1..47d603f 100644 --- a/src/Sessions.cs +++ b/src/Sessions.cs @@ -22,11 +22,11 @@ public override void Load(bool isReload) Logger.LogInformation($"Ip: {ip}:{port}"); Database.CreateTablesAsync().GetAwaiter().GetResult(); - _server = Database.GetServerAsync(ip, port).GetAwaiter().GetResult(); + Server = Database.GetServerAsync(ip, port).GetAwaiter().GetResult(); AddTimer(1.0f, Timer_Repeat, TimerFlags.REPEAT); RegisterListener(mapName => - _server.Map = Database.GetMapAsync(mapName).GetAwaiter().GetResult() + Server.Map = Database.GetMapAsync(mapName).GetAwaiter().GetResult() ); RegisterListener((playerSlot, steamId) => @@ -65,7 +65,7 @@ public override void Load(bool isReload) if (!isReload) return; - _server.Map = Database.GetMapAsync(Server.MapName).GetAwaiter().GetResult(); + Server.Map = Database.GetMapAsync(CounterStrikeSharp.API.Server.MapName).GetAwaiter().GetResult(); foreach (var player in Utilities.GetPlayers().Where(IsValidPlayer)) { @@ -96,7 +96,7 @@ private void Timer_Repeat() private async Task OnPlayerConnect(int playerSlot, ulong steamId, string ip) { Players[playerSlot] = await Database.GetPlayerAsync(steamId); - Players[playerSlot].Session = await Database.GetSessionAsync(Players[playerSlot].Id, _server!.Id, _server!.Map!.Id, ip); + Players[playerSlot].Session = await Database.GetSessionAsync(Players[playerSlot].Id, Server!.Id, Server!.Map!.Id, ip); } private async Task CheckAlias(int playerSlot, string name) diff --git a/src/SessionsAPI.cs b/src/SessionsAPI.cs index add42d8..49737ba 100644 --- a/src/SessionsAPI.cs +++ b/src/SessionsAPI.cs @@ -7,10 +7,12 @@ namespace Sessions; public partial class Sessions { private static PlayerCapability CapabilityPlayer { get; } = new("sessions:player"); + private static PluginCapability CapabilityServer { get; } = new("sessions:server"); private void RegisterCapabilities() { Capabilities.RegisterPlayerCapability(CapabilityPlayer, player => new SessionsPlayer(player, this)); + Capabilities.RegisterPluginCapability(CapabilityServer, () => new SessionsServer(this)); } } @@ -18,4 +20,9 @@ public class SessionsPlayer(CCSPlayerController player, Sessions plugin) : ISess { public Player? Player { get; } = plugin.Players.TryGetValue(player.Slot, out var value) ? value : null; public Session? Session => Player?.Session; +} + +public class SessionsServer(Sessions plugin) : ISessionsServer +{ + public Server? Server => plugin.Server; } \ No newline at end of file