Skip to content

Commit

Permalink
Improve code style
Browse files Browse the repository at this point in the history
  • Loading branch information
akemimadoka committed Sep 1, 2017
1 parent 81216bd commit 144cf7a
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/MineCase.Server.Grains/Game/GameSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task SendChatMessage(IUser sender, string message)
var command = message.Trim();
if (command[0] == '/')
{
if (!_commandMap.Dispatch(await sender.GetPlayer(), message))
if (!await _commandMap.Dispatch(await sender.GetPlayer(), message))
{
await sender.SendChatMessage(
await CreateStandardChatMessage(
Expand Down
39 changes: 33 additions & 6 deletions src/MineCase.Server.Interfaces/Game/Commands/CommandMap.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,59 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Text;
using System.Threading.Tasks;

namespace MineCase.Server.Game.Commands
{
/// <summary>
/// 命令 Map
/// </summary>
public class CommandMap
{
private readonly Dictionary<string, ICommand> _commandMap = new Dictionary<string, ICommand>();

/// <summary>
/// 注册一个命令
/// </summary>
/// <param name="command">要注册的命令</param>
/// <exception cref="ArgumentNullException"><paramref name="command"/> 为 null</exception>
/// <exception cref="ArgumentException">已有重名的 command 被注册</exception>
public void RegisterCommand(ICommand command)
{
if (command == null)
{
throw new ArgumentNullException(nameof(command));
}

Contract.EndContractBlock();

_commandMap.Add(command.Name, command);
}

public bool Dispatch(ICommandSender sender, string commandContent)
/// <summary>
/// 分派命令
/// </summary>
/// <param name="sender">命令的发送者</param>
/// <param name="commandContent">命令的内容</param>
public Task<bool> Dispatch(ICommandSender sender, string commandContent)
{
var (commandName, args) = CommandParser.ParseCommand(commandContent);

try
{
return _commandMap.TryGetValue(commandName, out var command) &&
(command.NeededPermission == null || sender.HasPermission(command.NeededPermission).Result) &&
command.Execute(sender, args);
if (_commandMap.TryGetValue(commandName, out var command) &&
(command.NeededPermission == null || sender.HasPermission(command.NeededPermission).Result))
{
return command.Execute(sender, args);
}

return Task.FromResult(false);
}
catch (CommandException e)
{
sender.SendMessage($"在执行指令 {commandName} 之时发生指令相关的异常 {e}");
return false;
sender.SendMessage($"在执行命令 {commandName} 之时发生命令相关的异常 {e}");
return Task.FromResult(false);
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/MineCase.Server.Interfaces/Game/Commands/CommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

namespace MineCase.Server.Game.Commands
{
/// <inheritdoc />
/// <summary>
/// 数据标签参数
/// </summary>
/// <remarks>表示一个 <see cref="NbtTag"/></remarks>
/// <remarks>表示一个 <see cref="NbtTag" /></remarks>
public class DataTagArgument : UnresolvedArgument
{
internal const char PrefixToken = '{';
Expand Down
7 changes: 5 additions & 2 deletions src/MineCase.Server.Interfaces/Game/Commands/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ public interface ICommand
/// <param name="args">命令的参数</param>
/// <returns>执行是否成功,如果成功则返回 true</returns>
/// <exception cref="CommandException">可能抛出派生自 <see cref="CommandException"/> 的异常</exception>
bool Execute(ICommandSender commandSender, IList<ICommandArgument> args);
Task<bool> Execute(ICommandSender commandSender, IReadOnlyList<ICommandArgument> args);
}

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

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

/// <inheritdoc />
/// <summary>
/// 可发送命令者接口
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MineCase.Server.Game.Commands
{
/// <inheritdoc />
/// <summary>
/// 简单指令
/// </summary>
Expand Down Expand Up @@ -30,6 +32,6 @@ protected SimpleCommand(string name, string description = null, Permission neede
}
}

public abstract bool Execute(ICommandSender commandSender, IList<ICommandArgument> args);
public abstract Task<bool> Execute(ICommandSender commandSender, IReadOnlyList<ICommandArgument> args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public enum TargetSelectorType
Executor
}

/// <inheritdoc cref="UnresolvedArgument" />
/// <summary>
/// 用于选择目标的 <see cref="ICommandArgument"/>
/// 用于选择目标的 <see cref="ICommandArgument" />
/// </summary>
public class TargetSelectorArgument : UnresolvedArgument, IEnumerable<KeyValuePair<string, string>>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MineCase.Server.Game.Commands
{
/// <inheritdoc />
/// <summary>
/// 波浪号记号参数
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions tests/UnitTest/CommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public Task SendMessage(string msg)
private class TestCommand : SimpleCommand
{
public TestCommand()
: base("test", null, null, null)
: base("test")
{
}

public override bool Execute(ICommandSender commandSender, IList<ICommandArgument> args)
public override Task<bool> Execute(ICommandSender commandSender, IReadOnlyList<ICommandArgument> args)
{
commandSender.SendMessage(string.Join(", ", args.Select(arg => arg.ToString())));
return true;
return Task.FromResult(true);
}
}

Expand Down

0 comments on commit 144cf7a

Please sign in to comment.