Exemple #1
0
 public List <PhaseCompetitorInfos> GetCompetitorPhaseInfos(int competitionPhaseInfo)
 {
     using (var competitionPhaseService = new CompetitionPhaseService(DbContext))
     {
         //var competitionPhaseSettings = competitionPhaseService.GetCompetitionPhaseInfoSettings(competitionPhaseInfo);
         return(DbContext.CompetitorPhaseInfoes.Include("Competitor").Where(x => x.IdCompetitionPhase == competitionPhaseInfo)
                .Select(x => new PhaseCompetitorInfos()
         {
             CompetitorId = x.IdCompetitor, PhaseInfoJSON = x.PhaseInfo, CompetitionInfoJSON = x.Competitor.CompetitionInfo
         }).ToList());
     }
 }
        private IXLWorksheet exportCompetitorsByGroup(XLWorkbook workBook, int competitionId)
        {
            var worksheet = workBook.Worksheets.Add("Raspored Igrača");

            using (var competitorsService = new CompetitorService())
                using (var competitionPhaseService = new CompetitionPhaseService(competitorsService.DbContext))
                {
                    var categoryId   = DbContext.Competitions.Where(x => x.Id == competitionId).Select(x => x.IdCategory).First();
                    var competitions = DbContext.Competitions.Include("CompetitionPhases").Where(x => x.IdCategory == categoryId).ToList();

                    var startRow = 2;

                    foreach (var competition in competitions)
                    {
                        worksheet.Cell(startRow, 2).Value = competition.DisplayName;
                        worksheet.Range(startRow, 2, startRow, 6).Merge().AddToNamed("Titles");
                        startRow++;

                        var competitors = competitorsService.GetCompetitors(competition.Id);
                        var phase       = competition.CompetitionPhases.FirstOrDefault();

                        if (phase == null)
                        {
                            startRow += 2;
                            continue;
                        }

                        var competitionPhaseSettings = competitionPhaseService.GetCompetitionPhaseInfoSettings(phase.Id) as GroupPhaseSettings;
                        var rows = new List <string[]>();
                        foreach (var groupedCompetitors in competitionPhaseSettings.CompetitorIds)
                        {
                            var groupRow = new List <string>();
                            groupRow.Add($"Grupa    {groupedCompetitors.Key + 1}");
                            foreach (var competitorId in groupedCompetitors.Value)
                            {
                                var competitor = competitors.First(x => x.Id == competitorId);
                                groupRow.Add($"{competitor.Name}        {competitor.Team} {competitor.Ranking} ");
                            }

                            rows.Add(groupRow.ToArray());
                        }

                        worksheet.Cell(startRow, 2).InsertData(rows);
                        startRow += rows.Count + 2;
                    }
                }

            worksheet.Columns().AdjustToContents();
            return(worksheet);
        }
