-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
136 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Bunkum.HttpServer; | ||
using Bunkum.HttpServer.Services; | ||
using JetBrains.Annotations; | ||
using MongoDB.Bson; | ||
using NotEnoughLogs; | ||
using Refresh.GameServer.Types.Commands; | ||
|
||
namespace Refresh.GameServer.Services; | ||
|
||
public class CommandService : EndpointService | ||
{ | ||
public CommandService(LoggerContainer<BunkumContext> logger) : base(logger) {} | ||
|
||
private readonly HashSet<ObjectId> _usersPublishing = new(); | ||
|
||
/// <summary> | ||
/// Start tracking the user, eg. they started publishing | ||
/// </summary> | ||
/// <param name="id">The user ID</param> | ||
public void StartPublishing(ObjectId id) | ||
{ | ||
//Unconditionally add the user to the set | ||
_ = this._usersPublishing.Add(id); | ||
} | ||
|
||
/// <summary> | ||
/// Stop tracking the user, eg. they stopped publishing | ||
/// </summary> | ||
/// <param name="id">The user ID</param> | ||
public void StopPublishing(ObjectId id) | ||
{ | ||
//Unconditionally remove the user from the set | ||
_ = this._usersPublishing.Remove(id); | ||
} | ||
|
||
public bool IsPublishing(ObjectId id) => this._usersPublishing.Contains(id); | ||
|
||
/// <summary> | ||
/// Parse a command string into a command object | ||
/// </summary> | ||
/// <param name="str">Command string</param> | ||
/// <returns>Parsed command</returns> | ||
/// <exception cref="FormatException">When the command is in an invalid format</exception> | ||
[Pure] | ||
public Command ParseCommand(string str) | ||
{ | ||
//Ensure the command string starts with a slash | ||
if (str[0] != '/') | ||
{ | ||
throw new FormatException("Commands must start with `/`"); | ||
} | ||
|
||
int idx = str.IndexOf(" ", StringComparison.Ordinal); | ||
|
||
//If idx is 1, the command name is blank | ||
// ReSharper disable once ConvertIfStatementToSwitchStatement | ||
if (idx == 1) | ||
{ | ||
throw new FormatException("Blank command name"); | ||
} | ||
|
||
//If theres no space after, or if the space is the last character, then there are no arguments | ||
if (idx == -1 || idx == str.Length - 1) | ||
{ | ||
return new Command(idx == str.Length - 1 ? str[1..idx] : str[1..], null); | ||
} | ||
|
||
return new Command(str[1..idx], str[(idx + 1)..]); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Refresh.GameServer.Types.Commands; | ||
|
||
public record Command(string Name, string? Arguments); |
35 changes: 35 additions & 0 deletions
35
RefreshTests.GameServer/Tests/Commands/CommandParseTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Bunkum.HttpServer; | ||
using NotEnoughLogs; | ||
using Refresh.GameServer.Services; | ||
using Refresh.GameServer.Types.Commands; | ||
|
||
namespace RefreshTests.GameServer.Tests.Commands; | ||
|
||
public class CommandParseTests : GameServerTest | ||
{ | ||
[Test] | ||
public void ParsingTest() | ||
{ | ||
CommandService service = new(new LoggerContainer<BunkumContext>()); | ||
|
||
Assert.That(service.ParseCommand("/parse test"), Is.EqualTo(new Command("parse", "test"))); | ||
Assert.That(service.ParseCommand("/noargs"), Is.EqualTo(new Command("noargs", null))); | ||
Assert.That(service.ParseCommand("/noargs "), Is.EqualTo(new Command("noargs", null))); | ||
} | ||
|
||
[Test] | ||
public void NoSlashThrows() | ||
{ | ||
CommandService service = new(new LoggerContainer<BunkumContext>()); | ||
|
||
Assert.That(() => service.ParseCommand("parse test"), Throws.Exception); | ||
} | ||
|
||
[Test] | ||
public void BlankCommandThrows() | ||
{ | ||
CommandService service = new(new LoggerContainer<BunkumContext>()); | ||
|
||
Assert.That(() => service.ParseCommand("/ test"), Throws.Exception); | ||
} | ||
} |