如何为多个用户实现命令列表?



我有一个命令列表,例如:

  • 命令 1命令 2测试退出

我也有用户:

管理员、用户
  • 1用户 2

目标是使用户能够调用他们可用的命令。 例如

  • 管理员- 命令1命令 2测试退出
  • 用户 1-命令 1测试
  • 用户 2-命令 2

如果我在输入中有两个变量 -命令名称和用户名,那么实现这一点的最佳方法是什么。

这目前是在交换机上完成的,但对于扩展项目来说非常不方便。

您的解决方案是权限。

  1. 创建角色并为其分配一组权限;
  2. 为用户分配角色,并创建包含特定用户的其他权限的功能;
  3. 将命令处理程序拆分为不同的方法(无开关!(,并在命令执行开始时检查权限。

这是一个伪代码,我认为命令处理程序应该是什么样子:

public ExecutionResult ExecuteCommand1(User user, string[] arguments) {
if(!user.HasPermission("admin.test.permission")) {
return new ExecutionResult() {
Error = true,
Message = "You don't have permissions to execute that command!"
};
}
user.GiveKarma(1337);
return new ExecutionResult {
Message = "Karma points were added!"
};
}

权限标识符不应像我的示例代码那样是字符串。

你可以使用任何东西:字符串、枚举等。

您可以创建包含命令和用户访问命令(或类似的数据库(的字典列表:

var commandList = new Dictionary<string, Action>();
commandList.Add("command1", () => { Console.WriteLine("command1 is executed"); });
commandList.Add("command2", () => { Console.WriteLine("command2 is executed"); });
commandList.Add("command3", () => { Console.WriteLine("command3 is executed"); });
var listOfUserCommands = new Dictionary<string, List<string>>();
listOfUserCommands.Add("user1", new List<string>() { "command1" });
listOfUserCommands.Add("user2", new List<string>() { "command2", "command3" });
var userName = "user1";
var commandName = "command2";
if (listOfUserCommands.ContainsKey(userName) && listOfUserCommands[userName].Contains(commandName))
{
commandList[commandName]();
}

可能会修改它以使用命令别名:

public interface IExecutionCommand
{
Action Action { set; get;}
List<string> Aliases { set; get; }
}
public class ExecutionCommand1: IExecutionCommand
{
public Action Action { set; get; }
public List<string> Aliases { set; get; }
public ExecutionCommand1()
{
Action = () => { Console.WriteLine("command1 is executed"); };
Aliases = new List<string>() { "cmd1" };
}
}
public class ExecutionCommand2: IExecutionCommand
{
public Action Action { set; get; }
public List<string> Aliases { set; get; }
public ExecutionCommand2()
{
Action = () => { Console.WriteLine("command2 is executed"); };
Aliases = new List<string>() { "cmd2" };
}
}
public class ExecutionCommand3: IExecutionCommand
{
public Action Action { set; get; }
public List<string> Aliases { set; get; }
public ExecutionCommand3()
{
Action = () => { Console.WriteLine("command3 is executed"); };
Aliases = new List<string>() { "cmd3" };
}
}
public static void Main()
{
//_startScanning();
var commandList = new List<IExecutionCommand>();
commandList.Add(new ExecutionCommand1());
commandList.Add(new ExecutionCommand2());
commandList.Add(new ExecutionCommand3());
var listOfUserCommands = new Dictionary<string, List<string>>();
listOfUserCommands.Add("user1", new List<string>() { "cmd2" });
listOfUserCommands.Add("user2", new List<string>() { "command1", "command3" });
var userName = "user1";
var commandName = "cmd2";
if (listOfUserCommands.ContainsKey(userName) && listOfUserCommands[userName].Contains(commandName))
{
IExecutionCommand command = commandList.FirstOrDefault(x => x.Aliases.Contains(commandName));
command?.Action();
}
}

这就是我遵循奥列格·邦达连科的例子。解决方案并不完美,但突然有人需要它。

Dictionary<string, List<IExecutionCommand>> listOfUserCommands = new Dictionary<string, List<IExecutionCommand>>();
public interface IExecutionCommand
{
Action Action { set; get; }
List<string> Aliases { set; get; }
}
public class commandTest : IExecutionCommand
{
public Action Action { set; get; }
public List<string> Aliases { set; get; }
public commandTest()
{
Action = () => { Console.WriteLine("command1 is executed"); };
Aliases = new List<string>() { "test", "check", "tests" };
}
}
public class commandEx2 : IExecutionCommand
{
public Action Action { set; get; }
public List<string> Aliases { set; get; }
public commandEx2()
{
Action = () => { Console.WriteLine("command2 is executed"); };
Aliases = new List<string>() { "cmd2" };
}
}

private IExecutionCommand FindingCommandFromUser(string commandName, List<IExecutionCommand> commandList)
{
foreach (IExecutionCommand command in commandList)
if (command.Aliases.Contains(commandName))
return command;
return null;
}
public static void Main()
{
listOfUserCommands.Add("user1",
new List<IExecutionCommand>()
{
new commandTest(),
new commandEx2()
});
string userName = "user1";
string commandName = "check";
IExecutionCommand curCommand = null;
if (listOfUserCommands.ContainsKey(userName) && ((curCommand = FindingCommandFromUser(commandName, listOfUserCommands[userName])) != null))
curCommand.Action();
}

最新更新