Skip to content

Commit 144cf7a

Browse files
committed
Improve code style
1 parent 81216bd commit 144cf7a

File tree

9 files changed

+54
-18
lines changed

9 files changed

+54
-18
lines changed

src/MineCase.Server.Grains/Game/GameSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public async Task SendChatMessage(IUser sender, string message)
6868
var command = message.Trim();
6969
if (command[0] == '/')
7070
{
71-
if (!_commandMap.Dispatch(await sender.GetPlayer(), message))
71+
if (!await _commandMap.Dispatch(await sender.GetPlayer(), message))
7272
{
7373
await sender.SendChatMessage(
7474
await CreateStandardChatMessage(

src/MineCase.Server.Interfaces/Game/Commands/CommandMap.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,59 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.Contracts;
34
using System.Text;
5+
using System.Threading.Tasks;
46

57
namespace MineCase.Server.Game.Commands
68
{
9+
/// <summary>
10+
/// 命令 Map
11+
/// </summary>
712
public class CommandMap
813
{
914
private readonly Dictionary<string, ICommand> _commandMap = new Dictionary<string, ICommand>();
1015

16+
/// <summary>
17+
/// 注册一个命令
18+
/// </summary>
19+
/// <param name="command">要注册的命令</param>
20+
/// <exception cref="ArgumentNullException"><paramref name="command"/> 为 null</exception>
21+
/// <exception cref="ArgumentException">已有重名的 command 被注册</exception>
1122
public void RegisterCommand(ICommand command)
1223
{
24+
if (command == null)
25+
{
26+
throw new ArgumentNullException(nameof(command));
27+
}
28+
29+
Contract.EndContractBlock();
30+
1331
_commandMap.Add(command.Name, command);
1432
}
1533

16-
public bool Dispatch(ICommandSender sender, string commandContent)
34+
/// <summary>
35+
/// 分派命令
36+
/// </summary>
37+
/// <param name="sender">命令的发送者</param>
38+
/// <param name="commandContent">命令的内容</param>
39+
public Task<bool> Dispatch(ICommandSender sender, string commandContent)
1740
{
1841
var (commandName, args) = CommandParser.ParseCommand(commandContent);
1942

2043
try
2144
{
22-
return _commandMap.TryGetValue(commandName, out var command) &&
23-
(command.NeededPermission == null || sender.HasPermission(command.NeededPermission).Result) &&
24-
command.Execute(sender, args);
45+
if (_commandMap.TryGetValue(commandName, out var command) &&
46+
(command.NeededPermission == null || sender.HasPermission(command.NeededPermission).Result))
47+
{
48+
return command.Execute(sender, args);
49+
}
50+
51+
return Task.FromResult(false);
2552
}
2653
catch (CommandException e)
2754
{
28-
sender.SendMessage($"在执行指令 {commandName} 之时发生指令相关的异常 {e}");
29-
return false;
55+
sender.SendMessage($"在执行命令 {commandName} 之时发生命令相关的异常 {e}");
56+
return Task.FromResult(false);
3057
}
3158
}
3259
}

src/MineCase.Server.Interfaces/Game/Commands/CommandParser.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace MineCase.Server.Game.Commands
99
{
10+
/// <inheritdoc />
1011
/// <summary>
1112
/// 未解析参数
1213
/// </summary>
@@ -42,7 +43,7 @@ public static class CommandParser
4243
/// <param name="input">输入,即作为命令被分析的文本,应当不为 null、经过 <see cref="string.Trim()"/> 处理且以 '/' 开头</param>
4344
/// <returns>命令名及命令的参数</returns>
4445
/// <exception cref="ArgumentException"><paramref name="input"/> 不合法</exception>
45-
public static (string, IList<ICommandArgument>) ParseCommand(string input)
46+
public static (string, IReadOnlyList<ICommandArgument>) ParseCommand(string input)
4647
{
4748
if (input == null || input.Length < 2 || input[0] != '/')
4849
{
@@ -58,8 +59,8 @@ public static (string, IList<ICommandArgument>) ParseCommand(string input)
5859
return (splitResult[0].Substring(1), ParseCommandArgument(splitResult.Skip(1)));
5960
}
6061

61-
// 参数必须保持原来的顺序,因此返回值使用 IList 而不是 IEnumerable
62-
private static IList<ICommandArgument> ParseCommandArgument(IEnumerable<string> input)
62+
// 参数必须保持原来的顺序,因此返回值使用 IReadOnlyList 而不是 IEnumerable
63+
private static IReadOnlyList<ICommandArgument> ParseCommandArgument(IEnumerable<string> input)
6364
{
6465
var result = new List<ICommandArgument>();
6566

src/MineCase.Server.Interfaces/Game/Commands/DataTagArgument.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
namespace MineCase.Server.Game.Commands
77
{
8+
/// <inheritdoc />
89
/// <summary>
910
/// 数据标签参数
1011
/// </summary>
11-
/// <remarks>表示一个 <see cref="NbtTag"/></remarks>
12+
/// <remarks>表示一个 <see cref="NbtTag" /></remarks>
1213
public class DataTagArgument : UnresolvedArgument
1314
{
1415
internal const char PrefixToken = '{';

src/MineCase.Server.Interfaces/Game/Commands/ICommand.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ public interface ICommand
4747
/// <param name="args">命令的参数</param>
4848
/// <returns>执行是否成功,如果成功则返回 true</returns>
4949
/// <exception cref="CommandException">可能抛出派生自 <see cref="CommandException"/> 的异常</exception>
50-
bool Execute(ICommandSender commandSender, IList<ICommandArgument> args);
50+
Task<bool> Execute(ICommandSender commandSender, IReadOnlyList<ICommandArgument> args);
5151
}
5252

53+
/// <inheritdoc />
5354
/// <summary>
5455
/// 命令执行过程中可能发生的异常的基类
5556
/// </summary>
56-
/// <remarks>派生自此类的异常在 <see cref="CommandMap.Dispatch(ICommandSender, string)"/> 中将会被吃掉,不会传播到外部</remarks>
57+
/// <remarks>派生自此类的异常在 <see cref="M:MineCase.Server.Game.Commands.CommandMap.Dispatch(MineCase.Server.Game.Commands.ICommandSender,System.String)" /> 中将会被吃掉,不会传播到外部</remarks>
5758
public class CommandException : Exception
5859
{
5960
public ICommand Command { get; }
@@ -65,6 +66,7 @@ public CommandException(ICommand command = null, string content = null, Exceptio
6566
}
6667
}
6768

69+
/// <inheritdoc />
6870
/// <summary>
6971
/// 表示命令的使用方式错误的异常
7072
/// </summary>
@@ -76,6 +78,7 @@ public CommandWrongUsageException(ICommand command, string content = null, Excep
7678
}
7779
}
7880

81+
/// <inheritdoc />
7982
/// <summary>
8083
/// 可发送命令者接口
8184
/// </summary>

src/MineCase.Server.Interfaces/Game/Commands/SimpleCommand.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34

45
namespace MineCase.Server.Game.Commands
56
{
7+
/// <inheritdoc />
68
/// <summary>
79
/// 简单指令
810
/// </summary>
@@ -30,6 +32,6 @@ protected SimpleCommand(string name, string description = null, Permission neede
3032
}
3133
}
3234

33-
public abstract bool Execute(ICommandSender commandSender, IList<ICommandArgument> args);
35+
public abstract Task<bool> Execute(ICommandSender commandSender, IReadOnlyList<ICommandArgument> args);
3436
}
3537
}

src/MineCase.Server.Interfaces/Game/Commands/TargetSelector.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ public enum TargetSelectorType
5252
Executor
5353
}
5454

55+
/// <inheritdoc cref="UnresolvedArgument" />
5556
/// <summary>
56-
/// 用于选择目标的 <see cref="ICommandArgument"/>
57+
/// 用于选择目标的 <see cref="ICommandArgument" />
5758
/// </summary>
5859
public class TargetSelectorArgument : UnresolvedArgument, IEnumerable<KeyValuePair<string, string>>
5960
{

src/MineCase.Server.Interfaces/Game/Commands/TildeNotationArgument.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MineCase.Server.Game.Commands
66
{
7+
/// <inheritdoc />
78
/// <summary>
89
/// 波浪号记号参数
910
/// </summary>

tests/UnitTest/CommandTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public Task SendMessage(string msg)
3636
private class TestCommand : SimpleCommand
3737
{
3838
public TestCommand()
39-
: base("test", null, null, null)
39+
: base("test")
4040
{
4141
}
4242

43-
public override bool Execute(ICommandSender commandSender, IList<ICommandArgument> args)
43+
public override Task<bool> Execute(ICommandSender commandSender, IReadOnlyList<ICommandArgument> args)
4444
{
4545
commandSender.SendMessage(string.Join(", ", args.Select(arg => arg.ToString())));
46-
return true;
46+
return Task.FromResult(true);
4747
}
4848
}
4949

0 commit comments

Comments
 (0)