FormBot.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using CommonLang.Log;
  2. using pomelo.area;
  3. using pomelo.connector;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using XmdsBattleClient.Client;
  13. using XmdsBattleClientBot.Bot;
  14. using XmdsBattleClientWin32.Func;
  15. namespace XmdsBattleClientWin32.WinForm
  16. {
  17. public partial class FormBot : Form
  18. {
  19. public readonly static Logger log = new ConsoleLogger();
  20. private readonly BotClient bot;
  21. private long last_update_time;
  22. private Battle.BattlePanelContainer battle_view;
  23. public BotClient Bot { get { return bot; } }
  24. public FormBot()
  25. {
  26. InitializeComponent();
  27. this.bot = new BotClient("bottest", "123456", FormLauncher.LastConfig, null);
  28. this.bot.Client.GameSocket.IsSaveResponse = true;
  29. // this.bot.Client.GateSocket.OnError += on_error;
  30. // this.bot.Client.GameSocket.OnError += on_error;
  31. this.bot.Client.OnBeginEnterScene += on_gs_begin_enter_scene;
  32. this.battle_view = new Battle.BattlePanelContainer(bot);
  33. this.battle_view.Dock = DockStyle.Fill;
  34. this.Controls.Add(battle_view);
  35. }
  36. private void FormBot_Load(object sender, EventArgs e)
  37. {
  38. bot.Start();
  39. }
  40. private void FormBot_Shown(object sender, EventArgs e)
  41. {
  42. relogin();
  43. }
  44. private void timer_Update_Tick(object sender, EventArgs e)
  45. {
  46. long curTime = CommonLang.CUtils.CurrentTimeMS;
  47. if (last_update_time == 0)
  48. {
  49. last_update_time = curTime;
  50. }
  51. int intervalMS = (int)(curTime - last_update_time);
  52. last_update_time = curTime;
  53. try
  54. {
  55. bot.Update(intervalMS);
  56. if (battle_view != null)
  57. {
  58. battle_view.UpdateBattle(intervalMS);
  59. }
  60. }
  61. catch (Exception err)
  62. {
  63. MessageBox.Show(err.Message);
  64. }
  65. }
  66. //-------------------------------------------------------------------------------------------------
  67. #region Runner
  68. public static void on_error(Exception err)
  69. {
  70. MessageBox.Show(err.Message);
  71. }
  72. private void relogin()
  73. {
  74. var login = new FormLogin(bot);
  75. login.OnLogin += on_login;
  76. login.ShowDialog(this);
  77. }
  78. private void select_role()
  79. {
  80. var r = new FormCreateRole(bot);
  81. r.ShowDialog(this);
  82. }
  83. private void on_login()
  84. {
  85. FormBot.log.Info("on_login...");
  86. bot.QueueTask(() =>
  87. {
  88. start_enter_server();
  89. });
  90. }
  91. private void start_enter_server()
  92. {
  93. bot.gate_EnterServer(on_gate_enter_server, (err) =>
  94. {
  95. on_error(err);
  96. relogin();
  97. });
  98. }
  99. protected virtual void on_gate_enter_server(EntryResponse e)
  100. {
  101. this.Text = string.Format("帐号:{0} 服务器:{1}", bot.Account, bot.ServerName);
  102. FormBot.log.Info("on_gate_enter_server : " + this.Text);
  103. bot.QueueTask(() =>
  104. {
  105. if (e.s2c_players.Count > 0 && Program.AUTO_LOGIN)
  106. {
  107. log.Info("auto bind player ");
  108. bot.gate_BindPlayer(e.s2c_players[0].id,
  109. (rsp) => { log.Info("auto bind player done "); },
  110. (err) =>
  111. {
  112. on_error(err);
  113. select_role();
  114. });
  115. }
  116. else
  117. {
  118. select_role();
  119. }
  120. });
  121. }
  122. protected virtual void on_gs_begin_enter_scene(XmdsBattleClient.Battle.XmdsBattleClient obj)
  123. {
  124. FormBot.log.Info("on_gs_begin_enter_scene : ");
  125. bot.gs_EnterScene(on_gs_enter_scene, (err) => { on_error(err); relogin(); });
  126. }
  127. protected virtual void on_gs_enter_scene(EnterSceneResponse response)
  128. {
  129. FormBot.log.Info("on_gs_enter_scene : ");
  130. if (Program.ARG_TC)
  131. {
  132. FormTC form = new FormTC(bot);
  133. form.Show();
  134. }
  135. }
  136. #endregion
  137. }
  138. }