Skip to content

Commit

Permalink
Begin work on leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Oct 30, 2024
1 parent ccfe5d7 commit 87011f6
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Gangs.sln
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EcoRewards", "src\EcoReward
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raffle", "src\CS2\Raffle\Raffle.csproj", "{05B36B8C-F430-411B-9B8D-61EB14D5E5FC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Leaderboard", "Leaderboard\Leaderboard.csproj", "{C45C866D-0F51-4C1A-80FD-BD2E6AB25EF5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -84,6 +86,10 @@ Global
{05B36B8C-F430-411B-9B8D-61EB14D5E5FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{05B36B8C-F430-411B-9B8D-61EB14D5E5FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{05B36B8C-F430-411B-9B8D-61EB14D5E5FC}.Release|Any CPU.Build.0 = Release|Any CPU
{C45C866D-0F51-4C1A-80FD-BD2E6AB25EF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C45C866D-0F51-4C1A-80FD-BD2E6AB25EF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C45C866D-0F51-4C1A-80FD-BD2E6AB25EF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C45C866D-0F51-4C1A-80FD-BD2E6AB25EF5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{74B15261-4B12-4EF6-859A-E46B315E7DD3} = {3AB7703F-880F-4A41-96EE-B891FA888C65}
Expand All @@ -97,5 +103,6 @@ Global
{B850CFA3-AFE8-4012-8BC2-9A4BC12B9748} = {AC07CD29-5C9D-4AD1-99C7-01DABAB8D0EC}
{253C7948-3411-4860-BDDE-B1CA23FCE4DC} = {AC07CD29-5C9D-4AD1-99C7-01DABAB8D0EC}
{05B36B8C-F430-411B-9B8D-61EB14D5E5FC} = {AC07CD29-5C9D-4AD1-99C7-01DABAB8D0EC}
{C45C866D-0F51-4C1A-80FD-BD2E6AB25EF5} = {AC07CD29-5C9D-4AD1-99C7-01DABAB8D0EC}
EndGlobalSection
EndGlobal
5 changes: 5 additions & 0 deletions Leaderboard/ILeaderboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Leaderboard;

public interface ILeaderboard {
Task<IEnumerable<(int, double)>> GetTopGangs(int limit = 10, int offset = 0);
}
19 changes: 19 additions & 0 deletions Leaderboard/Leaderboard.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<ProjectReference Include="..\src\GangsAPI\GangsAPI.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="MySqlConnector">
<HintPath>..\..\..\..\.nuget\packages\mysqlconnector\2.3.7\lib\net8.0\MySqlConnector.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions Leaderboard/LeaderboardCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using GangsAPI.Extensions;
using Microsoft.Extensions.DependencyInjection;

namespace Leaderboard;

public static class LeaderboardCollection {
public static void RegisterLeaderboard(this IServiceCollection provider) {
provider.AddPluginBehavior<ILeaderboard, MSLeaderboard>();
}
}
29 changes: 29 additions & 0 deletions Leaderboard/LeaderboardCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using GangsAPI.Data;
using GangsAPI.Data.Command;
using GangsAPI.Services.Commands;

namespace Leaderboard;

public class LeaderboardCommand(ILeaderboard leaderboard) : ICommand {
public string Name => "css_lb";
public string[] Aliases { get; } = ["css_lb", "css_leaderboard"];
private (int, double)[]? cachedLeaderboard;

public async Task<CommandResult> Execute(PlayerWrapper? executor,
CommandInfoWrapper info) {
cachedLeaderboard ??= (await leaderboard.GetTopGangs()).ToArray();

foreach (var (gangId, score) in cachedLeaderboard)
info.ReplySync($"Gang {gangId} has a score of {score}");

return CommandResult.SUCCESS;
}

[GameEventHandler]
public HookResult OnRoundStart(EventRoundStart ev, GameEventInfo info) {
cachedLeaderboard = null;
return HookResult.Continue;
}
}
39 changes: 39 additions & 0 deletions Leaderboard/MSLeaderboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Data;
using CounterStrikeSharp.API.Core;
using GangsAPI;
using GangsAPI.Data;
using GangsAPI.Services.Commands;
using Microsoft.Extensions.DependencyInjection;
using MySqlConnector;

namespace Leaderboard;

public class MSLeaderboard(IServiceProvider provider, IDBConfig config)
: ILeaderboard, IPluginBehavior {
public void Start(BasePlugin? plugin, bool hotReload) {
if (plugin == null) return;
var cmd = provider.GetRequiredService<ICommandManager>();
cmd.RegisterCommand(new LeaderboardCommand(this));
}

public async Task<IEnumerable<(int, double)>> GetTopGangs(int limit = 10,
int offset = 0) {
await using var connection = new MySqlConnection(config.ConnectionString);
await connection.OpenAsync();

var cmd = connection.CreateCommand();
cmd.CommandText =
$"SELECT GangId, Score FROM {config.TablePrefix}_leaderboard ORDER BY Score DESC LIMIT @limit OFFSET @offset";

cmd.Parameters.Add(new MySqlParameter("@limit", limit));
cmd.Parameters.Add(new MySqlParameter("@offset", offset));

await using var reader = await cmd.ExecuteReaderAsync();
var result = new List<(int, double)>();

while (await reader.ReadAsync())
result.Add((reader.GetInt32(0), reader.GetDouble(1)));

return result;
}
}
2 changes: 2 additions & 0 deletions src/CS2/Gangs/GangServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using GangsAPI.Services.Menu;
using GangsAPI.Services.Player;
using GangsAPI.Services.Server;
using Leaderboard;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Localization;
Expand Down Expand Up @@ -43,6 +44,7 @@ public void ConfigureServices(IServiceCollection serviceCollection) {
serviceCollection.RegisterPerks();
serviceCollection.RegisterRewards();
serviceCollection.RegisterRaffle();
serviceCollection.RegisterLeaderboard();

serviceCollection.AddPluginBehavior<PlayerJoinCreationListener>();
serviceCollection
Expand Down
1 change: 1 addition & 0 deletions src/CS2/Gangs/Gangs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Leaderboard\Leaderboard.csproj" />
<ProjectReference Include="..\..\..\Raffle\Raffle.csproj" />
<ProjectReference Include="..\..\EcoRewards\EcoRewards.csproj"/>
<ProjectReference Include="..\..\StatsTracker\StatsTracker.csproj"/>
Expand Down

0 comments on commit 87011f6

Please sign in to comment.