RobotViewModel.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. private string errorInfo = "";
  35. public IMessageChannel IMessageChannel { get; set; }
  36. public int FindTypeIndex
  37. {
  38. get
  39. {
  40. return this.findTypeIndex;
  41. }
  42. set
  43. {
  44. if (this.findTypeIndex == value)
  45. {
  46. return;
  47. }
  48. this.findTypeIndex = value;
  49. this.RaisePropertyChanged("FindTypeIndex");
  50. }
  51. }
  52. public string FindType
  53. {
  54. get
  55. {
  56. return this.findType;
  57. }
  58. set
  59. {
  60. if (this.findType == value)
  61. {
  62. return;
  63. }
  64. this.findType = value;
  65. this.RaisePropertyChanged("FindType");
  66. }
  67. }
  68. public Visibility DockPanelVisiable
  69. {
  70. get
  71. {
  72. return this.dockPanelVisiable;
  73. }
  74. set
  75. {
  76. if (this.dockPanelVisiable == value)
  77. {
  78. return;
  79. }
  80. this.dockPanelVisiable = value;
  81. this.RaisePropertyChanged("DockPanelVisiable");
  82. }
  83. }
  84. public ObservableCollection<ServerViewModel> ServerInfos
  85. {
  86. get
  87. {
  88. return this.serverInfos;
  89. }
  90. }
  91. public string Account
  92. {
  93. get
  94. {
  95. return this.account;
  96. }
  97. set
  98. {
  99. if (this.account == value)
  100. {
  101. return;
  102. }
  103. this.account = value;
  104. this.RaisePropertyChanged("Account");
  105. }
  106. }
  107. public string Name
  108. {
  109. get
  110. {
  111. return this.name;
  112. }
  113. set
  114. {
  115. if (this.name == value)
  116. {
  117. return;
  118. }
  119. this.name = value;
  120. this.RaisePropertyChanged("Name");
  121. }
  122. }
  123. public string Guid
  124. {
  125. get
  126. {
  127. return this.guid;
  128. }
  129. set
  130. {
  131. if (this.guid == value)
  132. {
  133. return;
  134. }
  135. this.guid = value;
  136. this.RaisePropertyChanged("Guid");
  137. }
  138. }
  139. public string ErrorInfo
  140. {
  141. get
  142. {
  143. return this.errorInfo;
  144. }
  145. set
  146. {
  147. if (this.errorInfo == value)
  148. {
  149. return;
  150. }
  151. this.errorInfo = value;
  152. this.RaisePropertyChanged("ErrorInfo");
  153. }
  154. }
  155. [ImportingConstructor]
  156. public RobotViewModel(IEventAggregator eventAggregator)
  157. {
  158. this.messageHandlers.Add(
  159. MessageOpcode.SMSG_BOSS_SERVERSINFO, this.Handle_SMSG_Boss_ServersInfo);
  160. this.messageHandlers.Add(
  161. MessageOpcode.SMSG_BOSS_COMMAND_RESPONSE, this.Handle_SMSG_Boss_Command_Response);
  162. eventAggregator.GetEvent<LoginOKEvent>().Subscribe(this.OnLoginOK);
  163. }
  164. ~RobotViewModel()
  165. {
  166. this.Disposing(false);
  167. }
  168. public void Dispose()
  169. {
  170. this.Disposing(true);
  171. GC.SuppressFinalize(this);
  172. }
  173. private void Disposing(bool disposing)
  174. {
  175. this.bossClient.Dispose();
  176. }
  177. public async void OnLoginOK(IMessageChannel messageChannel)
  178. {
  179. this.DockPanelVisiable = Visibility.Visible;
  180. this.IMessageChannel = messageChannel;
  181. try
  182. {
  183. while (true)
  184. {
  185. var result = await this.IMessageChannel.RecvMessage();
  186. ushort opcode = result.Item1;
  187. byte[] message = result.Item2;
  188. if (!messageHandlers.ContainsKey(opcode))
  189. {
  190. Logger.Debug("not found opcode: {0}", opcode);
  191. continue;
  192. }
  193. messageHandlers[opcode](message);
  194. }
  195. }
  196. catch (Exception e)
  197. {
  198. this.dockPanelVisiable = Visibility.Hidden;
  199. Logger.Trace(e.ToString());
  200. }
  201. }
  202. public void SendCommand(string command)
  203. {
  204. var cmsgBossGm = new CMSG_Boss_Gm
  205. {
  206. Message = command
  207. };
  208. this.IMessageChannel.SendMessage(MessageOpcode.CMSG_BOSS_GM, cmsgBossGm);
  209. }
  210. public void Servers()
  211. {
  212. this.SendCommand("servers");
  213. }
  214. public void Handle_SMSG_Boss_Command_Response(byte[] message)
  215. {
  216. var smsgBossCommandResponse = ProtobufHelper.FromBytes<SMSG_Boss_Command_Response>(message);
  217. this.ErrorInfo = smsgBossCommandResponse.ErrorCode.ToString();
  218. }
  219. public void Handle_SMSG_Boss_ServersInfo(byte[] message)
  220. {
  221. var smsgBossServersInfo = ProtobufHelper.FromBytes<SMSG_Boss_ServersInfo>(message);
  222. this.ServerInfos.Clear();
  223. foreach (var nm in smsgBossServersInfo.Name)
  224. {
  225. this.ServerInfos.Add(new ServerViewModel { Name = nm });
  226. }
  227. }
  228. public void Reload()
  229. {
  230. this.SendCommand("reload");
  231. }
  232. public void Find()
  233. {
  234. using (var entitys = new DataCenterEntities())
  235. {
  236. t_character result = null;
  237. switch (this.FindTypeIndex)
  238. {
  239. case 0:
  240. {
  241. result = entitys.t_character.FirstOrDefault(
  242. c => c.account == this.FindType);
  243. break;
  244. }
  245. case 1:
  246. {
  247. result = entitys.t_character.FirstOrDefault(
  248. c => c.character_name == this.FindType);
  249. break;
  250. }
  251. case 2:
  252. {
  253. var findGuid = Decimal.Parse(this.FindType);
  254. result = entitys.t_character.FirstOrDefault(
  255. c => c.character_guid == findGuid);
  256. break;
  257. }
  258. }
  259. if (result == null)
  260. {
  261. Logger.Debug("not find charactor info!");
  262. return;
  263. }
  264. this.Account = result.account;
  265. this.Name = result.character_name;
  266. this.Guid = result.character_guid.ToString(CultureInfo.InvariantCulture);
  267. }
  268. }
  269. public void ForbiddenBuy()
  270. {
  271. this.SendCommand(string.Format("{0} 600", guid));
  272. }
  273. }
  274. }