BotClient.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using CommonAI.ZoneClient;
  2. using CommonLang;
  3. using CommonLang.Log;
  4. using pomelo.area;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. using XmdsBattleClient.Client;
  12. using XmdsBattleClientBot.XmdsBot;
  13. using XmdsCommonServer.Message;
  14. namespace XmdsBattleClientBot.Bot
  15. {
  16. /// <summary>
  17. /// 全自动战斗机器人,基础类
  18. /// </summary>
  19. public partial class BotClient : IDisposable
  20. {
  21. private readonly Logger log;
  22. private readonly Random random;
  23. private readonly BotConfig config;
  24. private readonly XmdsNetClient client;
  25. private readonly IBotListViewItem mListViewCallback;
  26. private bool _IsInScene; //是否进入到游戏场景中
  27. private string account;
  28. private string password;
  29. public int ServerId;
  30. public string ServerName;
  31. public string Host;
  32. public int Port;
  33. public string Sign;
  34. private SyncMessageQueue<Action> tasks = new SyncMessageQueue<Action>();
  35. public int LineIndex;
  36. public bool IsFree = true;
  37. public string Account { get { return account; } }
  38. public string Password { get { return password; } }
  39. public BotConfig Config { get { return config; } }
  40. public XmdsNetClient Client { get { return client; } }
  41. public Random Random { get { return random; } }
  42. public Pomelo.DotNetClient.NetWorkState NetState { get; private set; }
  43. public virtual bool IsRunning { get; }
  44. public virtual string RoleName
  45. {
  46. get
  47. {
  48. if (client != null && client.BattleActor != null)
  49. {
  50. var prop = client.BattleActor.LoginData.GameServerProp as XmdsCommon.Plugin.XmdsUnitProperties;
  51. return prop.ServerData.BaseInfo.name;
  52. }
  53. return "";
  54. }
  55. }
  56. public virtual string SceneName
  57. {
  58. get
  59. {
  60. if (client != null && client.BattleActor != null && NetState == Pomelo.DotNetClient.NetWorkState.CONNECTED)
  61. {
  62. var prop = client.BattleClient.Layer.Data;
  63. return LineIndex == 0 ? prop.ToString() : prop.Name + "(" + LineIndex + "线)";
  64. }
  65. return "";
  66. }
  67. }
  68. public BotClient(string user, string pswd, BotConfig cfg, IBotListViewItem callBack)
  69. {
  70. this.log = LoggerFactory.GetLogger("bot[" + user + "]");
  71. this.config = cfg;
  72. this.account = user;
  73. this.password = pswd;
  74. this.random = new Random(account.GetHashCode() + DateTime.Now.Millisecond);
  75. this.mListViewCallback = callBack;
  76. this.client = new XmdsNetClient(true);
  77. this._IsInScene = false;
  78. connect();
  79. this.client.GateSocket.NetWorkStateChangedEvent += callback_gt_NetWorkStateChangedEvent;
  80. this.client.GameSocket.NetWorkStateChangedEvent += callback_gs_NetWorkStateChangedEvent;
  81. this.client.OnBeginEnterScene += callback_client_OnCreateBattleClient;
  82. this.client.OnBattlePlayerReady += callback_client_OnBattlePlayerReady;
  83. this.client.GameSocket.listen<TaskUpdatePush>(callback_gs_onTaskUpdatePush);
  84. this.client.GameSocket.listen<BagItemUpdatePush>(callback_gs_BagItemUpdatePush);
  85. this.client.GameSocket.listen<ChangeAreaPush>(callback_gs_ChangeAreaPush);
  86. }
  87. public void connect(Action onConnectted = null)
  88. {
  89. string[] host = Config.LoginUrl.Split(':');
  90. string ip = host[0];
  91. int port = Convert.ToInt32(host[1]);
  92. Host = ip;
  93. Port = port;
  94. Client.GateSocket.connect(ip, port, 15000, (result) =>
  95. {
  96. if (result != null && result.Count > 0)
  97. {
  98. //MessageBox.Show(Convert.ToString(result["s2c_msg"]));
  99. MessageBox.Show(ip + ":" + port + "网络连接失败");
  100. } else if(onConnectted != null)
  101. {
  102. onConnectted();
  103. }
  104. if(this.mListViewCallback != null)
  105. {
  106. this.mListViewCallback.Start();
  107. }
  108. });
  109. }
  110. public void SetAccount(string user, string pswd)
  111. {
  112. this.account = user;
  113. this.password = pswd;
  114. }
  115. private void callback_gt_NetWorkStateChangedEvent(Pomelo.DotNetClient.NetWorkState obj)
  116. {
  117. this.NetState = obj;
  118. }
  119. private void callback_gs_NetWorkStateChangedEvent(Pomelo.DotNetClient.NetWorkState obj)
  120. {
  121. this.NetState = obj;
  122. //网络异常状态,自动退出场景
  123. if(!Pomelo.DotNetClient.NetWorkState.CONNECTING.Equals(obj) || !Pomelo.DotNetClient.NetWorkState.CONNECTED.Equals(obj)) {
  124. this._IsInScene = false;
  125. }
  126. }
  127. private void callback_client_OnBattlePlayerReady(ZoneLayer arg1, ZoneActor arg2)
  128. {
  129. //arg2.SendAction(new MarkForTestBotAction());
  130. }
  131. public virtual void Start()
  132. {
  133. lock (this)
  134. {
  135. if (event_OnStart != null) event_OnStart.Invoke(this);
  136. }
  137. }
  138. public virtual void Stop()
  139. {
  140. lock (this)
  141. {
  142. client.Disconnect();
  143. if (event_OnStop != null) event_OnStop.Invoke(this);
  144. }
  145. }
  146. public virtual void Dispose()
  147. {
  148. event_OnStart = null;
  149. event_OnStop = null;
  150. event_OnBattleUpdate = null;
  151. event_OnUpdate = null;
  152. client.Dispose();
  153. }
  154. public virtual void Update(int intervalMS)
  155. {
  156. try
  157. {
  158. lock (this)
  159. {
  160. tasks.ProcessMessages(do_task);
  161. if (event_OnUpdate != null)
  162. event_OnUpdate.Invoke(this);
  163. if (client != null)
  164. {
  165. client.UpdateSocket(intervalMS);
  166. client.DoUpdateSend();
  167. //成功进入到场景里面才进行场景刷新,BindPlayer是一个点
  168. if (this._IsInScene)
  169. client.UpdateBattleClient(intervalMS);
  170. if (client.BattleClient != null && client.BattleClient.Actor != null && event_OnBattleUpdate != null)
  171. {
  172. event_OnBattleUpdate.Invoke(client.BattleClient.Actor);
  173. }
  174. if (CurrentRegionManager != null)
  175. {
  176. CurrentRegionManager.Update();
  177. }
  178. }
  179. }
  180. }
  181. catch (Exception err)
  182. {
  183. log.Error(err.Message, err);
  184. }
  185. }
  186. public void QueueTask(Action action)
  187. {
  188. tasks.Enqueue(action);
  189. }
  190. private void do_task(Action action)
  191. {
  192. action();
  193. }
  194. public event Action<BotClient> event_OnStart;
  195. public event Action<BotClient> event_OnStop;
  196. public event Action<BotClient> event_OnUpdate;
  197. public event Action<ZoneActor> event_OnBattleUpdate;
  198. }
  199. }