Skip to content

Commit

Permalink
Auto-Format
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 8, 2024
1 parent e5f1992 commit abc8409
Show file tree
Hide file tree
Showing 20 changed files with 53 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Commands/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void
Server.NextFrameAsync(async () => {
var result = await ProcessCommand(wrapper, wrappedInfo);
if (result == CommandResult.PLAYER_ONLY)
executor?.PrintToChat(Locale.Get(GangsAPI.MSG.GENERIC_PLAYER_ONLY));
executor?.PrintToChat(Locale.Get(MSG.GENERIC_PLAYER_ONLY));
});
}
}
3 changes: 1 addition & 2 deletions Commands/GangCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ public async Task<CommandResult> Execute(PlayerWrapper? executor,
return CommandResult.SUCCESS;
}

if (!sub.TryGetValue(info[1], out var command)) {
if (!sub.TryGetValue(info[1], out var command))
return CommandResult.UNKNOWN_COMMAND;
}

var newInfo = new CommandInfoWrapper(info, 1);
return await command.Execute(executor, newInfo);
Expand Down
1 change: 0 additions & 1 deletion GangsAPI/Data/Command/CommandInfoWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CounterStrikeSharp.API.Modules.Commands;
using Microsoft.Extensions.Localization;

namespace GangsAPI.Data.Command;

Expand Down
1 change: 1 addition & 0 deletions GangsAPI/Data/ICacher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
public interface ICacher {
[Obsolete]
void ClearCache();

[Obsolete]
Task Load();
}
3 changes: 1 addition & 2 deletions GangsAPI/MSG.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GangsAPI.Data.Command;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Localization;

namespace GangsAPI;

Expand Down
3 changes: 1 addition & 2 deletions GangsAPI/Services/Gang/IGangManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GangsAPI.Data;
using GangsAPI.Data.Gang;
using GangsAPI.Data.Gang;

namespace GangsAPI.Services.Gang;

Expand Down
1 change: 0 additions & 1 deletion GangsAPI/Services/Gang/IGangStatManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using GangsAPI.Data;
using GangsAPI.Data.Gang;
using GangsAPI.Data.Stat;

Expand Down
3 changes: 1 addition & 2 deletions GangsAPI/Services/IStatManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GangsAPI.Data;
using GangsAPI.Data.Stat;
using GangsAPI.Data.Stat;

namespace GangsAPI.Services;

Expand Down
3 changes: 1 addition & 2 deletions GangsAPI/Services/Player/IPlayerManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GangsAPI.Data;
using GangsAPI.Data.Gang;
using GangsAPI.Data.Gang;

namespace GangsAPI.Services.Player;

Expand Down
32 changes: 16 additions & 16 deletions GangsImpl/AbstractDB/AbstractDBGangManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using GangsAPI.Data.Gang;
using GangsAPI.Services.Gang;
using GangsAPI.Services.Player;
using Mock;

namespace GenericDB;

