RobotViewModel.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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.Threading.Tasks;
  7. using System.Windows;
  8. using BossCommand;
  9. using BossBase;
  10. using Helper;
  11. using Log;
  12. using Microsoft.Practices.Prism.Events;
  13. using Microsoft.Practices.Prism.ViewModel;
  14. namespace Modules.Robot
  15. {
  16. [Export(contractType: typeof (RobotViewModel)),
  17. PartCreationPolicy(creationPolicy: CreationPolicy.Shared)]
  18. internal sealed class RobotViewModel: NotificationObject, IDisposable
  19. {
  20. private readonly IEventAggregator eventAggregator;
  21. private string errorInfo = "";
  22. private int findTypeIndex;
  23. private string account = "";
  24. private string findType = "egametang@126.com";
  25. private string name = "";
  26. private string guid = "";
  27. private string command = "";
  28. private string subject = "";
  29. private string content = "";
  30. private string freeGold = "";
  31. private string silver = "";
  32. private string itemID = "";
  33. private string itemCount = "";
  34. private Visibility dockPanelVisiable = Visibility.Hidden;
  35. private readonly BossClient.BossClient bossClient = new BossClient.BossClient();
  36. private readonly ObservableCollection<ServerViewModel> serverInfos =
  37. new ObservableCollection<ServerViewModel>();
  38. public IMessageChannel IMessageChannel { get; set; }
  39. public int FindTypeIndex
  40. {
  41. get
  42. {
  43. return this.findTypeIndex;
  44. }
  45. set
  46. {
  47. if (this.findTypeIndex == value)
  48. {
  49. return;
  50. }
  51. this.findTypeIndex = value;
  52. this.RaisePropertyChanged("FindTypeIndex");
  53. }
  54. }
  55. public string FindType
  56. {
  57. get
  58. {
  59. return this.findType;
  60. }
  61. set
  62. {
  63. if (this.findType == value)
  64. {
  65. return;
  66. }
  67. this.findType = value;
  68. this.RaisePropertyChanged("FindType");
  69. }
  70. }
  71. public Visibility DockPanelVisiable
  72. {
  73. get
  74. {
  75. return this.dockPanelVisiable;
  76. }
  77. set
  78. {
  79. if (this.dockPanelVisiable == value)
  80. {
  81. return;
  82. }
  83. this.dockPanelVisiable = value;
  84. this.RaisePropertyChanged("DockPanelVisiable");
  85. }
  86. }
  87. public ObservableCollection<ServerViewModel> ServerInfos
  88. {
  89. get
  90. {
  91. return this.serverInfos;
  92. }
  93. }
  94. public string Account
  95. {
  96. get
  97. {
  98. return this.account;
  99. }
  100. set
  101. {
  102. if (this.account == value)
  103. {
  104. return;
  105. }
  106. this.account = value;
  107. this.RaisePropertyChanged("Account");
  108. }
  109. }
  110. public string Name
  111. {
  112. get
  113. {
  114. return this.name;
  115. }
  116. set
  117. {
  118. if (this.name == value)
  119. {
  120. return;
  121. }
  122. this.name = value;
  123. this.RaisePropertyChanged("Name");
  124. }
  125. }
  126. public string Guid
  127. {
  128. get
  129. {
  130. return this.guid;
  131. }
  132. set
  133. {
  134. if (this.guid == value)
  135. {
  136. return;
  137. }
  138. this.guid = value;
  139. this.RaisePropertyChanged("Guid");
  140. }
  141. }
  142. public string ErrorInfo
  143. {
  144. get
  145. {
  146. return this.errorInfo;
  147. }
  148. set
  149. {
  150. if (this.errorInfo == value)
  151. {
  152. return;
  153. }
  154. this.errorInfo = value;
  155. this.RaisePropertyChanged("ErrorInfo");
  156. }
  157. }
  158. public string Command
  159. {
  160. get
  161. {
  162. return this.command;
  163. }
  164. set
  165. {
  166. if (this.command == value)
  167. {
  168. return;
  169. }
  170. this.command = value;
  171. this.RaisePropertyChanged("Command");
  172. }
  173. }
  174. public string Subject
  175. {
  176. get
  177. {
  178. return this.subject;
  179. }
  180. set
  181. {
  182. if (this.subject == value)
  183. {
  184. return;
  185. }
  186. this.subject = value;
  187. this.RaisePropertyChanged("Subject");
  188. }
  189. }
  190. public string Content
  191. {
  192. get
  193. {
  194. return this.content;
  195. }
  196. set
  197. {
  198. if (this.content == value)
  199. {
  200. return;
  201. }
  202. this.content = value;
  203. this.RaisePropertyChanged("Content");
  204. }
  205. }
  206. public string FreeGold
  207. {
  208. get
  209. {
  210. return this.freeGold;
  211. }
  212. set
  213. {
  214. if (this.freeGold == value)
  215. {
  216. return;
  217. }
  218. this.freeGold = value;
  219. this.RaisePropertyChanged("FreeGold");
  220. }
  221. }
  222. public string Silver
  223. {
  224. get
  225. {
  226. return this.silver;
  227. }
  228. set
  229. {
  230. if (this.silver == value)
  231. {
  232. return;
  233. }
  234. this.silver = value;
  235. this.RaisePropertyChanged("Silver");
  236. }
  237. }
  238. public string ItemID
  239. {
  240. get
  241. {
  242. return this.itemID;
  243. }
  244. set
  245. {
  246. if (this.itemID == value)
  247. {
  248. return;
  249. }
  250. this.itemID = value;
  251. this.RaisePropertyChanged("ItemID");
  252. }
  253. }
  254. public string ItemCount
  255. {
  256. get
  257. {
  258. return this.itemCount;
  259. }
  260. set
  261. {
  262. if (this.itemCount == value)
  263. {
  264. return;
  265. }
  266. this.itemCount = value;
  267. this.RaisePropertyChanged("ItemCount");
  268. }
  269. }
  270. [ImportingConstructor]
  271. public RobotViewModel(IEventAggregator eventAggregator)
  272. {
  273. this.eventAggregator = eventAggregator;
  274. eventAggregator.GetEvent<LoginOKEvent>().Subscribe(this.OnLoginOKEvent);
  275. }
  276. ~RobotViewModel()
  277. {
  278. this.Disposing();
  279. }
  280. public void Dispose()
  281. {
  282. this.Disposing();
  283. GC.SuppressFinalize(this);
  284. }
  285. private void Disposing()
  286. {
  287. this.bossClient.Dispose();
  288. }
  289. public void OnLoginOKEvent(IMessageChannel messageChannel)
  290. {
  291. this.DockPanelVisiable = Visibility.Visible;
  292. this.IMessageChannel = messageChannel;
  293. }
  294. public void ReLogin()
  295. {
  296. this.DockPanelVisiable = Visibility.Hidden;
  297. this.eventAggregator.GetEvent<ReLoginEvent>().Publish(null);
  298. }
  299. public void ShowErrorInfo(uint errorCode, string commandString)
  300. {
  301. if (errorCode == ErrorCode.RESPONSE_SUCCESS)
  302. {
  303. this.ErrorInfo = string.Format("{0} Succeed!", commandString);
  304. return;
  305. }
  306. this.ErrorInfo = string.Format("{0} Fail, error code: {1}", commandString, errorCode);
  307. }
  308. public async Task Servers()
  309. {
  310. ABossCommand bossCommand = new BCServerInfo(this.IMessageChannel);
  311. var result = await bossCommand.DoAsync();
  312. var smsgBossServersInfo = result as SMSG_Boss_ServersInfo;
  313. if (smsgBossServersInfo == null)
  314. {
  315. this.ErrorInfo = "查询服务器失败!";
  316. return;
  317. }
  318. this.ServerInfos.Clear();
  319. foreach (var nm in smsgBossServersInfo.Name)
  320. {
  321. this.ServerInfos.Add(new ServerViewModel { Name = nm });
  322. }
  323. this.ErrorInfo = "查询服务器成功!";
  324. }
  325. public void Reload()
  326. {
  327. ABossCommand bossCommand = new BCReloadWorld(this.IMessageChannel);
  328. bossCommand.DoAsync();
  329. }
  330. public async Task GetCharacterInfo()
  331. {
  332. ABossCommand bossCommand = new BCGetCharacterInfo(this.IMessageChannel)
  333. {
  334. FindTypeIndex = this.FindTypeIndex,
  335. FindType = this.FindType
  336. };
  337. var result = await bossCommand.DoAsync();
  338. if (result == null)
  339. {
  340. this.ErrorInfo = string.Format("{0} Fail!", bossCommand.CommandString);
  341. return;
  342. }
  343. var characterInfo = (CharacterInfo)result;
  344. this.Account = characterInfo.Account;
  345. this.Name = characterInfo.Name;
  346. this.Guid = characterInfo.Guid.ToString();
  347. this.ErrorInfo = string.Format("{0} Succeed!", bossCommand.CommandString);
  348. }
  349. public async Task ForbidCharacter(string forbiddenCommand, string forbiddenTime)
  350. {
  351. if (this.Guid == "")
  352. {
  353. this.ErrorInfo = "请先指定玩家";
  354. return;
  355. }
  356. ulong ulongGuid = 0;
  357. if (!ulong.TryParse(this.Guid, out ulongGuid))
  358. {
  359. this.ErrorInfo = "Guid必须是数字";
  360. return;
  361. }
  362. int time = 0;
  363. if (!int.TryParse(forbiddenTime, out time))
  364. {
  365. this.ErrorInfo = "时间请输入数字";
  366. return;
  367. }
  368. ABossCommand bossCommand = new BCForbiddenCharacter(this.IMessageChannel)
  369. {
  370. Command = forbiddenCommand,
  371. Guid = this.Guid,
  372. ForbiddenTime = forbiddenTime
  373. };
  374. var result = await bossCommand.DoAsync();
  375. var errorCode = (uint)result;
  376. ShowErrorInfo(errorCode, bossCommand.CommandString);
  377. }
  378. public async Task ForbiddenLogin(
  379. string forbiddenCommand, string forbiddenContent, string forbiddenTime)
  380. {
  381. int time = 0;
  382. if (!int.TryParse(forbiddenTime, out time))
  383. {
  384. this.ErrorInfo = "时间请输入数字";
  385. return;
  386. }
  387. ABossCommand bossCommand = new BCForbidLogin(this.IMessageChannel)
  388. {
  389. Command = forbiddenCommand,
  390. Content = forbiddenContent,
  391. ForbiddenLoginTime = forbiddenTime
  392. };
  393. var result = await bossCommand.DoAsync();
  394. var errorCode = (uint)result;
  395. ShowErrorInfo(errorCode, bossCommand.CommandString);
  396. }
  397. public async Task SendMail()
  398. {
  399. BossMail bossMail = null;
  400. try
  401. {
  402. bossMail = new BossMail
  403. {
  404. sender_name = "系统",
  405. receiver_guid = ulong.Parse(this.Guid),
  406. subject = this.Subject,
  407. content = this.Content,
  408. free_gold = uint.Parse(this.FreeGold),
  409. silver = uint.Parse(this.Silver),
  410. item_dict = new Dictionary<int, int>()
  411. };
  412. if (this.ItemID != "")
  413. {
  414. bossMail.item_dict.Add(int.Parse(this.ItemID), int.Parse(this.ItemCount));
  415. }
  416. }
  417. catch (Exception e)
  418. {
  419. Logger.Trace(e.ToString());
  420. this.ErrorInfo = "输入错误!";
  421. return;
  422. }
  423. ABossCommand bossCommand = new BCSendMail(this.IMessageChannel)
  424. {
  425. BossMail = bossMail
  426. };
  427. var result = await bossCommand.DoAsync();
  428. var errorCode = (uint) result;
  429. ShowErrorInfo(errorCode, bossCommand.CommandString);
  430. }
  431. public async Task SendCommand()
  432. {
  433. if (this.Command.StartsWith("gm ", true, CultureInfo.CurrentCulture))
  434. {
  435. this.Command = this.Command.Substring(3);
  436. }
  437. ABossCommand bossCommand = new BCCommand(this.IMessageChannel)
  438. { Command = this.Command };
  439. string commandString = this.Command;
  440. object result = null;
  441. try
  442. {
  443. result = await bossCommand.DoAsync();
  444. }
  445. catch(Exception e)
  446. {
  447. Logger.Trace(e.ToString());
  448. return;
  449. }
  450. var smsgBossCommandResponse = (SMSG_Boss_Command_Response)result;
  451. this.ErrorInfo = string.Format(" send command: {0}, error code: {1}",
  452. commandString, JsonHelper.ToString(smsgBossCommandResponse));
  453. }
  454. }
  455. }