RobotViewModel.cs 6.3 KB

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