示例#1
0
        public void UpdateRankings(FixtureMatchVM fixtureMatch)
        {
            int nPositions = _context.Divisions.FirstOrDefault(d => d.ID == fixtureMatch.Fixture.DivisionID).PositionNo;

            Fixture fixture = fixtureMatch.Fixture;

            TeamRanking teamRankingHome         = new TeamRanking();
            var         teamRankingHomeToUpdate = _context.TeamRankings.FirstOrDefault(d => d.TeamID == fixture.HomeTeamID && d.SeasonID == fixture.SeasonID && d.DivisionID == fixture.DivisionID);

            TeamRanking teamRankingAway         = new TeamRanking();
            var         teamRankingAwayToUpdate = _context.TeamRankings.FirstOrDefault(d => d.TeamID == fixture.AwayTeamID && d.SeasonID == fixture.SeasonID && d.DivisionID == fixture.DivisionID);

            if (teamRankingHomeToUpdate == null)
            {
                teamRankingHome.TeamID     = fixture.HomeTeamID;
                teamRankingHome.DivisionID = fixture.DivisionID;
                teamRankingHome.SeasonID   = fixture.SeasonID;
                teamRankingHome.Points     = (double)(fixture.HomeTeamScore + fixture.HomeTeamBonus);
                teamRankingHome.Won        = (fixture.HomeTeamScore > fixture.AwayTeamScore) ? 1 : 0;
                teamRankingHome.Lost       = (fixture.HomeTeamScore < fixture.AwayTeamScore) ? 1 : 0;
                teamRankingHome.Played     = 1;

                _context.Add(teamRankingHome);
                _context.SaveChanges();
            }
            else
            {
                teamRankingHome         = teamRankingHomeToUpdate;
                teamRankingHome.Points += (double)(fixture.HomeTeamScore + fixture.HomeTeamBonus);
                teamRankingHome.Won    += (fixture.HomeTeamScore > fixture.AwayTeamScore) ? 1 : 0;
                teamRankingHome.Lost   += (fixture.HomeTeamScore < fixture.AwayTeamScore) ? 1 : 0;
                teamRankingHome.Played += 1;
                _context.Update(teamRankingHome);
                _context.SaveChanges();
            }

            if (teamRankingAwayToUpdate == null)
            {
                teamRankingAway.TeamID     = fixture.AwayTeamID;
                teamRankingAway.DivisionID = fixture.DivisionID;
                teamRankingAway.SeasonID   = fixture.SeasonID;
                teamRankingAway.Points     = (double)(fixture.AwayTeamScore + fixture.AwayTeamBonus);
                teamRankingAway.Won        = (fixture.HomeTeamScore < fixture.AwayTeamScore) ? 1 : 0;
                teamRankingAway.Lost       = (fixture.HomeTeamScore > fixture.AwayTeamScore) ? 1 : 0;
                teamRankingAway.Played     = 1;

                _context.Add(teamRankingAway);
                _context.SaveChanges();
            }
            else
            {
                teamRankingAway         = teamRankingAwayToUpdate;
                teamRankingAway.Points += (double)(fixture.AwayTeamScore + fixture.AwayTeamBonus);
                teamRankingAway.Won    += (fixture.HomeTeamScore < fixture.AwayTeamScore) ? 1 : 0;
                teamRankingAway.Lost   += (fixture.HomeTeamScore > fixture.AwayTeamScore) ? 1 : 0;
                teamRankingAway.Played += 1;
                _context.Update(teamRankingAway);
                _context.SaveChanges();
            }

            //Loop trough the matches to update the rankings
            foreach (Match match in fixtureMatch.Matches)
            {
                if (match.HomePlayerID != 1)
                {
                    //Add statistics for home player
                    PlayerRanking playerRankingHome         = new PlayerRanking();
                    var           playerRankingHomeToUpdate = _context.PlayerRankings.FirstOrDefault(d => d.PlayerID == match.HomePlayerID && d.SeasonID == fixture.SeasonID && d.DivisionID == fixture.DivisionID);

                    if (playerRankingHomeToUpdate == null)
                    {
                        playerRankingHome.TotalPositions = (int)match.PositionID;
                        playerRankingHome.PlayerID       = match.HomePlayerID;
                        playerRankingHome.SeasonID       = fixture.SeasonID;
                        playerRankingHome.DivisionID     = fixture.DivisionID;
                        playerRankingHome.Played         = 1;
                        if (match.HomePlayerScore > match.AwayPlayerScore)
                        {
                            playerRankingHome.WonMatches  = 1;
                            playerRankingHome.LostMatches = 0;
                        }
                        else
                        {
                            playerRankingHome.WonMatches  = 0;
                            playerRankingHome.LostMatches = 1;
                        }

                        playerRankingHome.WonGames  = (short)match.HomePlayerScore;
                        playerRankingHome.LostGames = (short)match.AwayPlayerScore;
                        playerRankingHome.Points    = RankPlayer.CalcPoints(match.Position.Name, (short)match.HomePlayerScore, (short)match.AwayPlayerScore, ResultFor.Home);
                        playerRankingHome.Average   = playerRankingHome.Points / playerRankingHome.Played;
                        _context.Add(playerRankingHome);
                    }
                    else
                    {
                        playerRankingHome                 = playerRankingHomeToUpdate;
                        playerRankingHome.Played         += 1;
                        playerRankingHome.TotalPositions += (int)match.PositionID;
                        if (match.HomePlayerScore > match.AwayPlayerScore)
                        {
                            playerRankingHome.WonMatches += 1;
                        }
                        else
                        {
                            playerRankingHome.LostMatches += 1;
                        }


                        playerRankingHome.WonGames  += (short)match.HomePlayerScore;
                        playerRankingHome.LostGames += (short)match.AwayPlayerScore;
                        playerRankingHome.Points    += RankPlayer.CalcPoints(match.Position.Name, (short)match.HomePlayerScore, (short)match.AwayPlayerScore, ResultFor.Home);
                        playerRankingHome.Average    = playerRankingHome.Points / playerRankingHome.Played;
                        _context.Update(playerRankingHome);
                    }
                }


                if (match.AwayPlayerID != 1)
                {
                    //Add statistics for away player
                    PlayerRanking playerRankingAway         = new PlayerRanking();
                    var           playerRankingAwayToUpdate = _context.PlayerRankings.FirstOrDefault(d => d.PlayerID == match.AwayPlayerID && d.SeasonID == fixture.SeasonID && d.DivisionID == fixture.DivisionID);

                    if (playerRankingAwayToUpdate == null)
                    {
                        playerRankingAway.PlayerID       = match.AwayPlayerID;
                        playerRankingAway.SeasonID       = fixture.SeasonID;
                        playerRankingAway.DivisionID     = fixture.DivisionID;
                        playerRankingAway.Played         = 1;
                        playerRankingAway.TotalPositions = (int)match.PositionID;
                        if (match.HomePlayerScore < match.AwayPlayerScore)
                        {
                            playerRankingAway.WonMatches  = 1;
                            playerRankingAway.LostMatches = 0;
                        }
                        else
                        {
                            playerRankingAway.WonMatches  = 0;
                            playerRankingAway.LostMatches = 1;
                        }


                        playerRankingAway.WonGames  = (short)match.AwayPlayerScore;
                        playerRankingAway.LostGames = (short)match.HomePlayerScore;
                        playerRankingAway.Points    = RankPlayer.CalcPoints(match.Position.Name, (short)match.HomePlayerScore, (short)match.AwayPlayerScore, ResultFor.Away);
                        playerRankingAway.Average   = playerRankingAway.Points / playerRankingAway.Played;
                        _context.Add(playerRankingAway);
                    }
                    else
                    {
                        playerRankingAway                 = playerRankingAwayToUpdate;
                        playerRankingAway.Played         += 1;
                        playerRankingAway.TotalPositions += (int)match.PositionID;
                        if (match.HomePlayerScore < match.AwayPlayerScore)
                        {
                            playerRankingAway.WonMatches += 1;
                        }
                        else
                        {
                            playerRankingAway.LostMatches += 1;
                        }

                        playerRankingAway.WonGames  += (short)match.AwayPlayerScore;
                        playerRankingAway.LostGames += (short)match.HomePlayerScore;
                        playerRankingAway.Points    += RankPlayer.CalcPoints(match.Position.Name, (short)match.HomePlayerScore, (short)match.AwayPlayerScore, ResultFor.Away);
                        playerRankingAway.Average    = playerRankingAway.Points / playerRankingAway.Played;
                        _context.Update(playerRankingAway);
                    }
                }
            }
            _context.SaveChanges();
        }
        public IActionResult Index()
        {
            ViewData["Account"] = HttpContext.Session.GetString("Account");

            List <RankPlayer> top;

            using (GameContext game = new GameContext())
            {
                var player = (from p in game.Players
                              orderby p.TotalExperience descending
                              select p).Take(50);

                top = new List <RankPlayer>();

                int rank = 1;
                foreach (var p in player)
                {
                    string rLevel = "Benginer";
                    if (p.Level == 0)
                    {
                        rLevel = "Benginer";
                    }
                    else if (p.Level <= 20)
                    {
                        rLevel = "Rookie";
                    }
                    else if (p.Level <= 40)
                    {
                        rLevel = "Semi-Pro";
                    }
                    else if (p.Level <= 80)
                    {
                        rLevel = "Pro";
                    }
                    else if (p.Level <= 120)
                    {
                        rLevel = "S1";
                    }
                    else if (p.Level <= 160)
                    {
                        rLevel = "S2";
                    }
                    else if (p.Level <= 200)
                    {
                        rLevel = "S3";
                    }
                    else if (p.Level <= 240)
                    {
                        rLevel = "S4";
                    }
                    var pp = new RankPlayer()
                    {
                        Id    = p.Id,
                        Rank  = rank,
                        Name  = "",
                        Level = rLevel,
                        Exp   = p.TotalExperience
                    };
                    top.Add(pp);
                }
            }
            using (AuthContext auth = new AuthContext())
            {
                var player = from a in auth.Accounts
                             join p in top on a.Id equals p.Id
                             select new RankPlayer()
                {
                    Id    = p.Id,
                    Rank  = p.Rank,
                    Name  = a.Nickname,
                    Level = p.Level,
                    Exp   = p.Exp
                };
                ViewData["Rank"] = player.ToList();
            }

            return(View());
        }