RobotViewModel.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 BossClient;
  9. using BossCommand;
  10. using DataCenter;
  11. using BossBase;
  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 string errorInfo = "";
  21. private int findTypeIndex;
  22. private string account = "";
  23. private string findType = "egametang@163.com";
  24. private string name = "";
  25. private string guid = "";
  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. private DataCenterEntities entities = new DataCenterEntities();
  32. private IMessageChannel iMessageChannel;
  33. public DataCenterEntities Entities
  34. {
  35. get
  36. {
  37. return this.entities;
  38. }
  39. set
  40. {
  41. this.entities = value;
  42. }
  43. }
  44. public IMessageChannel IMessageChannel
  45. {
  46. get
  47. {
  48. return this.iMessageChannel;
  49. }
  50. set
  51. {
  52. this.iMessageChannel = value;
  53. }
  54. }
  55. public int FindTypeIndex
  56. {
  57. get
  58. {
  59. return this.findTypeIndex;
  60. }
  61. set
  62. {
  63. if (this.findTypeIndex == value)
  64. {
  65. return;
  66. }
  67. this.findTypeIndex = value;
  68. this.RaisePropertyChanged("FindTypeIndex");
  69. }
  70. }
  71. public string FindType
  72. {
  73. get
  74. {
  75. return this.findType;
  76. }
  77. set
  78. {
  79. if (this.findType == value)
  80. {
  81. return;
  82. }
  83. this.findType = value;
  84. this.RaisePropertyChanged("FindType");
  85. }
  86. }
  87. public Visibility DockPanelVisiable
  88. {
  89. get
  90. {
  91. return this.dockPanelVisiable;
  92. }
  93. set
  94. {
  95. if (this.dockPanelVisiable == value)
  96. {
  97. return;
  98. }
  99. this.dockPanelVisiable = value;
  100. this.RaisePropertyChanged("DockPanelVisiable");
  101. }
  102. }
  103. public ObservableCollection<ServerViewModel> ServerInfos
  104. {
  105. get
  106. {
  107. return this.serverInfos;
  108. }
  109. }
  110. public string Account
  111. {
  112. get
  113. {
  114. return this.account;
  115. }
  116. set
  117. {
  118. if (this.account == value)
  119. {
  120. return;
  121. }
  122. this.account = value;
  123. this.RaisePropertyChanged("Account");
  124. }
  125. }
  126. public string Name
  127. {
  128. get
  129. {
  130. return this.name;
  131. }
  132. set
  133. {
  134. if (this.name == value)
  135. {
  136. return;
  137. }
  138. this.name = value;
  139. this.RaisePropertyChanged("Name");
  140. }
  141. }
  142. public string Guid
  143. {
  144. get
  145. {
  146. return this.guid;
  147. }
  148. set
  149. {
  150. if (this.guid == value)
  151. {
  152. return;
  153. }
  154. this.guid = value;
  155. this.RaisePropertyChanged("Guid");
  156. }
  157. }
  158. public string ErrorInfo
  159. {
  160. get
  161. {
  162. return this.errorInfo;
  163. }
  164. set
  165. {
  166. if (this.errorInfo == value)
  167. {
  168. return;
  169. }
  170. this.errorInfo = value;
  171. this.RaisePropertyChanged("ErrorInfo");
  172. }
  173. }
  174. public bool IsGMEnable
  175. {
  176. get
  177. {
  178. return this.isGMEnable;
  179. }
  180. set
  181. {
  182. if (this.isGMEnable == value)
  183. {
  184. return;
  185. }
  186. this.isGMEnable = value;
  187. this.RaisePropertyChanged("IsGMEnable");
  188. }
  189. }
  190. [ImportingConstructor]
  191. public RobotViewModel(IEventAggregator eventAggregator)
  192. {
  193. eventAggregator.GetEvent<LoginOKEvent>().Subscribe(this.OnLoginOK);
  194. }
  195. ~RobotViewModel()
  196. {
  197. this.Disposing(false);
  198. }
  199. public void Dispose()
  200. {
  201. this.Disposing(true);
  202. GC.SuppressFinalize(this);
  203. }
  204. private void Disposing(bool disposing)
  205. {
  206. this.entities.Dispose();
  207. this.bossClient.Dispose();
  208. }
  209. public void OnLoginOK(IMessageChannel messageChannel)
  210. {
  211. this.DockPanelVisiable = Visibility.Visible;
  212. this.IMessageChannel = messageChannel;
  213. }
  214. public async void Servers()
  215. {
  216. ABossCommand bossCommand = new BCServerInfo(this.IMessageChannel, this.Entities);
  217. var result = await bossCommand.DoAsync();
  218. var smsgBossServersInfo = result as SMSG_Boss_ServersInfo;
  219. if (smsgBossServersInfo == null)
  220. {
  221. this.ErrorInfo = "查询服务器失败!";
  222. return;
  223. }
  224. this.ServerInfos.Clear();
  225. foreach (var nm in smsgBossServersInfo.Name)
  226. {
  227. this.ServerInfos.Add(new ServerViewModel { Name = nm });
  228. }
  229. this.ErrorInfo = "查询服务器成功!";
  230. }
  231. public void Reload()
  232. {
  233. ABossCommand bossCommand = new BCReloadWorld(this.IMessageChannel, this.Entities);
  234. bossCommand.Do();
  235. }
  236. public void FindPlayer()
  237. {
  238. ABossCommand bossCommand = new BCFindPlayer(this.IMessageChannel, this.Entities)
  239. {
  240. FindTypeIndex = this.FindTypeIndex,
  241. FindType = this.FindType
  242. };
  243. var result = bossCommand.Do() as t_character;
  244. if (result == null)
  245. {
  246. this.ErrorInfo = "查询失败";
  247. return;
  248. }
  249. this.Account = result.account;
  250. this.Name = result.character_name;
  251. this.Guid = result.character_guid.ToString(CultureInfo.InvariantCulture);
  252. this.IsGMEnable = true;
  253. this.ErrorInfo = "查询成功";
  254. }
  255. public async void ForbiddenBuy()
  256. {
  257. if (this.Guid == "")
  258. {
  259. this.ErrorInfo = "请先指定玩家";
  260. return;
  261. }
  262. ABossCommand bossCommand = new BCForbiddenBuy(this.IMessageChannel, this.Entities)
  263. {
  264. Guid = this.Guid
  265. };
  266. var result = await bossCommand.DoAsync();
  267. var errorCode = (int)result;
  268. if (errorCode == ErrorCode.RESPONSE_SUCCESS)
  269. {
  270. this.ErrorInfo = "禁止交易成功";
  271. return;
  272. }
  273. this.ErrorInfo = string.Format("禁止交易失败, error code: {0}", errorCode);
  274. }
  275. public async void AllowBuy()
  276. {
  277. if (this.Guid == "")
  278. {
  279. this.ErrorInfo = "请先指定玩家";
  280. return;
  281. }
  282. ABossCommand bossCommand = new BCAllowBuy(this.IMessageChannel, this.Entities)
  283. {
  284. Guid = this.Guid
  285. };
  286. var result = await bossCommand.DoAsync();
  287. var errorCode = (int) result;
  288. if (errorCode == ErrorCode.RESPONSE_SUCCESS)
  289. {
  290. this.ErrorInfo = "允许交易成功";
  291. return;
  292. }
  293. this.ErrorInfo = errorCode.ToString();
  294. }
  295. }
  296. }