mirror of
https://github.com/edgegamers/Gangs.git
synced 2025-12-06 04:42:56 -08:00
Feat/leaderboard (#31)
* Begin work on leaderboard * Reorganize * Slight cleanup
This commit is contained in:
@@ -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", "src\CS2\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
|
||||
|
||||
@@ -8,6 +8,7 @@ using GangsAPI.Services.Gang;
|
||||
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 class GangServiceCollection : IPluginServiceCollection<CS2Gangs> {
|
||||
serviceCollection.RegisterPerks();
|
||||
serviceCollection.RegisterRewards();
|
||||
serviceCollection.RegisterRaffle();
|
||||
serviceCollection.RegisterLeaderboard();
|
||||
|
||||
serviceCollection.AddPluginBehavior<PlayerJoinCreationListener>();
|
||||
serviceCollection
|
||||
|
||||
@@ -8,12 +8,14 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\Leaderboard\Leaderboard.csproj" />
|
||||
<ProjectReference Include="..\..\..\Raffle\Raffle.csproj" />
|
||||
<ProjectReference Include="..\..\EcoRewards\EcoRewards.csproj"/>
|
||||
<ProjectReference Include="..\..\StatsTracker\StatsTracker.csproj"/>
|
||||
<ProjectReference Include="..\Commands\Commands.csproj"/>
|
||||
<ProjectReference Include="..\..\GangsAPI\GangsAPI.csproj"/>
|
||||
<ProjectReference Include="..\..\GangsImpl\MySQL\MySQL.csproj"/>
|
||||
<ProjectReference Include="..\Leaderboard\Leaderboard.csproj" />
|
||||
<ProjectReference Include="..\Raffle\Raffle.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
5
src/CS2/Leaderboard/ILeaderboard.cs
Normal file
5
src/CS2/Leaderboard/ILeaderboard.cs
Normal file
@@ -0,0 +1,5 @@
|
||||
namespace Leaderboard;
|
||||
|
||||
public interface ILeaderboard {
|
||||
Task<IEnumerable<(int, double)>> GetTopGangs(int limit = 10, int offset = 0);
|
||||
}
|
||||
19
src/CS2/Leaderboard/Leaderboard.csproj
Normal file
19
src/CS2/Leaderboard/Leaderboard.csproj
Normal file
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\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
src/CS2/Leaderboard/LeaderboardCollection.cs
Normal file
10
src/CS2/Leaderboard/LeaderboardCollection.cs
Normal file
@@ -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
src/CS2/Leaderboard/LeaderboardCommand.cs
Normal file
29
src/CS2/Leaderboard/LeaderboardCommand.cs
Normal file
@@ -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 => ["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
src/CS2/Leaderboard/MSLeaderboard.cs
Normal file
39
src/CS2/Leaderboard/MSLeaderboard.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user