public async Task <decimal[]> CalculateScoreAsync(FantasyMatchup matchup)
        {
            var matchupWeek = await _fantasyMatchupsWeeksService.GetFantasyMatchupWeekByLeagueAsync(matchup.FantasyLeagueID, matchup.Week);

            decimal homeScore = 0;
            decimal awayScore = 0;

            if (matchupWeek != null)
            {
                var home = await _playerMyTeamService.GetRosterAsync(matchup.HomeTeamID.Value);

                if (home.Any() && home != null)
                {
                    List <PlayerGameStats> homeStats = await GetGameStatsListAsync(home, matchupWeek);

                    if (homeStats != null)
                    {
                        homeScore = homeStats.Sum(x => x.FantasyPoints);
                    }
                }

                var away = await _playerMyTeamService.GetRosterAsync(matchup.AwayTeamID.Value);

                if (away.Any() && away != null)
                {
                    List <PlayerGameStats> awayStats = await GetGameStatsListAsync(away, matchupWeek);

                    if (awayStats != null)
                    {
                        awayScore = awayStats.Sum(x => x.FantasyPoints);
                    }
                }
            }
            return(new decimal[] { homeScore, awayScore });
        }
        private async Task CreateMatchupsAsync(FantasyLeague fantasyLeague)
        {
            List <MyTeam> teams = fantasyLeague.TeamsNav;

            if (!await MatchupsExists(fantasyLeague.FantasyLeagueID))
            {
                int numWeeks = (teams.Count() - 1) * 2;
                int halfSize = teams.Count() / 2;

                List <MyTeam> listTeams = new List <MyTeam>();

                listTeams.AddRange(teams);
                listTeams.RemoveAt(0);

                int teamSize = listTeams.Count();

                List <FantasyMatchup> matchups = new List <FantasyMatchup>();

                for (int week = 0; week < numWeeks; week++)
                {
                    var matchup = new FantasyMatchup();
                    matchup.Week            = week + 1;
                    matchup.FantasyLeagueID = fantasyLeague.FantasyLeagueID;
                    matchup.Status          = "Scheduled";

                    int teamIdx = week % teamSize;

                    matchup.AwayTeamNav   = listTeams[teamIdx];
                    matchup.HomeTeamNav   = teams[0];
                    matchup.HomeTeamScore = 0;
                    matchup.AwayTeamScore = 0;

                    matchups.Add(matchup);

                    for (int i = 1; i < halfSize; i++)
                    {
                        var nextMatchup = new FantasyMatchup();
                        nextMatchup.Week            = week + 1;
                        nextMatchup.FantasyLeagueID = fantasyLeague.FantasyLeagueID;
                        nextMatchup.Status          = "Scheduled";

                        nextMatchup.AwayTeamNav   = listTeams[(week + i) % teamSize];
                        nextMatchup.HomeTeamNav   = listTeams[(week + teamSize - i) % teamSize];
                        nextMatchup.AwayTeamScore = 0;
                        nextMatchup.HomeTeamScore = 0;
                        matchups.Add(nextMatchup);
                    }
                }
                _context.FantasyMatchup.AddRange(matchups);
                await _context.SaveChangesAsync();

                return;
            }
        }
        private async Task <List <FantasyMatchup> > UpdateScoreAsync(FantasyMatchup matchup, List <FantasyMatchup> updateList)
        {
            var scores = await CalculateScoreAsync(matchup);

            matchup.HomeTeamScore = scores[0];
            matchup.AwayTeamScore = scores[1];
            matchup.UpdatedAt     = DateTime.Now;

            updateList.Add(matchup);
            return(updateList);
        }
 public FantasyMatchup SetStatus(FantasyMatchup matchup, string status)
 {
     matchup.Status = status;
     return(matchup);
 }