RobotViewModel.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 string forbiddenLoginTime = "";
  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. public IMessageChannel IMessageChannel { get; set; }
  34. public int FindTypeIndex
  35. {
  36. get
  37. {
  38. return this.findTypeIndex;
  39. }
  40. set
  41. {
  42. if (this.findTypeIndex == value)
  43. {
  44. return;
  45. }
  46. this.findTypeIndex = value;
  47. this.RaisePropertyChanged("FindTypeIndex");
  48. }
  49. }
  50. public string FindType
  51. {
  52. get
  53. {
  54. return this.findType;
  55. }
  56. set
  57. {
  58. if (this.findType == value)
  59. {
  60. return;
  61. }
  62. this.findType = value;
  63. this.RaisePropertyChanged("FindType");
  64. }
  65. }
  66. public Visibility DockPanelVisiable
  67. {
  68. get
  69. {
  70. return this.dockPanelVisiable;
  71. }
  72. set
  73. {
  74. if (this.dockPanelVisiable == value)
  75. {
  76. return;
  77. }
  78. this.dockPanelVisiable = value;
  79. this.RaisePropertyChanged("DockPanelVisiable");
  80. }
  81. }
  82. public ObservableCollection<ServerViewModel> ServerInfos
  83. {
  84. get
  85. {
  86. return this.serverInfos;
  87. }
  88. }
  89. public string Account
  90. {
  91. get
  92. {
  93. return this.account;
  94. }
  95. set
  96. {
  97. if (this.account == value)
  98. {
  99. return;
  100. }
  101. this.account = value;
  102. this.RaisePropertyChanged("Account");
  103. }
  104. }
  105. public string Name
  106. {
  107. get
  108. {
  109. return this.name;
  110. }
  111. set
  112. {
  113. if (this.name == value)
  114. {
  115. return;
  116. }
  117. this.name = value;
  118. this.RaisePropertyChanged("Name");
  119. }
  120. }
  121. public string Guid
  122. {
  123. get
  124. {
  125. return this.guid;
  126. }
  127. set
  128. {
  129. if (this.guid == value)
  130. {
  131. return;
  132. }
  133. this.guid = value;
  134. this.RaisePropertyChanged("Guid");
  135. }
  136. }
  137. public string ErrorInfo
  138. {
  139. get
  140. {
  141. return this.errorInfo;
  142. }
  143. set
  144. {
  145. if (this.errorInfo == value)
  146. {
  147. return;
  148. }
  149. this.errorInfo = value;
  150. this.RaisePropertyChanged("ErrorInfo");
  151. }
  152. }
  153. public bool IsGMEnable
  154. {
  155. get
  156. {
  157. return this.isGMEnable;
  158. }
  159. set
  160. {
  161. if (this.isGMEnable == value)
  162. {
  163. return;
  164. }
  165. this.isGMEnable = value;
  166. this.RaisePropertyChanged("IsGMEnable");
  167. }
  168. }
  169. public string ForbiddenLoginTime
  170. {
  171. get
  172. {
  173. return this.forbiddenLoginTime;
  174. }
  175. set
  176. {
  177. if (this.forbiddenLoginTime == value)
  178. {
  179. return;
  180. }
  181. this.forbiddenLoginTime = value;
  182. this.RaisePropertyChanged("ForbiddenLoginTime");
  183. }
  184. }
  185. public string Command
  186. {
  187. get
  188. {
  189. return this.command;
  190. }
  191. set
  192. {
  193. if (this.command == value)
  194. {
  195. return;
  196. }
  197. this.command = value;
  198. this.RaisePropertyChanged("Command");
  199. }
  200. }
  201. [ImportingConstructor]
  202. public RobotViewModel(IEventAggregator eventAggregator)
  203. {
  204. this.eventAggregator = eventAggregator;
  205. eventAggregator.GetEvent<LoginOKEvent>().Subscribe(this.OnLoginOKEvent);
  206. }
  207. ~RobotViewModel()
  208. {
  209. this.Disposing();
  210. }
  211. public void Dispose()
  212. {
  213. this.Disposing();
  214. GC.SuppressFinalize(this);
  215. }
  216. private void Disposing()
  217. {
  218. this.bossClient.Dispose();
  219. }
  220. public void OnLoginOKEvent(IMessageChannel messageChannel)
  221. {
  222. this.DockPanelVisiable = Visibility.Visible;
  223. this.IMessageChannel = messageChannel;
  224. }
  225. public void ReLogin()
  226. {
  227. this.DockPanelVisiable = Visibility.Hidden;
  228. this.eventAggregator.GetEvent<ReLoginEvent>().Publish(null);
  229. }
  230. public async Task Servers()
  231. {
  232. ABossCommand bossCommand = new BCServerInfo(this.IMessageChannel);
  233. var result = await bossCommand.DoAsync();
  234. var smsgBossServersInfo = result as SMSG_Boss_ServersInfo;
  235. if (smsgBossServersInfo == null)
  236. {
  237. this.ErrorInfo = "查询服务器失败!";
  238. return;
  239. }
  240. this.ServerInfos.Clear();
  241. foreach (var nm in smsgBossServersInfo.Name)
  242. {
  243. this.ServerInfos.Add(new ServerViewModel { Name = nm });
  244. }
  245. this.ErrorInfo = "查询服务器成功!";
  246. }
  247. public void Reload()
  248. {
  249. ABossCommand bossCommand = new BCReloadWorld(this.IMessageChannel);
  250. bossCommand.DoAsync();
  251. }
  252. public async void GetCharacterInfo()
  253. {
  254. ABossCommand bossCommand = new BCGetCharacterInfo(this.IMessageChannel)
  255. {
  256. FindTypeIndex = this.FindTypeIndex,
  257. FindType = this.FindType
  258. };
  259. var result = await bossCommand.DoAsync();
  260. if (result == null)
  261. {
  262. this.ErrorInfo = string.Format("获取玩家信息失败");
  263. return;
  264. }
  265. this.ErrorInfo = "获取玩家信息成功";
  266. var characterInfo = (CharacterInfo)result;
  267. this.Account = characterInfo.Account;
  268. this.Name = characterInfo.Name;
  269. this.Guid = characterInfo.Guid.ToString();
  270. }
  271. public async Task ForbiddenBuy()
  272. {
  273. if (this.Guid == "")
  274. {
  275. this.ErrorInfo = "请先指定玩家";
  276. return;
  277. }
  278. ABossCommand bossCommand = new BCForbiddenBuy(this.IMessageChannel)
  279. {
  280. Guid = this.Guid
  281. };
  282. var result = await bossCommand.DoAsync();
  283. var errorCode = (int)result;
  284. if (errorCode == ErrorCode.RESPONSE_SUCCESS)
  285. {
  286. this.ErrorInfo = "禁止交易成功";
  287. return;
  288. }
  289. this.ErrorInfo = string.Format("禁止交易失败, error code: {0}", errorCode);
  290. }
  291. public async Task AllowBuy()
  292. {
  293. if (this.Guid == "")
  294. {
  295. this.ErrorInfo = "请先指定玩家";
  296. return;
  297. }
  298. ABossCommand bossCommand = new BCAllowBuy(this.IMessageChannel)
  299. {
  300. Guid = this.Guid
  301. };
  302. var result = await bossCommand.DoAsync();
  303. var errorCode = (uint) result;
  304. if (errorCode == ErrorCode.RESPONSE_SUCCESS)
  305. {
  306. this.ErrorInfo = "允许交易成功";
  307. return;
  308. }
  309. this.ErrorInfo = errorCode.ToString();
  310. }
  311. public async Task ForbiddenAccountLogin()
  312. {
  313. int time = 0;
  314. if (!int.TryParse(this.ForbiddenLoginTime, out time))
  315. {
  316. this.ErrorInfo = "禁止时间请输入数字";
  317. return;
  318. }
  319. if (time < 0)
  320. {
  321. this.ErrorInfo = "禁止时间必须>=0";
  322. return;
  323. }
  324. ABossCommand bossCommand = new BCForbiddenAccountLogin(this.IMessageChannel)
  325. { Account = this.Account, ForbiddenLoginTime = this.ForbiddenLoginTime };
  326. var result = await bossCommand.DoAsync();
  327. var errorCode = (uint)result;
  328. if (errorCode == ErrorCode.RESPONSE_SUCCESS)
  329. {
  330. this.ErrorInfo = "禁止帐号登录成功";
  331. return;
  332. }
  333. this.ErrorInfo = string.Format("禁止帐号登录失败, error code: {0}", errorCode);
  334. }
  335. public async Task AllowAccountLogin()
  336. {
  337. ABossCommand bossCommand = new BCForbiddenAccountLogin(this.IMessageChannel)
  338. {
  339. Account = this.Account, ForbiddenLoginTime = "-1"
  340. };
  341. var result = await bossCommand.DoAsync();
  342. var errorCode = (uint)result;
  343. if (errorCode == ErrorCode.RESPONSE_SUCCESS)
  344. {
  345. this.ErrorInfo = "允许帐号登录成功";
  346. return;
  347. }
  348. this.ErrorInfo = string.Format("允许帐号登录失败, error code: {0}", errorCode);
  349. }
  350. public async Task SendCommand()
  351. {
  352. if (this.Command.StartsWith("gm ", true, CultureInfo.CurrentCulture))
  353. {
  354. this.Command = this.Command.Substring(3);
  355. }
  356. ABossCommand bossCommand = new BCCommand(this.IMessageChannel)
  357. { Command = this.Command };
  358. string commandString = this.Command;
  359. object result = null;
  360. try
  361. {
  362. result = await bossCommand.DoAsync();
  363. }
  364. catch(Exception e)
  365. {
  366. Logger.Trace(e.ToString());
  367. return;
  368. }
  369. var smsgBossCommandResponse = (SMSG_Boss_Command_Response)result;
  370. this.ErrorInfo = string.Format(" send command: {0}, error code: {1}",
  371. commandString, JsonHelper.ToString(smsgBossCommandResponse));
  372. }
  373. }
  374. }