Expand All @@ -13,8 +12,6 @@ public abstract class AbstractDBGangManager(IPlayerManager playerMgr,
: IGangManager {
protected DbConnection Connection = null!;
protected DbTransaction? Transaction;
public void ClearCache() { }
public Task Load() { return Task.CompletedTask; }

public void Start(BasePlugin? plugin, bool hotReload) {
Connection = CreateDbConnection(connectionString);
Expand All @@ -37,14 +34,6 @@ public void Start(BasePlugin? plugin, bool hotReload) {
}
}

abstract protected DbConnection CreateDbConnection(string connectionString);

virtual protected string CreateTableQuery(string tableName, bool inTesting) {
return inTesting ?
$"CREATE TEMPORARY TABLE IF NOT EXISTS {tableName} (GangId INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(255) NOT NULL)" :
$"CREATE TABLE IF NOT EXISTS {tableName} (GangId INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(255) NOT NULL)";
}

public async Task<IEnumerable<IGang>> GetGangs() {
var query = $"SELECT * FROM {table}";
return await Connection.QueryAsync<DBGang>(query, transaction: Transaction);
Expand Down Expand Up @@ -79,11 +68,6 @@ public async Task<bool> DeleteGang(int id) {
return await Connection.ExecuteAsync(query, new { id }, Transaction) > 0;
}

virtual protected async Task<int> GetLastId() {
return await Connection.ExecuteScalarAsync<int>("SELECT LAST_INSERT_ID()",
transaction: Transaction);
}

public async Task<IGang?> CreateGang(string name, ulong owner) {
var player = await playerMgr.GetPlayer(owner);
if (player == null) return null;
Expand All @@ -105,4 +89,20 @@ public void Dispose() {
Transaction?.Dispose();
Connection.Dispose();
}

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

abstract protected DbConnection CreateDbConnection(string connectionString);

virtual protected string CreateTableQuery(string tableName, bool inTesting) {
return inTesting ?
$"CREATE TEMPORARY TABLE IF NOT EXISTS {tableName} (GangId INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(255) NOT NULL)" :
$"CREATE TABLE IF NOT EXISTS {tableName} (GangId INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(255) NOT NULL)";
}

virtual protected async Task<int> GetLastId() {
return await Connection.ExecuteScalarAsync<int>("SELECT LAST_INSERT_ID()",
transaction: Transaction);
}
}
24 changes: 12 additions & 12 deletions GangsImpl/AbstractDB/AbstractDBPlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
using Dapper;
using GangsAPI.Data.Gang;
using GangsAPI.Services.Player;
using Mock;

namespace GenericDB;

public abstract class AbstractDBPlayerManager(string connectionString,
string table = "gang_players", bool testing = false) : IPlayerManager {
protected DbConnection Connection = null!;
protected DbTransaction? Transaction;
public void ClearCache() { }
public Task Load() { return Task.CompletedTask; }

public void Start(BasePlugin? plugin, bool hotReload) {
Connection = CreateDbConnection(connectionString);
Expand All @@ -35,14 +32,6 @@ public void Start(BasePlugin? plugin, bool hotReload) {
}
}

abstract protected DbConnection CreateDbConnection(string connectionString);

virtual protected string CreateTableQuery(string tableName, bool inTesting) {
return inTesting ?
$"CREATE TEMPORARY TABLE IF NOT EXISTS {tableName} (Steam INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(255), GangId INT, GangRank INT)" :
$"CREATE TABLE IF NOT EXISTS {tableName} (Steam INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(255), GangId INT, GangRank INT)";
}

public void Dispose() {
Transaction?.Dispose();
Connection.Dispose();
Expand All @@ -58,7 +47,7 @@ public async Task<IGangPlayer> CreatePlayer(ulong steamId,
string? name = null) {
var existing = await GetPlayer(steamId, false);
if (existing != null) return existing;
var player = new DBPlayer() { Steam = steamId, Name = name };
var player = new DBPlayer { Steam = steamId, Name = name };
var query = $"INSERT INTO {table} (Steam, Name) VALUES (@Steam, @Name)";
await Connection.ExecuteAsync(query, player, Transaction);
return player;
Expand Down Expand Up @@ -87,4 +76,15 @@ public async Task<bool> DeletePlayer(ulong steamId) {
return await Connection.ExecuteAsync(query, new { steamId }, Transaction)
== 1;
}

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

abstract protected DbConnection CreateDbConnection(string connectionString);

virtual protected string CreateTableQuery(string tableName, bool inTesting) {
return inTesting ?
$"CREATE TEMPORARY TABLE IF NOT EXISTS {tableName} (Steam INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(255), GangId INT, GangRank INT)" :
$"CREATE TABLE IF NOT EXISTS {tableName} (Steam INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(255), GangId INT, GangRank INT)";
}
}
6 changes: 3 additions & 3 deletions GangsImpl/Mock/MockGangManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public virtual async Task<bool> DeleteGang(int id) {
return gang.Clone() as IGang;
}

public virtual void Start(BasePlugin? plugin, bool hotReload) { }
public virtual void Dispose() { }

public virtual void ClearCache() { CachedGangs.Clear(); }

public virtual Task Load() {
CachedGangs.UnionWith(BackendGangs);
return Task.CompletedTask;
}

public virtual void Start(BasePlugin? plugin, bool hotReload) { }
public virtual void Dispose() { }
}
1 change: 0 additions & 1 deletion GangsImpl/SQLite/SQLiteGangManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Data.Common;
using Dapper;
using GangsAPI.Data.Gang;
using GangsAPI.Services.Player;
using GenericDB;
using Microsoft.Data.Sqlite;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

namespace GangsTest.API.Services.Commands.Command.Concrete;

public class GangTests(ICommandManager commands, IGangManager gangMgr, IStringLocalizer locale)
public class GangTests(ICommandManager commands, IGangManager gangMgr,
IStringLocalizer locale)
: TestParent(commands, new GangCommand(gangMgr, locale)) {
[Fact]
public async Task Gang_TestBase() {
Expand Down
1 change: 0 additions & 1 deletion GangsTest/API/Services/Gang/CreationTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using GangsAPI.Data.Gang;
using GangsAPI.Services.Gang;
using GangsAPI.Services.Player;
using MathNet.Numerics.Random;

namespace GangsTest.API.Services.Gang;

Expand Down
1 change: 0 additions & 1 deletion GangsTest/API/Services/Gang/PersistenceTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GangsAPI.Services.Gang;
using GangsAPI.Services.Player;

namespace GangsTest.API.Services.Gang;

Expand Down
4 changes: 1 addition & 3 deletions GangsTest/TestLocale/FormatTests.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.Text.RegularExpressions;
using GangsAPI;
using Microsoft.Extensions.Localization;
using Xunit.Abstractions;

namespace GangsTest.TestLocale;

public partial class FormatTests(IStringLocalizer localizer,
ITestOutputHelper output) {
public partial class FormatTests(IStringLocalizer localizer) {
[Fact]
public void Handles_Formatting() {
var perm = "@test/permission";
Expand Down
2 changes: 1 addition & 1 deletion GangsTest/TestLocale/LocaleImplData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace GangsTest.TestLocale;

public class LocaleImplData : TheoryData<string> {
public LocaleImplData() {
foreach (var key in Enum.GetValues<GangsAPI.MSG>()) { Add(key.Key()); }
foreach (var key in Enum.GetValues<MSG>()) Add(key.Key());
}
}
6 changes: 3 additions & 3 deletions GangsTest/TestLocale/ParityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
namespace GangsTest.TestLocale;

public class ParityTests(IStringLocalizer localizer) {
private readonly IEnumerable<string> localeKeys =
Enum.GetValues<MSG>().Select(s => s.Key());

[Theory]
[ClassData(typeof(LocaleImplData))]
public void Localize_Exists_FromImpl(string key) {
Expand All @@ -16,7 +19,4 @@ public void Localize_Exists_FromFile(string key, string _) {
// Make sure we have an enum whose key is the same as the key in the file
Assert.Contains(key, localeKeys);
}

private readonly IEnumerable<string> localeKeys =
Enum.GetValues<GangsAPI.MSG>().Select(s => s.Key());
}
15 changes: 8 additions & 7 deletions GangsTest/TestLocale/StringLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace GangsTest.TestLocale;

public partial class StringLocalizer : IStringLocalizer {
private readonly IStringLocalizer localizer;

internal static readonly StringLocalizer Instance =
new(new LocalFileLocalizerFactory());

private readonly IStringLocalizer localizer;

public StringLocalizer(IStringLocalizerFactory factory) {
var type = typeof(StringLocalizer);
var assemblyName =
Expand All @@ -22,23 +22,24 @@ public StringLocalizer(IStringLocalizerFactory factory) {
public LocalizedString this[string name, params object[] arguments]
=> new(name, string.Format(GetString(name).Value, arguments));

public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
=> localizer.GetAllStrings(includeParentCultures)
public IEnumerable<LocalizedString>
GetAllStrings(bool includeParentCultures) {
return localizer.GetAllStrings(includeParentCultures)
.Select(str => GetString(str.Name));
}

private LocalizedString GetString(string name) {
// Replace %[key]% with that key's value
// Eg: if we have a locale key of "prefix", then
// other locale values can use %prefix% to reference it.
var value = localizer[name].Value;
var matches = Percents().Matches(value);
foreach (Match match in matches) {
foreach (Match match in matches)
// Check if the key exists
try {
var key = match.Groups[0].Value;
value = value.Replace(key, localizer[key[1..^1]].Value);
} catch (NullReferenceException) { continue; }
}
} catch (NullReferenceException) { }

return new LocalizedString(name, value);
}
Expand Down

0 comments on commit abc8409

Please sign in to comment.