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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 WebConnect;
  10. using System.Net.Http;
  11. namespace starter.viewmodel.settings
  12. {
  13. public class SettingsViewModel : NotificationObject
  14. {
  15. /// <summary>
  16. /// Model object
  17. /// </summary>
  18. private SettingsModel obj = new SettingsModel();
  19. /// <summary>
  20. /// initializer
  21. /// </summary>
  22. public SettingsViewModel()
  23. {
  24. //Program.Tencent_cos_download.UpdateHash();
  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 string Route
  134. {
  135. get => obj.Route;
  136. set
  137. {
  138. obj.Route = value;
  139. this.RaisePropertyChanged("Route");
  140. }
  141. }
  142. public string Username
  143. {
  144. get => obj.Username;
  145. set
  146. {
  147. obj.Username = value;
  148. this.RaisePropertyChanged("Username");
  149. }
  150. }
  151. public string Password
  152. {
  153. get => obj.Password;
  154. set
  155. {
  156. obj.Password = value;
  157. this.RaisePropertyChanged("Password");
  158. }
  159. }
  160. public string CodeName
  161. {
  162. get
  163. {
  164. return obj.CodeRoute.Substring(obj.CodeRoute.LastIndexOf('/') == -1 ? obj.CodeRoute.LastIndexOf('\\') + 1 : obj.CodeRoute.LastIndexOf('/') + 1);
  165. }
  166. }
  167. public Visibility MenuVis
  168. {
  169. get
  170. {
  171. return Status == SettingsModel.Status.menu ? Visibility.Visible : Visibility.Collapsed;
  172. }
  173. }
  174. public Visibility RouteBoxVis
  175. {
  176. get
  177. {
  178. return (Status == SettingsModel.Status.newUser || Status == SettingsModel.Status.move) ? Visibility.Visible : Visibility.Collapsed;
  179. }
  180. }
  181. public Visibility LoginVis
  182. {
  183. get
  184. {
  185. return Status == SettingsModel.Status.login ? Visibility.Visible : Visibility.Collapsed;
  186. }
  187. }
  188. public Visibility ProgressVis
  189. {
  190. get
  191. {
  192. return Status == SettingsModel.Status.working ? Visibility.Visible : Visibility.Collapsed;
  193. }
  194. }
  195. public Visibility CompleteVis
  196. {
  197. get
  198. {
  199. return Status == SettingsModel.Status.successful ? Visibility.Visible : Visibility.Collapsed;
  200. }
  201. }
  202. public Visibility WebVis
  203. {
  204. get
  205. {
  206. return Status == SettingsModel.Status.web ? Visibility.Visible : Visibility.Collapsed;
  207. }
  208. }
  209. public Visibility CoverVis
  210. {
  211. get
  212. {
  213. return Status == SettingsModel.Status.web && !obj.UploadReady ? Visibility.Visible : Visibility.Collapsed;
  214. }
  215. }
  216. public Visibility LoginFailVis
  217. {
  218. get
  219. {
  220. return obj.LoginFailed ? Visibility.Visible : Visibility.Collapsed;
  221. }
  222. }
  223. public Visibility MatchFinishedVis
  224. {
  225. get
  226. {
  227. return obj.CombatCompleted ? Visibility.Visible : Visibility.Collapsed;
  228. }
  229. }
  230. public Visibility UploadReadyVis
  231. {
  232. get { return obj.UploadReady ? Visibility.Visible : Visibility.Collapsed; }
  233. }
  234. public string UpdateBtnCont
  235. {
  236. get
  237. {
  238. return obj.UpdatePlanned ? "Update" : "Check Updates";
  239. }
  240. }
  241. public string UpdateInfo
  242. {
  243. get
  244. {
  245. if (obj.UpdatePlanned)
  246. return obj.Updates;
  247. else
  248. return "";
  249. }
  250. }
  251. public string LaunchBtnCont
  252. {
  253. get
  254. {
  255. return obj.UpdatePlanned ? "更新" : "启动";
  256. }
  257. }
  258. public string UploadBtnCont
  259. {
  260. get
  261. {
  262. return obj.UploadReady ? "上传代码" : "选择代码上传";
  263. }
  264. }
  265. public string RouteSelectWindow(string type)
  266. {
  267. if (type == "Folder")
  268. {
  269. using (FolderBrowserDialog dialog = new FolderBrowserDialog())
  270. {
  271. _ = dialog.ShowDialog();
  272. if (dialog.SelectedPath != String.Empty)
  273. return dialog.SelectedPath;
  274. }
  275. }
  276. else if (type == "File")
  277. {
  278. var openFileDialog = new Microsoft.Win32.OpenFileDialog()
  279. {
  280. Filter = "c++ Source Files (.cpp)|*.cpp|c++ Header File (.h)|*.h|python Source File (.py)|*.py"
  281. };
  282. var result = openFileDialog.ShowDialog();
  283. if (result == true)
  284. {
  285. return openFileDialog.FileName;
  286. }
  287. }
  288. return "";
  289. }
  290. private BaseCommand clickBrowseCommand;
  291. public BaseCommand ClickBrowseCommand
  292. {
  293. get
  294. {
  295. if (clickBrowseCommand == null)
  296. {
  297. clickBrowseCommand = new BaseCommand(new Action<object>(o =>
  298. {
  299. Route = RouteSelectWindow("Folder");
  300. }));
  301. }
  302. return clickBrowseCommand;
  303. }
  304. }
  305. private BaseCommand clickConfirmCommand;
  306. public BaseCommand ClickConfirmCommand
  307. {
  308. get
  309. {
  310. if (clickConfirmCommand == null)
  311. {
  312. clickConfirmCommand = new BaseCommand(new Action<object>(o =>
  313. {
  314. if (Status == SettingsModel.Status.newUser)
  315. {
  316. Status = SettingsModel.Status.working;
  317. this.RaisePropertyChanged("ProgressVis");
  318. if (obj.install())
  319. {
  320. Status = SettingsModel.Status.successful;
  321. }
  322. }
  323. else if (Status == SettingsModel.Status.move)
  324. {
  325. Status = SettingsModel.Status.working;
  326. this.RaisePropertyChanged("ProgressVis");
  327. switch (obj.move())
  328. {
  329. case -1:
  330. MessageBox.Show("文件已打开或者目标路径下有同名文件!", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  331. break;
  332. case -2:
  333. Status = SettingsModel.Status.error;
  334. break;
  335. }
  336. Status = SettingsModel.Status.successful;
  337. }
  338. }));
  339. }
  340. return clickConfirmCommand;
  341. }
  342. }
  343. private BaseCommand clickUpdateCommand;
  344. public BaseCommand ClickUpdateCommand
  345. {
  346. get
  347. {
  348. if (clickUpdateCommand == null)
  349. {
  350. clickUpdateCommand = new BaseCommand(new Action<object>(o =>
  351. {
  352. if (obj.UpdatePlanned)
  353. {
  354. Status = SettingsModel.Status.working;
  355. this.RaisePropertyChanged("ProgressVis");
  356. if (obj.Update())
  357. {
  358. Status = SettingsModel.Status.successful;
  359. this.RaisePropertyChanged("UpdateBtnCont");
  360. this.RaisePropertyChanged("UpdateInfo");
  361. }
  362. else
  363. Status = SettingsModel.Status.error;
  364. }
  365. else
  366. {
  367. Status = SettingsModel.Status.working;
  368. this.RaisePropertyChanged("ProgressVis");
  369. Status = obj.checkUpdate();
  370. this.RaisePropertyChanged("UpdateBtnCont");
  371. this.RaisePropertyChanged("UpdateInfo");
  372. }
  373. }));
  374. }
  375. return clickUpdateCommand;
  376. }
  377. }
  378. private BaseCommand clickMoveCommand;
  379. public BaseCommand ClickMoveCommand
  380. {
  381. get
  382. {
  383. if (clickMoveCommand == null)
  384. {
  385. clickMoveCommand = new BaseCommand(new Action<object>(o =>
  386. {
  387. Status = SettingsModel.Status.move;
  388. }));
  389. }
  390. return clickMoveCommand;
  391. }
  392. }
  393. private BaseCommand clickUninstCommand;
  394. public BaseCommand ClickUninstCommand
  395. {
  396. get
  397. {
  398. if (clickUninstCommand == null)
  399. {
  400. clickUninstCommand = new BaseCommand(new Action<object>(o =>
  401. {
  402. Status = SettingsModel.Status.working;
  403. this.RaisePropertyChanged("ProgressVis");
  404. switch (obj.Uninst())
  405. {
  406. case -1:
  407. Status = SettingsModel.Status.menu;
  408. MessageBox.Show("文件已经打开,请关闭后再删除", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  409. break;
  410. case 0:
  411. Status = SettingsModel.Status.successful;
  412. break;
  413. default:
  414. Status = SettingsModel.Status.error;
  415. break;
  416. }
  417. }));
  418. }
  419. return clickUninstCommand;
  420. }
  421. }
  422. private BaseCommand clickLoginCommand;
  423. public BaseCommand ClickLoginCommand
  424. {
  425. get
  426. {
  427. if (clickLoginCommand == null)
  428. {
  429. clickLoginCommand = new BaseCommand(new Action<object>(async o =>
  430. {
  431. if (!(await obj.Login()))
  432. {
  433. obj.LoginFailed = true;
  434. }
  435. else
  436. {
  437. Status = SettingsModel.Status.web;
  438. }
  439. this.RaisePropertyChanged("LoginFailVis");
  440. }));
  441. }
  442. return clickLoginCommand;
  443. }
  444. }
  445. private BaseCommand clickLaunchCommand;
  446. public BaseCommand ClickLaunchCommand
  447. {
  448. get
  449. {
  450. if (clickLaunchCommand == null)
  451. {
  452. clickLaunchCommand = new BaseCommand(new Action<object>(o =>
  453. {
  454. if (obj.UpdatePlanned)
  455. {
  456. Status = SettingsModel.Status.working;
  457. this.RaisePropertyChanged("ProgressVis");
  458. if (obj.Update())
  459. {
  460. this.RaisePropertyChanged("UpdateBtnCont");
  461. this.RaisePropertyChanged("LaunchBtnCont");
  462. Status = SettingsModel.Status.login;
  463. this.RaisePropertyChanged("UpdateInfo");
  464. }
  465. else
  466. Status = SettingsModel.Status.error;
  467. }
  468. else if (!obj.Launch())
  469. {
  470. Status = SettingsModel.Status.menu;
  471. }
  472. }));
  473. }
  474. return clickLaunchCommand;
  475. }
  476. }
  477. private BaseCommand clickEditCommand;
  478. public BaseCommand ClickEditCommand
  479. {
  480. get
  481. {
  482. if (clickEditCommand == null)
  483. {
  484. clickEditCommand = new BaseCommand(new Action<object>(o =>
  485. {
  486. Status = SettingsModel.Status.menu;
  487. }));
  488. }
  489. return clickEditCommand;
  490. }
  491. }
  492. private BaseCommand clickBackCommand;
  493. public BaseCommand ClickBackCommand
  494. {
  495. get
  496. {
  497. if (clickBackCommand == null)
  498. {
  499. clickBackCommand = new BaseCommand(new Action<object>(o =>
  500. {
  501. Status = SettingsModel.Status.login;
  502. }));
  503. }
  504. return clickBackCommand;
  505. }
  506. }
  507. private BaseCommand clickUploadCommand;
  508. public BaseCommand ClickUploadCommand
  509. {
  510. get
  511. {
  512. if (clickUploadCommand == null)
  513. {
  514. clickUploadCommand = new BaseCommand(new Action<object>(async o =>
  515. {
  516. if (obj.UploadReady)
  517. {
  518. switch (await obj.Upload())
  519. {
  520. case -1:
  521. MessageBox.Show("Token失效!", "", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
  522. break;
  523. case -2:
  524. MessageBox.Show("目标路径不存在!", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  525. break;
  526. case -3:
  527. MessageBox.Show("服务器错误", "", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
  528. break;
  529. case -4:
  530. MessageBox.Show("您未登录或登录失效", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  531. Status = SettingsModel.Status.login;
  532. break;
  533. case -5:
  534. MessageBox.Show("您未报名THUAI!", "", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
  535. break;
  536. case -6:
  537. MessageBox.Show("读取文件失败,请确认文件是否被占用", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  538. break;
  539. case -7:
  540. MessageBox.Show("网络错误,请检查你的网络", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  541. break;
  542. case -8:
  543. MessageBox.Show("不是c++或python源文件", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  544. break;
  545. }
  546. obj.CodeRoute = "";
  547. obj.UploadReady = false;
  548. this.RaisePropertyChanged("UploadBtnCont");
  549. this.RaisePropertyChanged("UploadReadyVis");
  550. }
  551. else
  552. {
  553. obj.CodeRoute = RouteSelectWindow("File");
  554. if (obj.CodeRoute != "")
  555. {
  556. obj.UploadReady = true;
  557. this.RaisePropertyChanged("UploadBtnCont");
  558. this.RaisePropertyChanged("UploadReadyVis");
  559. this.RaisePropertyChanged("CodeName");
  560. }
  561. else
  562. {
  563. MessageBox.Show("未选择代码,请重新选择!", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
  564. }
  565. }
  566. }));
  567. }
  568. return clickUploadCommand;
  569. }
  570. }
  571. }
  572. }