/// <summary>
        /// Adds points etc to a new result
        /// </summary>
        /// <param name="newResult"></param>
        /// <param name="homeStats"></param>
        /// <param name="awayStats"></param>
        /// <returns></returns>
        private void SetNewResult(DataRepository.DataEntities.Match newResult, TeamSeasonStats homeStats, TeamSeasonStats awayStats)
        {
            var newMatchWinner = newResult.HomeScore > newResult.AwayScore ? WhoWon.Home :
                                 newResult.AwayScore > newResult.HomeScore ? WhoWon.Away : WhoWon.Draw;

            homeStats.G++;
            awayStats.G++;
            homeStats.GF += newResult.HomeScore.GetValueOrDefault();
            homeStats.GA += newResult.AwayScore.GetValueOrDefault();
            awayStats.GF += newResult.AwayScore.GetValueOrDefault();
            awayStats.GA += newResult.HomeScore.GetValueOrDefault();

            switch (newMatchWinner)
            {
            case WhoWon.Home:
                homeStats.W++;
                awayStats.L++;
                break;

            case WhoWon.Away:
                homeStats.L++;
                awayStats.W++;
                break;

            case WhoWon.Draw:
                homeStats.D++;
                awayStats.D++;
                break;
            }
        }
        /// <summary>
        /// Remove points etc from a result
        /// </summary>
        /// <param name="oldResult"></param>
        /// <param name="homeStats"></param>
        /// <param name="awayStats"></param>
        /// <returns></returns>
        private void RemoveOldResult(DataRepository.DataEntities.Match oldResult, TeamSeasonStats homeStats, TeamSeasonStats awayStats)
        {
            var matchWinner = oldResult.HomeScore > oldResult.AwayScore ? WhoWon.Home :
                              oldResult.AwayScore > oldResult.HomeScore ? WhoWon.Away : WhoWon.Draw;

            homeStats.G--;
            awayStats.G--;
            homeStats.GF -= oldResult.HomeScore.GetValueOrDefault();
            homeStats.GA -= oldResult.AwayScore.GetValueOrDefault();
            awayStats.GF -= oldResult.AwayScore.GetValueOrDefault();
            awayStats.GA -= oldResult.HomeScore.GetValueOrDefault();

            switch (matchWinner)
            {
            case WhoWon.Home:
                homeStats.W--;
                awayStats.L--;
                break;

            case WhoWon.Away:
                homeStats.L--;
                awayStats.W--;
                break;

            case WhoWon.Draw:
                homeStats.D--;
                awayStats.D--;
                break;
            }
        }
Exemplo n.º 3
0
 public Task UpdateTeamStatsAsync(int teamId, short year, byte season, TeamSeasonStats stats)
 {
     return(_statsRepository.UpdateAsync(_mapper.Map <DataRepository.DataEntities.TeamSeasonStats>(stats, opt =>
     {
         opt.Items["year"] = year;
         opt.Items["season"] = season;
     })));
 }
Exemplo n.º 4
0
        internal async Task <Team> SetTeamStats(string uncheckedTeamCode, string seasonCode, int weekNumber, TeamStatsUpdate teamStatsUpdate)
        {
            if (teamStatsUpdate == null)
            {
                throw new ArgumentNullException("teamStatsUpdate");
            }

            using (var dbSession = _documentStore.LightweightSession())
            {
                var teamData = await this.GetTeam(dbSession, uncheckedTeamCode);

                // does season exist, if not add
                if (teamData.Seasons == null)
                {
                    teamData.Seasons = new List <TeamSeasonStats>();
                }

                TeamSeasonStats teamSeasonStats = teamData.Seasons.SingleOrDefault(s => s.SeasonCodeRef == seasonCode);
                if (teamSeasonStats == null)
                {
                    teamSeasonStats = new TeamSeasonStats();
                    teamSeasonStats.SeasonCodeRef = seasonCode;
                    teamData.Seasons.Add(teamSeasonStats);
                }

                if (teamSeasonStats.WeekStats == null)
                {
                    teamSeasonStats.WeekStats = new List <TeamWeekStats>();
                }

                TeamWeekStats teamWeekStats = teamSeasonStats.WeekStats.SingleOrDefault(w => w.WeekNumberRef == weekNumber);
                if (teamWeekStats == null)
                {
                    teamWeekStats = new TeamWeekStats();
                    teamWeekStats.WeekNumberRef = weekNumber;
                    teamSeasonStats.WeekStats.Add(teamWeekStats);
                }

                teamWeekStats.FbsRank = teamStatsUpdate.FbsRank;
                teamWeekStats.Losses  = teamStatsUpdate.Losses;
                teamWeekStats.Wins    = teamStatsUpdate.Wins;

                dbSession.Store(teamData);
                dbSession.SaveChanges();

                var pickemEvent = new PickemSystemEvent(PickemSystemEventTypes.TeamStatsChanged, seasonCode, weekNumber);
                pickemEvent.DynamicKeys.teamCode = teamData.TeamCode;
                _pickemEventer.Emit(pickemEvent);

                return(MapTeamData(teamData));
            }
        }
        /// <summary>
        /// Validate New and Old are valid
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="oldResult"></param>
        /// <param name="newResult"></param>
        /// <param name="homeStats"></param>
        /// <param name="awayStats"></param>
        /// <returns></returns>
        private async Task <bool> ValidateMatchAsync(ILambdaLogger logger, DataRepository.DataEntities.Match oldResult,
                                                     DataRepository.DataEntities.Match newResult, TeamSeasonStats homeStats, TeamSeasonStats awayStats)
        {
            if (oldResult.Year != newResult.Year || oldResult.Season != newResult.Season ||
                oldResult.Round != newResult.Round)
            {
                logger.LogLine($"Match is invalid {newResult.Year}#{newResult.Season}#{newResult.Round}#{newResult.MatchId}.");
                return(false);
            }

            if ((_year.HasValue && _season.HasValue) && (newResult.Year != _year.Value || newResult.Season != _season.Value))
            {
                logger.LogLine($"Match has invalid year or season {newResult.Year}#{newResult.Season}#{newResult.Round}#{newResult.MatchId}.");
                return(false);
            }

            if (oldResult.MatchId != newResult.MatchId || oldResult.HomeTeamId != newResult.HomeTeamId ||
                oldResult.AwayTeamId != newResult.AwayTeamId)
            {
                logger.LogLine($"Match is invalid, teams don't match {newResult.Year}#{newResult.Season}#{newResult.Round}#{newResult.MatchId}.");
                return(false);
            }

            if (newResult.WasPlayed && (!newResult.HomeScore.HasValue || !newResult.AwayScore.HasValue))
            {
                logger.LogLine($"Match is invalid, match played but no score {newResult.Year}#{newResult.Season}#{newResult.Round}#{newResult.MatchId}.");
                return(false);
            }

            return(true);
        }