public SingleMatchViewModel(SingleTournament tournament)
 {
     //Winner = true;
     Tournament = tournament;
     TournamentId = tournament.Id;
     BaseTournamentId = tournament.GetBaseTournamentId();
 }
        protected void UpdatePlacesFromConsolationTournament(SingleTournament tournament, SingleTournament baseTournament)
        {
            foreach (var t in tournament.ConsolationTournaments)
            {
                var final = t.GetMatchesByStage(Stage.Final)[0];

                //DataAccess.Entry(final).Reference(f => f.Player1).Load();
                //DataAccess.Entry(final).Reference(f => f.Player2).Load();
                //DataAccess.Entry(final).Reference(f => f.Winner).Load();

                if (final.Winner != null)
                {
                    baseTournament.Places.Add(new SinglePlace()
                    {
                        Player = final.Winner,
                        Position = t.WinnerPlace,
                        Tournament = baseTournament
                    });
                }
                if (final.Looser != null)
                {
                    baseTournament.Places.Add(new SinglePlace()
                    {
                        Player = final.Looser,
                        Position = t.WinnerPlace + 1,
                        Tournament = baseTournament
                    });
                }

                UpdatePlacesFromConsolationTournament(t, baseTournament);
            }
        }
        protected void CreateConsolationTournaments(SingleTournament tournament)
        {
            var stage = tournament.FirstStage.GetNext();
            while (stage != Stage.None)
            {
                var newTournament = new SingleTournament()
                {
                    NumberOfPlayers = stage.GetNumberOfParticipants(),
                    Name = stage.GetPrev().GetDisplayName()
                };

                switch (stage)
                {
                    case Stage.Final:
                        newTournament.WinnerPlace = tournament.WinnerPlace + 2;
                        break;
                    case Stage.SemiFinal:
                        newTournament.WinnerPlace = tournament.WinnerPlace + 4;
                        break;
                    case Stage.QuarterFinal:
                        newTournament.WinnerPlace = tournament.WinnerPlace + 8;
                        break;
                    case Stage.OneEighthFinals:
                        newTournament.WinnerPlace = tournament.WinnerPlace + 16;
                        break;
                    case Stage.OneSixteenthFinals:
                        newTournament.WinnerPlace = tournament.WinnerPlace + 32;
                        break;
                }

                //newTournament.WinnerPlace = tournament.WinnerPlace + stage.GetNumberOfParticipants();

                newTournament.BaseTournament = tournament;
                DataAccess.SingleTournaments.Add(newTournament);
                DataAccess.SaveChanges();

                newTournament.CreateMatches();
                DataAccess.SaveChanges();

                CreateConsolationTournaments(newTournament);

                stage = stage.GetNext();
            }
        }
 protected void DeleteConsolationTournaments(SingleTournament tournament)
 {
     var tournaments = DataAccess.SingleTournaments.Where(t => t.BaseTournament.Id == tournament.Id).ToList();
     foreach (var tourn in tournaments)
     {
         DataAccess.SingleTournaments.Remove(tourn);
         DeleteConsolationTournaments(tourn);
     }
 }
        public ActionResult Create(TournamentViewModel model, int? page, int? count)
        {
            ViewBag.Page = page = page.HasValue && page > 1 ? page : 1;
            ViewBag.Count = count = count.HasValue && count > 1 ? count : __default_count;

            if (model.Document != null && !DocumentHelper.ValidExtension(model.Document.FileName))
            {
                ModelState.AddModelError("Document", "Необходимо загружать документы формата doc (MS Word) или pdf.");
            }

            if (!ModelState.IsValid)
            {
                model.Categories = InitCategories();
                return View(model);
            }

            var tournament = new SingleTournament()
            {
                Name = model.Name,
                StartDate = model.StartDate,
                EndDate = model.EndDate,
                NumberOfPlayers = model.NumberOfPlayers,
                EndOfRegistration = model.EndOfRegistration,
                Type = model.Type
            };

            var category = DataAccess.Categories.Find(model.CategoryId);
            if (category != null)
            {
                tournament.Category = category;
            }

            if (model.Document != null)
            {
                var helper = new DocumentHelper(DocumentStoragePath);
                if (!string.IsNullOrEmpty(tournament.DocumentName))
                {
                    helper.Delete(tournament.DocumentName);
                }
                tournament.DocumentName = helper.Upload(model.Document);
            }

            tournament.CreateMatches();

            DataAccess.SingleTournaments.Add(tournament);
            DataAccess.SaveChanges();

            CreateConsolationTournaments(tournament);

            return RedirectToAction("index", new { page = page, count = count });
        }