Exemplo n.º 1
0
 private static Penalization ExtractPenalization(PenalizationDto dto)
 {
     return(new Penalization
     {
         IsShortPerformance = dto.IsShortPerformance,
         PenalizationId = dto.PenalizationId,
         Performance = ExtractPerformance(dto.Performance),
         Reason = dto.Reason,
         RuleInput = dto.RuleInput,
         ShortReason = dto.ShortReason,
         CardResult = CardResult.Parse(dto.CardResult)
     });
 }
Exemplo n.º 2
0
        public void PostAthleteResult(string raceId, string athleteId, Judge authenticatedJudge, ActualResultDto incomingResult)
        {
            if (string.IsNullOrEmpty(raceId))
            {
                throw new ArgumentNullException("Missing RaceId");
            }
            if (string.IsNullOrEmpty(athleteId))
            {
                throw new ArgumentNullException("Missing AthleteId");
            }
            if (authenticatedJudge == null)
            {
                throw new ArgumentNullException("Missing AuthenticationToken");
            }
            if (string.IsNullOrEmpty(incomingResult.DisciplineId))
            {
                throw new ArgumentNullException("Missing DisciplineId");
            }

            var repositorySet = repositorySetProvider.GetRepositorySet(raceId);
            var athlete       = repositorySet.Athletes.FindAthlete(athleteId);

            if (athlete == null)
            {
                throw new ArgumentOutOfRangeException("Unknown AthleteId " + athleteId);
            }
            var discipline = repositorySet.Disciplines.FindDiscipline(incomingResult.DisciplineId);

            if (discipline == null)
            {
                throw new ArgumentOutOfRangeException("Wrong DisciplineId " + incomingResult.DisciplineId);
            }
            var rules = rulesRepository.Get(discipline.Rules);

            if (discipline.ResultsClosed)
            {
                throw new ArgumentOutOfRangeException("Discipline " + incomingResult.DisciplineId + " already closed results");
            }

            ActualResult finalResult = new ActualResult();

            finalResult.DisciplineId  = discipline.DisciplineId;
            finalResult.JudgeId       = authenticatedJudge.JudgeId;
            finalResult.Penalizations = new List <Penalization>();
            finalResult.CardResult    = CardResult.Parse(incomingResult.CardResult);
            finalResult.AddedDate     = DateTimeOffset.UtcNow;

            if (incomingResult.JudgeOverride)
            {
                finalResult.Performance      = ExtractPerformance(incomingResult.Performance);
                finalResult.FinalPerformance = ExtractPerformance(incomingResult.FinalPerformance);
                finalResult.JudgeComment     = incomingResult.JudgeComment;
            }
            else
            {
                var announcement = athlete.Announcements.FirstOrDefault(a => a.DisciplineId == incomingResult.DisciplineId);
                if (announcement == null)
                {
                    throw new ArgumentOutOfRangeException("No announcement for " + incomingResult.DisciplineId);
                }

                foreach (var incomingPenalization in incomingResult.Penalizations)
                {
                    if (incomingPenalization.IsShortPerformance)
                    {
                        continue;                                                                                       // we will calculate this ourselves
                    }
                    if (incomingPenalization.PenalizationId == null || incomingPenalization.PenalizationId == "Custom") // custom penalization
                    {
                        VerifyResult(rules.HasDepth, false, incomingPenalization.Performance.Depth, "Penalization.Depth");
                        VerifyResult(rules.HasDuration, false, incomingPenalization.Performance.DurationSeconds(), "Penalization.Duration");
                        VerifyResult(rules.HasDistance, false, incomingPenalization.Performance.Distance, "Penalization.Distance");
                        VerifyResult(rules.HasPoints, false, incomingPenalization.Performance.Points, "Penalization.Points");
                        finalResult.Penalizations.Add(ExtractPenalization(incomingPenalization));
                    }
                    else
                    {
                        var rulesPenalization = rules.Penalizations.FirstOrDefault(p => p.Id == incomingPenalization.PenalizationId);
                        if (rulesPenalization == null)
                        {
                            throw new ArgumentOutOfRangeException("Unknown Penalization.Id " + incomingPenalization.PenalizationId);
                        }
                        var finalPenalization = rulesPenalization.BuildPenalization(incomingPenalization.RuleInput ?? 0, finalResult.Performance);
                        if (finalPenalization != null)
                        {
                            finalResult.Penalizations.Add(finalPenalization);
                            finalResult.CardResult = CombineCards(finalResult.CardResult, finalPenalization.CardResult);
                        }
                    }
                }

                bool didFinish = finalResult.CardResult == CardResult.White || finalResult.CardResult == CardResult.Yellow;
                VerifyResult(rules.HasDepth, rules.HasDepth && didFinish, incomingResult.Performance.Depth, "Performance.Depth");
                VerifyResult(rules.HasDuration, rules.HasDuration && didFinish, incomingResult.Performance.DurationSeconds(), "Performance.Duration");
                VerifyResult(rules.HasDistance, rules.HasDistance && didFinish, incomingResult.Performance.Distance, "Performance.Distance");
                finalResult.Performance = ExtractPerformance(incomingResult.Performance);
                if (!rules.HasPoints)
                {
                    finalResult.Performance.Points = null;
                }
                else
                {
                    finalResult.Performance.Points = rules.GetPoints(incomingResult.Performance);
                }

                bool skipShortCalculation = finalResult.CardResult == CardResult.Red || finalResult.CardResult == CardResult.DidNotStart;
                if (!skipShortCalculation)
                {
                    var shortPenalization = rules.BuildShortPenalization(announcement.Performance, finalResult.Performance);
                    if (shortPenalization != null)
                    {
                        finalResult.Penalizations.Insert(0, shortPenalization);
                        finalResult.CardResult = CombineCards(finalResult.CardResult, CardResult.Yellow);
                    }
                }

                finalResult.FinalPerformance = new Performance();
                CalculateFinalPerformance(finalResult, PerformanceComponent.Duration, rules.PenalizationsTarget);
                CalculateFinalPerformance(finalResult, PerformanceComponent.Depth, rules.PenalizationsTarget);
                CalculateFinalPerformance(finalResult, PerformanceComponent.Distance, rules.PenalizationsTarget);
                CalculateFinalPerformance(finalResult, PerformanceComponent.Points, rules.PenalizationsTarget);
            }

            athlete.ActualResults.Add(finalResult);
            repositorySet.Athletes.SaveAthlete(athlete);
        }
Exemplo n.º 3
0
 public virtual void RedPenalizationListed(string id, string reason, string shortReason, string cardResult)
 {
     RedPenalizationListed(id, reason, shortReason, CardResult.Parse(cardResult));
 }