Ejemplo n.º 1
0
        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();
            }
        }
Ejemplo n.º 2
0
        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();
        }
Ejemplo n.º 3
0
        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();
        }
Ejemplo n.º 4
0
        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();
        }
Ejemplo n.º 5
0
        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();
            }
        }
Ejemplo n.º 6
0
        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();
            }
        }
Ejemplo n.º 7
0
        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();
            }
        }