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();
            }
        }
Ejemplo n.º 2
0
        private void LoadMatchInfo()
        {
            this.btStartTournament.Visible = _t.TournamentState == TournamentState.NotStarted;
            this.matchUserControl.Visible  = _t.TournamentState != TournamentState.NotStarted;

            var matches = new TourCtrlContext().Match
                          .Include(x => x.Participant1)
                          .Include(x => x.Participant2)
                          .Include(x => x.WinnerParticipant)
                          .Where(x => x.TournamentId == _tournamentId)
                          .AsNoTracking()
                          .ToList();

            matchUserControl.Matches = matches;
        }
        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();
            }
        }
        private void LoadTournaments()
        {
            this.listTournaments.Items.Clear();
            var items = new TourCtrlContext().Tournament.AsNoTracking().Include(x => x.Game).Include(x => x.WinnerParticipant).ToList();

            foreach (var item in items)
            {
                var lvi = new ListViewItem(item.Title);
                lvi.SubItems.Add(item.Game.Name);
                lvi.SubItems.Add(item.GetWinner());

                lvi.Tag = item;

                this.listTournaments.Items.Add(lvi);
            }
        }
        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.º 7
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.º 8
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.º 9
0
        private void btExport_Click(object sender, EventArgs e)
        {
            var matches = new TourCtrlContext().Match.AsNoTracking()
                          .Include(x => x.Participant1)
                          .Include(x => x.Participant2)
                          .Where(x => x.TournamentId == _tournamentId).ToList();

            using (var dialog = new SaveFileDialog())
            {
                dialog.FileName = $"{_t.Title}-{DateTime.Now.Date:yyyyMMdd}.csv";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    using (var streamWriter = new StreamWriter(dialog.FileName))
                    {
                        string Encode(string t)
                        {
                            if (t.Contains(","))
                            {
                                return($"\"{t.Replace("\"", "\"\"")}\"");
                            }

                            return(t);
                        }

                        foreach (var match in matches.OrderBy(x => x.Stage).ThenBy(x => x.Order))
                        {
                            var winner = match.WinnerParticipantId == match.Participant1Id
                                ? match.Participant1
                                : (match.WinnerParticipantId == match.Participant2Id
                                    ? match.Participant2
                                    : null);
                            var looser = match.WinnerParticipantId != match.Participant1Id
                                ? match.Participant1
                                : match.Participant2;
                            streamWriter.WriteLine($"{match.Stage + 1},{match.Order + 1},{Encode(winner?.Name ?? "-")},{Encode(looser?.Name ?? "-")}");
                        }
                    }
                }
            }
        }
Ejemplo n.º 10
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();
        }