RobotViewModel.cs 5.3 KB

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