AddRoundWindow.xaml.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using ConnectionClass;
  2. using CybersportTournament.ElementsWindows;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. namespace CybersportTournament.AddWindows
  9. {
  10. /// <summary>
  11. /// Логика взаимодействия для AddRoundWindow.xaml
  12. /// </summary>
  13. public partial class AddRoundWindow : Window
  14. {
  15. List<char> chars = new List<char> { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ':' };
  16. Match match;
  17. public AddRoundWindow(int IDMatch)
  18. {
  19. InitializeComponent();
  20. match = Connection.db.Match.Where(item => item.ID == IDMatch).FirstOrDefault();
  21. Match.Content = match.Name;
  22. int numberRound;
  23. if (Connection.db.RoundsList.Where(item => item.IDMatch == match.ID).FirstOrDefault() == null)
  24. numberRound = 1;
  25. else
  26. numberRound = Connection.db.RoundsList.Where(item => item.IDMatch == match.ID).Max(item => item.Rounds.Name) + 1;
  27. Round.Content = numberRound;
  28. }
  29. private void AddButtonClick(object sender, RoutedEventArgs e)
  30. {
  31. #region Добавление раундreturn;
  32. Rounds round = new Rounds()
  33. {
  34. Name = Convert.ToInt32(Round.Content)
  35. };
  36. if (Period.Text != null)
  37. {
  38. int index = Period.Text.IndexOf(":");
  39. round.Period = new TimeSpan(Convert.ToInt32(Period.Text.Substring(0, index)), Convert.ToInt32(Period.Text.Substring(index + 1, Period.Text.Length - index - 1)), 0);
  40. }
  41. round.Result = "0:0";
  42. Connection.db.Rounds.Add(round);
  43. Connection.db.SaveChanges();
  44. if (Result.Text != null)
  45. {
  46. #region Авто изменение счёта матча
  47. int length = Result.Text.IndexOf(':');
  48. int firstTeamRoundScore = Convert.ToInt32(Result.Text.Substring(0, length));
  49. int secondTeamRoundScore = Convert.ToInt32(Result.Text.Substring(length + 1, Result.Text.Length - length - 1));
  50. length = match.Result.IndexOf(':');
  51. int firstTeamMatchScore = Convert.ToInt32(match.Result.Substring(0, length));
  52. int secondTeamMatchScore = Convert.ToInt32(match.Result.Substring(length + 1, match.Result.Length - length - 1));
  53. if (firstTeamRoundScore == secondTeamRoundScore)
  54. {
  55. ErrorWindow ew = new ErrorWindow("счёт не может быть одинаковым");
  56. ew.Show();
  57. return;
  58. }
  59. else if (firstTeamRoundScore > secondTeamRoundScore)
  60. {
  61. firstTeamMatchScore += 1;
  62. }
  63. else
  64. {
  65. secondTeamMatchScore += 1;
  66. }
  67. string matchResult = firstTeamMatchScore.ToString() + ":" + secondTeamMatchScore.ToString();
  68. match.Result = matchResult;
  69. #endregion
  70. round.Result = Result.Text;
  71. }
  72. Connection.db.SaveChanges();
  73. RoundsList roundList = new RoundsList()
  74. {
  75. IDRound = round.ID,
  76. IDMatch = match.ID
  77. };
  78. Connection.db.RoundsList.Add(roundList);
  79. Connection.db.SaveChanges();
  80. MatchWindow mw = new MatchWindow(match.ID);
  81. mw.Show();
  82. this.Close();
  83. #endregion
  84. }
  85. private void BackButtonClick(object sender, RoutedEventArgs e)
  86. {
  87. MainWindow mw = new MainWindow();
  88. mw.Show();
  89. this.Close();
  90. }
  91. private void TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
  92. {
  93. #region Валидация поля номера раунда
  94. if (((TextBox)sender).Text != "" && !chars.Contains(((TextBox)sender).Text[((TextBox)sender).Text.Length - 1]))
  95. {
  96. ((TextBox)sender).CaretIndex = ((TextBox)sender).Text.Length - 2;
  97. ((TextBox)sender).Text = ((TextBox)sender).Text.Remove(((TextBox)sender).Text.Length - 1, 1);
  98. }
  99. else
  100. {
  101. ((TextBox)sender).CaretIndex = ((TextBox)sender).Text.Length;
  102. }
  103. #endregion
  104. }
  105. }
  106. }