public ValidateGameResultsProcessor(ILogger <ValidateGameResultsProcessor> logger, IUnitOfWork unitOfWork, GameResultsValidator gameResultsValidator, IGameCalculationEngineRepository gameCalculationEngineRepository)
 {
     this._logger                          = logger;
     this._unitOfWork                      = unitOfWork;
     this._gameResultsValidator            = gameResultsValidator;
     this._gameCalculationEngineRepository = gameCalculationEngineRepository;
 }
        public GameResultsValidator(GameResultValidator gameResultValidator)
        {
            this.RuleForEach(x => x).SetValidator(gameResultValidator).DependentRules(() =>
            {
                this.RuleFor(x => x).Custom((data, context) =>
                {
                    var ok = true;
                    var anyDuplicateHomePlayers = data.SelectMany(x => x.HomePlayers.Where(id => id > 0)).GroupBy(g => g).Any(g => g.Count() > 1);
                    var anyDuplicateAwayPlayers = data.SelectMany(x => x.AwayPlayers.Where(id => id > 0)).GroupBy(g => g).Any(g => g.Count() > 1);

                    if (anyDuplicateHomePlayers)
                    {
                        context.AddFailure(new ValidationFailure(context.PropertyName, "A home player appears more than once"));
                        ok = false;
                    }

                    if (anyDuplicateAwayPlayers)
                    {
                        context.AddFailure(new ValidationFailure(context.PropertyName, "A away player appears more than once"));
                        ok = false;
                    }

                    if (ok)
                    {
                        var anyDuplicatePlayer = data.SelectMany(x => x.AllPlayers.Where(id => id > 0)).GroupBy(g => g).Any(g => g.Count() > 1);
                        if (anyDuplicatePlayer)
                        {
                            context.AddFailure(new ValidationFailure(context.PropertyName, "A player appears more than once"));
                        }
                    }
                });

                this.RuleFor(x => x).CustomAsync(async(data, context, cancellationToken) =>
                {
                    Match match = (Match)context.RootContextData["match"];
                    IGameCalculationEngineRepository gameCalculationEngineRepository = (IGameCalculationEngineRepository)context.RootContextData["gameCalculationEngineRepository"];

                    foreach (var gameResult in data)
                    {
                        if (!gameResult.VoidGame)
                        {
                            var matchFormatGameVariation = match.MatchFormat.GetVariationByID(gameResult.MatchFormatXGameVariationID);
                            var calculationEngine        = await gameCalculationEngineRepository.Get((short)matchFormatGameVariation.GameCalculationEngineID);

                            if (gameResult.HomeScore != calculationEngine.WinningScore && gameResult.AwayScore != calculationEngine.WinningScore)
                            {
                                context.AddFailure(new ValidationFailure(context.PropertyName, $"Game #{matchFormatGameVariation.Sequence} must have a winning score of {calculationEngine.WinningScore}."));
                            }
                        }
                    }
                });
            });
        }
Example #3
0
 public ValidateGameResultsProcessor(GameResultsValidator gameResultsValidator, IGameCalculationEngineRepository gameCalculationEngineRepository)
 {
     this._gameResultsValidator            = gameResultsValidator;
     this._gameCalculationEngineRepository = gameCalculationEngineRepository;
 }