BotRunner.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using DeepCore;
  2. using DeepCore.Concurrent;
  3. using DeepCore.IO;
  4. using DeepCore.Log;
  5. using DeepCore.Net;
  6. using DeepCore.Reflection;
  7. using DeepMMO.Protocol;
  8. using DeepMMO.Protocol.Client;
  9. using System;
  10. using System.Collections.Generic;
  11. namespace DeepMMO.Client.BotTest.Runner
  12. {
  13. public partial class BotRunner : Disposable
  14. {
  15. protected readonly ListLogger log;
  16. protected readonly RPGClient client;
  17. protected readonly AddBotConfig add;
  18. protected readonly BotConfig cfg;
  19. protected readonly string account;
  20. protected readonly AtomicReference<string> status = new AtomicReference<string>("");
  21. protected readonly AtomicReference<string> net_status = new AtomicReference<string>("");
  22. protected readonly Random random = new Random();
  23. public RPGClient Client { get { return client; } }
  24. public ListLogger LogEvents { get { return log; } }
  25. public string AccountName { get { return account; } }
  26. public string RoleName { get { return client.RoleName; } }
  27. public string RoleUUID { get { return client.RoleUUID; } }
  28. public string Status { get { return status.Value; } }
  29. public string NetState
  30. {
  31. get
  32. {
  33. if (client.GameClient.IsConnected)
  34. {
  35. return string.Format("{0} Ping={1}/{2}",
  36. client.GameClient.IsConnected ? "已连接" : (client.IsGameDisconnected ? "已掉线" : "未连接"),
  37. client.CurrentPing,
  38. client.CurrentBattlePing);
  39. }
  40. else if (client.GateClient.IsConnected)
  41. {
  42. return net_status.Value;
  43. }
  44. else
  45. {
  46. return string.Empty;
  47. }
  48. }
  49. }
  50. public string[] Columns
  51. {
  52. get
  53. {
  54. return new string[] { account, $"{RoleName}", $"{client.SceneData}", Status, NetState };
  55. }
  56. }
  57. public BotRunner(RPGClient c, BotConfig cfg, AddBotConfig add, string account)
  58. {
  59. this.log = new ListLogger("bot[" + account + "]-");
  60. this.cfg = cfg;
  61. this.add = add;
  62. this.account = account;
  63. this.client = c;
  64. this.client.OnGateEntered += on_gate_connect_callback;
  65. this.client.GateClient.Listen<ClientEnterGateInQueueNotify>(on_gate_in_queue);
  66. foreach (var type in BotFactory.Instance.GetModuleTypes())
  67. {
  68. var m = ReflectionUtil.CreateInterface<BotModule>(type, this);
  69. modules.Add(m.GetType(), m);
  70. }
  71. client.AddTimePeriodicMS(10000, (e) =>
  72. {
  73. if (client.CurrentZoneLayer != null && client.GameClient != null && client.GameClient.IsHandshake)
  74. {
  75. client.GameClient.Request<ClientPong>(new ClientPing(), (s, a) => { });
  76. }
  77. });
  78. }
  79. protected override void Disposing()
  80. {
  81. lock (this)
  82. {
  83. client.Disconnect();
  84. client.Dispose();
  85. modules.Clear();
  86. }
  87. }
  88. public void Start()
  89. {
  90. this.net_status.Value = "";
  91. lock (this)
  92. {
  93. if (client.IsDisposed) return;
  94. var server = RPGClientTemplateManager.Instance.GetServer(add.serverID);
  95. if (server != null && IPUtil.TryParseHostPort(server.address, out var host, out var port))
  96. {
  97. client.Gate_Connect(host, port, account, add.password, add.serverID, (rsp) =>
  98. {
  99. if (rsp.s2c_code == ClientEnterGateResponse.CODE_OK_IN_QUEUE)
  100. {
  101. this.net_status.Value = "排队中:";
  102. }
  103. });
  104. }
  105. }
  106. }
  107. public void Stop()
  108. {
  109. client.Disconnect();
  110. this.net_status.Value = string.Empty;
  111. this.status.Value = string.Empty;
  112. }
  113. public void Update(int intervalMS)
  114. {
  115. client.Update(intervalMS);
  116. foreach (var m in modules.Values)
  117. {
  118. m.InternalUpdate(intervalMS);
  119. }
  120. }
  121. public void Reconnect()
  122. {
  123. client.Connect_Connect(on_game_connect_callback);
  124. }
  125. public void Disconnect()
  126. {
  127. client.Disconnect();
  128. }
  129. public override string ToString()
  130. {
  131. return client.AccountName;
  132. }
  133. public void PopLogs(List<string> list)
  134. {
  135. log.PopLogs(list);
  136. }
  137. //--------------------------------------------------------------------------------------------------------
  138. protected virtual void on_error(Exception err)
  139. {
  140. log.Error(err.Message, err);
  141. }
  142. protected virtual void log_response(Response rsp, Exception err = null, string msg = null)
  143. {
  144. var suffix = (msg != null ? " - " + msg : string.Empty);
  145. var prefix = (rsp != null ? string.Format("{0}:{1}:{2} - ", rsp.GetType().Name, rsp.s2c_code, rsp.s2c_msg) : string.Format("{0}", rsp));
  146. if (err != null)
  147. log.Error(string.Format("{0}{1}{2}", prefix, err.Message, suffix), err);
  148. else if (rsp != null)
  149. log.Debug(string.Format("{0}{1}", prefix, suffix));
  150. }
  151. //--------------------------------------------------------------------------------------------------------
  152. }
  153. }