Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 6, 2024
1 parent 44876ce commit dc0921f
Show file tree
Hide file tree
Showing 29 changed files with 118 additions and 113 deletions.
7 changes: 3 additions & 4 deletions Commands/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@

namespace Commands;

public class CommandManager(IGangManager gangMgr, IPlayerManager playerMgr)
public class CommandManager(IGangManager gangMgr)
: MockCommandManager, IPluginBehavior {
private BasePlugin? plugin;

public void Start(BasePlugin? basePlugin, bool hotReload) {
plugin = basePlugin;

RegisterCommand(new GangCommand(gangMgr, playerMgr));
RegisterCommand(new GangCommand(gangMgr));
}

public override bool RegisterCommand(ICommand command) {
var result = base.RegisterCommand(command);
if (result == false) return false;
foreach (var alias in command.Aliases) {
foreach (var alias in command.Aliases)
plugin?.AddCommand(alias, command.Description ?? string.Empty,
(player, info) => {
var wrapper = player == null ? null : new PlayerWrapper(player);
Expand All @@ -31,7 +31,6 @@ public override bool RegisterCommand(ICommand command) {
await ProcessCommand(wrapper, wrappedInfo);
});
});
}

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion Commands/GangCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Commands;

public class GangCommand(IGangManager gangMgr, IPlayerManager playerMgr) : ICommand {
public class GangCommand(IGangManager gangMgr) : ICommand {
private readonly Dictionary<string, ICommand> sub = new() {
// ["delete"] = new DeleteGangCommand(),
// ["invite"] = new InviteGangCommand(),
Expand Down
2 changes: 1 addition & 1 deletion Commands/gang/CreateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Commands.gang;
// create [name]
public class CreateCommand(IGangManager gangs) : ICommand {
public string Name => "create";
public string? Description => "Creates a new gang";
public string Description => "Creates a new gang";
public string Usage => "[name]";

public async Task<CommandResult> Execute(PlayerWrapper? executor,
Expand Down
2 changes: 1 addition & 1 deletion Commands/gang/HelpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Commands.gang;

public class HelpCommand : ICommand {
public string Name => "help";
public string? Description => "Displays help for gangs";
public string Description => "Displays help for gangs";

public Task<CommandResult> Execute(PlayerWrapper? executor,
CommandInfoWrapper info) {
Expand Down
1 change: 0 additions & 1 deletion Gangs/GangServiceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using GangsAPI.Services.Commands;
using Microsoft.Extensions.DependencyInjection;
using Mock;
using SQLImpl;

namespace GangsImpl;

Expand Down
6 changes: 3 additions & 3 deletions GangsAPI/Data/Command/CommandInfoWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Commands;

namespace GangsAPI.Data.Command;

public class CommandInfoWrapper(PlayerWrapper? executor, int offset = 0,
params string[] args) {
private readonly string[] args = args;

public readonly CommandCallingContext CallingContext =
CommandCallingContext.Console;

public readonly PlayerWrapper? CallingPlayer = executor;
private readonly string[] args = args;

public CommandInfoWrapper(CommandInfo info, int offset = 0) : this(
info.CallingPlayer == null ? null : new PlayerWrapper(info.CallingPlayer),
Expand Down
5 changes: 1 addition & 4 deletions GangsAPI/Data/Gang/IGang.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.Collections;
using GangsAPI.Permissions;

namespace GangsAPI.Data.Gang;
namespace GangsAPI.Data.Gang;

/// <summary>
/// Represents an instance of a gang.
Expand Down
4 changes: 1 addition & 3 deletions GangsAPI/Data/Gang/IGangPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using GangsAPI.Permissions;

namespace GangsAPI.Data.Gang;
namespace GangsAPI.Data.Gang;

/// <summary>
/// A gang player is a player tracked by the gangs plugin.
Expand Down
40 changes: 24 additions & 16 deletions GangsAPI/Services/IGangStatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public interface IGangStatManager : ICacher {

#region Get

Task<bool> GetForGang<TV>(IGang gang, string statId, out TV? holder)
=> GetForGang(gang.GangId, statId, out holder);
Task<bool> GetForGang<TV>(IGang gang, string statId, out TV? holder) {
return GetForGang(gang.GangId, statId, out holder);
}

async Task<bool> GetForGang<TV>(IGang gang, IStat<TV> holder) {
var success = await GetForGang(gang, holder.StatId, out TV? tmp);
Expand All @@ -23,34 +24,41 @@ async Task<bool> GetForGang<TV>(IGang gang, IStat<TV> holder) {
return true;
}

Task<bool> GetForGang<TV>(IGang gang, IStat<TV> stat, out TV? holder)
=> GetForGang(gang, stat.StatId, out holder);
Task<bool> GetForGang<TV>(IGang gang, IStat<TV> stat, out TV? holder) {
return GetForGang(gang, stat.StatId, out holder);
}

#endregion

#region Set

Task<bool> SetForGang<TV>(int gangId, IStat<TV> stat)
=> SetForGang(gangId, stat.StatId, stat.Value);
Task<bool> SetForGang<TV>(int gangId, IStat<TV> stat) {
return SetForGang(gangId, stat.StatId, stat.Value);
}

Task<bool> SetForGang<TV>(IGang gang, IStat<TV> stat)
=> SetForGang(gang.GangId, stat);
Task<bool> SetForGang<TV>(IGang gang, IStat<TV> stat) {
return SetForGang(gang.GangId, stat);
}

Task<bool> SetForGang<TV>(IGang gang, string statId, TV value)
=> SetForGang(gang.GangId, statId, value);
Task<bool> SetForGang<TV>(IGang gang, string statId, TV value) {
return SetForGang(gang.GangId, statId, value);
}

#endregion

#region Remove

Task<bool> RemoveFromGang(int gangId, IStat stat)
=> RemoveFromGang(gangId, stat.StatId);
Task<bool> RemoveFromGang(int gangId, IStat stat) {
return RemoveFromGang(gangId, stat.StatId);
}

Task<bool> RemoveFromGang(IGang gang, string statId)
=> RemoveFromGang(gang.GangId, statId);
Task<bool> RemoveFromGang(IGang gang, string statId) {
return RemoveFromGang(gang.GangId, statId);
}

Task<bool> RemoveFromGang(IGang gang, IStat stat)
=> RemoveFromGang(gang, stat.StatId);
Task<bool> RemoveFromGang(IGang gang, IStat stat) {
return RemoveFromGang(gang, stat.StatId);
}

#endregion

Expand Down
2 changes: 1 addition & 1 deletion GangsAPI/Services/IPlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface IPlayerManager : IPluginBehavior, ICacher {
/// <param name="name">The name of the player.</param>
/// <returns>The new player.</returns>
Task<IGangPlayer> CreatePlayer(ulong steamId, string? name = null);

Task<bool> UpdatePlayer(IGangPlayer player);

/// <summary>
Expand Down
40 changes: 24 additions & 16 deletions GangsAPI/Services/IPlayerStatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ public interface IPlayerStatManager : IPluginBehavior, ICacher {
#region Get

Task<bool> GetForPlayer<TV>(PlayerWrapper wrapper, string statId,
out TV? holder)
=> GetForPlayer(wrapper.Steam, statId, out holder);
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);
Expand All @@ -23,34 +24,41 @@ async Task<bool> GetForPlayer<TV>(ulong steam, IStat<TV> holder) {
return true;
}

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

#endregion

#region Set

Task<bool> SetForPlayer<TV>(ulong steam, IStat<TV> stat)
=> SetForPlayer(steam, stat.StatId, stat.Value);
Task<bool> SetForPlayer<TV>(ulong steam, IStat<TV> stat) {
return SetForPlayer(steam, stat.StatId, stat.Value);
}

Task<bool> SetForPlayer<TV>(PlayerWrapper wrapper, IStat<TV> stat)
=> SetForPlayer(wrapper.Steam, stat);
Task<bool> SetForPlayer<TV>(PlayerWrapper wrapper, IStat<TV> stat) {
return SetForPlayer(wrapper.Steam, stat);
}

Task<bool> SetForPlayer<TV>(PlayerWrapper wrapper, string statId, TV value)
=> SetForPlayer(wrapper.Steam, statId, value);
Task<bool> SetForPlayer<TV>(PlayerWrapper wrapper, string statId, TV value) {
return SetForPlayer(wrapper.Steam, statId, value);
}

#endregion

#region Remove

Task<bool> RemoveFromPlayer(ulong steam, IStat stat)
=> RemoveFromPlayer(steam, stat.StatId);
Task<bool> RemoveFromPlayer(ulong steam, IStat stat) {
return RemoveFromPlayer(steam, stat.StatId);
}

Task<bool> RemoveFromPlayer(PlayerWrapper wrapper, string statId)
=> RemoveFromPlayer(wrapper.Steam, statId);
Task<bool> RemoveFromPlayer(PlayerWrapper wrapper, string statId) {
return RemoveFromPlayer(wrapper.Steam, statId);
}

Task<bool> RemoveFromPlayer(PlayerWrapper wrapper, IStat stat)
=> RemoveFromPlayer(wrapper, stat.StatId);
Task<bool> RemoveFromPlayer(PlayerWrapper wrapper, IStat stat) {
return RemoveFromPlayer(wrapper, stat.StatId);
}

#endregion

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

namespace GenericDB;

public abstract class AbstractDBGangManager(IPlayerManager playerMgr, string connectionString,
string table = "gang_gangs", bool testing = false) : MockGangManager(playerMgr) {
public abstract class AbstractDBGangManager(IPlayerManager playerMgr,
string connectionString, string table = "gang_gangs", bool testing = false)
: MockGangManager(playerMgr) {
protected DbConnection Connection = null!;
protected DbTransaction? Transaction;
public override void ClearCache() { CachedGangs.Clear(); }
Expand Down Expand Up @@ -49,7 +49,7 @@ public override async Task<bool> UpdateGang(IGang gang) {
var result = await base.UpdateGang(gang);
var query = $"UPDATE {table} SET Name = @Name WHERE GangId = @GangId";
await Connection.ExecuteAsync(query, new { gang.Name, gang.GangId },
transaction: Transaction);
Transaction);
return result;
}

Expand Down
1 change: 0 additions & 1 deletion GangsImpl/AbstractDB/DBGang.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using GangsAPI.Data.Gang;
using GangsAPI.Permissions;
using Mock;

namespace GenericDB;
Expand Down
12 changes: 6 additions & 6 deletions GangsImpl/Mock/MockCommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public bool UnregisterCommand(ICommand command) {
return command.Aliases.All(alias => commands.Remove(alias));
}

public async Task<CommandResult> ProcessCommand(PlayerWrapper? executor,
params string[] args) {
var info = new CommandInfoWrapper(executor, 0, args);
return await ProcessCommand(executor, info);
}

public async Task<CommandResult> ProcessCommand(PlayerWrapper? executor,
CommandInfo sourceInfo) {
var info = new CommandInfoWrapper(sourceInfo);
Expand Down Expand Up @@ -44,10 +50,4 @@ await Task.Run(async () => {

return result;
}

public async Task<CommandResult> ProcessCommand(PlayerWrapper? executor,
params string[] args) {
var info = new CommandInfoWrapper(executor, 0, args);
return await ProcessCommand(executor, info);
}
}
2 changes: 0 additions & 2 deletions GangsImpl/Mock/MockGang.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using GangsAPI.Data.Gang;
using GangsAPI.Data.Stat;
using GangsAPI.Permissions;

namespace Mock;

Expand Down
36 changes: 18 additions & 18 deletions GangsImpl/Mock/MockInstanceStatManager.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
using GangsAPI.Data.Stat;
using GangsAPI.Services;
using GangsAPI.Services;

namespace Mock;

public class MockInstanceStatManager : IPlayerStatManager, IGangStatManager {
public void ClearCache() {
cachedGangValues.Clear();
cachedPlayerValues.Clear();
}

public Task Load() {
cachedGangValues.Clear();
cachedPlayerValues.Clear();
foreach (var (gangId, gangStatMap) in backendGangValues)
cachedGangValues[gangId] = new Dictionary<string, object>(gangStatMap);
foreach (var (steam, playerStatMap) in backendPlayerValues)
cachedPlayerValues[steam] = new Dictionary<string, object>(playerStatMap);
return Task.CompletedTask;
}

#region Player

private readonly Dictionary<ulong, Dictionary<string, object>>
cachedPlayerValues = [], backendPlayerValues = [];

public Task<bool> GetForPlayer<TV>(ulong steam, string statId, out TV? result) {
public Task<bool>
GetForPlayer<TV>(ulong steam, string statId, out TV? result) {
result = default;
if (!cachedPlayerValues.TryGetValue(steam, out var playerStatMap))
return Task.FromResult(false);
Expand Down Expand Up @@ -79,19 +94,4 @@ public Task<bool> RemoveFromGang(int gangId, string statId) {
}

#endregion

public void ClearCache() {
cachedGangValues.Clear();
cachedPlayerValues.Clear();
}

public Task Load() {
cachedGangValues.Clear();
cachedPlayerValues.Clear();
foreach (var (gangId, gangStatMap) in backendGangValues)
cachedGangValues[gangId] = new Dictionary<string, object>(gangStatMap);
foreach (var (steam, playerStatMap) in backendPlayerValues)
cachedPlayerValues[steam] = new Dictionary<string, object>(playerStatMap);
return Task.CompletedTask;
}
}
2 changes: 0 additions & 2 deletions GangsImpl/Mock/MockPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using GangsAPI.Data.Gang;
using GangsAPI.Data.Stat;
using GangsAPI.Permissions;

namespace Mock;

Expand Down
9 changes: 5 additions & 4 deletions GangsImpl/Mock/MockStat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
namespace Mock;

public class MockStat(string statId, string name, string? desc = null) : IStat {
public bool Equals(MockStat? other) {
return other is not null && ((IStat)this).Equals(other);
}

public string StatId { get; } = statId;
public string Name { get; } = name;
public string? Description { get; } = desc;

public bool Equals(IStat? other) {
return other is not null && StatId == other.StatId;
}

public bool Equals(MockStat? other) {
return other is not null && ((IStat)this).Equals(other);
}

public override bool Equals(object? obj) { return Equals(obj as MockStat); }

public override int GetHashCode() { return HashCode.Combine(StatId); }
Expand Down
Loading

0 comments on commit dc0921f

Please sign in to comment.