Exemplo n.º 1
0
        public ResultsListReport GetReportResultsList(string raceId, string resultsListId, JudgePrincipal principal)
        {
            if (string.IsNullOrEmpty(raceId))
            {
                throw new ArgumentNullException("Missing RaceId");
            }
            if (string.IsNullOrEmpty(resultsListId))
            {
                throw new ArgumentNullException("Missing ResultsListId");
            }
            IRepositorySet repositorySet   = repositorySetProvider.GetRepositorySet(raceId);
            var            isAuthenticated = principal != null;

            var resultsList = repositorySet.ResultsLists.FindResultsList(resultsListId);

            if (resultsList == null)
            {
                throw new ArgumentOutOfRangeException("Wrong ResultsListId");
            }

            var disciplines  = repositorySet.Disciplines.GetDisciplines().ToDictionary(d => d.DisciplineId);
            var columns      = new List <ResultsListColumn>();
            var sortingIndex = -1;

            foreach (var columnSource in resultsList.Columns)
            {
                ResultsListColumn column = new ResultsListColumn(columnSource.Title, columnSource.IsFinal, isAuthenticated);
                foreach (var component in columnSource.Components)
                {
                    Discipline discipline;
                    if (!disciplines.TryGetValue(component.DisciplineId, out discipline))
                    {
                        continue;
                    }
                    IRules rules = rulesProvider.Get(discipline.Rules);
                    column.AddComponent(discipline, rules, component.FinalPointsCoeficient ?? 1.0);
                }
                if (column.IsSortingColumn)
                {
                    sortingIndex = columns.Count;
                }
                columns.Add(column);
            }

            var athletes = repositorySet.Athletes.GetAthletes();
            var entries  = new List <ResultsListReportEntry>();

            foreach (var athlete in athletes)
            {
                if (columns.Any(c => c.IsParticipating(athlete)))
                {
                    entries.Add(new ResultsListReportEntry
                    {
                        Athlete    = ApiAthlete.BuildProfile(athlete),
                        Subresults = columns.Select(c => c.BuildSubresult(athlete)).ToList()
                    });
                }
            }

            if (sortingIndex >= 0)
            {
                entries.Sort(new ResultsListReportEntryComparer(columns[sortingIndex].SortingComparer, sortingIndex));
            }

            return(new ResultsListReport
            {
                Metadata = new ResultsListMetadata
                {
                    Title = resultsList.Title,
                    ResultsListId = resultsList.ResultsListId,
                    Columns = columns.Select(c => c.Metadata).ToList()
                },
                Results = entries
            });
        }
Exemplo n.º 2
0
        public StartingListReport GetReportStartingList(string raceId, string startingLaneId, JudgePrincipal principal)
        {
            if (string.IsNullOrEmpty(raceId))
            {
                throw new ArgumentNullException("Missing RaceId");
            }
            IRepositorySet repositorySet = repositorySetProvider.GetRepositorySet(raceId);

            var rootStartingLanes    = repositorySet.StartingLanes.GetStartingLanes();
            var titleSource          = flattener.GetParent(rootStartingLanes, startingLaneId);
            var allowedStartingLanes = flattener.GetLeaves(rootStartingLanes, startingLaneId).ToDictionary(s => s.StartingLaneId);
            var athletes             = repositorySet.Athletes.GetAthletes().ToDictionary(a => a.AthleteId);
            var disciplines          = repositorySet.Disciplines.GetDisciplines().ToDictionary(d => d.DisciplineId);
            var startingList         = repositorySet.StartingList.GetStartingList();
            var judges          = repositorySet.Judges.GetJudges().ToDictionary(j => j.JudgeId);
            var isAuthenticated = principal != null;

            var report = new StartingListReport();

            report.Title   = BuildStartingListTitle(titleSource);
            report.Entries = new List <StartingListReportEntry>();

            foreach (var startingListEntry in startingList)
            {
                Athlete          athlete;
                Discipline       discipline;
                StartingLaneFlat startingLane;
                Judge            judge = null;
                if (!athletes.TryGetValue(startingListEntry.AthleteId, out athlete))
                {
                    continue;                                                                       // athlete not found, give up
                }
                if (!disciplines.TryGetValue(startingListEntry.DisciplineId, out discipline))
                {
                    continue;                                                                               // discipline does not exist, give up
                }
                if (!allowedStartingLanes.TryGetValue(startingListEntry.StartingLaneId, out startingLane))
                {
                    continue;                                                                                               // not showing this lane, skip
                }
                Announcement announcement = athlete.Announcements.FirstOrDefault(a => a.DisciplineId == startingListEntry.DisciplineId);
                ActualResult latestResult = athlete.ActualResults.LastOrDefault(r => r.DisciplineId == startingListEntry.DisciplineId);
                if (latestResult != null)
                {
                    judges.TryGetValue(latestResult.JudgeId, out judge);
                }

                var allowAnnouncement = isAuthenticated || discipline.AnnouncementsPublic;
                var allowResult       = isAuthenticated || discipline.ResultsPublic;

                StartingListReportEntry entry = new StartingListReportEntry();
                entry.Announcement  = allowAnnouncement ? BuildReportAnnouncement(announcement) : null;
                entry.Athlete       = ApiAthlete.BuildProfile(athlete);
                entry.CurrentResult = allowResult ? BuildReportActualResult(latestResult, judge) : null;
                entry.Discipline    = BuildReportDiscipline(discipline);
                entry.Start         = BuildReportStart(startingListEntry, startingLane);
                report.Entries.Add(entry);
            }

            report.Entries.Sort(CompareStartingListEntries);

            return(report);
        }
