public void Post(CreateNewRoundRequest request)
        {
            var currentFinalDayCompetition = Db.SingleById <FinalDayCompetition>(request.FinalDayCompetitionId);
            var competitionMode            =
                CompetitionModeFactory.GetCompetitionMode(Db, currentFinalDayCompetition.CompetitionMode);

            var generatedMatches = competitionMode.GenerateMatches(request.FinalDayCompetitionId);

            Db.SaveAll(generatedMatches);
        }
        private void CreateFinalDayMatches(int finalDayCompetitionId)
        {
            var currentFinalDayCompetition = Db.SingleById <FinalDayCompetition>(finalDayCompetitionId);
            var competitionMode            =
                CompetitionModeFactory.GetCompetitionMode(Db, currentFinalDayCompetition.CompetitionMode);

            var generatedMatches = competitionMode.GenerateMatches(finalDayCompetitionId);

            Db.SaveAll(generatedMatches);

            competitionMode.AfterMatchSafe(generatedMatches, finalDayCompetitionId);
        }
        private FinalDayCompetitionProgressDto GetProgressForCompetition(FinalDayCompetition finalDayCompetition)
        {
            var competitionMode = CompetitionModeFactory.GetCompetitionMode(Db, finalDayCompetition.CompetitionMode);

            var progress = new FinalDayCompetitionProgressDto
            {
                CompetitionState = finalDayCompetition.State,
                CompetitionName  = finalDayCompetition.Name,
                CompetitionId    = finalDayCompetition.Id,
                CompetitionMode  = finalDayCompetition.CompetitionMode,
                MatchesRunning   = Db.Count(Db.From <MatchView>().Where(x =>
                                                                        x.FinalDayCompetitionId == finalDayCompetition.Id && x.PlayDate != null && x.ResultDate == null)),
            };

            var finishedMatches = Db.Select(Db.From <MatchView>().Where(x =>
                                                                        x.FinalDayCompetitionId == finalDayCompetition.Id &&
                                                                        (x.PlayDate != null && x.ResultDate != null || x.IsFreeTicket)));

            progress.MatchesPlayed = finishedMatches.Count;
            progress.MatchesOpen   = competitionMode.GetNumberOfMatches(finalDayCompetition.Id) - progress.MatchesPlayed -
                                     progress.MatchesRunning;

            var playedMatches = finishedMatches.Where(x => !x.IsFreeTicket).ToList();

            if (playedMatches.Any())
            {
                progress.AverageMatchDuration = (int)TimeSpan
                                                .FromSeconds(playedMatches.Average(x => (x.ResultDate - x.PlayDate).Value.TotalSeconds))
                                                .TotalMinutes;

                var numberOfTables = GetNumberOfTablesForFinalDayCompetition(finalDayCompetition.Id);
                if (numberOfTables != 0)
                {
                    double totalDurationInMinutes =
                        progress.AverageMatchDuration * progress.MatchesOpen / numberOfTables;
                    var competitionDuration = TimeSpan.FromMinutes(totalDurationInMinutes);

                    progress.ExpectedEnd = DateTime.Now + competitionDuration;
                }
            }

            return(progress);
        }