/// <summary>
        ///
        /// </summary>
        private void UpdatePlayerHandicaps(teammatchup tm)
        {
            var playerIds = new List <int>();

            foreach (var match in tm.matches)
            {
                playerIds.AddRange(match.players.Select(x => x.id));
            }

            var resultsForTeamMatchup = this.database.results.Where(x => playerIds.Contains(x.playerId) && x.year.value == tm.week.year.value).ToList();
            var resultsLookup         = resultsForTeamMatchup.ToLookup(x => x.playerId);

            var scoreResultFactory = new ScoreResultFactory(tm.week.year.value);
            var hc = new HandicapCalculator(scoreResultFactory);

            foreach (var match in tm.matches)
            {
                // Only compute handicaps for valid matches.
                if (!match.IsComplete())
                {
                    continue;
                }

                foreach (var player in match.players)
                {
                    if (!player.validPlayer)
                    {
                        continue;
                    }

                    var pyd     = player.playeryeardatas.FirstOrDefault(x => x.year.value == tm.week.year.value);
                    var results = resultsLookup[player.id].Where(x => x.IsComplete()).OrderBy(x => x.match.teammatchup.week.date);

                    if (results.Count() == 0)
                    {
                        continue;
                    }

                    // This is done to ensure that prior handicap is correct.
                    var mostRecentHandicap = hc.CalculateAndCascadeHandicaps(results, pyd.week0Score, pyd.isRookie);

                    player.currentHandicap = mostRecentHandicap.Handicap;
                    pyd.finishingHandicap  = mostRecentHandicap.Handicap;
                }
            }
        }
Exemplo n.º 2
0
 public HandicapCalculator(ScoreResultFactory scoreResultFactory)
 {
     this.scoreResultFactory = scoreResultFactory;
 }