BotConsole.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using CommonLang;
  2. using CommonLang.Property;
  3. using CommonServer.Command;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace CommonAIServer.Connector.Bot
  11. {
  12. public class BotConsoleCommand : CommandList<BotConsoleCommand.Cmd>
  13. {
  14. public static BotConsoleCommand Instance { get; private set; }
  15. public BotConsoleCommand()
  16. {
  17. Instance = this;
  18. }
  19. public void Run()
  20. {
  21. Console.WriteLine("使用cmdlist列出所有指令");
  22. while (BotRunner.Instance.IsRunning)
  23. {
  24. string input = Console.In.ReadLine();
  25. if (!DoCommand(input, Console.Out))
  26. {
  27. Console.WriteLine();
  28. Console.WriteLine("未知的命令:" + input);
  29. Console.WriteLine("使用cmdlist列出所有指令");
  30. }
  31. else
  32. {
  33. Console.WriteLine();
  34. }
  35. }
  36. }
  37. // -------------------------------------------------------------------------------------------------
  38. public abstract class Cmd : AbstractCommand
  39. {
  40. }
  41. [DescAttribute("列出所有命令")]
  42. public class CMD_LIST : Cmd
  43. {
  44. public override string Key { get { return "cmdlist"; } }
  45. public override string Help
  46. {
  47. get
  48. {
  49. return
  50. "列出所有命令\n" +
  51. " cmdlist 列出所有控制台命令\n" +
  52. " cmdlist [前缀] 列出所有前缀相符的控制台命令";
  53. }
  54. }
  55. public override void DoCommand(string arg, TextWriter output)
  56. {
  57. output.WriteLine(BotConsoleCommand.Instance.ListCommand(arg));
  58. }
  59. }
  60. [DescAttribute("关闭")]
  61. public class CMD_EXIT : Cmd
  62. {
  63. public override string Key { get { return "exit"; } }
  64. public override void DoCommand(string arg, TextWriter output)
  65. {
  66. BotRunner.Instance.Shutdown();
  67. }
  68. }
  69. [DescAttribute("清理内存")]
  70. public class CMD_GC : Cmd
  71. {
  72. public override string Key { get { return "gc"; } }
  73. public override void DoCommand(string arg, TextWriter output)
  74. {
  75. GC.Collect();
  76. output.WriteLine(string.Format("HEAP = {0}", CUtils.ToBytesSizeString(GC.GetTotalMemory(false))));
  77. }
  78. }
  79. //---------------------------------------------------------------------------------------------------
  80. [DescAttribute("添加机器人", "机器人")]
  81. public class CMD_AddBots : Cmd
  82. {
  83. public override string Key { get { return "add"; } }
  84. public override string Help { get { return " add <数量> <阵营> <模板组>"; } }
  85. public override void DoCommand(string arg, TextWriter output)
  86. {
  87. int count = 1;
  88. int force = 2;
  89. List<int> templates = null;
  90. var args = ToArgs(arg);
  91. if (args.Length >= 1)
  92. {
  93. count = int.Parse(args[0]);
  94. }
  95. if (args.Length >= 2)
  96. {
  97. force = int.Parse(args[1]);
  98. }
  99. if (args.Length >= 3)
  100. {
  101. Parser.StringToObject(args[2], out templates);
  102. }
  103. BotRunner.Instance.AddBots(count, force, templates);
  104. }
  105. }
  106. [DescAttribute("清理机器人", "机器人")]
  107. public class CMD_CleanupBots : Cmd
  108. {
  109. public override string Key { get { return "clean"; } }
  110. public override void DoCommand(string arg, TextWriter output)
  111. {
  112. BotRunner.Instance.StopAllBots();
  113. BotRunner.Instance.CleanupBots();
  114. }
  115. }
  116. [DescAttribute("机器人状态", "机器人")]
  117. public class CMD_ListBots : Cmd
  118. {
  119. public override string Key { get { return "ls"; } }
  120. public override void DoCommand(string arg, TextWriter output)
  121. {
  122. output.WriteLine(BotRunner.Instance.BotsStatus);
  123. }
  124. }
  125. //---------------------------------------------------------------------------------------------------
  126. [DescAttribute("开始批量增加定时器", "机器人")]
  127. public class CMD_StartTimer : Cmd
  128. {
  129. private static AtomicState<bool> mIsRunning = new AtomicState<bool>(false);
  130. public static bool IsRunning
  131. {
  132. get { return mIsRunning.Value; }
  133. set { mIsRunning.ChangeState(value); }
  134. }
  135. public override string Key { get { return "start"; } }
  136. public override string Help { get { return " start <数量> <阵营组> <模板组>"; } }
  137. public override void DoCommand(string arg, TextWriter output)
  138. {
  139. var args = ToArgs(arg);
  140. int count = int.Parse(args[0]);
  141. List<int> forces = null;
  142. List<int> templates = null;
  143. Parser.StringToObject(args[1], out forces);
  144. Parser.StringToObject(args[2], out templates);
  145. Random rd = new Random();
  146. IsRunning = true;
  147. BotRunner.Instance.AddBots(count, CUtils.GetRandomInArray(forces, rd), templates);
  148. System.Threading.Thread timer = new System.Threading.Thread((o) =>
  149. {
  150. System.Threading.Thread.Sleep(1000);
  151. while (IsRunning)
  152. {
  153. try
  154. {
  155. var list = BotRunner.Instance.BotsList;
  156. foreach (var bot in list)
  157. {
  158. if (bot.IsRunning)
  159. {
  160. BotRunner.Instance.StopBot(bot);
  161. break;
  162. }
  163. }
  164. System.Threading.Thread.Sleep(1000);
  165. BotRunner.Instance.AddBots(1, CUtils.GetRandomInArray(forces, rd), templates);
  166. }
  167. catch (Exception err)
  168. {
  169. Console.WriteLine(err.Message + "\n" + err.StackTrace);
  170. }
  171. }
  172. });
  173. timer.Start();
  174. }
  175. }
  176. [DescAttribute("停止批量增加定时器", "机器人")]
  177. public class CMD_StopTimer : Cmd
  178. {
  179. public override string Key { get { return "stop"; } }
  180. public override void DoCommand(string arg, TextWriter output)
  181. {
  182. CMD_StartTimer.IsRunning = false;
  183. }
  184. }
  185. //---------------------------------------------------------------------------------------------------
  186. }
  187. }