Skip to content

Commit

Permalink
Reorganize
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 7, 2024
1 parent de2fd89 commit 8dbbd98
Show file tree
Hide file tree
Showing 63 changed files with 649 additions and 561 deletions.
1 change: 1 addition & 0 deletions Commands/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using GangsAPI.Data.Command;
using GangsAPI.Services;
using GangsAPI.Services.Commands;
using GangsAPI.Services.Gang;
using Mock;

namespace Commands;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
using GangsAPI.Data.Command;
using GangsAPI.Services;
using GangsAPI.Services.Commands;
using GangsAPI.Services.Gang;

namespace Commands.gang;
namespace Commands.Gang;

// create [name]
public class CreateCommand(IGangManager gangs) : ICommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using GangsAPI.Data.Command;
using GangsAPI.Services.Commands;

namespace Commands.gang;
namespace Commands.Gang;

public class HelpCommand : ICommand {
public string Name => "help";
Expand Down
3 changes: 2 additions & 1 deletion Commands/GangCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Commands.gang;
using Commands.Gang;
using CounterStrikeSharp.API.Modules.Utils;
using GangsAPI.Data;
using GangsAPI.Data.Command;
using GangsAPI.Services;
using GangsAPI.Services.Commands;
using GangsAPI.Services.Gang;

namespace Commands;

Expand Down
4 changes: 3 additions & 1 deletion Gangs/GangServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using GangsAPI.Extensions;
using GangsAPI.Services;
using GangsAPI.Services.Commands;
using GangsAPI.Services.Gang;
using GangsAPI.Services.Player;
using Microsoft.Extensions.DependencyInjection;
using Mock;

Expand All @@ -12,7 +14,7 @@ public class GangServiceCollection : IPluginServiceCollection<CS2Gangs> {
public void ConfigureServices(IServiceCollection serviceCollection) {
serviceCollection.AddPluginBehavior<IGangManager, MockGangManager>();
serviceCollection.AddPluginBehavior<IPlayerManager, MockPlayerManager>();
serviceCollection.AddPluginBehavior<IStatManager, MockStatManager>();
serviceCollection.AddPluginBehavior<IStatManager, Creation>();
serviceCollection
.AddPluginBehavior<IGangStatManager, MockInstanceStatManager>();
serviceCollection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GangsAPI.Data;
using GangsAPI.Data.Gang;

namespace GangsAPI.Services;
namespace GangsAPI.Services.Gang;

/// <summary>
/// A manager for gangs. Allows for the creation, retrieval, updating, and deletion of gangs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using GangsAPI.Data.Gang;
using GangsAPI.Data.Stat;

namespace GangsAPI.Services;
namespace GangsAPI.Services.Gang;

public interface IGangStatManager : ICacher {
public interface IGangStatManager : IPluginBehavior, ICacher {
Task<(bool, TV?)> GetForGang<TV>(int key, string statId);
Task<bool> SetForGang<TV>(int gangId, string statId, TV value);
Task<bool> RemoveFromGang(int gangId, string statId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GangsAPI.Data;
using GangsAPI.Data.Gang;

namespace GangsAPI.Services;
namespace GangsAPI.Services.Player;

/// <summary>
/// A manager for players. Allows for the retrieval and creation of players.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
using GangsAPI.Data;
using GangsAPI.Data.Stat;

namespace GangsAPI.Services;
namespace GangsAPI.Services.Player;

public interface IPlayerStatManager : IPluginBehavior, ICacher {
Task<bool> GetForPlayer<TV>(ulong steam, string statId, out TV? result);
Task<(bool, TV?)> GetForPlayer<TV>(ulong steam, string statId);
Task<bool> SetForPlayer<TV>(ulong steam, string statId, TV value);
Task<bool> RemoveFromPlayer(ulong steam, string statId);

#region Aliases

#region Get

Task<bool> GetForPlayer<TV>(PlayerWrapper wrapper, string statId,
out TV? holder) {
return GetForPlayer(wrapper.Steam, statId, out holder);
}

async Task<bool> GetForPlayer<TV>(ulong steam, IStat<TV> holder) {
var success = await GetForPlayer(steam, holder.StatId, out TV? tmp);
if (!success || tmp == null) return false;
holder.Value = tmp;
return true;
}

Task<bool> GetForPlayer<TV>(PlayerWrapper wrapper, IStat<TV> holder) {
return GetForPlayer(wrapper.Steam, holder);
Task<(bool, TV?)> GetForPlayer<TV>(PlayerWrapper wrapper, string statId) {
return GetForPlayer<TV>(wrapper.Steam, statId);
}

#endregion
Expand Down
1 change: 1 addition & 0 deletions GangsImpl/AbstractDB/AbstractDBGangManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Dapper;
using GangsAPI.Data.Gang;
using GangsAPI.Services;
using GangsAPI.Services.Player;
using Mock;

namespace GenericDB;
Expand Down
2 changes: 1 addition & 1 deletion GangsImpl/AbstractDB/AbstractDBStatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace GenericDB;

public abstract class AbstractDBStatManager(string connectionString,
string table = "gang_stats", bool testing = false) : MockStatManager {
string table = "gang_stats", bool testing = false) : Creation {
private DbConnection connection = null!;
private DbTransaction? transaction;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,30 @@
using System.Reflection;
using CounterStrikeSharp.API.Core;
using Dapper;
using GangsAPI;
using GangsAPI.Services;
using MySqlConnector;

namespace SQLImpl;
namespace GenericDB;

public class SQLInstanceManager(string connectionString, string table_prefix,
string gangsTable, bool testing = false) : IPluginBehavior, IGangStatManager {
private DbConnection connection = null!;
public abstract class AbstractInstanceManager<TK>(string connectionString,

Check warning on line 9 in GangsImpl/AbstractDB/AbstractInstanceManager.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x)

Non-nullable field 'Connection' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 9 in GangsImpl/AbstractDB/AbstractInstanceManager.cs

View workflow job for this annotation

GitHub Actions / build (6.0.x)

Non-nullable field 'Connection' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 9 in GangsImpl/AbstractDB/AbstractInstanceManager.cs

View workflow job for this annotation

GitHub Actions / test (8.0.x)

Non-nullable field 'Connection' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 9 in GangsImpl/AbstractDB/AbstractInstanceManager.cs

View workflow job for this annotation

GitHub Actions / test (8.0.x)

Non-nullable field 'Connection' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 9 in GangsImpl/AbstractDB/AbstractInstanceManager.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Non-nullable field 'Connection' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 9 in GangsImpl/AbstractDB/AbstractInstanceManager.cs

View workflow job for this annotation

GitHub Actions / build (7.0.x)

Non-nullable field 'Connection' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 9 in GangsImpl/AbstractDB/AbstractInstanceManager.cs

View workflow job for this annotation

GitHub Actions / test (9.0.x)

Non-nullable field 'Connection' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 9 in GangsImpl/AbstractDB/AbstractInstanceManager.cs

View workflow job for this annotation

GitHub Actions / test (9.0.x)

Non-nullable field 'Connection' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 9 in GangsImpl/AbstractDB/AbstractInstanceManager.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

Non-nullable field 'Connection' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 9 in GangsImpl/AbstractDB/AbstractInstanceManager.cs

View workflow job for this annotation

GitHub Actions / build (8.0.x)

Non-nullable field 'Connection' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 9 in GangsImpl/AbstractDB/AbstractInstanceManager.cs

View workflow job for this annotation

GitHub Actions / build (9.0.x)

Non-nullable field 'Connection' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 9 in GangsImpl/AbstractDB/AbstractInstanceManager.cs

View workflow job for this annotation

GitHub Actions / build (9.0.x)

Non-nullable field 'Connection' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
string table_prefix, bool testing = false) {
protected DbConnection Connection;
abstract protected string PrimaryKey { get; }
private string primaryTypeString => GetDBType(typeof(TK));

public void ClearCache() { }

public Task Load() { return Task.CompletedTask; }

public async Task<(bool, TV?)> GetForGang<TV>(int key, string statId) {
public async Task<(bool, TV?)> Get<TV>(TK key, string statId) {
await createTable<TV>(statId);
try {
var result = await connection.QuerySingleAsync<TV>(
$"SELECT {(typeof(TV).IsPrimitive ? statId : getFieldNames<TV>())} FROM {table_prefix}_{statId} WHERE GangId = @GangId",
var result = await Connection.QuerySingleAsync<TV>(
$"SELECT {(typeof(TV).IsPrimitive ? statId : getFieldNames<TV>())} FROM {table_prefix}_{statId} WHERE {PrimaryKey} = @{PrimaryKey}",
new { GangId = key });
return (true, result);
} catch (InvalidOperationException) { return (false, default); }
}

public async Task<bool> SetForGang<TV>(int gangId, string statId, TV value) {
public async Task<bool> Set<TV>(TK key, string statId, TV value) {
await createTable<TV>(statId);
var fields = typeof(TV)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
Expand All @@ -45,12 +44,12 @@ public async Task<bool> SetForGang<TV>(int gangId, string statId, TV value) {
fields.Select(f => $"{f.Name} = @{f.Name}"));
if (typeof(TV).IsPrimitive) onDuplicate = $"{statId} = @{statId}";
var cmd =
$"INSERT INTO {table_prefix}_{statId} (GangId, {columns}) VALUES (@GangId, {values}) ON DUPLICATE KEY UPDATE {onDuplicate}";
$"INSERT INTO {table_prefix}_{statId} ({PrimaryKey}, {columns}) VALUES (@{PrimaryKey}, {values}) ON DUPLICATE KEY UPDATE {onDuplicate}";

Debug.WriteLine(cmd);

var fieldValues = new DynamicParameters();
fieldValues.Add("@GangId", gangId);
fieldValues.Add("@" + PrimaryKey, key);

if (typeof(TV).IsPrimitive)
fieldValues.Add($"@{statId}", value);
Expand All @@ -59,23 +58,25 @@ public async Task<bool> SetForGang<TV>(int gangId, string statId, TV value) {
fieldValues.Add($"@{field.Name}", field.GetValue(value));

Console.WriteLine(cmd, fieldValues);
await connection.ExecuteAsync(cmd, fieldValues);
await Connection.ExecuteAsync(cmd, fieldValues);
return true;
}

public async Task<bool> RemoveFromGang(int gangId, string statId) {
public async Task<bool> Remove(TK key, string statId) {
try {
await connection.ExecuteAsync(
$"DELETE FROM {table_prefix}_{statId} WHERE GangId = @GangId",
new { GangId = gangId });
var dynamicParameters = new DynamicParameters();
dynamicParameters.Add(PrimaryKey, key);
await Connection.ExecuteAsync(
$"DELETE FROM {table_prefix}_{statId} WHERE {PrimaryKey} = @{PrimaryKey}",
dynamicParameters);
return true;
} catch (DbException) { return false; }
}

public void Start(BasePlugin? plugin, bool hotReload) {
connection = new MySqlConnection(connectionString);
Connection = CreateDbConnection(connectionString);

connection.Open();
Connection.Open();
}

virtual protected string GetDBType(Type type) {
Expand Down Expand Up @@ -107,8 +108,8 @@ private async Task createTable<TV>(string id) {
.ToList();

var cmd = testing ?
$"CREATE TEMPORARY TABLE IF NOT EXISTS {table_prefix}_{id} (GangId INT NOT NULL PRIMARY KEY, " :
$"CREATE TABLE IF NOT EXISTS {table_prefix}_{id} (GangId INT NOT NULL PRIMARY KEY, ";
$"CREATE TEMPORARY TABLE IF NOT EXISTS {table_prefix}_{id} ({PrimaryKey} {primaryTypeString} NOT NULL PRIMARY KEY, " :
$"CREATE TABLE IF NOT EXISTS {table_prefix}_{id} ({PrimaryKey} {primaryTypeString} NOT NULL PRIMARY KEY, ";

if (typeof(TV).IsPrimitive) {
cmd += $"{id} {GetDBType(typeof(TV))}";
Expand All @@ -126,7 +127,7 @@ private async Task createTable<TV>(string id) {

Console.WriteLine(cmd);

await connection.ExecuteAsync(cmd);
await Connection.ExecuteAsync(cmd);
}

private string getFieldNames<TV>(string prefix = "") {
Expand All @@ -136,4 +137,6 @@ private string getFieldNames<TV>(string prefix = "") {

return string.Join(", ", fields.Select(f => $"{prefix}{f.Name}"));
}

abstract protected DbConnection CreateDbConnection(string connectionString);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Mock;

public class MockStatManager : IStatManager {
public class Creation : IStatManager {
private readonly HashSet<IStat> backendStats = [];
protected readonly HashSet<IStat> CachedStats = [];

Expand Down
2 changes: 1 addition & 1 deletion GangsImpl/Mock/MemoryImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace Mock;

public static class MemoryImpl {
public static void AddMemoryImpl(this IServiceCollection collection) {
collection.AddPluginBehavior<IStatManager, MockStatManager>();
collection.AddPluginBehavior<IStatManager, Creation>();
}
}
2 changes: 2 additions & 0 deletions GangsImpl/Mock/MockGangManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using CounterStrikeSharp.API.Core;
using GangsAPI.Data.Gang;
using GangsAPI.Services;
using GangsAPI.Services.Gang;
using GangsAPI.Services.Player;

namespace Mock;

Expand Down
16 changes: 8 additions & 8 deletions GangsImpl/Mock/MockInstanceStatManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using GangsAPI.Services;
using GangsAPI.Services.Gang;
using GangsAPI.Services.Player;

namespace Mock;

Expand All @@ -23,16 +25,14 @@ public Task Load() {
private readonly Dictionary<ulong, Dictionary<string, object>>
cachedPlayerValues = [], backendPlayerValues = [];

public Task<bool>
GetForPlayer<TV>(ulong steam, string statId, out TV? result) {
result = default;
public Task<(bool, TV?)> GetForPlayer<TV>(ulong steam, string statId) {
if (!cachedPlayerValues.TryGetValue(steam, out var playerStatMap))
return Task.FromResult(false);
return Task.FromResult<(bool, TV?)>((false, default));
if (!playerStatMap.TryGetValue(statId, out var value))
return Task.FromResult(false);
if (value is not TV v) return Task.FromResult(false);
result = v;
return Task.FromResult(true);
return Task.FromResult<(bool, TV?)>((false, default));
return value is not TV val ?
Task.FromResult<(bool, TV?)>((false, default)) :
Task.FromResult((true, (TV?)val));
}

public Task<bool> SetForPlayer<TV>(ulong steam, string statId, TV value) {
Expand Down
1 change: 1 addition & 0 deletions GangsImpl/Mock/MockPlayerManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GangsAPI.Data.Gang;
using GangsAPI.Services;
using GangsAPI.Services.Player;

namespace Mock;

Expand Down
55 changes: 55 additions & 0 deletions GangsImpl/SQL/SQLGangInstanceManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Data.Common;
using GangsAPI;
using GangsAPI.Services;
using GangsAPI.Services.Gang;
using GangsAPI.Services.Player;
using GenericDB;
using MySqlConnector;

namespace SQLImpl;

public class SQLGangInstanceManager(string connectionString,
string table_prefix, bool testing = false)
: AbstractInstanceManager<int>(connectionString, table_prefix, testing),
IPluginBehavior, IGangStatManager {
override protected string PrimaryKey => "GangId";

public Task<(bool, TV?)> GetForGang<TV>(int key, string statId) {
return Get<TV>(key, statId);
}

public Task<bool> SetForGang<TV>(int gangId, string statId, TV value) {
return Set(gangId, statId, value);
}

public Task<bool> RemoveFromGang(int gangId, string statId) {
return Remove(gangId, statId);
}

override protected DbConnection CreateDbConnection(string connectionString) {
return new MySqlConnection(connectionString);
}
}

public class SQLPlayerInstanceManager(string connectionString,
string table_prefix, bool testing = false)
: AbstractInstanceManager<ulong>(connectionString, table_prefix, testing),
IPlayerStatManager {
override protected string PrimaryKey => "GangId";

public Task<(bool, TV?)> GetForPlayer<TV>(ulong steam, string statId) {
return Get<TV>(steam, statId);
}

public Task<bool> SetForPlayer<TV>(ulong steam, string statId, TV value) {
return Set(steam, statId, value);
}

public Task<bool> RemoveFromPlayer(ulong steam, string statId) {
return Remove(steam, statId);
}

override protected DbConnection CreateDbConnection(string connectionString) {
return new MySqlConnection(connectionString);
}
}
1 change: 1 addition & 0 deletions GangsImpl/SQL/SQLGangManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Data.Common;
using GangsAPI.Services;
using GangsAPI.Services.Player;
using GenericDB;
using MySqlConnector;

Expand Down
1 change: 1 addition & 0 deletions GangsImpl/SQLite/SQLiteGangManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Dapper;
using GangsAPI.Data.Gang;
using GangsAPI.Services;
using GangsAPI.Services.Player;
using GenericDB;
using Microsoft.Data.Sqlite;

Expand Down
Loading

0 comments on commit 8dbbd98

Please sign in to comment.