Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-wos committed Jul 22, 2024
1 parent 592c19d commit 50adb79
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 27 deletions.
6 changes: 6 additions & 0 deletions Sessions.API/IEventSender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Sessions.API;

public interface IEventSender
{
public event EventHandler<Player> PlayerConnected;
}
4 changes: 4 additions & 0 deletions Sessions.API/PlayerConnectedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using CounterStrikeSharp.API.Core;
using Sessions.API;

public record PlayerConnectedEvent(CCSPlayerController Controller, Player Player);
2 changes: 1 addition & 1 deletion src/DatabaseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public DatabaseFactory(SessionsConfig config, Sessions plugin)

Database = config.DatabaseType switch
{
"postgres" => new PostgresService(config, logger),
"pgsql" => new PostgresService(config, logger),
"mysql" => new SqlService(config, logger),
_ => throw new InvalidOperationException("Database type is not supported"),
};
Expand Down
4 changes: 2 additions & 2 deletions src/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ namespace Sessions;

public partial class Sessions
{
private readonly Ip _ip = new();
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.4";
public override string ModuleVersion => "1.3.5";

public Server? Server;
private readonly Ip _ip = new();
public required IDatabase Database;
public readonly Dictionary<int, Player> Players = [];
}
4 changes: 2 additions & 2 deletions src/Listeners.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ private void OnClientAuthorized(int playerSlot, SteamID steamId)
if (!IsValidPlayer(controller))
return;

OnPlayerConnect(playerSlot, steamId.SteamId64, NativeAPI.GetPlayerIpAddress(playerSlot).Split(":")[0]).GetAwaiter().GetResult();
CheckAlias(playerSlot, controller!.PlayerName).GetAwaiter().GetResult();
PlayerConnect(playerSlot, steamId.SteamId64, controller!.IpAddress!.Split(":")[0]).GetAwaiter().GetResult();
CheckAlias(playerSlot, controller.PlayerName).GetAwaiter().GetResult();
}

private void OnClientDisconnect(int playerSlot)
Expand Down
29 changes: 13 additions & 16 deletions src/Sessions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ public void OnConfigParsed(SessionsConfig config)
}

public override void Load(bool isReload)
{
RegisterCapabilities();
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<Listeners.OnClientAuthorized>(OnClientAuthorized);
RegisterListener<Listeners.OnClientDisconnect>(OnClientDisconnect);
RegisterEventHandler<EventPlayerChat>(OnPlayerChat);
AddTimer(1.0f, Timer_Repeat, TimerFlags.REPEAT);
}

public override void OnAllPluginsLoaded(bool isReload)
{
var ip = _ip.GetPublicIp()!;
var port = (ushort)ConVar.Find("hostport")!.GetPrimitiveValue<int>();
Expand All @@ -33,15 +23,22 @@ public override void OnAllPluginsLoaded(bool isReload)
Server.Port = port;
Server.MapName = CounterStrikeSharp.API.Server.MapName;

RegisterCapabilities();
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<Listeners.OnClientAuthorized>(OnClientAuthorized);
RegisterListener<Listeners.OnClientDisconnect>(OnClientDisconnect);
RegisterEventHandler<EventPlayerChat>(OnPlayerChat);
AddTimer(1.0f, Timer_Repeat, TimerFlags.REPEAT);

if (!isReload)
return;

Server.Map = Database.GetMapAsync(Server.MapName).GetAwaiter().GetResult();

foreach (var player in Utilities.GetPlayers().Where(IsValidPlayer))
foreach (var controller in Utilities.GetPlayers().Where(IsValidPlayer))
{
OnPlayerConnect(player.Slot, player.AuthorizedSteamID!.SteamId64, NativeAPI.GetPlayerIpAddress(player.Slot).Split(":")[0]).GetAwaiter().GetResult();
CheckAlias(player.Slot, player.PlayerName).GetAwaiter().GetResult();
PlayerConnect(controller.Slot, controller.AuthorizedSteamID!.SteamId64, controller.IpAddress!.Split(":")[0]).GetAwaiter().GetResult();
CheckAlias(controller.Slot, controller.PlayerName).GetAwaiter().GetResult();
}
}

Expand All @@ -50,9 +47,9 @@ private void Timer_Repeat()
List<int> playerIds = [];
List<long> sessionIds = [];

foreach (var player in Utilities.GetPlayers())
foreach (var controller in Utilities.GetPlayers().Where(IsValidPlayer))
{
if (!IsValidPlayer(player) || !Players.TryGetValue(player.Slot, out var value))
if (!Players.TryGetValue(controller.Slot, out var value))
continue;

playerIds.Add(value.Id);
Expand All @@ -64,7 +61,7 @@ private void Timer_Repeat()
Database.UpdateSessions(playerIds, sessionIds);
}

private async Task OnPlayerConnect(int playerSlot, ulong steamId, string ip)
private async Task PlayerConnect(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);
Expand Down
22 changes: 17 additions & 5 deletions src/SessionsAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,33 @@ public partial class Sessions
{
private static PlayerCapability<ISessionsPlayer> CapabilityPlayer { get; } = new("sessions:player");
private static PluginCapability<ISessionsServer> CapabilityServer { get; } = new("sessions:server");
private static PlayerCapability<PlayerConnectedEvent> CapabilityPlayerConnected { get; } = new("sessions:player_connected");

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

public class SessionsPlayer(CCSPlayerController player, Sessions plugin) : ISessionsPlayer
public class SessionsPlayer(CCSPlayerController controller, Sessions plugin) : ISessionsPlayer
{
public Player? Player { get; } = plugin.Players.TryGetValue(player.Slot, out var value) ? value : null;
public Session? Session => Player?.Session;
public Player? Player => plugin.Players.TryGetValue(controller.Slot, out var value) ? value : null;
public Session? Session => Player?.Session ?? null;
}

public class SessionsServer(Sessions plugin) : ISessionsServer
{
public Server? Server => plugin.Server;
public Server? Server => plugin.Server ?? null;
}

public class PlayerConnectedEvent(CCSPlayerController controller)

Check warning on line 32 in src/SessionsAPI.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'controller' is unread.

Check warning on line 32 in src/SessionsAPI.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'controller' is unread.

Check warning on line 32 in src/SessionsAPI.cs

View workflow job for this annotation

GitHub Actions / release

Parameter 'controller' is unread.

Check warning on line 32 in src/SessionsAPI.cs

View workflow job for this annotation

GitHub Actions / release

Parameter 'controller' is unread.
{
public event EventHandler<Player>? PlayerConnected;

public void TriggerEvent(CCSPlayerController controller, Player player)
{
PlayerConnected?.Invoke(this, player);
}
}
1 change: 0 additions & 1 deletion src/SqlService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Dapper;
using Microsoft.Extensions.Logging;
using MySqlConnector;
using Npgsql;
using Sessions.API;

namespace Sessions;
Expand Down

0 comments on commit 50adb79

Please sign in to comment.