Skip to content

Commit

Permalink
feat: CapabilityServer (server_id/map_id)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-wos committed Jun 2, 2024
1 parent 9d8d7a6 commit ce58638
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 10 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ src/bin/
src/obj/
Sessions.API/bin/
Sessions.API/obj/
TestPlugin/bin/
TestPlugin/bin
TestPlugin/obj/
6 changes: 6 additions & 0 deletions Sessions.API/ISessionsServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Sessions.API;

public interface ISessionsServer
{
Server? Server { get; }
}
27 changes: 24 additions & 3 deletions TestPlugin/TestPlugin.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,12 +12,32 @@ public class TestPlugin : BasePlugin
public override string ModuleVersion => "1.0.0";

private static PlayerCapability<ISessionsPlayer> CapabilityPlayer { get; } = new("sessions:player");
private static PluginCapability<ISessionsServer> 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<EventPlayerConnect>((@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);
}
}
4 changes: 2 additions & 2 deletions src/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, Player> Players = [];
Expand Down
8 changes: 4 additions & 4 deletions src/Sessions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Listeners.OnMapStart>(mapName =>
_server.Map = Database.GetMapAsync(mapName).GetAwaiter().GetResult()
Server.Map = Database.GetMapAsync(mapName).GetAwaiter().GetResult()
);

RegisterListener<Listeners.OnClientAuthorized>((playerSlot, steamId) =>
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/SessionsAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ namespace Sessions;
public partial class Sessions
{
private static PlayerCapability<ISessionsPlayer> CapabilityPlayer { get; } = new("sessions:player");
private static PluginCapability<ISessionsServer> CapabilityServer { get; } = new("sessions:server");

private void RegisterCapabilities()
{
Capabilities.RegisterPlayerCapability(CapabilityPlayer, player => new SessionsPlayer(player, this));
Capabilities.RegisterPluginCapability(CapabilityServer, () => new SessionsServer(this));
}
}

public class SessionsPlayer(CCSPlayerController player, Sessions plugin) : ISessionsPlayer
{
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;
}

0 comments on commit ce58638

Please sign in to comment.