Exemplo n.º 3
0
        public ResultsListReport GetReportDisciplineResults(string raceId, string disciplineId, JudgePrincipal principal)
        {
            if (string.IsNullOrEmpty(raceId))
            {
                throw new ArgumentNullException("Missing RaceId");
            }
            if (string.IsNullOrEmpty(disciplineId))
            {
                throw new ArgumentNullException("Missing DisciplineId");
            }
            IRepositorySet repositorySet = repositorySetProvider.GetRepositorySet(raceId);

            var discipline = repositorySet.Disciplines.FindDiscipline(disciplineId);

            if (discipline == null)
            {
                throw new ArgumentOutOfRangeException("Unknown DisciplineId " + disciplineId);
            }

            var rules = rulesProvider.Get(discipline.Rules);

            if (rules == null)
            {
                throw new ArgumentOutOfRangeException("Unknown DisciplineRules " + discipline.Rules);
            }

            var athletes = repositorySet.Athletes.GetAthletes();
            var judges   = repositorySet.Judges.GetJudges();

            var allowAnnouncement = principal != null || discipline.AnnouncementsPublic;
            var allowResult       = principal != null || discipline.ResultsPublic;

            var entries = new List <ResultsListReportEntry>();

            foreach (var athlete in athletes)
            {
                var announcement = athlete.Announcements.FirstOrDefault(a => a.DisciplineId == disciplineId);
                var actualResult = athlete.ActualResults.LastOrDefault(r => r.DisciplineId == disciplineId);
                if (announcement == null)
                {
                    continue;                           // does not compete in this discipline, skip
                }
                var judge = actualResult == null ? null : judges.FirstOrDefault(j => j.JudgeId == actualResult.JudgeId);

                var entry = new ResultsListReportEntry();
                entry.Athlete    = ApiAthlete.BuildProfile(athlete);
                entry.Subresults = new List <ResultsListReportEntrySubresult>();
                var subresult = new ResultsListReportEntrySubresult();
                if (allowAnnouncement)
                {
                    subresult.Announcement = BuildReportAnnouncement(announcement);
                }
                if (allowResult)
                {
                    subresult.CurrentResult = BuildReportActualResult(actualResult, judge);
                    subresult.FinalPoints   = subresult.CurrentResult?.FinalPerformance?.Points;
                }
                entry.Subresults.Add(subresult);

                entries.Add(entry);
            }

            entries.Sort(new ResultsListReportEntryComparer(rules.ResultsComparer, 0));

            return(new ResultsListReport
            {
                Metadata = BuildDisciplineReportMetadata(discipline, rules),
                Results = entries
            });
        }