// TODO: works only for existing competition
        public async Task <int> CreateNewCompetitionPhase(int competitionId, int stageId, CompetitionCreationInfoDTO competitionSettings)
        {
            using (var competitorService = new CompetitorService(DbContext))
            {
                var competitionPhase = InsertNewCompetitionPhase(competitionId, stageId, competitionSettings.AdvancedOptions.CompetitionPhaseType);

                var competitorIds     = competitionSettings.Competitors.Select(x => x.Id).ToList();
                var competitorsLookup = competitorService.GetCompetitorsLookup(competitionId);
                var competitors       = new Dictionary <int, Competitor>();

                foreach (var competitor in competitorsLookup)
                {
                    if (competitorIds.Contains(competitor.Key))
                    {
                        competitors.Add(competitor.Key, competitor.Value);
                    }
                }

                var scheduleGenerator = ScheduleGeneratorFactory.Instance.GetScheduleGenerator(competitionSettings.AdvancedOptions.ScheduleType);
                var matchesByGroup    = scheduleGenerator.GenerateSchedule(competitionSettings.CompetitorsAllocation as JArray, competitors, competitionPhase);

                foreach (var matches in matchesByGroup)
                {
                    foreach (var match in matches.Value)
                    {
                        DbContext.Matches.Add(match);
                    }
                }

                await SaveChangesAsync();

                var allCompetitors = new List <Competitor>();
                foreach (var competitor in competitors)
                {
                    allCompetitors.Add(competitor.Value);
                }

                UpdateCompetitionPhaseSettings(competitionPhase, competitionSettings.AdvancedOptions, matchesByGroup, competitionSettings.CompetitorsAllocation as JArray, competitors);
                competitorService.InsertNewCompetitorPhaseInfos(competitionPhase, allCompetitors);
                await SaveChangesAsync();

                return(competitionPhase.Id);
            }
        }
예제 #2
0
        public async Task <int> CreateNewCompetitionWizard(CompetitionCreationInfoDTO competitionSettings)
        {
            using (var competitorService = new CompetitorService(DbContext))
                using (var competitionPhaseService = new CompetitionPhaseService(DbContext))
                {
                    Competition                  competition = new Competition();
                    CompetitionPhase             competitionPhase;
                    Dictionary <int, Competitor> competitors;

                    competition      = CreateNewCompetition(competitionSettings.Options);
                    competitors      = competitorService.InsertNewCompetitors(competition, competitionSettings.Competitors);
                    competitionPhase = competitionPhaseService.InsertNewCompetitionPhase(competition, 1, competitionSettings.AdvancedOptions.CompetitionPhaseType);

                    var scheduleGenerator = ScheduleGeneratorFactory.Instance.GetScheduleGenerator(competitionSettings.AdvancedOptions.ScheduleType);
                    var matchesByGroup    = scheduleGenerator.GenerateSchedule(competitionSettings.CompetitorsAllocation as JArray, competitors, competitionPhase);

                    foreach (var matches in matchesByGroup)
                    {
                        foreach (var match in matches.Value)
                        {
                            DbContext.Matches.Add(match);
                        }
                    }

                    await SaveChangesAsync();

                    var allCompetitors = new List <Competitor>();
                    foreach (var competitor in competitors)
                    {
                        allCompetitors.Add(competitor.Value);
                    }

                    competitionPhaseService.UpdateCompetitionPhaseSettings(competitionPhase, competitionSettings.AdvancedOptions, matchesByGroup, competitionSettings.CompetitorsAllocation as JArray, competitors);
                    competitorService.InsertNewCompetitorPhaseInfos(competitionPhase, allCompetitors);
                    await SaveChangesAsync();

                    return(competition.Id);
                }
        }