Skip to content

Commit

Permalink
fix: ListenersMerge pull request #4 from oscar-wos/1.3.1
Browse files Browse the repository at this point in the history
feat: TestPlugin
  • Loading branch information
oscar-wos committed Jun 2, 2024
2 parents 44a26d9 + dd2a9aa commit 88e9188
Show file tree
Hide file tree
Showing 10 changed files with 815 additions and 11 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
src/bin/
src/obj/
Sessions.API/bin/
Sessions.API/obj/
Sessions.API/obj/
TestPlugin/bin/
TestPlugin/obj/
675 changes: 675 additions & 0 deletions LICENSE.md

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Supports postgresql + mysql
`counterstrikesharp/configs/plugins/Sessions/Sessions.json`
```
{
"DatabaseType": "mysql",
}
{
"DatabaseType": "postgresql",
}
```
### Aliases
![image](https://github.com/oscar-wos/Sessions/assets/29248751/983c65eb-746a-4759-9b39-76efded4177b)

### Players
![image](https://github.com/oscar-wos/Sessions/assets/29248751/7edd304a-6b4c-4d22-98b5-54cd24cba64c)

### Messages
![image](https://github.com/oscar-wos/Sessions/assets/29248751/097d1018-9c78-418e-8583-1d454721a800)

### Sessions
![image](https://github.com/oscar-wos/Sessions/assets/29248751/278eadbd-e76c-4801-ab07-f9d4933955af)
![image](https://github.com/oscar-wos/Sessions/assets/29248751/331a4858-6611-476a-ba8e-ddd43fa6ede0)
4 changes: 2 additions & 2 deletions Sessions.API/Sessions.API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Server
{
public short Id { get; init; }
public Map? Map { get; init; }
public Map? Map { get; set; }
}

public class Map
Expand All @@ -14,7 +14,7 @@ public class Map
public class Player
{
public int Id { get; init; }
public Session? Session { get; init; }
public Session? Session { get; set; }
}

public class Session
Expand Down
10 changes: 8 additions & 2 deletions Sessions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34928.147
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sessions", "src\Sessions.csproj", "{94ACB340-F4BC-4913-88D6-4153C265DE13}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sessions", "src\Sessions.csproj", "{94ACB340-F4BC-4913-88D6-4153C265DE13}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sessions.API", "Sessions.API\Sessions.API.csproj", "{2EF1ABD8-B691-4C06-84F4-3BE8901DE14F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sessions.API", "Sessions.API\Sessions.API.csproj", "{2EF1ABD8-B691-4C06-84F4-3BE8901DE14F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestPlugin", "TestPlugin\TestPlugin.csproj", "{CB4816B2-D0B7-4B6F-8FAA-CC1611007E1E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +23,10 @@ Global
{2EF1ABD8-B691-4C06-84F4-3BE8901DE14F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2EF1ABD8-B691-4C06-84F4-3BE8901DE14F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2EF1ABD8-B691-4C06-84F4-3BE8901DE14F}.Release|Any CPU.Build.0 = Release|Any CPU
{CB4816B2-D0B7-4B6F-8FAA-CC1611007E1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CB4816B2-D0B7-4B6F-8FAA-CC1611007E1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB4816B2-D0B7-4B6F-8FAA-CC1611007E1E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB4816B2-D0B7-4B6F-8FAA-CC1611007E1E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
22 changes: 22 additions & 0 deletions TestPlugin/TestPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Capabilities;
using Sessions.API;

namespace TestPlugin;

public class TestPlugin : BasePlugin
{
public override string ModuleName => "TestPlugin";
public override string ModuleVersion => "1.0.0";

private static PlayerCapability<ISessionsPlayer> CapabilityPlayer { get; } = new("sessions:player");

public override void Load(bool isReload)
{
foreach (var session in Utilities.GetPlayers().Select(player => CapabilityPlayer.Get(player)!.Session))
{
Console.WriteLine(session!.Id);
}
}
}
14 changes: 14 additions & 0 deletions TestPlugin/TestPlugin.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="*" ExcludeAssets="runtime" />
<ProjectReference Include="..\Sessions.API\Sessions.API.csproj" />
</ItemGroup>

</Project>
70 changes: 66 additions & 4 deletions src/Sessions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,59 @@ public override void Load(bool isReload)

var ip = _ip.GetPublicIp();
var port = (ushort)ConVar.Find("hostport")!.GetPrimitiveValue<int>();

//#if DEBUG
Logger.LogInformation($"{nameof(Ip)}: ${ip}:${port}");
//#endif
Logger.LogInformation($"Ip: {ip}:{port}");

Database.CreateTablesAsync().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()
);

RegisterListener<Listeners.OnClientAuthorized>((playerSlot, steamId) =>
{
var player = Utilities.GetPlayerFromSlot(playerSlot);
if (!IsValidPlayer(player))
return;
OnPlayerConnect(playerSlot, steamId.SteamId64, NativeAPI.GetPlayerIpAddress(playerSlot).Split(":")[0]).GetAwaiter().GetResult();
CheckAlias(playerSlot, player!.PlayerName).GetAwaiter().GetResult();
});

RegisterListener<Listeners.OnClientDisconnect>(playerSlot =>
{
if (!Players.TryGetValue(playerSlot, out var value))
return;
Database.UpdateSeen(value.Id);
Players.Remove(playerSlot);
});

RegisterEventHandler<EventPlayerChat>((@event, _) =>
{
var player = Utilities.GetPlayerFromUserid(@event.Userid);
if (!IsValidPlayer(player) || !Players.TryGetValue(player!.Slot, out var value) || value.Session == null)
return HookResult.Continue;
var messageType = @event.Teamonly ? MessageType.TeamChat : MessageType.Chat;
Database.InsertMessage(value.Session.Id, value.Id, messageType, @event.Text);
return HookResult.Continue;
});

if (!isReload)
return;

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

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

private void Timer_Repeat()
Expand All @@ -48,6 +93,23 @@ private void Timer_Repeat()
Database.UpdateSessions(playerIds, sessionIds);
}

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);
}

private async Task CheckAlias(int playerSlot, string name)
{
if (!Players.TryGetValue(playerSlot, out var value) || value.Session == null)
return;

var recentAlias = await Database.GetAliasAsync(value.Id);

if (recentAlias == null || recentAlias.Name != name)
Database.InsertAlias(value.Session.Id, value.Id, name);
}

private static bool IsValidPlayer(CCSPlayerController? player)
{
return player != null && player is { IsValid: true, IsBot: false, IsHLTV: false };
Expand Down
2 changes: 1 addition & 1 deletion src/Sessions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Dapper" Version="*">
<PrivateAssets>false</PrivateAssets>
</PackageReference>
<PackageReference Include="Npgsql" Version="*" >
<PackageReference Include="Npgsql" Version="*">
<PrivateAssets>false</PrivateAssets>
</PackageReference>
<PackageReference Include="MySqlConnector" Version="*">
Expand Down
2 changes: 1 addition & 1 deletion src/SqlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task CreateTablesAsync()
}
}

public async Task<Server?> GetServerAsync(string serverIp, ushort serverPort)
public async Task<Server> GetServerAsync(string serverIp, ushort serverPort)
{
try
{
Expand Down

0 comments on commit 88e9188

Please sign in to comment.