From 87011f6ce68100a8ada2e2e41c5d286cc9e9ff49 Mon Sep 17 00:00:00 2001 From: MSWS Date: Wed, 30 Oct 2024 15:05:01 -0700 Subject: [PATCH] Begin work on leaderboard --- Gangs.sln | 7 +++++ Leaderboard/ILeaderboard.cs | 5 ++++ Leaderboard/Leaderboard.csproj | 19 +++++++++++++ Leaderboard/LeaderboardCollection.cs | 10 +++++++ Leaderboard/LeaderboardCommand.cs | 29 +++++++++++++++++++ Leaderboard/MSLeaderboard.cs | 39 ++++++++++++++++++++++++++ src/CS2/Gangs/GangServiceCollection.cs | 2 ++ src/CS2/Gangs/Gangs.csproj | 1 + 8 files changed, 112 insertions(+) create mode 100644 Leaderboard/ILeaderboard.cs create mode 100644 Leaderboard/Leaderboard.csproj create mode 100644 Leaderboard/LeaderboardCollection.cs create mode 100644 Leaderboard/LeaderboardCommand.cs create mode 100644 Leaderboard/MSLeaderboard.cs diff --git a/Gangs.sln b/Gangs.sln index ad4b554..734aef3 100644 --- a/Gangs.sln +++ b/Gangs.sln @@ -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 @@ -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} @@ -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 diff --git a/Leaderboard/ILeaderboard.cs b/Leaderboard/ILeaderboard.cs new file mode 100644 index 0000000..69a1537 --- /dev/null +++ b/Leaderboard/ILeaderboard.cs @@ -0,0 +1,5 @@ +namespace Leaderboard; + +public interface ILeaderboard { + Task> GetTopGangs(int limit = 10, int offset = 0); +} \ No newline at end of file diff --git a/Leaderboard/Leaderboard.csproj b/Leaderboard/Leaderboard.csproj new file mode 100644 index 0000000..8815eda --- /dev/null +++ b/Leaderboard/Leaderboard.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + ..\..\..\..\.nuget\packages\mysqlconnector\2.3.7\lib\net8.0\MySqlConnector.dll + + + + diff --git a/Leaderboard/LeaderboardCollection.cs b/Leaderboard/LeaderboardCollection.cs new file mode 100644 index 0000000..44d39c6 --- /dev/null +++ b/Leaderboard/LeaderboardCollection.cs @@ -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(); + } +} \ No newline at end of file diff --git a/Leaderboard/LeaderboardCommand.cs b/Leaderboard/LeaderboardCommand.cs new file mode 100644 index 0000000..edf55c9 --- /dev/null +++ b/Leaderboard/LeaderboardCommand.cs @@ -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 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; + } +} \ No newline at end of file diff --git a/Leaderboard/MSLeaderboard.cs b/Leaderboard/MSLeaderboard.cs new file mode 100644 index 0000000..4ee0c88 --- /dev/null +++ b/Leaderboard/MSLeaderboard.cs @@ -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(); + cmd.RegisterCommand(new LeaderboardCommand(this)); + } + + public async Task> 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; + } +} \ No newline at end of file diff --git a/src/CS2/Gangs/GangServiceCollection.cs b/src/CS2/Gangs/GangServiceCollection.cs index ef097e4..a69a081 100644 --- a/src/CS2/Gangs/GangServiceCollection.cs +++ b/src/CS2/Gangs/GangServiceCollection.cs @@ -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; @@ -43,6 +44,7 @@ public void ConfigureServices(IServiceCollection serviceCollection) { serviceCollection.RegisterPerks(); serviceCollection.RegisterRewards(); serviceCollection.RegisterRaffle(); + serviceCollection.RegisterLeaderboard(); serviceCollection.AddPluginBehavior(); serviceCollection diff --git a/src/CS2/Gangs/Gangs.csproj b/src/CS2/Gangs/Gangs.csproj index f122087..fe5d4d2 100644 --- a/src/CS2/Gangs/Gangs.csproj +++ b/src/CS2/Gangs/Gangs.csproj @@ -8,6 +8,7 @@ +