public string FormattedBoardValueForPlayer(PlayerLeaderBoard plb, player p, year y)
        {
            var value = this.CalculateBoardValueForPlayer(plb, p, y);

            return(plb.Format.FormatValue(value));
        }
        public void CalculateAndSaveLeaderBoards()
        {
            this.year = this.db.years.First(x => x.id == this.year.id);

            this.lbc.Initialize();

            var allPlayersForYear = this.db.AllPlayersForYear(this.year, includeResults: true);
            var playerIds         = allPlayersForYear.Select(x => x.id);

            PlayerLeaderBoard playerLeaderBoard = null;
            player            currPlayer        = null;

            try
            {
                foreach (var lb in LeaderBoards.PlayerBoards)
                {
                    playerLeaderBoard = lb;

                    this.CleanUpLeaderBoardDatas <player>(playerIds, lb);

                    foreach (player p in allPlayersForYear)
                    {
                        currPlayer = p;

                        var results = p.AllResultsForYear(year);

                        this.CalculateAndSetValue(p.id, lb, results, p);
                    }

                    var leaderBoardDatas = this.lbc.GetBoardData(lb.LeaderBoardKey);

                    this.SortAndRankLeaderBoardData(leaderBoardDatas, lb.Ascending);
                }
            }
            catch (Exception e)
            {
                var thrown = new Exception("Error calculating player leaderboard: " + playerLeaderBoard.LeaderBoardName + ", " + currPlayer.name, e);
                Logger.Error(thrown);
                throw thrown;
            }

            TeamLeaderBoard teamLeaderBoard = null;
            team            currTeam        = null;

            try
            {
                var teams   = this.db.TeamsForYear(this.year.value).Where(x => !string.Equals("league subs", x.teamName, StringComparison.OrdinalIgnoreCase));
                var teamIds = teams.Select(x => x.id);

                foreach (var lb in LeaderBoards.TeamBoards)
                {
                    teamLeaderBoard = lb;

                    this.CleanUpLeaderBoardDatas <team>(teamIds, lb);

                    foreach (var t in teams)
                    {
                        currTeam = t;

                        var results = t.AllResultsForYear(year);

                        this.CalculateAndSetValue(t.id, lb, results, t);
                    }

                    var leaderBoardDatas = this.lbc.GetBoardData(lb.LeaderBoardKey);

                    this.SortAndRankLeaderBoardData(leaderBoardDatas, lb.Ascending);
                }
            }
            catch (Exception e)
            {
                var thrown = new Exception("Error calculating team board: " + teamLeaderBoard.LeaderBoardName + ", " + currTeam.teamName, e);
                Logger.Error(thrown);
                throw thrown;
            }

            // For each multi value board, create it if it doesn't exist
            // get leaderboard datas
            // execute query/code

            this.db.SaveChanges();
        }
        public double CalculateBoardValueForPlayer(PlayerLeaderBoard plb, player p, year y)
        {
            var value = plb.DoCalculation(p, y, p.AllResultsForYear(y));

            return(value.HasValue ? value.Value : 0);
        }