RobotViewModel.cs 12 KB

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