You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ViewModel.cs 22 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. 
  2. using starter.viewmodel.common;
  3. using System;
  4. using System.Windows;
  5. using System.Windows.Forms;
  6. using Downloader;
  7. using MessageBox = System.Windows.MessageBox;
  8. using System.Configuration;
  9. namespace starter.viewmodel.settings
  10. {
  11. public class SettingsViewModel : NotificationObject
  12. {
  13. /// <summary>
  14. /// Model object
  15. /// </summary>
  16. private SettingsModel obj = new SettingsModel();
  17. /// <summary>
  18. /// initializer
  19. /// </summary>
  20. public SettingsViewModel()
  21. {
  22. //Program.Tencent_cos_download.UpdateHash();
  23. if (Downloader.Program.Tencent_cos_download.CheckAlreadyDownload())
  24. {
  25. obj.checkUpdate();
  26. Status = SettingsModel.Status.login;
  27. this.RaisePropertyChanged("WindowWidth");
  28. //TODO:在启动时立刻检查更新,确保选手启动最新版选手包
  29. //TODO:若有更新,将启动键改为更新键;
  30. //TODO:相应地,使用login界面启动;
  31. //TODO:结构:上方为登录框架,下方有“修改选手包”按钮
  32. }
  33. else
  34. {
  35. Route = Environment.GetEnvironmentVariable("USERPROFILE") + "\\THUAI6";
  36. Status = SettingsModel.Status.newUser;
  37. this.RaisePropertyChanged("WindowWidth");
  38. }
  39. }
  40. //TODO:参赛界面:包括上传参赛代码、申请对战
  41. //TODO:界面中应包含上次对战完成提示及下载回放按钮
  42. public int ExtraColumn
  43. {
  44. get
  45. {
  46. if (Status == SettingsModel.Status.newUser || Status == SettingsModel.Status.move)
  47. return 75;
  48. else
  49. return 0;
  50. }
  51. }
  52. public int WindowWidth
  53. {
  54. get
  55. {
  56. switch (Status)
  57. {
  58. case SettingsModel.Status.newUser:
  59. return 505;
  60. case SettingsModel.Status.move:
  61. return 505;
  62. case SettingsModel.Status.working:
  63. return 435;
  64. case SettingsModel.Status.successful:
  65. return 435;
  66. default:
  67. return 355;
  68. }
  69. }
  70. }
  71. public SettingsModel.Status Status
  72. {
  73. get
  74. {
  75. return obj.status;
  76. }
  77. set
  78. {
  79. obj.status = value;
  80. this.RaisePropertyChanged("ExtraColumn");
  81. this.RaisePropertyChanged("Intro");
  82. this.RaisePropertyChanged("RouteBoxIntro");
  83. this.RaisePropertyChanged("LoginVis");
  84. this.RaisePropertyChanged("MenuVis");
  85. this.RaisePropertyChanged("RouteBoxVis");
  86. this.RaisePropertyChanged("ProgressVis");
  87. //TODO: Thread will be taken by process working and window will not refresh.
  88. this.RaisePropertyChanged("CompleteVis");
  89. this.RaisePropertyChanged("WindowWidth");
  90. this.RaisePropertyChanged("WebVis");
  91. }
  92. }
  93. public string Intro
  94. {
  95. get
  96. {
  97. switch (Status)
  98. {
  99. case SettingsModel.Status.newUser:
  100. return "欢迎使用选手包,请选择你想要安装选手包的位置:";
  101. case SettingsModel.Status.menu:
  102. return "你已经安装了选手包,请选择想要进行的操作:";
  103. case SettingsModel.Status.login:
  104. return "使用EESAST账号登录";
  105. case SettingsModel.Status.web:
  106. return "THUAI6 赛场:";
  107. case SettingsModel.Status.disconnected:
  108. return "你可能没有连接到网络,无法下载/更新选手包";
  109. case SettingsModel.Status.error:
  110. return "我们遇到了一些问题,请向[]反馈";
  111. default:
  112. return "";
  113. }
  114. }
  115. }
  116. public string RouteBoxIntro
  117. {
  118. get
  119. {
  120. switch (Status)
  121. {
  122. case SettingsModel.Status.newUser:
  123. return "将主体程序安装在:";
  124. case SettingsModel.Status.move:
  125. return "将主体程序移动到:";
  126. default:
  127. return "";
  128. }
  129. }
  130. }
  131. public string Route
  132. {
  133. get => obj.Route;
  134. set
  135. {
  136. obj.Route = value;
  137. this.RaisePropertyChanged("Route");
  138. }
  139. }
  140. public string Username
  141. {
  142. get => obj.Username;
  143. set
  144. {
  145. obj.Username = value;
  146. this.RaisePropertyChanged("Username");
  147. }
  148. }
  149. public string Password
  150. {
  151. get => obj.Password;
  152. set
  153. {
  154. obj.Password = value;
  155. this.RaisePropertyChanged("Password");
  156. }
  157. }
  158. public string CodeName
  159. {
  160. get
  161. {
  162. return obj.CodeRoute.Substring(obj.CodeRoute.LastIndexOf('/') == -1 ? obj.CodeRoute.LastIndexOf('\\') + 1 : obj.CodeRoute.LastIndexOf('/') + 1);
  163. }
  164. }
  165. public Visibility MenuVis
  166. {
  167. get
  168. {
  169. return Status == SettingsModel.Status.menu ? Visibility.Visible : Visibility.Collapsed;
  170. }
  171. }
  172. public Visibility RouteBoxVis
  173. {
  174. get
  175. {
  176. return (Status == SettingsModel.Status.newUser || Status == SettingsModel.Status.move) ? Visibility.Visible : Visibility.Collapsed;
  177. }
  178. }
  179. public Visibility LoginVis
  180. {
  181. get
  182. {
  183. return Status == SettingsModel.Status.login ? Visibility.Visible : Visibility.Collapsed;
  184. }
  185. }
  186. public Visibility ProgressVis
  187. {
  188. get
  189. {
  190. return Status == SettingsModel.Status.working ? Visibility.Visible : Visibility.Collapsed;
  191. }
  192. }
  193. public Visibility CompleteVis
  194. {
  195. get
  196. {
  197. return Status == SettingsModel.Status.successful ? Visibility.Visible : Visibility.Collapsed;
  198. }
  199. }
  200. public Visibility WebVis
  201. {
  202. get
  203. {
  204. return Status == SettingsModel.Status.web ? Visibility.Visible : Visibility.Collapsed;
  205. }
  206. }
  207. public Visibility CoverVis
  208. {
  209. get
  210. {
  211. return Status == SettingsModel.Status.web && !obj.UploadReady ? Visibility.Visible : Visibility.Collapsed;
  212. }
  213. }
  214. public Visibility LoginFailVis
  215. {
  216. get
  217. {
  218. return obj.LoginFailed ? Visibility.Visible : Visibility.Collapsed;
  219. }
  220. }
  221. public Visibility MatchFinishedVis
  222. {
  223. get
  224. {
  225. return obj.CombatCompleted ? Visibility.Visible : Visibility.Collapsed;
  226. }
  227. }
  228. public Visibility UploadReadyVis
  229. {
  230. get { return obj.UploadReady ? Visibility.Visible : Visibility.Collapsed; }
  231. }
  232. public string UpdateBtnCont
  233. {
  234. get
  235. {
  236. return obj.UpdatePlanned ? "Update" : "Check Updates";
  237. }
  238. }
  239. public string UpdateInfo
  240. {
  241. get
  242. {
  243. if (obj.UpdatePlanned)
  244. return obj.Updates;
  245. else
  246. return "";
  247. }
  248. }
  249. public string LaunchBtnCont
  250. {
  251. get
  252. {
  253. return obj.UpdatePlanned ? "更新" : "启动";
  254. }
  255. }
  256. public string UploadBtnCont
  257. {
  258. get
  259. {
  260. return obj.UploadReady ? "上传代码" : "选择代码上传";
  261. }
  262. }
  263. public string RouteSelectWindow(string type)
  264. {
  265. if (type == "Folder")
  266. {
  267. using (FolderBrowserDialog dialog = new FolderBrowserDialog())
  268. {
  269. _ = dialog.ShowDialog();
  270. if (dialog.SelectedPath != String.Empty)
  271. return dialog.SelectedPath;
  272. }
  273. }
  274. else if (type == "File")
  275. {
  276. var openFileDialog = new Microsoft.Win32.OpenFileDialog()
  277. {
  278. Filter = "c++ Source Files (.cpp)|*.cpp|c++ Header File (.h)|*.h|python Source File (.py)|*.py"
  279. };
  280. var result = openFileDialog.ShowDialog();
  281. if (result == true)
  282. {
  283. return openFileDialog.FileName;
  284. }
  285. }
  286. return "";
  287. }
  288. private BaseCommand clickBrowseCommand;
  289. public BaseCommand ClickBrowseCommand
  290. {
  291. get
  292. {
  293. if (clickBrowseCommand == null)
  294. {
  295. clickBrowseCommand = new BaseCommand(new Action<object>(o =>
  296. {
  297. Route = RouteSelectWindow("Folder");
  298. }));
  299. }
  300. return clickBrowseCommand;
  301. }
  302. }
  303. private BaseCommand clickConfirmCommand;
  304. public BaseCommand ClickConfirmCommand
  305. {
  306. get
  307. {
  308. if (clickConfirmCommand == null)
  309. {
  310. clickConfirmCommand = new BaseCommand(new Action<object>(o =>
  311. {
  312. if (Status == SettingsModel.Status.newUser)
  313. {
  314. Status = SettingsModel.Status.working;
  315. this.RaisePropertyChanged("ProgressVis");
  316. if (obj.install())
  317. {
  318. Status = SettingsModel.Status.successful;
  319. }
  320. }
  321. else if (Status == SettingsModel.Status.move)
  322. {
  323. Status = SettingsModel.Status.working;
  324. this.RaisePropertyChanged("ProgressVis");
  325. switch (obj.move())
  326. {
  327. case -1:
  328. MessageBox.Show("文件已打开或者目标路径下有同名文件!", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  329. break;
  330. case -2:
  331. Status = SettingsModel.Status.error;
  332. break;
  333. }
  334. Status = SettingsModel.Status.successful;
  335. }
  336. }));
  337. }
  338. return clickConfirmCommand;
  339. }
  340. }
  341. private BaseCommand clickUpdateCommand;
  342. public BaseCommand ClickUpdateCommand
  343. {
  344. get
  345. {
  346. if (clickUpdateCommand == null)
  347. {
  348. clickUpdateCommand = new BaseCommand(new Action<object>(o =>
  349. {
  350. if (obj.UpdatePlanned)
  351. {
  352. Status = SettingsModel.Status.working;
  353. this.RaisePropertyChanged("ProgressVis");
  354. if (obj.Update())
  355. {
  356. Status = SettingsModel.Status.successful;
  357. this.RaisePropertyChanged("UpdateBtnCont");
  358. this.RaisePropertyChanged("UpdateInfo");
  359. }
  360. else
  361. Status = SettingsModel.Status.error;
  362. }
  363. else
  364. {
  365. Status = SettingsModel.Status.working;
  366. this.RaisePropertyChanged("ProgressVis");
  367. Status = obj.checkUpdate();
  368. this.RaisePropertyChanged("UpdateBtnCont");
  369. this.RaisePropertyChanged("UpdateInfo");
  370. }
  371. }));
  372. }
  373. return clickUpdateCommand;
  374. }
  375. }
  376. private BaseCommand clickMoveCommand;
  377. public BaseCommand ClickMoveCommand
  378. {
  379. get
  380. {
  381. if (clickMoveCommand == null)
  382. {
  383. clickMoveCommand = new BaseCommand(new Action<object>(o =>
  384. {
  385. Status = SettingsModel.Status.move;
  386. }));
  387. }
  388. return clickMoveCommand;
  389. }
  390. }
  391. private BaseCommand clickUninstCommand;
  392. public BaseCommand ClickUninstCommand
  393. {
  394. get
  395. {
  396. if (clickUninstCommand == null)
  397. {
  398. clickUninstCommand = new BaseCommand(new Action<object>(o =>
  399. {
  400. Status = SettingsModel.Status.working;
  401. this.RaisePropertyChanged("ProgressVis");
  402. switch (obj.Uninst())
  403. {
  404. case -1:
  405. Status = SettingsModel.Status.menu;
  406. MessageBox.Show("文件已经打开,请关闭后再删除", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  407. break;
  408. case 0:
  409. Status = SettingsModel.Status.successful;
  410. break;
  411. default:
  412. Status = SettingsModel.Status.error;
  413. break;
  414. }
  415. }));
  416. }
  417. return clickUninstCommand;
  418. }
  419. }
  420. private BaseCommand clickLoginCommand;
  421. public BaseCommand ClickLoginCommand
  422. {
  423. get
  424. {
  425. if (clickLoginCommand == null)
  426. {
  427. clickLoginCommand = new BaseCommand(new Action<object>(async o =>
  428. {
  429. if (!(await obj.Login()))
  430. {
  431. obj.LoginFailed = true;
  432. }
  433. else
  434. {
  435. Status = SettingsModel.Status.web;
  436. }
  437. this.RaisePropertyChanged("LoginFailVis");
  438. }));
  439. }
  440. return clickLoginCommand;
  441. }
  442. }
  443. private BaseCommand clickLaunchCommand;
  444. public BaseCommand ClickLaunchCommand
  445. {
  446. get
  447. {
  448. if (clickLaunchCommand == null)
  449. {
  450. clickLaunchCommand = new BaseCommand(new Action<object>(o =>
  451. {
  452. if (obj.UpdatePlanned)
  453. {
  454. Status = SettingsModel.Status.working;
  455. this.RaisePropertyChanged("ProgressVis");
  456. if (obj.Update())
  457. {
  458. this.RaisePropertyChanged("UpdateBtnCont");
  459. this.RaisePropertyChanged("LaunchBtnCont");
  460. Status = SettingsModel.Status.login;
  461. this.RaisePropertyChanged("UpdateInfo");
  462. }
  463. else
  464. Status = SettingsModel.Status.error;
  465. }
  466. else if (!obj.Launch())
  467. {
  468. Status = SettingsModel.Status.menu;
  469. }
  470. }));
  471. }
  472. return clickLaunchCommand;
  473. }
  474. }
  475. private BaseCommand clickEditCommand;
  476. public BaseCommand ClickEditCommand
  477. {
  478. get
  479. {
  480. if (clickEditCommand == null)
  481. {
  482. clickEditCommand = new BaseCommand(new Action<object>(o =>
  483. {
  484. Status = SettingsModel.Status.menu;
  485. }));
  486. }
  487. return clickEditCommand;
  488. }
  489. }
  490. private BaseCommand clickBackCommand;
  491. public BaseCommand ClickBackCommand
  492. {
  493. get
  494. {
  495. if (clickBackCommand == null)
  496. {
  497. clickBackCommand = new BaseCommand(new Action<object>(o =>
  498. {
  499. Status = SettingsModel.Status.login;
  500. }));
  501. }
  502. return clickBackCommand;
  503. }
  504. }
  505. private BaseCommand clickUploadCommand;
  506. public BaseCommand ClickUploadCommand
  507. {
  508. get
  509. {
  510. if (clickUploadCommand == null)
  511. {
  512. clickUploadCommand = new BaseCommand(new Action<object>(async o =>
  513. {
  514. if (obj.UploadReady)
  515. {
  516. switch (await obj.Upload())
  517. {
  518. case -1:
  519. MessageBox.Show("Token失效!", "", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
  520. break;
  521. case -2:
  522. MessageBox.Show("目标路径不存在!", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  523. break;
  524. case -3:
  525. MessageBox.Show("服务器错误", "", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
  526. break;
  527. case -4:
  528. MessageBox.Show("您未登录或登录失效", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  529. Status = SettingsModel.Status.login;
  530. break;
  531. case -5:
  532. MessageBox.Show("您未报名THUAI!", "", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
  533. break;
  534. case -6:
  535. MessageBox.Show("读取文件失败,请确认文件是否被占用", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  536. break;
  537. case -7:
  538. MessageBox.Show("网络错误,请检查你的网络", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  539. break;
  540. case -8:
  541. MessageBox.Show("不是c++或python源文件", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  542. break;
  543. }
  544. obj.CodeRoute = "";
  545. obj.UploadReady = false;
  546. this.RaisePropertyChanged("UploadBtnCont");
  547. this.RaisePropertyChanged("UploadReadyVis");
  548. }
  549. else
  550. {
  551. obj.CodeRoute = RouteSelectWindow("File");
  552. if (obj.CodeRoute != "")
  553. {
  554. obj.UploadReady = true;
  555. this.RaisePropertyChanged("UploadBtnCont");
  556. this.RaisePropertyChanged("UploadReadyVis");
  557. this.RaisePropertyChanged("CodeName");
  558. }
  559. else
  560. {
  561. MessageBox.Show("未选择代码,请重新选择!", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  562. }
  563. }
  564. }));
  565. }
  566. return clickUploadCommand;
  567. }
  568. }
  569. }
  570. }