Ejemplo n.º 1
0
        public IActionResult TeamStats(string name)
        {
            var teamStatsData =
                this.statsDbContext.Statistic.Where(x => x.HomeTeam == name || x.AwayTeam == name)
                .OrderByDescending(x => x.Date)
                .Take(10)
                .ToList();

            var model = new TeamStatsModel()
            {
                Name = name
            };

            model.WinsCount = teamStatsData.Count(
                x =>
                x.HomeTeam == name
                        ? x.Ftr.Equals("H", StringComparison.OrdinalIgnoreCase)
                        : x.Ftr.Equals("A", StringComparison.OrdinalIgnoreCase));

            model.LooseCount = teamStatsData.Count(
                x =>
                x.HomeTeam == name
                        ? x.Ftr.Equals("A", StringComparison.OrdinalIgnoreCase)
                        : x.Ftr.Equals("H", StringComparison.OrdinalIgnoreCase));

            model.DrawsCount = teamStatsData.Count(
                x => x.Ftr.Equals("D", StringComparison.OrdinalIgnoreCase));

            model.HomeGoals = teamStatsData.Sum(
                x =>
                x.HomeTeam == name
                        ? x.Fthg ?? 0
                        : 0);

            model.AwayGoals = teamStatsData.Sum(
                x =>
                x.HomeTeam == name
                        ? x.Ftag ?? 0
                        : 0);

            model.Games = teamStatsData;

            return(PartialView(model));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Stats(int id)
        {
            var stats = new TeamStatsModel();

            // Finds the team corresponding to the given id
            var team = await _teams.GetTeamById(id, true);

            // Only take seasons from the championship that is currently in use
            var seasons = Context.Seasons
                          .Where(s => s.Championship.ActiveChampionship)
                          .ToList();

            // Basic information about team
            stats.TeamId    = team.Id;
            stats.TeamShort = team.Abbreviation;
            stats.TeamBio   = team.Biography;

            // Tries to find the last season in which this team was used so their possible long name and team colours can be set
            var lastSeasonTeam = Context.SeasonTeams
                                 .ToList()
                                 .LastOrDefault(st => st.TeamId == id);

            if (lastSeasonTeam != null)
            {
                stats.TeamLong   = lastSeasonTeam.Name;
                stats.TeamColour = lastSeasonTeam.Colour;
                stats.TeamAccent = lastSeasonTeam.Accent;
            }

            // Selects which drivers have driven for the team
            var drivers = Context.SeasonDrivers
                          .IgnoreQueryFilters()
                          .Where(sd => sd.SeasonTeam.TeamId == id)
                          .Include(sd => sd.Driver)
                          .ToList();

            stats.Drivers = drivers
                            .Select(d => d.Driver)
                            .Distinct()
                            .Select(d => d.Name);

            var results = Context.DriverResults
                          .Where(dr => dr.SeasonDriver.SeasonTeam.TeamId == id && dr.SeasonDriver.Season.Championship.ActiveChampionship)
                          .ToList();

            stats.RaceEntries     = results.GroupBy(r => r.RaceId).Count();
            stats.TotalCarEntries = results.Count;
            for (int i = 1; i <= 20; i++)
            {
                int positionCount = results.Count(res => res.Position == i && res.Status == Status.Finished);
                stats.PositionList.Add(i);
                stats.ResultList.Add(positionCount);
            }
            stats.Poles          = results.Count(r => r.Grid == 1);
            stats.RaceWins       = results.Count(r => r.Position == 1);
            stats.SecondFinishes = results.Count(r => r.Position == 2);
            stats.ThirdFinishes  = results.Count(r => r.Position == 3);
            stats.AveragePos     = Math.Round(results.Where(res => res.Status == Status.Finished).Average(res => res.Position), 2);
            stats.DidNotFinish   = results.Count(r => r.Status == Status.DNF || r.Status == Status.DSQ);

            // Calculate the amount of times the drivers from the team has finished inside the points
            int pointCount = 0;

            foreach (var season in seasons)
            {
                var current   = results.Where(r => r.SeasonDriver.SeasonId == season.SeasonId);
                var pointsMax = season.PointsPerPosition.Keys.Max();
                pointCount += (current.Count(dr => dr.Position > 3 && dr.Position <= pointsMax));
            }

            // Apply point finishes and subtract others to form outside point finishes
            stats.PointFinishes   = pointCount;
            stats.NoPointFinishes = (stats.TotalCarEntries - stats.RaceWins - stats.SecondFinishes - stats.ThirdFinishes - pointCount - stats.DidNotFinish);

            // Calculates the amount of championships a team has won.
            int teamchamps = 0;

            foreach (var season in seasons)
            {
                var teamwinner = Context.SeasonTeams
                                 .IgnoreQueryFilters()
                                 .Where(s => s.SeasonId == season.SeasonId && s.Season.State == SeasonState.Finished)
                                 .OrderByDescending(dr => dr.Points)
                                 .FirstOrDefault();

                if (teamwinner != null)
                {
                    if (teamwinner.TeamId == id)
                    {
                        teamchamps++;
                    }
                }
            }

            stats.ConstructorTitles = teamchamps;
            return(View(stats));
        }
        public TeamStatsModel GetTeamStats()
        {
            TeamStatsModel tsm = new TeamStatsModel();

            //Home Team Stats
            int teamid = _statreader.ReadStat(10411);
            string team = Utils.GetTeamAbbreviation(teamid);
            int score = _statreader.ReadStat(9121);
            int shots = _statreader.ReadStat(9101);
            int breakaways = _statreader.ReadStat(9173);
            int breakawygoals = _statreader.ReadStat(9177);
            int onetimers = _statreader.ReadStat(9193);
            int onetimergoals = _statreader.ReadStat(9197);
            int penaltyshots = _statreader.ReadStat(9181);
            int penaltyshotgoals = _statreader.ReadStat(9185);
            int faceoffsWon = _statreader.ReadStat(9125);
            int bodychecks = _statreader.ReadStat(9161);
            int passes = _statreader.ReadStat(9169);
            int passesreceived = _statreader.ReadStat(9165);
            int powerplays = _statreader.ReadStat(9049);
            int powerplaygoals = _statreader.ReadStat(9045);
            int powerplayshots = _statreader.ReadStat(9069);
            int shorthandedgoals = _statreader.ReadStat(9077);
            int penalities = _statreader.ReadStat(9053);
            int penaltytime = _statreader.ReadStat(9057);
            int period1shots = _statreader.ReadStat(9085);
            int period2shots = _statreader.ReadStat(9089);
            int period3shots = _statreader.ReadStat(9093);
            int periodotshots = _statreader.ReadStat(9097);
            int period1goals = _statreader.ReadStat(9105);
            int period2goals = _statreader.ReadStat(9109);
            int period3goals = _statreader.ReadStat(9113);
            int periodotgoals = _statreader.ReadStat(9117);
            string offensivetimezone = TimeSpan.FromSeconds(_statreader.ReadlLittleEndian(9061, 9062)).ToString(@"mm\:ss");
            string powerplaytime = TimeSpan.FromSeconds(_statreader.ReadlLittleEndian(9065, 9066)).ToString(@"mm\:ss");

            var homets = new TeamStatsModel.TeamStats() { Bodychecks = bodychecks, BreakawayGoals = breakawygoals, Breakaways = breakaways, FaceoffsWon = faceoffsWon, HomeOrAway = HomeorAwayTeam.Home, OffensiveTimeZone = offensivetimezone, OneTimerGoals = onetimergoals, OneTimers = onetimers, OTGoals = periodotgoals, OTShots = periodotshots, Passes = passes, PassesReceived = passesreceived, Penalities = penalities, PenaltyShotGoals = penaltyshotgoals, PenaltyShots = penaltyshots, PenaltyTime = penaltytime, Period1Goals = period1goals, Period1Shots = period1shots, Period2Goals = period2goals, Period2Shots = period2shots, Period3Goals = period3goals, Period3Shots = period3shots, PowerplayGoals = powerplaygoals, Powerplays = powerplays, PowerplayShots = powerplayshots, PowerplayTime = powerplaytime, Score = score, ShorthandedGoals = shorthandedgoals, Shots = shots, Team = team, TeamID = teamid   };
            tsm.Add(homets);

            //Away Team Stats
            teamid = _statreader.ReadStat(10413);
            team = Utils.GetTeamAbbreviation(teamid);
            score = _statreader.ReadStat(9123);
            shots = _statreader.ReadStat(9103);
            breakaways = _statreader.ReadStat(9175);
            breakawygoals = _statreader.ReadStat(9179);
            onetimers = _statreader.ReadStat(9195);
            onetimergoals = _statreader.ReadStat(9199);
            penaltyshots = _statreader.ReadStat(9183);
            penaltyshotgoals = _statreader.ReadStat(9187);
            faceoffsWon = _statreader.ReadStat(9127);
            bodychecks = _statreader.ReadStat(9163);
            passes = _statreader.ReadStat(9171);
            passesreceived = _statreader.ReadStat(9167);
            powerplays = _statreader.ReadStat(9051);
            powerplaygoals = _statreader.ReadStat(9047);
            powerplayshots = _statreader.ReadStat(9071);
            shorthandedgoals = _statreader.ReadStat(9079);
            penalities = _statreader.ReadStat(9055);
            penaltytime = _statreader.ReadStat(9059);
            period1shots = _statreader.ReadStat(9087);
            period2shots = _statreader.ReadStat(9091);
            period3shots = _statreader.ReadStat(9095);
            periodotshots = _statreader.ReadStat(9099);
            period1goals = _statreader.ReadStat(9107);
            period2goals = _statreader.ReadStat(9111);
            period3goals = _statreader.ReadStat(9115);
            periodotgoals = _statreader.ReadStat(9119);
            offensivetimezone = TimeSpan.FromSeconds(_statreader.ReadlLittleEndian(9063, 9064)).ToString(@"mm\:ss");
            powerplaytime = TimeSpan.FromSeconds(_statreader.ReadlLittleEndian(9067, 9068)).ToString(@"mm\:ss");

            var awayts = new TeamStatsModel.TeamStats() { Bodychecks = bodychecks, BreakawayGoals = breakawygoals, Breakaways = breakaways, FaceoffsWon = faceoffsWon, HomeOrAway = HomeorAwayTeam.Away, OffensiveTimeZone = offensivetimezone, OneTimerGoals = onetimergoals, OneTimers = onetimers, OTGoals = periodotgoals, OTShots = periodotshots, Passes = passes, PassesReceived = passesreceived, Penalities = penalities, PenaltyShotGoals = penaltyshotgoals, PenaltyShots = penaltyshots, PenaltyTime = penaltytime, Period1Goals = period1goals, Period1Shots = period1shots, Period2Goals = period2goals, Period2Shots = period2shots, Period3Goals = period3goals, Period3Shots = period3shots, PowerplayGoals = powerplaygoals, Powerplays = powerplays, PowerplayShots = powerplayshots, PowerplayTime = powerplaytime, Score = score, ShorthandedGoals = shorthandedgoals, Shots = shots, Team = team, TeamID = teamid };
            tsm.Add(awayts);

            return tsm;
        }