RobotViewModel.cs 5.2 KB

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