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();
            }
        }
        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 });
        }