public void GenerateData() { var startingTime = new DateTime(2018, 1, 12, 19, 0, 0); using (var context = new MarathonContext()) { var homePlayers = context.Players.Where(x => x.Team.IsHome).ToList(); var guestPlayers = context.Players.Where(x => !x.Team.IsHome).ToList(); var totalPoints = 3000; var random = new Random(DateTime.Now.Second); var currentTime = startingTime; for (var i = 0; i < totalPoints; i++) { currentTime = currentTime.AddSeconds(random.Next() % 40); if (i % 20 == 0) { GenerateRandomPlayers(homePlayers, guestPlayers); context.SaveChanges(); } var teamId = random.Next() % 3; AddPoint(teamId <= 0 ? 1 : teamId, _firstHomePlayerId, _secondHomePlayerId, _firstGuestPlayerId, _secondGuestPlayerId, currentTime, context); } context.SaveChanges(); } }
private void RefreshPoints() { using (var context = new MarathonContext()) { HomePointsLabel.Text = context.Points.Count(x => x.GainTeamId == _homeTeamId).ToString("D5"); GuestPointsLabel.Text = context.Points.Count(x => x.GainTeamId == _guestTeamId).ToString("D5"); } }
private void InitialzeFromDatabase() { LoadPlayers(); using (var context = new MarathonContext()) { var settings = context.Settings.SingleOrDefault(); if (settings == null) { context.Settings.Add(new Settings()); context.SaveChanges(); } else { if (settings.MarathonStart.HasValue) { _marathonStart = settings.MarathonStart.Value; StartTimeTextBox.Text = $"{settings.MarathonStart.Value.ToShortDateString()} {settings.MarathonStart.Value.ToLongTimeString()}"; marathonTimer.Start(); } } _homeTeamId = context.Teams.Where(x => x.IsHome).Select(x => x.Id).Single(); _guestTeamId = context.Teams.Where(x => !x.IsHome).Select(x => x.Id).Single(); if (settings == null) { settings = context.Settings.Single(); } if (settings.ActualHomePlayerOne.HasValue) { HomePlayerOneComboBox.SelectedValue = settings.ActualHomePlayerOne.Value; } if (settings.ActualHomePlayerTwo.HasValue) { HomePlayerTwoComboBox.SelectedValue = settings.ActualHomePlayerTwo.Value; } if (settings.ActualGuestPlayerOne.HasValue) { GuestPlayerOneComboBox.SelectedValue = settings.ActualGuestPlayerOne.Value; } if (settings.ActualGuestPlayerTwo.HasValue) { GuestPlayerTwoComboBox.SelectedValue = settings.ActualGuestPlayerTwo.Value; } } RefreshPoints(); }
private void AddPoint(int teamId) { var homePlayerOneId = Convert.ToInt32(HomePlayerOneComboBox.SelectedValue); var homePlayerTwoId = Convert.ToInt32(HomePlayerTwoComboBox.SelectedValue); var guestPlayerOneId = Convert.ToInt32(GuestPlayerOneComboBox.SelectedValue); var guestPlayerTwoId = Convert.ToInt32(GuestPlayerTwoComboBox.SelectedValue); if (homePlayerOneId == 0 || homePlayerTwoId == 0 || guestPlayerOneId == 0 || guestPlayerTwoId == 0) { return; } using (var context = new MarathonContext()) { var lastPoint = context.Points.OrderByDescending(x => x.Created).FirstOrDefault(); var matchId = 1; if (lastPoint != null) { var savedPlayers = new List <int> { lastPoint.HomePlayerOneId, lastPoint.HomePlayerTwoId, lastPoint.GuestPlayerOneId, lastPoint.GuestPlayerTwoId }; var actualPlayers = new List <int> { homePlayerOneId, homePlayerTwoId, guestPlayerOneId, guestPlayerTwoId }; if (savedPlayers.Except(actualPlayers).Any()) { matchId = lastPoint.MatchId + 1; } else { matchId = lastPoint.MatchId; } } context.Points.Add(new Point { Created = DateTime.Now, GainTeamId = teamId, HomePlayerOneId = homePlayerOneId, HomePlayerTwoId = homePlayerTwoId, GuestPlayerOneId = guestPlayerOneId, GuestPlayerTwoId = guestPlayerTwoId, MatchId = matchId }); context.SaveChanges(); } RefreshPoints(); }
private void DeleteLastPoint(int teamId) { using (var context = new MarathonContext()) { var point = context.Points.Where(x => x.GainTeamId == teamId).OrderByDescending(x => x.Created).FirstOrDefault(); if (point != null) { context.Points.Remove(point); context.SaveChanges(); } } RefreshPoints(); }
private void LoadPlayers() { using (var context = new MarathonContext()) { var homePlayers = context.Teams.Where(x => x.IsHome).SelectMany(x => x.Players).ToList(); var guestPlayers = context.Teams.Where(x => !x.IsHome).SelectMany(x => x.Players).ToList(); HomePlayerBindingSource.DataSource = homePlayers; HomePlayerOneBindingSource.DataSource = homePlayers; HomePlayerTwoBindingSource.DataSource = homePlayers; GuestPlayerBindingSource.DataSource = guestPlayers; GuestPlayerOneBindingSource.DataSource = guestPlayers; GuestPlayerTwoBindingSource.DataSource = guestPlayers; } }
private void AdministrationForm_FormClosing(object sender, FormClosingEventArgs e) { using (var context = new MarathonContext()) { var homePlayerOneId = Convert.ToInt32(HomePlayerOneComboBox.SelectedValue); var homePlayerTwoId = Convert.ToInt32(HomePlayerTwoComboBox.SelectedValue); var guestPlayerOneId = Convert.ToInt32(GuestPlayerOneComboBox.SelectedValue); var guestPlayerTwoId = Convert.ToInt32(GuestPlayerTwoComboBox.SelectedValue); var settings = context.Settings.Single(); settings.ActualHomePlayerOne = homePlayerOneId; settings.ActualHomePlayerTwo = homePlayerTwoId; settings.ActualGuestPlayerOne = guestPlayerOneId; settings.ActualGuestPlayerTwo = guestPlayerTwoId; context.SaveChanges(); } }
private void StartMarathonButton_Click(object sender, EventArgs e) { if (MessageBox.Show("Jste si jisti, že chcete odstartovat nový maraton?", "Potvrzení", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { using (var context = new MarathonContext()) { var settings = context.Settings.Single(); var now = DateTime.Now; settings.MarathonStart = now; context.SaveChanges(); _marathonStart = now; } StartTimeTextBox.Text = $"{_marathonStart.ToShortDateString()} {_marathonStart.ToLongTimeString()}"; marathonTimer.Start(); } }
private void RefreshResultsForm() { if (CanUseResultsForm()) { using (var context = new MarathonContext()) { var teams = context.Teams.ToList(); var homeTeamName = teams.Where(x => x.IsHome).Select(x => x.Name).Single(); var guestTeamName = teams.Where(x => !x.IsHome).Select(x => x.Name).Single(); if (_resultsForm.LeftSideTeamId == _homeTeamId) { _resultsForm.LeftTeamLabel.Text = homeTeamName; _resultsForm.RightTeamLabel.Text = guestTeamName; _resultsForm.LeftPlayerOneLabel.Text = HomePlayerOneComboBox.Text; _resultsForm.LeftPlayerTwoLabel.Text = HomePlayerTwoComboBox.Text; _resultsForm.RightPlayerOneLabel.Text = GuestPlayerOneComboBox.Text; _resultsForm.RightPlayerTwoLabel.Text = GuestPlayerTwoComboBox.Text; } else { _resultsForm.LeftTeamLabel.Text = guestTeamName; _resultsForm.RightTeamLabel.Text = homeTeamName; _resultsForm.LeftPlayerOneLabel.Text = GuestPlayerOneComboBox.Text; _resultsForm.LeftPlayerTwoLabel.Text = GuestPlayerTwoComboBox.Text; _resultsForm.RightPlayerOneLabel.Text = HomePlayerOneComboBox.Text; _resultsForm.RightPlayerTwoLabel.Text = HomePlayerTwoComboBox.Text; } } RefreshResultsFormScore(); } }
private void SavePlayer(int id, string name, int teamId) { if (!string.IsNullOrEmpty(name)) { using (var context = new MarathonContext()) { if (id == 0) { context.Players.Add(new Player { Name = name, TeamId = teamId }); } else { var player = context.Players.Single(x => x.Id == id); player.Name = name; } context.SaveChanges(); } LoadPlayers(); } }
private void AddPoint(int teamId, int homePlayerOneId, int homePlayerTwoId, int guestPlayerOneId, int guestPlayerTwoId, DateTime currentTime, MarathonContext context) { var lastPoint = context.Points.OrderByDescending(x => x.Created).FirstOrDefault(); var matchId = 1; if (lastPoint != null) { var savedPlayers = new List <int> { lastPoint.HomePlayerOneId, lastPoint.HomePlayerTwoId, lastPoint.GuestPlayerOneId, lastPoint.GuestPlayerTwoId }; var actualPlayers = new List <int> { homePlayerOneId, homePlayerTwoId, guestPlayerOneId, guestPlayerTwoId }; if (savedPlayers.Except(actualPlayers).Any()) { matchId = lastPoint.MatchId + 1; } else { matchId = lastPoint.MatchId; } } context.Points.Add(new Point { Created = currentTime, GainTeamId = teamId, HomePlayerOneId = homePlayerOneId, HomePlayerTwoId = homePlayerTwoId, GuestPlayerOneId = guestPlayerOneId, GuestPlayerTwoId = guestPlayerTwoId, MatchId = matchId }); }