RobotViewModel.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 string errorInfo = "";
  23. private int findTypeIndex;
  24. private string account = "";
  25. private string findType = "egametang@163.com";
  26. private string name = "";
  27. private string guid = "";
  28. private bool isGMEnable;
  29. private Visibility dockPanelVisiable = Visibility.Hidden;
  30. private readonly BossClient.BossClient bossClient = new BossClient.BossClient();
  31. private readonly ObservableCollection<ServerViewModel> serverInfos =
  32. new ObservableCollection<ServerViewModel>();
  33. private readonly DataCenterEntities entities = new DataCenterEntities();
  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. public string ErrorInfo
  139. {
  140. get
  141. {
  142. return this.errorInfo;
  143. }
  144. set
  145. {
  146. if (this.errorInfo == value)
  147. {
  148. return;
  149. }
  150. this.errorInfo = value;
  151. this.RaisePropertyChanged("ErrorInfo");
  152. }
  153. }
  154. public bool IsGMEnable
  155. {
  156. get
  157. {
  158. return this.isGMEnable;
  159. }
  160. set
  161. {
  162. if (this.isGMEnable == value)
  163. {
  164. return;
  165. }
  166. this.isGMEnable = value;
  167. this.RaisePropertyChanged("IsGMEnable");
  168. }
  169. }
  170. [ImportingConstructor]
  171. public RobotViewModel(IEventAggregator eventAggregator)
  172. {
  173. eventAggregator.GetEvent<LoginOKEvent>().Subscribe(this.OnLoginOK);
  174. }
  175. ~RobotViewModel()
  176. {
  177. this.Disposing(false);
  178. }
  179. public void Dispose()
  180. {
  181. this.Disposing(true);
  182. GC.SuppressFinalize(this);
  183. }
  184. private void Disposing(bool disposing)
  185. {
  186. this.entities.Dispose();
  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. t_character result = null;
  236. switch (this.FindTypeIndex)
  237. {
  238. case 0:
  239. {
  240. result = entities.t_character.FirstOrDefault(
  241. c => c.account == this.FindType);
  242. break;
  243. }
  244. case 1:
  245. {
  246. result = entities.t_character.FirstOrDefault(
  247. c => c.character_name == this.FindType);
  248. break;
  249. }
  250. case 2:
  251. {
  252. var findGuid = Decimal.Parse(this.FindType);
  253. result = entities.t_character.FirstOrDefault(
  254. c => c.character_guid == findGuid);
  255. break;
  256. }
  257. }
  258. if (result == null)
  259. {
  260. this.ErrorInfo = "没有找到该玩家!";
  261. return;
  262. }
  263. this.Account = result.account;
  264. this.Name = result.character_name;
  265. this.Guid = result.character_guid.ToString(CultureInfo.InvariantCulture);
  266. this.IsGMEnable = true;
  267. this.ErrorInfo = "查询成功";
  268. }
  269. public async void ForbiddenBuy()
  270. {
  271. if (this.Guid == "")
  272. {
  273. this.ErrorInfo = "请先指定玩家";
  274. return;
  275. }
  276. this.SendCommand(string.Format("forbidden_buy_item {0} {1}", guid, int.MaxValue));
  277. var smsgBossCommandResponse = await RecvMessage<SMSG_Boss_Command_Response>();
  278. if (smsgBossCommandResponse.ErrorCode == ErrorCode.RESPONSE_SUCCESS)
  279. {
  280. this.ErrorInfo = "禁止交易成功";
  281. return;
  282. }
  283. if (smsgBossCommandResponse.ErrorCode == ErrorCode.BOSS_PLAYER_NOT_FOUND)
  284. {
  285. decimal character_guid = decimal.Parse(this.Guid);
  286. var removeBuffs = entities.t_city_buff.Where(
  287. c => c.buff_id == BuffId.BUFF_FORBIDDEN_PLAYER_BUY_ITEM &&
  288. c.character_guid == character_guid);
  289. foreach (var removeBuff in removeBuffs)
  290. {
  291. entities.t_city_buff.Remove(removeBuff);
  292. }
  293. var newBuff = new t_city_buff
  294. {
  295. buff_guid = RandomHelper.RandUInt64(),
  296. buff_id = BuffId.BUFF_FORBIDDEN_PLAYER_BUY_ITEM,
  297. buff_time = 0,
  298. buff_values = "{}".ToByteArray(),
  299. character_guid = decimal.Parse(this.Guid),
  300. create_time = DateTime.Now,
  301. modify_time = DateTime.Now,
  302. stack = 1
  303. };
  304. entities.t_city_buff.Add(newBuff);
  305. entities.SaveChanges();
  306. this.ErrorInfo = "禁止交易成功";
  307. return;
  308. }
  309. this.ErrorInfo = smsgBossCommandResponse.ErrorCode.ToString();
  310. }
  311. public async void AllowBuy()
  312. {
  313. if (this.Guid == "")
  314. {
  315. this.ErrorInfo = "请先指定玩家";
  316. return;
  317. }
  318. this.SendCommand(string.Format("forbidden_buy_item {0} 0", guid));
  319. var smsgBossCommandResponse = await RecvMessage<SMSG_Boss_Command_Response>();
  320. if (smsgBossCommandResponse.ErrorCode == ErrorCode.RESPONSE_SUCCESS)
  321. {
  322. this.ErrorInfo = "允许交易成功";
  323. return;
  324. }
  325. if (smsgBossCommandResponse.ErrorCode == ErrorCode.BOSS_PLAYER_NOT_FOUND)
  326. {
  327. decimal character_guid = decimal.Parse(this.Guid);
  328. var removeBuffs = entities.t_city_buff.Where(
  329. c => c.buff_id == BuffId.BUFF_FORBIDDEN_PLAYER_BUY_ITEM &&
  330. c.character_guid == character_guid);
  331. foreach (var removeBuff in removeBuffs)
  332. {
  333. entities.t_city_buff.Remove(removeBuff);
  334. }
  335. entities.SaveChanges();
  336. this.ErrorInfo = "允许交易成功";
  337. return;
  338. }
  339. this.ErrorInfo = smsgBossCommandResponse.ErrorCode.ToString();
  340. }
  341. }
  342. }