RobotViewModel.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.ComponentModel.Composition;
  4. using System.Globalization;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using BossCommand;
  8. using BossBase;
  9. using Helper;
  10. using Log;
  11. using Microsoft.Practices.Prism.Events;
  12. using Microsoft.Practices.Prism.ViewModel;
  13. namespace Modules.Robot
  14. {
  15. [Export(contractType: typeof (RobotViewModel)),
  16. PartCreationPolicy(creationPolicy: CreationPolicy.Shared)]
  17. internal sealed class RobotViewModel: NotificationObject, IDisposable
  18. {
  19. private readonly IEventAggregator eventAggregator;
  20. private string errorInfo = "";
  21. private int findTypeIndex;
  22. private string account = "";
  23. private string findType = "egametang@126.com";
  24. private string name = "";
  25. private string guid = "";
  26. private string command = "";
  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 IMessageChannel IMessageChannel { get; set; }
  33. public int FindTypeIndex
  34. {
  35. get
  36. {
  37. return this.findTypeIndex;
  38. }
  39. set
  40. {
  41. if (this.findTypeIndex == value)
  42. {
  43. return;
  44. }
  45. this.findTypeIndex = value;
  46. this.RaisePropertyChanged("FindTypeIndex");
  47. }
  48. }
  49. public string FindType
  50. {
  51. get
  52. {
  53. return this.findType;
  54. }
  55. set
  56. {
  57. if (this.findType == value)
  58. {
  59. return;
  60. }
  61. this.findType = value;
  62. this.RaisePropertyChanged("FindType");
  63. }
  64. }
  65. public Visibility DockPanelVisiable
  66. {
  67. get
  68. {
  69. return this.dockPanelVisiable;
  70. }
  71. set
  72. {
  73. if (this.dockPanelVisiable == value)
  74. {
  75. return;
  76. }
  77. this.dockPanelVisiable = value;
  78. this.RaisePropertyChanged("DockPanelVisiable");
  79. }
  80. }
  81. public ObservableCollection<ServerViewModel> ServerInfos
  82. {
  83. get
  84. {
  85. return this.serverInfos;
  86. }
  87. }
  88. public string Account
  89. {
  90. get
  91. {
  92. return this.account;
  93. }
  94. set
  95. {
  96. if (this.account == value)
  97. {
  98. return;
  99. }
  100. this.account = value;
  101. this.RaisePropertyChanged("Account");
  102. }
  103. }
  104. public string Name
  105. {
  106. get
  107. {
  108. return this.name;
  109. }
  110. set
  111. {
  112. if (this.name == value)
  113. {
  114. return;
  115. }
  116. this.name = value;
  117. this.RaisePropertyChanged("Name");
  118. }
  119. }
  120. public string Guid
  121. {
  122. get
  123. {
  124. return this.guid;
  125. }
  126. set
  127. {
  128. if (this.guid == value)
  129. {
  130. return;
  131. }
  132. this.guid = value;
  133. this.RaisePropertyChanged("Guid");
  134. }
  135. }
  136. public string ErrorInfo
  137. {
  138. get
  139. {
  140. return this.errorInfo;
  141. }
  142. set
  143. {
  144. if (this.errorInfo == value)
  145. {
  146. return;
  147. }
  148. this.errorInfo = value;
  149. this.RaisePropertyChanged("ErrorInfo");
  150. }
  151. }
  152. public string Command
  153. {
  154. get
  155. {
  156. return this.command;
  157. }
  158. set
  159. {
  160. if (this.command == value)
  161. {
  162. return;
  163. }
  164. this.command = value;
  165. this.RaisePropertyChanged("Command");
  166. }
  167. }
  168. [ImportingConstructor]
  169. public RobotViewModel(IEventAggregator eventAggregator)
  170. {
  171. this.eventAggregator = eventAggregator;
  172. eventAggregator.GetEvent<LoginOKEvent>().Subscribe(this.OnLoginOKEvent);
  173. }
  174. ~RobotViewModel()
  175. {
  176. this.Disposing();
  177. }
  178. public void Dispose()
  179. {
  180. this.Disposing();
  181. GC.SuppressFinalize(this);
  182. }
  183. private void Disposing()
  184. {
  185. this.bossClient.Dispose();
  186. }
  187. public void OnLoginOKEvent(IMessageChannel messageChannel)
  188. {
  189. this.DockPanelVisiable = Visibility.Visible;
  190. this.IMessageChannel = messageChannel;
  191. }
  192. public void ReLogin()
  193. {
  194. this.DockPanelVisiable = Visibility.Hidden;
  195. this.eventAggregator.GetEvent<ReLoginEvent>().Publish(null);
  196. }
  197. public async Task Servers()
  198. {
  199. ABossCommand bossCommand = new BCServerInfo(this.IMessageChannel);
  200. var result = await bossCommand.DoAsync();
  201. var smsgBossServersInfo = result as SMSG_Boss_ServersInfo;
  202. if (smsgBossServersInfo == null)
  203. {
  204. this.ErrorInfo = "查询服务器失败!";
  205. return;
  206. }
  207. this.ServerInfos.Clear();
  208. foreach (var nm in smsgBossServersInfo.Name)
  209. {
  210. this.ServerInfos.Add(new ServerViewModel { Name = nm });
  211. }
  212. this.ErrorInfo = "查询服务器成功!";
  213. }
  214. public void Reload()
  215. {
  216. ABossCommand bossCommand = new BCReloadWorld(this.IMessageChannel);
  217. bossCommand.DoAsync();
  218. }
  219. public async Task GetCharacterInfo()
  220. {
  221. ABossCommand bossCommand = new BCGetCharacterInfo(this.IMessageChannel)
  222. {
  223. FindTypeIndex = this.FindTypeIndex,
  224. FindType = this.FindType
  225. };
  226. var result = await bossCommand.DoAsync();
  227. if (result == null)
  228. {
  229. this.ErrorInfo = string.Format("获取玩家信息失败");
  230. return;
  231. }
  232. var characterInfo = (CharacterInfo)result;
  233. this.ErrorInfo = "获取玩家信息成功";
  234. this.Account = characterInfo.Account;
  235. this.Name = characterInfo.Name;
  236. this.Guid = characterInfo.Guid.ToString();
  237. }
  238. public async Task ForbidCharacter(string forbiddenCommand, string forbiddenTime)
  239. {
  240. if (this.Guid == "")
  241. {
  242. this.ErrorInfo = "请先指定玩家";
  243. return;
  244. }
  245. int time = 0;
  246. if (!int.TryParse(forbiddenTime, out time))
  247. {
  248. this.ErrorInfo = "时间请输入数字";
  249. return;
  250. }
  251. ABossCommand bossCommand = new BCForbiddenCharacter(this.IMessageChannel)
  252. {
  253. Guid = this.Guid,
  254. Command = forbiddenCommand,
  255. ForbiddenTime = forbiddenTime
  256. };
  257. var result = await bossCommand.DoAsync();
  258. var errorCode = (uint)result;
  259. if (errorCode == ErrorCode.RESPONSE_SUCCESS)
  260. {
  261. this.ErrorInfo = string.Format(
  262. "{0} {1} {2} Succeed!", forbiddenCommand, this.Guid, forbiddenTime);
  263. return;
  264. }
  265. this.ErrorInfo = string.Format("{0} Fail, error code: {1}", forbiddenCommand, errorCode);
  266. }
  267. public async Task ForbiddenLogin(
  268. string forbiddenCommand, string forbiddenContent, string forbiddenTime)
  269. {
  270. int time = 0;
  271. if (!int.TryParse(forbiddenTime, out time))
  272. {
  273. this.ErrorInfo = "时间请输入数字";
  274. return;
  275. }
  276. ABossCommand bossCommand = new BCForbidLogin(this.IMessageChannel)
  277. {
  278. Command = forbiddenCommand,
  279. Content = forbiddenContent,
  280. ForbiddenLoginTime = forbiddenTime
  281. };
  282. var result = await bossCommand.DoAsync();
  283. var errorCode = (uint)result;
  284. if (errorCode == ErrorCode.RESPONSE_SUCCESS)
  285. {
  286. this.ErrorInfo = string.Format(
  287. "{0} {1} {2} Succeed!", forbiddenCommand, forbiddenContent, forbiddenTime);
  288. return;
  289. }
  290. this.ErrorInfo = string.Format("{0}, error code: {1}", forbiddenCommand, errorCode);
  291. }
  292. public async Task SendCommand()
  293. {
  294. if (this.Command.StartsWith("gm ", true, CultureInfo.CurrentCulture))
  295. {
  296. this.Command = this.Command.Substring(3);
  297. }
  298. ABossCommand bossCommand = new BCCommand(this.IMessageChannel)
  299. { Command = this.Command };
  300. string commandString = this.Command;
  301. object result = null;
  302. try
  303. {
  304. result = await bossCommand.DoAsync();
  305. }
  306. catch(Exception e)
  307. {
  308. Logger.Trace(e.ToString());
  309. return;
  310. }
  311. var smsgBossCommandResponse = (SMSG_Boss_Command_Response)result;
  312. this.ErrorInfo = string.Format(" send command: {0}, error code: {1}",
  313. commandString, JsonHelper.ToString(smsgBossCommandResponse));
  314. }
  315. }
  316. }