public Player CreateNewPlayer(int tournamentId)
        {
            var player = new Player();

            var newPlayer = Database.Players.Add(player);
            Database.SaveChanges();

            var playerHistory = new PlayerGlickoHistory
            {
                PlayerId = newPlayer.PlayerId,
                Ranking = 1500,
                Deviation = 300,
                Volatility = 0.06m,
                TournamentId = tournamentId
            };

            //TODO: This may work instead of current implemenatation
            //newPlayer.PlayerGlickoHistories.Add(new PlayerGlickoHistory
            //{
            //    PlayerPlayerId = newPlayer.PlayerId,
            //    Ranking = 1500,
            //    Deviation = 300,
            //    Volatility = 0.06m,
            //    Date = tournamentDateTime
            //});

            Database.PlayerGlickoHistories.Add(playerHistory);
            Database.SaveChanges();

            return newPlayer;
        }
        public void RankTournament(int tournamentId)
        {
            var tournament = _tournamentDbHelper.GetTournamentPlayersAndMatches(tournamentId);
            var newRankings = new List<PlayerGlickoHistory>();

            foreach (var attendee in tournament.TournamentAttendees)
            {
                var playerOpponents = new List<GlickoOpponent>();
                var playerRanking = attendee.Player.PlayerGlickoHistories.OrderByDescending(x => x.Tournament.Date).First();

                var glickoPlayer = new GlickoPlayer((double)playerRanking.Ranking, (double)playerRanking.Deviation,
                    (double)playerRanking.Volatility);

                var matches =
                    tournament.Matches.Where(x => x.Player1Id == attendee.PlayerId || x.Player2Id == attendee.PlayerId);

                foreach (var match in matches)
                {
                    var opponentPlayer = match.Player1Id != attendee.PlayerId ? match.Player1 : match.Player2;
                    var opponentRank = opponentPlayer.PlayerGlickoHistories.OrderByDescending(x => x.TournamentId == tournamentId).First();

                    var opponentGlicko = new GlickoPlayer((double)opponentRank.Ranking,
                        (double)opponentRank.Deviation, (double)opponentRank.Volatility);

                    var result = match.WinnerId == attendee.PlayerId ? 1 : 0;

                    var glickoOpponent = new GlickoOpponent(opponentGlicko, result);

                    playerOpponents.Add(glickoOpponent);
                }

                glickoPlayer = GlickoCalculator.CalculateRanking(glickoPlayer, playerOpponents);

                var newRank = new PlayerGlickoHistory()
                {
                    Deviation = Convert.ToDecimal(glickoPlayer.RatingDeviation),
                    Ranking = Convert.ToDecimal(glickoPlayer.Rating),
                    Volatility = Convert.ToDecimal(glickoPlayer.Volatility),
                    PlayerId = attendee.PlayerId,
                    TournamentId = tournamentId
                };

                newRankings.Add(newRank);
            }

            UpdateRankingsDb(newRankings);
        }
 public void AddPlayerHistory(PlayerGlickoHistory history)
 {
     Database.PlayerGlickoHistories.Add(history);
     Database.SaveChanges();
 }