RobotViewModel.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. public DataCenterEntities Entities { get; set; }
  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 bool IsGMEnable
  153. {
  154. get
  155. {
  156. return this.isGMEnable;
  157. }
  158. set
  159. {
  160. if (this.isGMEnable == value)
  161. {
  162. return;
  163. }
  164. this.isGMEnable = value;
  165. this.RaisePropertyChanged("IsGMEnable");
  166. }
  167. }
  168. [ImportingConstructor]
  169. public RobotViewModel(IEventAggregator eventAggregator)
  170. {
  171. this.Entities = new DataCenterEntities();
  172. eventAggregator.GetEvent<LoginOKEvent>().Subscribe(this.OnLoginOK);
  173. }
  174. ~RobotViewModel()
  175. {
  176. this.Disposing(false);
  177. }
  178. public void Dispose()
  179. {
  180. this.Disposing(true);
  181. GC.SuppressFinalize(this);
  182. }
  183. private void Disposing(bool disposing)
  184. {
  185. this.Entities.Dispose();
  186. this.bossClient.Dispose();
  187. }
  188. public void OnLoginOK(IMessageChannel messageChannel)
  189. {
  190. this.DockPanelVisiable = Visibility.Visible;
  191. this.IMessageChannel = messageChannel;
  192. }
  193. public async Task Servers()
  194. {
  195. ABossCommand bossCommand = new BCServerInfo(this.IMessageChannel, this.Entities);
  196. var result = await bossCommand.DoAsync();
  197. var smsgBossServersInfo = result as SMSG_Boss_ServersInfo;
  198. if (smsgBossServersInfo == null)
  199. {
  200. this.ErrorInfo = "查询服务器失败!";
  201. return;
  202. }
  203. this.ServerInfos.Clear();
  204. foreach (var nm in smsgBossServersInfo.Name)
  205. {
  206. this.ServerInfos.Add(new ServerViewModel { Name = nm });
  207. }
  208. this.ErrorInfo = "查询服务器成功!";
  209. }
  210. public void Reload()
  211. {
  212. ABossCommand bossCommand = new BCReloadWorld(this.IMessageChannel, this.Entities);
  213. bossCommand.Do();
  214. }
  215. public void FindPlayer()
  216. {
  217. ABossCommand bossCommand = new BCFindPlayer(this.IMessageChannel, this.Entities)
  218. {
  219. FindTypeIndex = this.FindTypeIndex,
  220. FindType = this.FindType
  221. };
  222. var result = bossCommand.Do() as t_character;
  223. if (result == null)
  224. {
  225. this.ErrorInfo = "查询失败";
  226. return;
  227. }
  228. this.Account = result.account;
  229. this.Name = result.character_name;
  230. this.Guid = result.character_guid.ToString(CultureInfo.InvariantCulture);
  231. this.IsGMEnable = true;
  232. this.ErrorInfo = "查询成功";
  233. }
  234. public async Task ForbiddenBuy()
  235. {
  236. if (this.Guid == "")
  237. {
  238. this.ErrorInfo = "请先指定玩家";
  239. return;
  240. }
  241. ABossCommand bossCommand = new BCForbiddenBuy(this.IMessageChannel, this.Entities)
  242. {
  243. Guid = this.Guid
  244. };
  245. var result = await bossCommand.DoAsync();
  246. var errorCode = (int)result;
  247. if (errorCode == ErrorCode.RESPONSE_SUCCESS)
  248. {
  249. this.ErrorInfo = "禁止交易成功";
  250. return;
  251. }
  252. this.ErrorInfo = string.Format("禁止交易失败, error code: {0}", errorCode);
  253. }
  254. public async Task AllowBuy()
  255. {
  256. if (this.Guid == "")
  257. {
  258. this.ErrorInfo = "请先指定玩家";
  259. return;
  260. }
  261. ABossCommand bossCommand = new BCAllowBuy(this.IMessageChannel, this.Entities)
  262. {
  263. Guid = this.Guid
  264. };
  265. var result = await bossCommand.DoAsync();
  266. var errorCode = (int) result;
  267. if (errorCode == ErrorCode.RESPONSE_SUCCESS)
  268. {
  269. this.ErrorInfo = "允许交易成功";
  270. return;
  271. }
  272. this.ErrorInfo = errorCode.ToString();
  273. }
  274. }
  275. }