Ejemplo n.º 1
0
        private void EditParticipant_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.OK)
            {
                var(valid, message) = IsValid();
                if (!valid)
                {
                    this.lbError.Text = message;
                    e.Cancel          = true;
                    return;
                }

                var ctx = new TourCtrlContext();
                ParticipantInTournament t;
                if (_p != null)
                {
                    t = ctx.ParticipantInTournament.Include(x => x.Participant).First(x => x.Id == _p.Id);
                }
                else
                {
                    t = new ParticipantInTournament
                    {
                        Participant  = new Participant(),
                        TournamentId = _tid
                    };
                    ctx.ParticipantInTournament.Add(t);
                }

                t.Participant.IsTemporary = this.cbTemporary.Checked;
                t.Participant.IsTeam      = this.rbTeam.Checked;
                t.Participant.Name        = this.tbName.Text;

                ctx.SaveChanges();
            }
        }
        public static bool CreateStage(int tournament, int stage)
        {
            var ctx = new TourCtrlContext();

            var participants = stage == 0
                ? ctx.ParticipantInTournament.Include(x => x.Participant).Where(x => x.TournamentId == tournament).Select(x => x.Participant)
                : ctx.Match.Include(x => x.WinnerParticipant).Where(x => x.Stage == stage - 1 && x.TournamentId == tournament).Select(x => x.WinnerParticipant);

            var random = new Random();
            var all    = participants.ToList().OrderBy(x => random.Next()).ToList();

            if (all.Count == 1)
            {
                return(false);
            }

            var newMatches = new List <Match>();
            var i          = 0;

            while (all.Count > 0)
            {
                var nMatch = new Match
                {
                    TournamentId = tournament,
                    Stage        = stage,
                    Order        = i++
                };
                newMatches.Add(nMatch);

                if (all.Count >= 2)
                {
                    nMatch.Participant1Id = all[0].Id;
                    nMatch.Participant2Id = all[1].Id;
                    all.RemoveAt(0);
                    all.RemoveAt(0);
                }
                else
                {
                    nMatch.Participant1Id      = all[0].Id;
                    nMatch.WinnerParticipantId = all[0].Id;
                    all.RemoveAt(0);
                }
            }

            ctx.Match.AddRange(newMatches);
            ctx.SaveChanges();
            return(true);
        }
        private void EditTournament_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.OK)
            {
                var ctx = new TourCtrlContext();
                ctx.Tournament.Add(new Tournament
                {
                    TournamentState = TournamentState.NotStarted,
                    GameId          = (this.cbGame.SelectedItem as Game).Id,
                    Title           = this.tbGame.Text,
                    Size            = (int)this.numSize.Value
                });

                ctx.SaveChanges();
            }
        }
        public static bool StartTournamnet(Tournament t)
        {
            if (t.ParticipantInTournament.Count() != t.Size)
            {
                MessageBox.Show("Need cool size of many");
                return(false);
            }

            var ctx = new TourCtrlContext();

            var loaded = ctx.Tournament.Find(t.Id);

            loaded.TournamentState = TournamentState.Started;

            ctx.SaveChanges();

            return(CreateStage(t.Id, 0));
        }
Ejemplo n.º 5
0
        private void btRemove_Click(object sender, EventArgs e)
        {
            if (this.listParticipants.SelectedItems.Count == 1 && this.listParticipants.SelectedItems[0].Tag is ParticipantInTournament t)
            {
                var ctx  = new TourCtrlContext();
                var item = ctx.ParticipantInTournament.Include(x => x.Participant).First(x => x.Id == t.Id);

                if (item.Participant.IsTemporary)
                {
                    ctx.Participant.Remove(item.Participant);
                }

                ctx.ParticipantInTournament.Remove(item);
                ctx.SaveChanges();

                this.LoadTournament();
            }
        }
Ejemplo n.º 6
0
        private void MakeWin(bool part1)
        {
            var ctx   = new TourCtrlContext();
            var match = ctx.Match.Find(_match.Id);

            match.WinnerParticipantId = part1 ? match.Participant1Id : match.Participant2Id;

            ctx.SaveChanges();

            if (ctx.Match.Where(x => x.TournamentId == match.TournamentId && x.Stage == match.Stage).All(x => x.WinnerParticipantId.HasValue))
            {
                if (StageLogic.CreateStage(match.TournamentId.Value, match.Stage + 1))
                {
                    this.OnNewStageAdded.Invoke(this, new EventArgs());
                }
            }

            _match.WinnerParticipantId = match.WinnerParticipantId;
            RenderMatch();
        }
Ejemplo n.º 7
0
        private void btFillRandom_Click(object sender, EventArgs e)
        {
            var ctx   = new TourCtrlContext();
            var toGen = _t.Size - _t.ParticipantInTournament.Count();

            for (var i = 0; i < toGen; i++)
            {
                ctx.ParticipantInTournament.Add(new ParticipantInTournament
                {
                    TournamentId = this._tournamentId,
                    Participant  = new Participant
                    {
                        IsTeam      = true,
                        IsTemporary = true,
                        Name        = $"Participant #{i + 1}"
                    }
                });
            }

            ctx.SaveChanges();
            this.LoadTournament();
        }