MatchWindow.xaml.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Shapes;
  14. using Курсовой_проект_3._1.UserControls;
  15. namespace Курсовой_проект_3._1.Windows
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для MatchWindow.xaml
  19. /// </summary>
  20. public partial class MatchWindow : Window
  21. {
  22. MyTeamContext _context;
  23. int nowTournamentId;
  24. int roundNum;
  25. public MatchWindow(int tournamentId, string frstTeamName, string scndTeamName, int roundNum)
  26. {
  27. InitializeComponent();
  28. _context = new MyTeamContext();
  29. nowTournamentId = tournamentId;
  30. this.roundNum = roundNum;
  31. FrstTeamTBlock.Text = frstTeamName;
  32. ScndTeamTBlock.Text = scndTeamName;
  33. }
  34. public MatchWindow(MatchControl match, int tournamentId, int roundNum)
  35. {
  36. InitializeComponent();
  37. _context = new MyTeamContext();
  38. nowTournamentId = tournamentId;
  39. this.roundNum = roundNum;
  40. FrstTeamTBlock.Text = match.TeamNameFrst;
  41. ScndTeamTBlock.Text = match.TeamNameScnd;
  42. FrstTeamScoreTBox.Text = match.ScoreFrst;
  43. ScndTeamScoreTBox.Text = match.ScoreScnd;
  44. }
  45. private void AcceptBtn_Click(object sender, RoutedEventArgs e)
  46. {
  47. List<Matches> match = _context.Matches.Where(m => m.FK_Tournament_Id == nowTournamentId && m.RoundNum == roundNum
  48. && m.Teams.Name == FrstTeamTBlock.Text && m.Teams1.Name == ScndTeamTBlock.Text).ToList();
  49. if (match.Count() != 0)
  50. {
  51. Matches editMatch = match[0];
  52. editMatch.FK_FrstTeam_Id = _context.Teams.Where(t => t.Name == FrstTeamTBlock.Text).Select(t => t.Id).Single();
  53. editMatch.FK_ScndTeam_Id = _context.Teams.Where(t => t.Name == ScndTeamTBlock.Text).Select(t => t.Id).Single();
  54. editMatch.FrstScore = Convert.ToInt32(FrstTeamScoreTBox.Text);
  55. editMatch.ScndScore = Convert.ToInt32(ScndTeamScoreTBox.Text);
  56. if ((bool)FrstTeamRB.IsChecked)
  57. {
  58. editMatch.FK_WinnerTeam_Id = editMatch.FK_FrstTeam_Id;
  59. }
  60. else if ((bool)ScndTeamRB.IsChecked)
  61. {
  62. editMatch.FK_WinnerTeam_Id = editMatch.FK_ScndTeam_Id;
  63. }
  64. else
  65. {
  66. editMatch.FK_WinnerTeam_Id = null;
  67. }
  68. }
  69. else
  70. {
  71. string frstTeam = FrstTeamTBlock.Text;
  72. string scndTeam = ScndTeamTBlock.Text;
  73. Matches newMatch = new Matches()
  74. {
  75. FK_FrstTeam_Id = _context.Teams.Where(t => t.Name == frstTeam).Select(t => t.Id).Single(),
  76. FK_ScndTeam_Id = _context.Teams.Where(t => t.Name == scndTeam).Select(t => t.Id).Single(),
  77. FrstScore = Convert.ToInt32(FrstTeamScoreTBox.Text),
  78. ScndScore = Convert.ToInt32(ScndTeamScoreTBox.Text),
  79. RoundNum = roundNum,
  80. GameDate = DateTime.Now,
  81. FK_Tournament_Id = nowTournamentId
  82. };
  83. if ((bool)FrstTeamRB.IsChecked)
  84. {
  85. newMatch.FK_WinnerTeam_Id = newMatch.FK_FrstTeam_Id;
  86. }
  87. else if ((bool)ScndTeamRB.IsChecked)
  88. {
  89. newMatch.FK_WinnerTeam_Id = newMatch.FK_ScndTeam_Id;
  90. }
  91. else
  92. {
  93. newMatch.FK_WinnerTeam_Id = null;
  94. }
  95. _context.Matches.Add(newMatch);
  96. }
  97. _context.SaveChanges();
  98. // Закрываем окно и пересоздаем окно турнира
  99. foreach (var wnd in Application.Current.Windows)
  100. {
  101. if (wnd is TournamentWindow)
  102. {
  103. TournamentWindow tournamentWnd = (TournamentWindow)wnd;
  104. tournamentWnd.Close();
  105. TournamentWindow newTournamentWnd = new TournamentWindow(nowTournamentId);
  106. newTournamentWnd.Show();
  107. }
  108. }
  109. Close();
  110. }
  111. private void TeamTBlock_MouseDown(object sender, MouseButtonEventArgs e)
  112. {
  113. if (e.ClickCount == 2)
  114. {
  115. SelectTeamsInMatchWindow wnd = new SelectTeamsInMatchWindow(nowTournamentId, roundNum);
  116. wnd.ShowDialog();
  117. Close();
  118. }
  119. }
  120. }
  121. }