Example #1
0
        /// <summary>
        /// Gets the average number of runs conceded per over
        /// </summary>
        /// <returns></returns>
        public decimal?BowlingEconomy(BowlingFigures bowlingFigures)
        {
            if (bowlingFigures is null)
            {
                throw new ArgumentNullException(nameof(bowlingFigures));
            }

            if (bowlingFigures.Overs > 0 && bowlingFigures.RunsConceded.HasValue)
            {
                var oversInDecimal = ((decimal)_oversHelper.OversToBallsBowled(bowlingFigures.Overs.Value)) / StatisticsConstants.BALLS_PER_OVER;
                return(Math.Round((decimal)bowlingFigures.RunsConceded / oversInDecimal, 2));
            }

            else
            {
                return(null);
            }
        }
        private void FindOrCreateInningsRecordForFielder(List <PlayerInMatchStatisticsRecord> records, Match match, TeamInMatch homeTeam, TeamInMatch awayTeam, MatchInnings innings, bool homeTeamIsBatting, PlayerIdentity fielder)
        {
            var record = records.SingleOrDefault(x => x.MatchTeamId == innings.BowlingMatchTeamId && x.PlayerIdentityId == fielder.PlayerIdentityId && x.MatchInningsPair == innings.InningsPair() && (x.PlayerInningsNumber == 1 || x.PlayerInningsNumber == null));

            if (record == null)
            {
                record = CreateRecordForPlayerInInningsPair(match, innings, fielder, homeTeamIsBatting ? awayTeam : homeTeam, homeTeamIsBatting ? homeTeam : awayTeam);
                record.PlayerInningsNumber = null;
                records.Add(record);
            }
            var bowlingFigures = innings.BowlingFigures.SingleOrDefault(x => x.Bowler.PlayerIdentityId == fielder.PlayerIdentityId);
            var oversBowled    = innings.OversBowled.Where(x => x.Bowler.PlayerIdentityId == fielder.PlayerIdentityId);

            record.Catches = innings.PlayerInnings.Count(x => (x.DismissalType == DismissalType.Caught && x.DismissedBy?.PlayerIdentityId == fielder.PlayerIdentityId) ||
                                                         (x.DismissalType == DismissalType.CaughtAndBowled && x.Bowler?.PlayerIdentityId == fielder.PlayerIdentityId));
            record.RunOuts = innings.PlayerInnings.Count(x => x.DismissalType == DismissalType.RunOut && x.DismissedBy?.PlayerIdentityId == fielder.PlayerIdentityId);

            record.BowlingFiguresId            = bowlingFigures?.BowlingFiguresId;
            record.OverNumberOfFirstOverBowled = innings.OversBowled.OrderBy(x => x.OverNumber).FirstOrDefault(x => x.Bowler.PlayerIdentityId == fielder.PlayerIdentityId)?.OverNumber;
            if (oversBowled.Any())
            {
                record.BallsBowled = oversBowled.Where(x => x.BallsBowled.HasValue).Sum(x => x.BallsBowled) + (oversBowled.Count(x => !x.BallsBowled.HasValue) * StatisticsConstants.BALLS_PER_OVER);
                record.NoBalls     = oversBowled.Sum(x => x.NoBalls);
                record.Wides       = oversBowled.Sum(x => x.Wides);
            }
            else
            {
                record.BallsBowled = bowlingFigures?.Overs != null?_oversHelper.OversToBallsBowled(bowlingFigures.Overs.Value) : (int?)null;
            }
            record.Overs              = bowlingFigures?.Overs;
            record.Maidens            = bowlingFigures?.Maidens;
            record.RunsConceded       = bowlingFigures?.RunsConceded;
            record.HasRunsConceded    = bowlingFigures?.RunsConceded != null;
            record.Wickets            = bowlingFigures?.Wickets;
            record.WicketsWithBowling = (bowlingFigures != null && oversBowled.Any()) ? bowlingFigures.Wickets : (int?)null;
        }