2014-02-10 117 views
0

所以在我的遊戲服務器上我們有服務器端命令。我想知道是否有可能把這個返回一個字符串數組?

public string Command 
{ 
    get { return "g"; } 
} 

弄成這個樣子

public string Command 
{ 
    get { return "g", "guild", "group"; } 
} 

這裏的命令接口代碼

internal interface ICommand 
{ 
    string Command { get; } 
    int RequiredRank { get; } 
    void Execute(Player player, string[] args); 
} 

這裏的命令處理程序的代碼
第1部分:

ProcessCmd(x[0].Trim('/'), x.Skip(1).ToArray()); 

第2部分:

private void ProcessCmd(string cmd, string[] args) 
{ 
    if (cmds == null) 
    { 
     cmds = new Dictionary<string, ICommand>(); 
     var t = typeof (ICommand); 
     foreach (var i in t.Assembly.GetTypes()) 
      if (t.IsAssignableFrom(i) && i != t) 
      { 
       var instance = (ICommand) Activator.CreateInstance(i); 
       cmds.Add(instance.Command, instance); 
      } 
    } 

    ICommand command; 
    if (!cmds.TryGetValue(cmd, out command)) 
    { 
     psr.SendPacket(new TextPacket 
     { 
      BubbleTime = 0, 
      Stars = -1, 
      Name = "*Error*", 
      Text = "Unknown Command!" 
     }); 
     return; 
    } 
    try 
    { 
     ExecCmd(command, args); 
    } 
    catch (Exception e) 
    { 
     Console.Out.WriteLine(e); 
     psr.SendPacket(new TextPacket 
     { 
      BubbleTime = 0, 
      Stars = -1, 
      Name = "*Error*", 
      Text = "Error when executing the command!" 
     }); 
    } 
} 
+0

你可以命令屬性更改爲字符串[] –

+0

你可以更改界面? –

+1

我真的不知道關於這個問題,但它聽起來那麼簡單,用'字符串替換字符串'Command' [] – Habib

回答

3

你想這樣的:

public string[] Commands 
{ 
    get { return new string[] {"g", "guild", "group"}; } 
} 

開始與此有關。它會產生你應該修復的錯誤,並通過在修復過程中推進,你將重構代碼的很好的一部分,如ICommand界面。

+0

此外,'ExecCmd()'和其它下游代碼將不得不改變到一個字符串數組上進行操作。 –

+0

準確。我也感* *一個'的foreach的必要性()'* *某處將蠕變有.... :) – pid

+0

我不認爲你必須改變'ExecCmd',因爲它需要一個'ICommand'並可能只是運行'ICommand.Execute(...)'。 – sloth

0

如果您不允許更改界面,請使用分隔符。

public string Command 
{ 
    get { return string.Join("|", "g", "guild", "group"); } 
} 

用例:

foreach (var command in source.Command.Split('|')) 
{ 
    ExecCmd(command, args); 
} 
0

如果你的命令,不保持任何狀態,只是基本上由一個單一的方法(好像是),我建議取下接口和類共只需使用附帶一些元數據(屬性)的簡單函數即可。

這將通過刪除一堆類來簡化代碼。

例子:

// simple attribute to mark functions as commands 
class CommandAttribute : Attribute 
{ 
    public string Name {get;private set;} 
    public string[] Keys {get;private set;} 
    public int Rank {get;private set;} 

    public CommandAttribute(string name, int rank, params string[] keys) 
    { 
     Name = name; 
     Keys = keys; 
     Rank = rank; 
    } 
} 

// example commands 
[Command("Guild", 15, "g", "guild", "group")] 
public static void Guild(Player player,string[] args) 
{ 
    // do stuff 
} 

[Command("Something other", 5, "f", "foo", "foobar")] 
public static void FooIt(Player player,string[] args) 
{ 
    // do stuff 
} 

尋找一個組件內部的功能也很容易:

delegate void CommandHandler(Player player, string[] args); 

var commands = from t in Assembly.GetExecutingAssembly().GetTypes() 
       from m in t.GetMethods(BindingFlags.Static|BindingFlags.Public) 
       let attr = (CommandAttribute)m.GetCustomAttribute(typeof(CommandAttribute)) 
       where attr != null 
       select new 
       { 
        attr, 
        function = Delegate.CreateDelegate(typeof(CommandHandler), m) 
       }; 

// probably use some other data structure if you want to lookup attr.Name and attr.Rank, too 
var cmds = new Dictionary<string, CommandHandler>(); 
foreach(var item in commands) 
    foreach(var key in item.attr.Keys) 
     cmds.Add(key, (CommandHandler)item.function); 
相關問題