CreateRobotConsoleHandler.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using CommandLine;
  4. namespace ET.Server
  5. {
  6. [ConsoleHandler(ConsoleMode.CreateRobot)]
  7. public class CreateRobotConsoleHandler: IConsoleHandler
  8. {
  9. public async ETTask Run(Fiber fiber, ModeContex contex, string content)
  10. {
  11. switch (content)
  12. {
  13. case ConsoleMode.CreateRobot:
  14. {
  15. Log.Console("CreateRobot args error!");
  16. break;
  17. }
  18. default:
  19. {
  20. CreateRobotArgs options = null;
  21. Parser.Default.ParseArguments<CreateRobotArgs>(content.Split(' '))
  22. .WithNotParsed(error => throw new Exception($"CreateRobotArgs error!"))
  23. .WithParsed(o => { options = o; });
  24. RobotManagerComponent robotManagerComponent =
  25. fiber.Root.GetComponent<RobotManagerComponent>() ?? fiber.Root.AddComponent<RobotManagerComponent>();
  26. // 创建机器人
  27. TimerComponent timerComponent = fiber.Root.GetComponent<TimerComponent>();
  28. for (int i = 0; i < options.Num; ++i)
  29. {
  30. await robotManagerComponent.NewRobot($"Robot_{i}");
  31. Log.Console($"create robot {i}");
  32. await timerComponent.WaitAsync(2000);
  33. }
  34. break;
  35. }
  36. }
  37. contex.Parent.RemoveComponent<ModeContex>();
  38. await ETTask.CompletedTask;
  39. }
  40. }
  41. }