RobotCaseHelper.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ET.Server
  4. {
  5. public static class RobotCaseSystem
  6. {
  7. // 创建机器人,生命周期是RobotCase
  8. public static async ETTask NewRobot(this RobotCase self, int count, List<Scene> scenes)
  9. {
  10. ETTask[] tasks = new ETTask[count];
  11. for (int i = 0; i < count; ++i)
  12. {
  13. tasks[i] = self.NewRobot(scenes);
  14. }
  15. await ETTaskHelper.WaitAll(tasks);
  16. }
  17. private static async ETTask NewRobot(this RobotCase self, List<Scene> scenes)
  18. {
  19. try
  20. {
  21. scenes.Add(await self.NewRobot());
  22. }
  23. catch (Exception e)
  24. {
  25. Log.Error(e);
  26. }
  27. }
  28. // 创建机器人,生命周期是RobotCase
  29. public static async ETTask NewZoneRobot(this RobotCase self, int zone, int count, List<Scene> scenes)
  30. {
  31. ETTask[] tasks = new ETTask[count];
  32. for (int i = 0; i < count; ++i)
  33. {
  34. tasks[i] = self.NewZoneRobot(zone + i, scenes);
  35. }
  36. await ETTaskHelper.WaitAll(tasks);
  37. }
  38. // 这个方法创建的是进程所属的机器人,建议使用RobotCase.NewRobot来创建
  39. private static async ETTask NewZoneRobot(this RobotCase self, int zone, List<Scene> scenes)
  40. {
  41. try
  42. {
  43. scenes.Add(await self.NewRobot(zone));
  44. }
  45. catch (Exception e)
  46. {
  47. Log.Error(e);
  48. }
  49. }
  50. public static async ETTask<Scene> NewRobot(this RobotCase self, int zone)
  51. {
  52. return await self.NewRobot(zone, $"Robot_{zone}");
  53. }
  54. public static async ETTask<Scene> NewRobot(this RobotCase self, int zone, string name)
  55. {
  56. Scene clientScene = null;
  57. try
  58. {
  59. clientScene = await Client.SceneFactory.CreateClientScene(zone, name);
  60. await Client.LoginHelper.Login(clientScene, zone.ToString(), zone.ToString());
  61. await Client.EnterMapHelper.EnterMapAsync(clientScene);
  62. Log.Debug($"create robot ok: {zone}");
  63. return clientScene;
  64. }
  65. catch (Exception e)
  66. {
  67. clientScene?.Dispose();
  68. throw new Exception($"RobotCase create robot fail, zone: {zone}", e);
  69. }
  70. }
  71. private static async ETTask<Scene> NewRobot(this RobotCase self)
  72. {
  73. int zone = self.GetParent<RobotCaseComponent>().GetN();
  74. Scene clientScene = null;
  75. try
  76. {
  77. clientScene = await Client.SceneFactory.CreateClientScene(zone, $"Robot_{zone}");
  78. await Client.LoginHelper.Login(clientScene, zone.ToString(), zone.ToString());
  79. await Client.EnterMapHelper.EnterMapAsync(clientScene);
  80. Log.Debug($"create robot ok: {zone}");
  81. return clientScene;
  82. }
  83. catch (Exception e)
  84. {
  85. clientScene?.Dispose();
  86. throw new Exception($"RobotCase create robot fail, zone: {zone}", e);
  87. }
  88. }
  89. }
  90. }