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 24 kB

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