RobotViewModel.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel.Composition;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  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 account = "";
  24. private string findType = "";
  25. private string name = "";
  26. private string guid = "";
  27. private bool isGMEnable;
  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. public bool IsGMEnable
  156. {
  157. get
  158. {
  159. return this.isGMEnable;
  160. }
  161. set
  162. {
  163. if (this.isGMEnable == value)
  164. {
  165. return;
  166. }
  167. this.isGMEnable = value;
  168. this.RaisePropertyChanged("IsGMEnable");
  169. }
  170. }
  171. [ImportingConstructor]
  172. public RobotViewModel(IEventAggregator eventAggregator)
  173. {
  174. eventAggregator.GetEvent<LoginOKEvent>().Subscribe(this.OnLoginOK);
  175. }
  176. ~RobotViewModel()
  177. {
  178. this.Disposing(false);
  179. }
  180. public void Dispose()
  181. {
  182. this.Disposing(true);
  183. GC.SuppressFinalize(this);
  184. }
  185. private void Disposing(bool disposing)
  186. {
  187. this.bossClient.Dispose();
  188. }
  189. public void OnLoginOK(IMessageChannel messageChannel)
  190. {
  191. this.DockPanelVisiable = Visibility.Visible;
  192. this.IMessageChannel = messageChannel;
  193. }
  194. public void SendCommand(string command)
  195. {
  196. var cmsgBossGm = new CMSG_Boss_Gm
  197. {
  198. Message = command
  199. };
  200. this.IMessageChannel.SendMessage(MessageOpcode.CMSG_BOSS_GM, cmsgBossGm);
  201. }
  202. public async Task<T> RecvMessage<T>()
  203. {
  204. var result = await this.IMessageChannel.RecvMessage();
  205. ushort opcode = result.Item1;
  206. byte[] content = result.Item2;
  207. try
  208. {
  209. var message = ProtobufHelper.FromBytes<T>(content);
  210. return message;
  211. }
  212. catch (Exception)
  213. {
  214. Logger.Trace("parse message fail, opcode: {0}", opcode);
  215. throw;
  216. }
  217. }
  218. public async void Servers()
  219. {
  220. this.SendCommand("servers");
  221. var smsgBossServersInfo = await this.RecvMessage<SMSG_Boss_ServersInfo>();
  222. this.ServerInfos.Clear();
  223. foreach (var nm in smsgBossServersInfo.Name)
  224. {
  225. this.ServerInfos.Add(new ServerViewModel { Name = nm });
  226. }
  227. this.ErrorInfo = "查询服务器成功!";
  228. }
  229. public void Reload()
  230. {
  231. this.SendCommand("reload");
  232. }
  233. public void Find()
  234. {
  235. using (var entitys = new DataCenterEntities())
  236. {
  237. t_character result = null;
  238. switch (this.FindTypeIndex)
  239. {
  240. case 0:
  241. {
  242. result = entitys.t_character.FirstOrDefault(
  243. c => c.account == this.FindType);
  244. break;
  245. }
  246. case 1:
  247. {
  248. result = entitys.t_character.FirstOrDefault(
  249. c => c.character_name == this.FindType);
  250. break;
  251. }
  252. case 2:
  253. {
  254. var findGuid = Decimal.Parse(this.FindType);
  255. result = entitys.t_character.FirstOrDefault(
  256. c => c.character_guid == findGuid);
  257. break;
  258. }
  259. }
  260. if (result == null)
  261. {
  262. this.ErrorInfo = "没有找到该玩家!";
  263. return;
  264. }
  265. this.Account = result.account;
  266. this.Name = result.character_name;
  267. this.Guid = result.character_guid.ToString(CultureInfo.InvariantCulture);
  268. this.IsGMEnable = true;
  269. this.ErrorInfo = "查询成功";
  270. }
  271. }
  272. public async void ForbiddenBuy()
  273. {
  274. if (this.Guid == "")
  275. {
  276. this.ErrorInfo = "请先指定玩家";
  277. return;
  278. }
  279. this.SendCommand(string.Format("forbidden_buy_item {0} 600", guid));
  280. var smsgBossCommandResponse = await RecvMessage<SMSG_Boss_Command_Response>();
  281. if (smsgBossCommandResponse.ErrorCode == ErrorCode.RESPONSE_SUCCESS)
  282. {
  283. this.ErrorInfo = "禁止交易成功";
  284. return;
  285. }
  286. if (smsgBossCommandResponse.ErrorCode == ErrorCode.BOSS_PLAYER_NOT_FOUND)
  287. {
  288. using (var entitys = new DataCenterEntities())
  289. {
  290. var newBuff = new t_city_buff
  291. {
  292. buff_guid = RandomHelper.RandUInt64(),
  293. buff_id = 660100,
  294. buff_time = 0,
  295. buff_values = "{}".ToByteArray(),
  296. character_guid = decimal.Parse(this.Guid),
  297. create_time = DateTime.Now,
  298. modify_time = DateTime.Now,
  299. stack = 1
  300. };
  301. entitys.t_city_buff.Add(newBuff);
  302. entitys.SaveChanges();
  303. }
  304. this.ErrorInfo = "禁止交易成功";
  305. }
  306. this.ErrorInfo = smsgBossCommandResponse.ErrorCode.ToString();
  307. }
  308. }
  309. }