RobotViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 Events;
  12. using Helper;
  13. using Log;
  14. using Microsoft.Practices.Prism.Events;
  15. using Microsoft.Practices.Prism.ViewModel;
  16. namespace Modules.Robot
  17. {
  18. [Export(contractType: typeof (RobotViewModel)),
  19. PartCreationPolicy(creationPolicy: CreationPolicy.Shared)]
  20. internal sealed class RobotViewModel: NotificationObject, IDisposable
  21. {
  22. private int findTypeIndex;
  23. private string findType = "";
  24. private Visibility dockPanelVisiable = Visibility.Hidden;
  25. private readonly BossClient.BossClient bossClient = new BossClient.BossClient();
  26. private readonly ObservableCollection<ServerViewModel> serverInfos =
  27. new ObservableCollection<ServerViewModel>();
  28. public readonly Dictionary<ushort, Action<byte[]>> messageHandlers =
  29. new Dictionary<ushort, Action<byte[]>>();
  30. public int FindTypeIndex
  31. {
  32. get
  33. {
  34. return this.findTypeIndex;
  35. }
  36. set
  37. {
  38. if (this.findTypeIndex == value)
  39. {
  40. return;
  41. }
  42. this.findTypeIndex = value;
  43. this.RaisePropertyChanged("FindTypeIndex");
  44. }
  45. }
  46. public string FindType
  47. {
  48. get
  49. {
  50. return this.findType;
  51. }
  52. set
  53. {
  54. if (this.findType == value)
  55. {
  56. return;
  57. }
  58. this.findType = value;
  59. this.RaisePropertyChanged("FindType");
  60. }
  61. }
  62. public Visibility DockPanelVisiable
  63. {
  64. get
  65. {
  66. return this.dockPanelVisiable;
  67. }
  68. set
  69. {
  70. if (this.dockPanelVisiable == value)
  71. {
  72. return;
  73. }
  74. this.dockPanelVisiable = value;
  75. this.RaisePropertyChanged("DockPanelVisiable");
  76. }
  77. }
  78. public ObservableCollection<ServerViewModel> ServerInfos
  79. {
  80. get
  81. {
  82. return this.serverInfos;
  83. }
  84. }
  85. [ImportingConstructor]
  86. public RobotViewModel(IEventAggregator eventAggregator)
  87. {
  88. this.messageHandlers.Add(
  89. MessageOpcode.SMSG_BOSS_SERVERSINFO, Handle_SMSG_Boss_ServersInfo);
  90. eventAggregator.GetEvent<LoginOKEvent>().Subscribe(this.OnLoginOK);
  91. }
  92. ~RobotViewModel()
  93. {
  94. this.Disposing(false);
  95. }
  96. public void Dispose()
  97. {
  98. this.Disposing(true);
  99. GC.SuppressFinalize(this);
  100. }
  101. private void Disposing(bool disposing)
  102. {
  103. this.bossClient.Dispose();
  104. }
  105. public async void OnLoginOK(IMessageChannel messageChannel)
  106. {
  107. this.DockPanelVisiable = Visibility.Visible;
  108. try
  109. {
  110. while (true)
  111. {
  112. var result = await messageChannel.RecvMessage();
  113. ushort opcode = result.Item1;
  114. byte[] message = result.Item2;
  115. if (!messageHandlers.ContainsKey(opcode))
  116. {
  117. Logger.Debug("not found opcode: {0}", opcode);
  118. continue;
  119. }
  120. messageHandlers[opcode](message);
  121. }
  122. }
  123. catch (Exception e)
  124. {
  125. this.dockPanelVisiable = Visibility.Hidden;
  126. Logger.Trace(e.ToString());
  127. }
  128. }
  129. public void Servers()
  130. {
  131. this.bossClient.SendCommand("servers");
  132. }
  133. public void Handle_SMSG_Boss_ServersInfo(byte[] message)
  134. {
  135. var smsgBossServersInfo = ProtobufHelper.FromBytes<SMSG_Boss_ServersInfo>(message);
  136. this.ServerInfos.Clear();
  137. foreach (var name in smsgBossServersInfo.Name)
  138. {
  139. this.ServerInfos.Add(new ServerViewModel { Name = name });
  140. }
  141. }
  142. public void Reload()
  143. {
  144. this.bossClient.SendCommand("reload");
  145. }
  146. public void Find()
  147. {
  148. using (var entitys = new DataCenterEntities())
  149. {
  150. t_character result = null;
  151. switch (this.FindTypeIndex)
  152. {
  153. case 0:
  154. {
  155. result = entitys.t_character.FirstOrDefault(
  156. c => c.account == this.FindType);
  157. break;
  158. }
  159. case 1:
  160. {
  161. result = entitys.t_character.FirstOrDefault(
  162. c => c.character_name == this.FindType);
  163. break;
  164. }
  165. case 2:
  166. {
  167. var guid = Decimal.Parse(this.FindType);
  168. result = entitys.t_character.FirstOrDefault(
  169. c => c.character_guid == guid);
  170. break;
  171. }
  172. }
  173. if (result == null)
  174. {
  175. Logger.Debug("not find charactor info!");
  176. return;
  177. }
  178. Logger.Debug("Account: {0}, Name: {1}, GUID: {2}",
  179. result.account, result.character_name, result.character_guid);
  180. }
  181. }
  182. }
  183. }