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

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