-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Begin work on leaderboard * Reorganize * Slight cleanup
- Loading branch information
Showing
8 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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="..\..\GangsAPI\GangsAPI.csproj"/> | ||
|
||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="MySqlConnector"> | ||
<HintPath>..\..\..\..\..\..\.nuget\packages\mysqlconnector\2.3.7\lib\net8.0\MySqlConnector.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => ["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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |