RobotViewModel.cs 4.9 KB

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