Skip to content

Commit

Permalink
Basic gang creation working
Browse files Browse the repository at this point in the history
  • Loading branch information
MSWS committed Sep 3, 2024
1 parent 8d32ab7 commit b811027
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 20 deletions.
10 changes: 5 additions & 5 deletions Commands/GangCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ public class GangCommand(IGangManager gangMgr) : ICommand {

public async Task<CommandResult> Execute(PlayerWrapper? executor,
CommandInfoWrapper info) {
if (info.ArgCount == 0 || info[0] != Name) {
if (info.ArgCount == 0)
throw new InvalidOperationException(
"Attempted to execute GangCommand with no arguments");
if (info.ArgCount == 0)
throw new InvalidOperationException(
"Attempted to execute GangCommand with no arguments");
if (info[0] != Name)
throw new InvalidOperationException(
$"Attempted to execute GangCommand with invalid name: {info[0]}");
}


if (info.ArgCount == 1) return CommandResult.INVALID_ARGS;

Expand Down
12 changes: 10 additions & 2 deletions Commands/gang/CreateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task<CommandResult> Execute(PlayerWrapper? executor,

if (info.ArgCount < 2) {
info.ReplySync("Please provide a name for the gang");
return CommandResult.FAILURE;
return CommandResult.INVALID_ARGS;
}

var name = string.Join(' ', info.ArgString.Split(" ").Skip(1));
Expand All @@ -34,6 +34,14 @@ public async Task<CommandResult> Execute(PlayerWrapper? executor,
return CommandResult.FAILURE;
}

throw new NotImplementedException();
var newGang = await gang.CreateGang(name, executor.Steam);

if (newGang == null) {
info.ReplySync("Failed to create gang");
return CommandResult.FAILURE;
}

info.ReplySync($"Gang '{name}' (#{newGang.GangId}) created successfully");
return CommandResult.SUCCESS;
}
}
28 changes: 28 additions & 0 deletions GangsAPI/Data/Command/CommandResult.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
namespace GangsAPI.Data.Command;

public enum CommandResult {
/// <summary>
/// The command completed successfully
/// </summary>
SUCCESS,

/// <summary>
/// The command encountered an error or other
/// scenario that prevented success
/// </summary>
FAILURE,

/// <summary>
/// The command was improperly formatted
/// </summary>
UNKNOWN_COMMAND,

/// <summary>
/// The command has improper arguments, or
/// no sufficient arguments
/// </summary>
INVALID_ARGS,

/// <summary>
/// The executor of the command did not have
/// the required permissions
/// </summary>
NO_PERMISSION,


/// <summary>
/// This command can only be executed by a player
/// (i.e. not from the console)
/// </summary>
PLAYER_ONLY
}
27 changes: 17 additions & 10 deletions GangsImpl/Mock/MockGangManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
namespace Mock;

public class MockGangManager : IGangManager {
private readonly HashSet<IGang> gangs = [];
private readonly HashSet<IGang> cachedGangs = [], backendGangs = [];

public Task<IEnumerable<IGang>> GetGangs() {
return Task.FromResult(gangs.AsEnumerable());
return Task.FromResult(cachedGangs.AsEnumerable());
}

public Task<IGang?> GetGang(int id) {
return Task.FromResult(gangs.FirstOrDefault(g => g.GangId == id));
return Task.FromResult(cachedGangs.FirstOrDefault(g => g.GangId == id));
}

public Task<IGang?> GetGang(ulong steam) {
return Task.FromResult(
gangs.FirstOrDefault(g => g.Members.ContainsKey(steam)));
cachedGangs.FirstOrDefault(g => g.Members.ContainsKey(steam)));
}

public Task<bool> UpdateGang(IGang gang) {
var g = gangs.FirstOrDefault(g => g.GangId == gang.GangId);
var g = cachedGangs.FirstOrDefault(g => g.GangId == gang.GangId);
if (g == null) return Task.FromResult(false);
g.Name = gang.Name;
g.Members.Clear();
Expand All @@ -29,16 +29,23 @@ public Task<bool> UpdateGang(IGang gang) {
}

public Task<bool> DeleteGang(int id) {
return Task.FromResult(gangs.RemoveWhere(g => g.GangId == id) > 0);
return Task.FromResult(cachedGangs.RemoveWhere(g => g.GangId == id) > 0);
}

public Task<IGang?> CreateGang(string name, ulong owner) {
var id = gangs.Count + 1;
var id = cachedGangs.Count + 1;
var gang = new MockGang(id, name, owner);
return Task.FromResult((IGang?)(gangs.Add(gang) ? gang.Clone() : null));
if (cachedGangs.Any(g => g.GangId == id))
return Task.FromResult<IGang?>(null);
cachedGangs.Add(gang);
backendGangs.Add(gang);
return Task.FromResult(gang.Clone() as IGang);
}

public void ClearCache() { gangs.Clear(); }
public void ClearCache() { cachedGangs.Clear(); }

public Task Load() { return Task.CompletedTask; }
public Task Load() {
cachedGangs.UnionWith(backendGangs);
return Task.CompletedTask;
}
}
72 changes: 69 additions & 3 deletions GangsTest/Commands/CreateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,81 @@ public class CreateTests(ICommandManager commands, IGangManager gangMgr)
"Test Player");

