RobotViewModel.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel.Composition;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Windows.Threading;
  8. using BossClient;
  9. using Helper;
  10. using Log;
  11. using Microsoft.Practices.Prism.ViewModel;
  12. namespace Modules.Robot
  13. {
  14. [Export(contractType: typeof (RobotViewModel)),
  15. PartCreationPolicy(creationPolicy: CreationPolicy.NonShared)]
  16. internal sealed class RobotViewModel: NotificationObject, IDisposable
  17. {
  18. private string account = "egametang@163.com";
  19. private string password = "163bio1";
  20. private int findTypeIndex;
  21. private string findType = "";
  22. private bool isButtonEnable;
  23. private readonly BossClient.BossClient bossClient = new BossClient.BossClient();
  24. private readonly ObservableCollection<ServerViewModel> serverInfos =
  25. new ObservableCollection<ServerViewModel>();
  26. public readonly Dictionary<ushort, Action<byte[]>> messageHandlers =
  27. new Dictionary<ushort, Action<byte[]>>();
  28. private readonly DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
  29. { Interval = new TimeSpan(0, 0, 0, 0, 50) };
  30. public string Account
  31. {
  32. get
  33. {
  34. return this.account;
  35. }
  36. set
  37. {
  38. if (this.account == value)
  39. {
  40. return;
  41. }
  42. this.account = value;
  43. this.RaisePropertyChanged("Account");
  44. }
  45. }
  46. public string Password
  47. {
  48. get
  49. {
  50. return this.password;
  51. }
  52. set
  53. {
  54. if (this.password == value)
  55. {
  56. return;
  57. }
  58. this.password = value;
  59. this.RaisePropertyChanged("Password");
  60. }
  61. }
  62. public int FindTypeIndex
  63. {
  64. get
  65. {
  66. return this.findTypeIndex;
  67. }
  68. set
  69. {
  70. if (this.findTypeIndex == value)
  71. {
  72. return;
  73. }
  74. this.findTypeIndex = value;
  75. this.RaisePropertyChanged("FindTypeIndex");
  76. }
  77. }
  78. public string FindType
  79. {
  80. get
  81. {
  82. return this.findType;
  83. }
  84. set
  85. {
  86. if (this.findType == value)
  87. {
  88. return;
  89. }
  90. this.findType = value;
  91. this.RaisePropertyChanged("FindType");
  92. }
  93. }
  94. public bool IsButtonEnable
  95. {
  96. get
  97. {
  98. return this.isButtonEnable;
  99. }
  100. set
  101. {
  102. if (this.isButtonEnable == value)
  103. {
  104. return;
  105. }
  106. this.isButtonEnable = value;
  107. this.RaisePropertyChanged("IsButtonEnable");
  108. }
  109. }
  110. public ObservableCollection<ServerViewModel> ServerInfos
  111. {
  112. get
  113. {
  114. return this.serverInfos;
  115. }
  116. }
  117. public RobotViewModel()
  118. {
  119. this.messageHandlers.Add(
  120. MessageOpcode.SMSG_BOSS_SERVERSINFO, Handle_SMSG_Boss_ServersInfo);
  121. this.timer.Tick += delegate { this.bossClient.RunOnce(); };
  122. this.timer.Start();
  123. }
  124. ~RobotViewModel()
  125. {
  126. this.Disposing(false);
  127. }
  128. public void Dispose()
  129. {
  130. this.Disposing(true);
  131. GC.SuppressFinalize(this);
  132. }
  133. private void Disposing(bool disposing)
  134. {
  135. this.bossClient.Dispose();
  136. }
  137. public async void Login()
  138. {
  139. var config = Config.Instance;
  140. await this.bossClient.Login(config.IP, config.Port,
  141. this.Account, this.Password);
  142. this.IsButtonEnable = true;
  143. this.HandleMessages();
  144. }
  145. public async void HandleMessages()
  146. {
  147. try
  148. {
  149. while (true)
  150. {
  151. var result = await this.bossClient.GateSession.IMessageChannel.RecvMessage();
  152. ushort opcode = result.Item1;
  153. byte[] message = result.Item2;
  154. if (!messageHandlers.ContainsKey(opcode))
  155. {
  156. Logger.Debug("not found opcode: {0}", opcode);
  157. continue;
  158. }
  159. messageHandlers[opcode](message);
  160. }
  161. }
  162. catch (Exception e)
  163. {
  164. this.IsButtonEnable = false;
  165. Logger.Trace(e.ToString());
  166. }
  167. }
  168. public void Servers()
  169. {
  170. this.bossClient.SendCommand("servers");
  171. }
  172. public void Handle_SMSG_Boss_ServersInfo(byte[] message)
  173. {
  174. var smsgBossServersInfo = ProtobufHelper.FromBytes<SMSG_Boss_ServersInfo>(message);
  175. this.ServerInfos.Clear();
  176. foreach (var name in smsgBossServersInfo.Name)
  177. {
  178. this.ServerInfos.Add(new ServerViewModel { Name = name });
  179. }
  180. }
  181. public void Reload()
  182. {
  183. this.bossClient.SendCommand("reload");
  184. }
  185. public void Find()
  186. {
  187. using (var entitys = new DataCenterEntities())
  188. {
  189. t_character result = null;
  190. switch (this.FindTypeIndex)
  191. {
  192. case 0:
  193. {
  194. result = entitys.t_character.FirstOrDefault(
  195. c => c.account == this.FindType);
  196. break;
  197. }
  198. case 1:
  199. {
  200. result = entitys.t_character.FirstOrDefault(
  201. c => c.character_name == this.FindType);
  202. break;
  203. }
  204. case 2:
  205. {
  206. var guid = Decimal.Parse(this.FindType);
  207. result = entitys.t_character.FirstOrDefault(
  208. c => c.character_guid == guid);
  209. break;
  210. }
  211. }
  212. if (result == null)
  213. {
  214. Logger.Debug("not find charactor info!");
  215. return;
  216. }
  217. Logger.Debug("Account: {0}, Name: {1}, GUID: {2}",
  218. result.account, result.character_name, result.character_guid);
  219. }
  220. }
  221. }
  222. }