Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Aug 31, 2024
1 parent 0fbf1a5 commit 150c4d5
Show file tree
Hide file tree
Showing 44 changed files with 739 additions and 123 deletions.
2 changes: 1 addition & 1 deletion Gangs.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GangsPlugin", "GangsPlugin\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GangsTest", "GangsTest\GangsTest.csproj", "{B1D1E7C7-BDF3-4238-9025-4FEB2B7DAB89}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GangsImpl.Memory", "GangsImpl.Memory\GangsImpl.Memory.csproj", "{140E1706-30E8-4440-AAA0-56E8DD32F054}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GangsImpl.Mock", "GangsImpl.Mock\GangsImpl.Mock.csproj", "{140E1706-30E8-4440-AAA0-56E8DD32F054}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
44 changes: 34 additions & 10 deletions GangsAPI/Struct/Gang/IGang.cs → GangsAPI/Data/Gang/IGang.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
using GangsAPI.Permissions;
using GangsAPI.Struct.Stat;
using System.Collections;
using GangsAPI.Data.Stat;
using GangsAPI.Permissions;

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

/// <summary>
/// Represents an instance of a gang.
/// </summary>
public interface IGang : IEqualityComparer<IGang> {
public interface IGang : IEqualityComparer<IGang>, IEquatable<IGang>,
IEnumerable<ulong>, ICloneable {
/// <summary>
/// The unique identifier of the gang.
/// All gangs have a unique identifier.
/// </summary>
int Id { get; }
int GangId { get; }

/// <summary>
/// The name of the gang.
Expand All @@ -22,22 +24,31 @@ public interface IGang : IEqualityComparer<IGang> {

/// <summary>
/// The members of the gang.
/// All gangs have at least one owner.
/// </summary>
IDictionary<ulong, IGangRank> Members { get; }

/// <summary>
/// The ranks of the gang.
/// </summary>
ISet<IGangRank> Ranks { get; }

ulong Owner
=> Members.First(m => m.Value.Perms.HasFlag(IGangRank.Permissions.OWNER))
.Key;

/// <summary>
/// The amount of currency the gang has in its bank.
/// </summary>
int? Bank {
get {
var stat = GetStat("gang_bank") as IStat<ulong, int>;
var stat = GetStat("gang_bank") as IStat<int>;
return stat?.Value;
}

set {
ArgumentNullException.ThrowIfNull(value);
if (GetStat("gang_bank") is IStat<ulong, int> stat)
stat.Value = value.Value;
if (GetStat("gang_bank") is IStat<int> stat) stat.Value = value.Value;
}
}

Expand Down Expand Up @@ -92,11 +103,16 @@ public interface IGang : IEqualityComparer<IGang> {

bool IEqualityComparer<IGang>.Equals(IGang? x, IGang? y) {
if (x is null || y is null) return false;
return x.Id == y.Id;
return x.GangId == y.GangId;
}

int IEqualityComparer<IGang>.GetHashCode(IGang obj) {
return obj.Id.GetHashCode();
return obj.GangId.GetHashCode();
}

bool IEquatable<IGang>.Equals(IGang? other) {
if (other is null) return false;
return GangId == other.GangId;
}

IStat? GetPerk(string perkId) {
Expand All @@ -106,4 +122,12 @@ int IEqualityComparer<IGang>.GetHashCode(IGang obj) {
IStat? GetStat(string statId) {
return Stats.FirstOrDefault(s => s.StatId.Equals(statId));
}

IEnumerator IEnumerable.GetEnumerator() {
return Members.Keys.GetEnumerator();
}

IEnumerator<ulong> IEnumerable<ulong>.GetEnumerator() {
return Members.Keys.GetEnumerator();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using GangsAPI.Permissions;
using GangsAPI.Struct.Stat;
using GangsAPI.Data.Stat;
using GangsAPI.Permissions;

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

/// <summary>
/// A gang player is a player tracked by the gangs plugin.
Expand Down Expand Up @@ -33,27 +33,28 @@ public interface IGangPlayer {
/// </summary>
DateTime? LastSeen {
get {
var stat = GetStat("last_seen") as IStat<ulong, DateTime>;
var stat = GetStat("gang_native_lastseen") as IStat<DateTime>;
return stat?.Value;
}

set {
if (GetStat("last_seen") is IStat<ulong, DateTime?> stat)
if (GetStat("gang_native_lastseen") is IStat<DateTime?> stat)
stat.Value = value;
}
}

ISet<IStat> Stats { get; }
ISet<IStat> Perks { get; }

int? Balance {
get {
var stat = GetStat("balance") as IStat<ulong, int>;
var stat = GetStat("gang_native_balance") as IStat<int>;
return stat?.Value;
}

set {
ArgumentNullException.ThrowIfNull(value);
if (GetStat("balance") is IStat<ulong, int> stat)
if (GetStat("gang_native_balance") is IStat<int> stat)
stat.Value = value.Value;
}
}
Expand Down
3 changes: 3 additions & 0 deletions GangsAPI/Data/Stat/IGangStat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace GangsAPI.Data.Stat;

public interface IGangStat<T> : IStat<T> { }
3 changes: 3 additions & 0 deletions GangsAPI/Data/Stat/IPlayerStat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace GangsAPI.Data.Stat;

public interface IPlayerStat<T> : IStat<T> { }
9 changes: 2 additions & 7 deletions GangsAPI/Struct/Stat/IStat.cs → GangsAPI/Data/Stat/IStat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GangsAPI.Struct.Stat;
namespace GangsAPI.Data.Stat;

/// <summary>
/// Represents a numerical statistic.
Expand Down Expand Up @@ -34,12 +34,7 @@ bool IEquatable<IStat>.Equals(IStat? other) {
}
}

public interface IStat<K, T> : IStat {
/// <summary>
/// A key identifying an instance of the statistic.
/// </summary>
K Key { get; init; }

public interface IStat<T> : IStat {
/// <summary>
/// The value of the statistic.
/// </summary>
Expand Down
12 changes: 7 additions & 5 deletions GangsAPI/Services/IGangManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GangsAPI.Struct.Gang;
using GangsAPI.Data.Gang;

namespace GangsAPI.Services;

Expand Down Expand Up @@ -31,12 +31,14 @@ public interface IGangManager : IPluginBehavior {
Task<IGang?> GetGang(ulong steam);

/// <summary>
/// Pushes a gang to the database.
/// Used for updating or creating gangs.
/// Updates a gang to the database.
/// Used for updating a gang's name or members.
/// All other changes should be done through the respective managers.
/// </summary>
/// <param name="gang"></param>
/// <returns>True if the update was successful</returns>
Task<bool> PushGang(IGang gang);
/// <returns>True if the update was successful, false otherwise (e.g. gang
/// was not previously registered)</returns>
Task<bool> UpdateGang(IGang gang);

/// <summary>
/// Deletes a gang by its ID.
Expand Down
42 changes: 40 additions & 2 deletions GangsAPI/Services/IGangStatManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,46 @@
using GangsAPI.Struct.Stat;
using System.Collections;
using GangsAPI.Data.Gang;
using GangsAPI.Data.Stat;

namespace GangsAPI.Services;

public interface IGangStatManager {
Task<IGangStat<V>?> GetForGang<V>(int key, string id);
Task<bool> PushToGang<V>(int key, string id, V value);
Task<bool> PushToGang<V>(int gangId, string id, V value);

Task<IGangStat<V>?> GetForGang<V>(IGang gang, string id) {
return GetForGang<V>(gang.GangId, id);
}

Task<bool> PushToGang<V>(IGang gang, string id, V value) {
return PushToGang<V>(gang.GangId, id, value);
}

Task<IGangStat<V>?> GetForGang<V>(IGang gang, IStat stat, V value) {
return GetForGang<V>(gang, stat.StatId);
}

Task<bool> PushToGang<V>(IGang gang, IStat stat, V value) {
return PushToGang<V>(gang, stat.StatId, value);
}

Task<bool> PushToGang<V>(int gangId, IStat<V> stat) {
return PushToGang<V>(gangId, stat.StatId, stat.Value);
}

Task<bool> PushToGang<V>(IGang gang, IStat<V> stat) {
return PushToGang<V>(gang, stat.StatId, stat.Value);
}

Task<IGangStat<V>?> GetForGang<V>(int key, IStat stat, V value) {
return GetForGang<V>(key, stat.StatId);
}

Task<bool> PushToGang<V>(int gangId, IStat stat, V value) {
return PushToGang<V>(gangId, stat.StatId, value);
}

Task<IGangStat<V>?> GetForGang<V>(IGang gang, IStat stat) {
return GetForGang<V>(gang, stat.StatId);
}
}
11 changes: 7 additions & 4 deletions GangsAPI/Services/IPlayerManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GangsAPI.Struct.Gang;
using GangsAPI.Data.Gang;

namespace GangsAPI.Services;

Expand All @@ -7,14 +7,17 @@ namespace GangsAPI.Services;
/// </summary>
public interface IPlayerManager : IPluginBehavior {
/// <summary>
/// Gets a player by their SteamID64, creating them if they do not exist.
/// Gets a player by their SteamID64.
/// </summary>
/// <param name="steamId">The SteamID64 of the player.</param>
/// <returns>The player, or null if there was an error creating one.</returns>
Task<IGangPlayer?> GetPlayer(ulong steamId);
/// <returns>The player, or null if they did not exist.</returns>
Task<IGangPlayer?> GetPlayer(ulong steamId) {
return GetPlayer(steamId, false);
}

/// <summary>
/// Gets a player by their SteamID64.
/// If the player was already created, returns them.
/// </summary>
/// <param name="steamId">The SteamID64 of the player.</param>
/// <param name="create">True if the manager should create a new player if they don't exist.</param>
Expand Down
13 changes: 10 additions & 3 deletions GangsAPI/Services/IPlayerStatManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
using GangsAPI.Struct.Stat;
using GangsAPI.Data.Gang;
using GangsAPI.Data.Stat;

namespace GangsAPI.Services;

public interface IPlayerStatManager {
Task<IGangStat<V>?> GetForPlayer<V>(ulong key, string id);
Task<bool> PushToPlayer<V>(ulong key, string id, V value);
Task<IPlayerStat<V>?> GetForPlayer<V>(ulong key, string statId);

Task<bool> PushToPlayer<V>(ulong key, IPlayerStat<V> value) {
return PushToPlayer(key, value.StatId, value.Value);
}

Task<bool> PushToPlayer<V>(ulong key, string statId, V value);

}
24 changes: 0 additions & 24 deletions GangsAPI/Services/IStatInstanceManager.cs

This file was deleted.

2 changes: 1 addition & 1 deletion GangsAPI/Services/IStatManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using GangsAPI.Struct.Stat;
using GangsAPI.Data.Stat;

namespace GangsAPI.Services;

Expand Down
3 changes: 0 additions & 3 deletions GangsAPI/Struct/Stat/IGangStat.cs

This file was deleted.

3 changes: 0 additions & 3 deletions GangsAPI/Struct/Stat/IPlayerStat.cs

This file was deleted.

9 changes: 0 additions & 9 deletions GangsImpl.Memory/EphemeralStat.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<RootNamespace>GangsImpl.Memory</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace GangsImpl.Memory;

public static class MemoryImpl {
public static void AddMemoryImpl(this IServiceCollection collection) {
collection.AddPluginBehavior<IStatManager, MemoryStatManager>();
collection.AddPluginBehavior<IStatManager, MockStatManager>();
}
}
Loading

0 comments on commit 150c4d5

Please sign in to comment.