コード例 #1
0
        public async Task <IEnumerable <LeagueStandingResource> > GetLeagueStandings(int leagueId)
        {
            var teams = await _repository.GetLeagueTeams(leagueId);

            var standings = new int[teams.Count() + 1, 5];
            var matches   = await _repository.GetLeagueMatches(leagueId);

            foreach (var match in matches)
            {
                if (match.HomeScore > match.AwayScore)
                {
                    standings[match.HomeTeamId, 0]++;
                    standings[match.AwayTeamId, 2]++;
                    standings[match.HomeTeamId, 3] += match.HomeScore;
                    standings[match.HomeTeamId, 4] += match.AwayScore;
                    standings[match.AwayTeamId, 3] += match.AwayScore;
                    standings[match.AwayTeamId, 4] += match.HomeScore;
                }

                if (match.HomeScore < match.AwayScore)
                {
                    standings[match.AwayTeamId, 0]++;
                    standings[match.HomeTeamId, 2]++;
                    standings[match.AwayTeamId, 3] += match.AwayScore;
                    standings[match.AwayTeamId, 4] += match.HomeScore;
                    standings[match.HomeTeamId, 3] += match.HomeScore;
                    standings[match.HomeTeamId, 4] += match.AwayScore;
                }

                if (match.HomeScore == match.AwayScore)
                {
                    standings[match.HomeTeamId, 1]++;
                    standings[match.AwayTeamId, 1]++;
                }
            }

            var result = teams.Select(t => new LeagueStandingResource {
                TeamId         = t.Id,
                TeamName       = t.Name,
                TeamLogoUrl    = t.LogoUrl,
                Played         = standings[t.Id, 0] + standings[t.Id, 1] + standings[t.Id, 2],
                Won            = standings[t.Id, 0],
                Drawn          = standings[t.Id, 1],
                Lost           = standings[t.Id, 2],
                GoalFor        = standings[t.Id, 3],
                GoalAgainst    = standings[t.Id, 4],
                GoalDifference = standings[t.Id, 3] - standings[t.Id, 4],
                Points         = standings[t.Id, 0] * 3 + standings[t.Id, 1],
            }).OrderByDescending(ob => ob.Points).ThenByDescending(tb => tb.GoalDifference).ThenByDescending(tb => tb.GoalFor).ToList();

            return(result);
        }