[Fact]
public async Task Create_TestNonPlayer() {
public async Task Create_NonPlayer() {
Assert.Equal(CommandResult.PLAYER_ONLY,
await Commands.ProcessCommand(null, "create"));
}

[Fact]
public async Task Create_TestNoName() {
Assert.Equal(CommandResult.FAILURE,
public async Task Create_NoName() {
Assert.Equal(CommandResult.INVALID_ARGS,
await Commands.ProcessCommand(player, "create"));
Assert.Contains("Please provide a name for the gang", player.ConsoleOutput);
}

[Fact]
public async Task Create_Simple() {
var gang = await gangMgr.GetGang(player.Steam);
Assert.Null(gang);
Assert.Equal(CommandResult.SUCCESS,
await Commands.ProcessCommand(player, "create", "foobar"));
gang = await gangMgr.GetGang(player.Steam);
Assert.NotNull(gang);
Assert.Equal("foobar", gang.Name);
Assert.Contains($"Gang 'foobar' (#{gang.GangId}) created successfully",
player.ConsoleOutput);
}

[Fact]
public async Task Create_MultiWord() {
var gang = await gangMgr.GetGang(player.Steam);
Assert.Null(gang);
Assert.Equal(CommandResult.SUCCESS,
await Commands.ProcessCommand(player, "create", "foo bar"));
gang = await gangMgr.GetGang(player.Steam);
Assert.NotNull(gang);
Assert.Equal("foo bar", gang.Name);
}

[Fact]
public async Task Create_MultiParam() {
var gang = await gangMgr.GetGang(player.Steam);
Assert.Null(gang);
Assert.Equal(CommandResult.SUCCESS,
await Commands.ProcessCommand(player, "create", "foo bar", "baz"));
gang = await gangMgr.GetGang(player.Steam);
Assert.NotNull(gang);
Assert.Equal("foo bar baz", gang.Name);
}

[Fact]
public async Task Create_Already_Ganged() {
Assert.Equal(CommandResult.SUCCESS,
await Commands.ProcessCommand(player, "create", "foo bar"));
Assert.Equal(CommandResult.FAILURE,
await Commands.ProcessCommand(player, "create", "bar foo"));
Assert.Contains("You are already in a gang", player.ConsoleOutput);
}

[Fact]
public async Task Create_Already_Ganged_Uncached() {
Assert.Equal(CommandResult.SUCCESS,
await Commands.ProcessCommand(player, "create", "foo bar"));
gangMgr.ClearCache();
await gangMgr.Load();
Assert.Equal(CommandResult.FAILURE,
await Commands.ProcessCommand(player, "create", "bar foo"));
Assert.Contains("You are already in a gang", player.ConsoleOutput);
}

[Fact]
public async Task Create_Duplicate_Name() {
var other =
new PlayerWrapper((ulong)new Random().NextInt64(), "Other Player");
Assert.Equal(CommandResult.SUCCESS,
await Commands.ProcessCommand(player, "create", "foo bar"));
Assert.Equal(CommandResult.FAILURE,
await Commands.ProcessCommand(other, "create", "foo bar"));
Assert.Contains("Gang 'foo bar' already exists", other.ConsoleOutput);
}
}
15 changes: 15 additions & 0 deletions GangsTest/Commands/GangCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ public async Task Gang_TestBase() {
await Commands.ProcessCommand(TestPlayer, Command.Name));
}

[Fact]
public async Task Gang_TestInvalid_Name() {
await Assert.ThrowsAnyAsync<InvalidOperationException>(async () => {
await Command.Execute(TestPlayer,
new CommandInfoWrapper(TestPlayer, 0, "foobar"));
});
}

[Fact]
public async Task Gang_TestInvalid_Null() {
await Assert.ThrowsAnyAsync<InvalidOperationException>(async () => {
await Command.Execute(TestPlayer, new CommandInfoWrapper(TestPlayer, 0));
});
}

[Fact]
public async Task Gang_TestUnknown() {
Assert.Equal(CommandResult.UNKNOWN_COMMAND,
Expand Down

0 comments on commit b811027

Please sign in to comment.