MainWindow.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Data;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Imaging;
  10. using Курсовой_проект_3._1.Windows;
  11. namespace Курсовой_проект_3._1
  12. {
  13. /// <summary>
  14. /// Логика взаимодействия для MainWindow.xaml
  15. /// </summary>
  16. public partial class MainWindow : Window
  17. {
  18. int nowUserId; // id открытого профиля
  19. MyTeamContext _context;
  20. // TeamsGrid
  21. List<PartInTeams> teams = new List<PartInTeams>(5);
  22. // MatchesGrid
  23. List<Match> matches = new List<Match>(5);
  24. public MainWindow(int userId)
  25. {
  26. InitializeComponent();
  27. _context = new MyTeamContext();
  28. nowUserId = userId;
  29. int? nowTeamId = _context.TeamsUsers.Where(tuIn => tuIn.FK_User_Id == nowUserId && tuIn.DateEnd == null).Select(tuIn => tuIn.FK_Team_Id).FirstOrDefault();
  30. // адаптация к размеру окна
  31. SizeChanged += MainWindow_SizeChanged;
  32. // Доступность кнопки настроек
  33. if (nowUserId == App.UserId)
  34. {
  35. SettingsBtn.Visibility = Visibility.Visible;
  36. }
  37. // Подгрузка краткой информации
  38. Users user = _context.Users.Find(nowUserId);
  39. RealNameTB.Text += $"{user.LName} {user.FName} {user.MName}";
  40. NicknameTB.Text += user.Nickname;
  41. BirthDateTB.Text += user.Birthday.ToString("D") + " (" + Func.CalculateAge(user.Birthday).ToString() + " лет)";
  42. if (user.Countries != null)
  43. {
  44. CountryTB.Text += user.Countries.Name;
  45. }
  46. PhoneNumberTB.Text += "+" + user.PhoneNumber;
  47. EmailTB.Text += user.Email;
  48. if (user.About != null)
  49. {
  50. AboutMeTB.Text += user.About;
  51. }
  52. if (user.PhotoPath != null)
  53. {
  54. UserPhotoImg.Source = new BitmapImage(new Uri(user.PhotoPath));
  55. }
  56. if (user.Disciplines != null)
  57. {
  58. DisciplineIconImg.Source = new BitmapImage(new Uri(user.Disciplines.IconPath));
  59. }
  60. else
  61. {
  62. DisciplineIconImg.Source = new BitmapImage(new Uri(@"C:\Users\nikich4523\source\repos\Курсовой проект 3.1\Курсовой проект 3.1\img\Default\discipline.png"));
  63. }
  64. // Подгрузка кубков и призовых
  65. if (nowTeamId != 0)
  66. {
  67. TeamTBlock.Text = "(" +_context.Teams.Find(nowTeamId).Name + ")";
  68. int teamMembersCount = _context.TeamsUsers.Where(tu => tu.FK_Team_Id == nowTeamId && tu.DateEnd == null).Count();
  69. decimal teamPrize = 0;
  70. if (_context.Achievements.Where(a => a.FK_Team_Id == nowTeamId).Select(a => a.Prize).Count() != 0)
  71. teamPrize = _context.Achievements.Where(a => a.FK_Team_Id == nowTeamId).Select(a => a.Prize).Sum();
  72. PriceMoneyTB.Text = "Призовые: " + Convert.ToString(teamPrize / teamMembersCount) + "$";
  73. List<int> teamIds = Func.GetTeamIds(nowUserId);
  74. int frstCupCount = 0;
  75. int scndCupCount = 0;
  76. int thrdCupCount = 0;
  77. foreach(int teamId in teamIds)
  78. {
  79. // Получаем даты нахождения в команде
  80. var param = _context.TeamsUsers.Where(p => p.FK_User_Id == userId && p.FK_Team_Id == teamId).Select(p => new
  81. {
  82. DateStart = p.DateStart,
  83. DateEnd = p.DateEnd
  84. }).ToList();
  85. DateTime dateStart = param[0].DateStart;
  86. DateTime? dateEnd = param[0].DateEnd;
  87. if (dateEnd == null)
  88. {
  89. dateEnd = Convert.ToDateTime("01/01/2099");
  90. }
  91. frstCupCount += _context.Achievements.Where(a => a.Place == "1" && a.FK_Team_Id == teamId && a.Tournaments.DateEnd >= dateStart && a.Tournaments.DateEnd >= dateEnd).Count();
  92. scndCupCount += _context.Achievements.Where(a => a.Place == "2" && a.FK_Team_Id == teamId && a.Tournaments.DateEnd >= dateStart && a.Tournaments.DateEnd >= dateEnd).Count();
  93. thrdCupCount += _context.Achievements.Where(a => a.Place == "3" && a.FK_Team_Id == teamId && a.Tournaments.DateEnd >= dateStart && a.Tournaments.DateEnd >= dateEnd).Count();
  94. }
  95. FirstPlaceCountTB.Text = ":" + Convert.ToString(frstCupCount);
  96. SecondPlaceCountTB.Text = ":" + Convert.ToString(scndCupCount);
  97. ThirdPlaceCountTB.Text = ":" + Convert.ToString(thrdCupCount);
  98. }
  99. // Подгрузка статистики
  100. var (winCount, drawCount, loseCount) = Func.GetWinDrawLoseCountUser(nowUserId);
  101. int gamesCount = winCount + drawCount + loseCount;
  102. if (gamesCount != 0)
  103. {
  104. LineBarWin.Width = new GridLength(winCount * 100 / gamesCount, GridUnitType.Star);
  105. LineBarDraw.Width = new GridLength(drawCount * 100 / gamesCount, GridUnitType.Star);
  106. LineBarLose.Width = new GridLength(loseCount * 100 / gamesCount, GridUnitType.Star);
  107. WinCountTB.Text = winCount.ToString() + " побед";
  108. DrawCountTB.Text = drawCount.ToString() + " ничьих";
  109. LoseCountTB.Text = loseCount.ToString() + " поражений";
  110. WinPercentTB.Text = Math.Round(Convert.ToDouble(winCount) * 100.0 / Convert.ToDouble(gamesCount), 2).ToString() + "%";
  111. DrawPercentTB.Text = Math.Round(Convert.ToDouble(drawCount) * 100 / Convert.ToDouble(gamesCount), 2).ToString() + "%";
  112. LosePercentTB.Text = Math.Round(Convert.ToDouble(loseCount) * 100 / Convert.ToDouble(gamesCount), 2).ToString() + "%";
  113. }
  114. // Подгрузка команд
  115. foreach (int teamId in Func.GetTeamIds(userId))
  116. {
  117. teams.Add(GetPartInTeams(userId, teamId));
  118. }
  119. List<PartInTeams> subTeamsList = new List<PartInTeams>(5);
  120. if (teams.Count < 5)
  121. {
  122. TeamsGrid.ItemsSource = teams;
  123. }
  124. else
  125. {
  126. for (int i = 0; i < 5; i++)
  127. {
  128. subTeamsList.Add(teams[i]);
  129. }
  130. TeamsGrid.ItemsSource = subTeamsList;
  131. }
  132. // Подгрузка матчей
  133. foreach (Matches match in Func.GetAllMatchesUser(userId))
  134. {
  135. matches.Add(GetMatch(match));
  136. }
  137. List<Match> subMatchList = new List<Match>();
  138. if (matches.Count < 5)
  139. {
  140. MatchesGrid.ItemsSource = matches;
  141. }
  142. else
  143. {
  144. for (int i = 0; i < 5; i++)
  145. {
  146. subMatchList.Add(matches[i]);
  147. }
  148. MatchesGrid.ItemsSource = subMatchList;
  149. }
  150. }
  151. private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
  152. {
  153. ProfileDockPanel.MinHeight = MyWindow.Height * 4 / 10;
  154. ProfileDockPanel.MaxHeight = ProfileDockPanel.MinHeight + 100;
  155. }
  156. private void MoreGamesButton_Click(object sender, RoutedEventArgs e)
  157. {
  158. if (matches.Count - MatchesGrid.Items.Count != 0)
  159. {
  160. List<Match> subMatchList = new List<Match>();
  161. for (int i = 0; i <= matches.Count - MatchesGrid.Items.Count; i++)
  162. {
  163. subMatchList.Add(matches[matches.Count + i]);
  164. }
  165. List<Match> newMatch = new List<Match>();
  166. newMatch.AddRange((List<Match>)MatchesGrid.ItemsSource);
  167. newMatch.AddRange(subMatchList);
  168. MatchesGrid.ItemsSource = new List<byte>();
  169. MatchesGrid.ItemsSource = newMatch;
  170. }
  171. else
  172. {
  173. MessageBox.Show("Больше матчей нет");
  174. return;
  175. }
  176. }
  177. private void MoreTeamsBtn_Click(object sender, RoutedEventArgs e)
  178. {
  179. if (teams.Count - TeamsGrid.Items.Count != 0)
  180. {
  181. List<PartInTeams> subTeamsList = new List<PartInTeams>();
  182. for (int i = 0; i <= teams.Count - TeamsGrid.Items.Count; i++)
  183. {
  184. subTeamsList.Add(teams[teams.Count + i]);
  185. }
  186. List<PartInTeams> newTeam = new List<PartInTeams>();
  187. newTeam.AddRange((List<PartInTeams>)TeamsGrid.ItemsSource);
  188. newTeam.AddRange(subTeamsList);
  189. TeamsGrid.ItemsSource = new List<byte>();
  190. TeamsGrid.ItemsSource = newTeam;
  191. }
  192. else
  193. {
  194. MessageBox.Show("Больше команд нет");
  195. return;
  196. }
  197. }
  198. private void SettingsBtn_Click(object sender, RoutedEventArgs e)
  199. {
  200. PlayerProfileSettingsWindow wnd = new PlayerProfileSettingsWindow();
  201. wnd.ShowDialog();
  202. }
  203. private void ToMainBtn_Click(object sender, RoutedEventArgs e)
  204. {
  205. ApplicationWindow wnd = new ApplicationWindow();
  206. wnd.Show();
  207. Close();
  208. }
  209. private void ToSearchBtn_Click(object sender, RoutedEventArgs e)
  210. {
  211. SearchWindow wnd = new SearchWindow();
  212. wnd.ShowDialog();
  213. }
  214. private void ToProfileBtn_Click(object sender, RoutedEventArgs e)
  215. {
  216. MainWindow wnd = new MainWindow(App.UserId);
  217. wnd.Show();
  218. Close();
  219. }
  220. private void TeamTBlock_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
  221. {
  222. TeamProfileWindow wnd = new TeamProfileWindow(_context.TeamsUsers.Where(tu => tu.FK_User_Id == nowUserId && tu.DateEnd == null).Select(tu => tu.FK_Team_Id).Single());
  223. wnd.Show();
  224. Close();
  225. }
  226. // ОБЪЕКТЫ ///
  227. // Возвращает объект класса PartInTeams
  228. private PartInTeams GetPartInTeams(int userId, int teamId)
  229. {
  230. // Получаем даты нахождения в команде
  231. var param = _context.TeamsUsers.Where(p => p.FK_User_Id == userId && p.FK_Team_Id == teamId).Select(p => new
  232. {
  233. DateStart = p.DateStart,
  234. DateEnd = p.DateEnd
  235. }).ToList();
  236. DateTime dateStart = param[0].DateStart;
  237. DateTime? dateEnd = param[0].DateEnd;
  238. // Создаем объект
  239. PartInTeams player = new PartInTeams();
  240. player.TeamName = Func.GetTeamName(teamId);
  241. player.DateOfStart = dateStart.ToString("d");
  242. player.DateOfEnd = dateEnd == null ? "наст. время" : Convert.ToDateTime(dateEnd).ToString("d");
  243. int winCount, drawCount, loseCount;
  244. (winCount, drawCount, loseCount) = Func.GetWinDrawLoseCountUserInOneTeam(userId, teamId);
  245. player.Stat = winCount.ToString() + " побед, " + drawCount.ToString() + " ничьих, " + loseCount.ToString() + " поражений";
  246. return player;
  247. }
  248. // Возвращает объект класса Match
  249. private Match GetMatch(Matches oldMatch)
  250. {
  251. Match match = new Match();
  252. match.FrstTeamName = Func.GetTeamName(oldMatch.FK_FrstTeam_Id);
  253. match.ScndTeamName = Func.GetTeamName(oldMatch.FK_ScndTeam_Id);
  254. match.Scores = oldMatch.FrstScore + " : " + oldMatch.ScndScore;
  255. match.Tournament = Func.GetTournamentName(oldMatch.FK_Tournament_Id);
  256. match.GameDate = oldMatch.GameDate.ToString("D");
  257. if (oldMatch.FK_WinnerTeam_Id != 1)
  258. {
  259. if (oldMatch.FK_WinnerTeam_Id == oldMatch.FK_FrstTeam_Id)
  260. {
  261. match.WinTeam = match.FrstTeamName;
  262. }
  263. else
  264. {
  265. match.WinTeam = match.ScndTeamName;
  266. }
  267. }
  268. return match;
  269. }
  270. } // class MainWindow
  271. public class MyConverter : IMultiValueConverter
  272. {
  273. public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
  274. {
  275. string team = value[0] as string;
  276. string winTeam = value[1] as string;
  277. if (team == winTeam)
  278. {
  279. return Brushes.LightGreen;
  280. }
  281. else
  282. {
  283. return DependencyProperty.UnsetValue;
  284. }
  285. }
  286. public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
  287. {
  288. throw new NotSupportedException();
  289. }
  290. }
  291. public class PartInTeams
  292. {
  293. public string TeamName { get; set; }
  294. public string Stat { get; set; }
  295. public string DateOfStart { get; set; }
  296. public string DateOfEnd { get; set; }
  297. }
  298. public class Match
  299. {
  300. public string FrstTeamName { get; set; }
  301. public string ScndTeamName { get; set; }
  302. public string Scores { get; set; }
  303. public string GameDate { get; set; }
  304. public string Tournament { get; set; }
  305. public string WinTeam { get; set; }
  306. }
  307. } // namespace