Skip to content

Commit

Permalink
Resolve errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 5, 2024
1 parent 432304e commit 81a6413
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Commands/gang/CreateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<CommandResult> Execute(PlayerWrapper? executor,
return CommandResult.INVALID_ARGS;
}

var name = string.Join(' ', info.ArgString.Split(" ").Skip(1));
var name = string.Join(' ', info.ArgString.Split(" "));

if (await gang.GetGang(executor.Steam) != null) {
info.ReplySync("You are already in a gang");
Expand Down
2 changes: 1 addition & 1 deletion GangsAPI/Data/Command/CommandInfoWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public CommandInfoWrapper(CommandInfoWrapper info, int offset) : this(
public string this[int index] => args[index + offset];

public string ArgString
=> string.Join(' ', GetCommandString.Split(' ').Skip(offset));
=> string.Join(' ', GetCommandString.Split(' ').Skip(offset + 1));

public string GetCommandString => string.Join(' ', args.Skip(offset));

Expand Down
19 changes: 2 additions & 17 deletions GangsAPI/Services/IGangStatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace GangsAPI.Services;
public interface IGangStatManager : ICacher {
Task<IStat<V>?> GetForGang<V>(int key, string id);
Task<IStat?> GetForGang(int key, string id);
Task<bool> PushToGang<V>(int gangId, string id, V value);

Task<IStat?> GetForGang(IGang gang, string id) {
return GetForGang(gang.GangId, id);
Expand All @@ -17,24 +16,10 @@ public interface IGangStatManager : ICacher {
return GetForGang<V>(gang.GangId, id);
}

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

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

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

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

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

Task<IStat?> GetForGang(IGang gang, IStat stat) {
Expand Down
10 changes: 6 additions & 4 deletions GangsImpl/Mock/MockInstanceStatManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ private readonly Dictionary<ulong, Dictionary<string, IStat>>
return Task.FromResult(result)!;
}

public async Task<bool> PushToGang<V>(int gangId, string id, V value) {
public async Task<bool> PushToGang<V>(int gangId, IStat<V> stat) {
var registered = await mgr.GetStat(stat.StatId);
if (registered == null) return false;
if (!gangStats.TryGetValue(gangId, out var gangStatMap))
gangStats[gangId] = gangStatMap = new Dictionary<string, IStat>();
var stat = await mgr.GetStat(id);
if (stat == null) return false;
gangStatMap[id] = new MockStat<V>(stat, value);
gangStatMap[stat.StatId] = stat;
gangStats[gangId] = gangStatMap;
return true;
}

Expand All @@ -49,6 +50,7 @@ public async Task<bool> PushToPlayer<V>(ulong key, string id, V value) {
var stat = await mgr.GetStat(id);
if (stat == null) return false;
playerStatMap[id] = new MockStat<V>(stat, value);
playerStats[key] = playerStatMap;
return true;
}

Expand Down
105 changes: 103 additions & 2 deletions GangsTest/API/CommandWrapperTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,104 @@
namespace GangsTest.API;
using GangsAPI.Data;
using GangsAPI.Data.Command;

public class CommandWrapperTests { }
namespace GangsTest.API;

public class CommandWrapperTests {
private readonly PlayerWrapper testPlayer =
new((ulong)new Random().NextInt64(), "Test Player");

[Fact]
public void CommandWrapper_Player() {
var info = new CommandInfoWrapper(testPlayer, 0, "test");
Assert.NotNull(info.CallingPlayer);
Assert.Equal(testPlayer.Steam, info.CallingPlayer.Steam);
Assert.Equal("Test Player", info.CallingPlayer.Name);
}

[Fact]
public void CommandWrapper_Console() {
var info = new CommandInfoWrapper(null, 0, "test");
Assert.Null(info.CallingPlayer);
}

[Fact]
public void ArgCount_Single() {
Assert.Equal(1, new CommandInfoWrapper(testPlayer, 0, "test").ArgCount);
}

[Fact]
public void ArgCount_Single_Space() {
Assert.Equal(1, new CommandInfoWrapper(testPlayer, 0, "test ing").ArgCount);
}

[Fact]
public void Offset_ArgCount_Single() {
Assert.Equal(0, new CommandInfoWrapper(testPlayer, 1, "test").ArgCount);
}

[Fact]
public void Offset_ArgCount_Space() {
Assert.Equal(0, new CommandInfoWrapper(testPlayer, 1, "test ing").ArgCount);
}

[Fact]
public void Offset_ArgCount_One() {
Assert.Equal(1,
new CommandInfoWrapper(testPlayer, 1, "test", "ing").ArgCount);
}

[Fact]
public void ArgString_Single() {
Assert.Equal("", new CommandInfoWrapper(testPlayer, 0, "test").ArgString);
}

[Fact]
public void ArgString_Single_Space() {
Assert.Equal("ing",
new CommandInfoWrapper(testPlayer, 0, "test ing").ArgString);
}

[Fact]
public void ArgString_Multi() {
Assert.Equal("ing",
new CommandInfoWrapper(testPlayer, 0, "test", "ing").ArgString);
}

[Fact]
public void ArgString_Multi_Space() {
Assert.Equal("ing foobar",
new CommandInfoWrapper(testPlayer, 0, "test", "ing", "foobar").ArgString);
}

[Fact]
public void CommandString_Simple() {
Assert.Equal("test ing foobar",
new CommandInfoWrapper(testPlayer, 0, "test", "ing", "foobar")
.GetCommandString);
}

[Fact]
public void CommandString_SubCommand() {
Assert.Equal("css_gang",
new CommandInfoWrapper(testPlayer, 0, "css_gang", "create", "foobar")[0]);
}

[Fact]
public void CommandString_SubCommand_Sub() {
Assert.Equal("create",
new CommandInfoWrapper(testPlayer, 1, "css_gang", "create", "foobar")[0]);
}

[Fact]
public void CommandString_SubCommand_Sub1() {
Assert.Equal("foobar",
new CommandInfoWrapper(testPlayer, 1, "css_gang", "create", "foobar")[1]);
}

[Fact]
public void CommandString_SubCommand_Sub2() {
Assert.Equal("create foobar",
new CommandInfoWrapper(testPlayer, 1, "css_gang", "create", "foobar")
.GetCommandString);
}
}

0 comments on commit 81a6413

Please sign in to comment.