Exemple #3
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);
                }
        }
        public void ExportTablesAndSchedules(int competitionId, string fileName)
        {
            // TODO for testing
            // string path = $"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\Dokumentacija_{DateTime.Now.ToShortTimeString()}.xlsx";
            string path     = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), $"Tablice_{DateTime.Now.ToFileTime()}.xlsx");
            var    workbook = new XLWorkbook();

            using (var competitorService = new CompetitorService())
                using (var competitionPhaseService = new CompetitionPhaseService(competitorService.DbContext))
                {
                    var categoryId   = DbContext.Competitions.Where(x => x.Id == competitionId).Select(x => x.IdCategory).First();
                    var competitions = DbContext.Competitions.Include("CompetitionPhases").Where(x => x.IdCategory == categoryId).ToList();

                    foreach (var competition in competitions)
                    {
                        var worksheet = workbook.Worksheets.Add(competition.DisplayName.Substring(0, 15));
                        setColumnWidths(worksheet);
                        var startRow = 2;

                        var phase = competitionPhaseService.GetCompetitionPhaseInfos(competition.Id).FirstOrDefault();

                        if (phase == null)
                        {
                            continue;
                        }

                        var phaseId       = phase.CompetitionPhaseId;
                        var phaseSettings = phase.Settings as GroupPhaseSettings;
                        var matchesDb     = competitionPhaseService.DbContext.Matches.Where(x => x.IdCompetitionPhase == phaseId).ToList();

                        var tableTennisTournament = new TableTennisTournament();
                        var matches     = tableTennisTournament.GenerateMatchesViewModel(matchesDb, phaseSettings).OrderBy(x => x.Leg).ThenBy(x => x.MatchId).ToList();
                        var competitors = tableTennisTournament.GeneratePlayersViewModel(competitorService.GetCompetitorPhaseInfos(phaseId));

                        int numberOfGroupsOnPage = 0;

                        foreach (var competitorsByGroup in phaseSettings.CompetitorIds)
                        {
                            if (numberOfGroupsOnPage >= 3)
                            {
                                numberOfGroupsOnPage = 0;
                                worksheet.PageSetup.AddHorizontalPageBreak(startRow);
                                startRow++;
                            }

                            generateGroupHeaderRow(worksheet, startRow, competitorsByGroup.Key, competitorsByGroup.Value.Count());
                            startRow += 2;

                            startRow = insertGroupTable(worksheet, startRow, matches, competitors, competitorsByGroup);

                            for (int setCells = 9; setCells < 14; setCells++)
                            {
                                addValueToCell(worksheet, $"{setCells - 8}. set", startRow, setCells, null, true);
                            }

                            addValueToCell(worksheet, $"rez.", startRow, 14, null, true);
                            startRow++;

                            int lastLeg = 1;
                            foreach (var match in matches.Where(x => x.GroupIndex == competitorsByGroup.Key))
                            {
                                if (lastLeg != match.Leg)
                                {
                                    lastLeg = match.Leg;
                                    startRow++;
                                }

                                var startCell = 1;
                                var comp1     = competitors.First(x => x.CompetitorId == match.CompetitorId1).DisplayName;
                                var comp2     = competitors.First(x => x.CompetitorId == match.CompetitorId2).DisplayName;

                                addValueToCell(worksheet, comp1, startRow, startCell, alignement: XLAlignmentHorizontalValues.Left);
                                worksheet.Range(startRow, startCell, startRow, startCell + 2).Merge();
                                worksheet.Range(startRow, startCell, startRow, startCell + 2).Style.Border.BottomBorder = XLBorderStyleValues.Medium;
                                startCell += 3;

                                addValueToCell(worksheet, "vs.", startRow, startCell++);

                                addValueToCell(worksheet, comp2, startRow, startCell, alignement: XLAlignmentHorizontalValues.Left);
                                worksheet.Range(startRow, startCell, startRow, startCell + 3).Merge();
                                worksheet.Range(startRow, startCell, startRow, startCell + 3).Style.Border.BottomBorder = XLBorderStyleValues.Medium;
                                startCell += 4;

                                for (int setIndex = 0; setIndex < match.Sets1.Count; setIndex++)
                                {
                                    var set1 = match.Sets1[setIndex];
                                    var set2 = match.Sets2[setIndex];

                                    if (set1 != null)
                                    {
                                        addValueToCell(worksheet, $"'{set1}:{set2}", startRow, startCell);
                                    }

                                    worksheet.Cell(startRow, startCell).Style.Border.OutsideBorder = XLBorderStyleValues.Medium;
                                    startCell++;
                                }

                                addValueToCell(worksheet, $"'{match.Result}", startRow, startCell);
                                worksheet.Cell(startRow, startCell).Style.Border.OutsideBorder = XLBorderStyleValues.Thick;

                                startRow++;
                            }

                            numberOfGroupsOnPage++;

                            worksheet.Range(startRow, 1, startRow, 14).Merge();
                            worksheet.Range(startRow, 1, startRow, 14).Style.Border.BottomBorder = XLBorderStyleValues.Medium;
                            startRow++;
                        }
                    }
                }


            workbook.SaveAs(path);
        }