示例#1
0
        /// <summary>
        /// Calculates Opponent's Win Percentage, weighted by number of times played, based on Pythagorean W/L
        /// </summary>
        /// <param name="teams"></param>
        public void CalcPythOppWinPercentage(SortableBindingList <TeamModel> teams)
        {
            //Opponents win percentage is calculated by:
            //1.  Getting wins and losses for each team
            //2.  For the team which you are caculating opponents win percentage, you remove the wins and losses
            //    for games played against said opponent
            //3.  Each opponent's Wins and Losses must be multiplied by the number of times the team was played.
            //4.  Tally all of these adjusted wins and losses together
            //5.  OpponentsWinPercentage = AdjustedOpponentWins/totalAdjustedOpponentGamesPlayed

            float totalAdjustedOppWins   = 0;
            float totalAdjustedOppLosses = 0;

            //1.  Adjust records of opponents by subtracting wins and losses vs the current team from opposing records
            foreach (var opponentModel in OpponentsList)
            {
                //Make sure they've actually played the opponent...
                if (opponentModel.WinsVersus != 0 || opponentModel.LossesVersus != 0)
                {
                    //Get current wins and losses from the team standings list
                    opponentModel.PythWins   = (teams.Where(x => x.Name.Equals(opponentModel.OpponentTeamName)).FirstOrDefault().PythWins);
                    opponentModel.PythLosses = (teams.Where(x => x.Name.Equals(opponentModel.OpponentTeamName)).FirstOrDefault().PythLosses);

                    //Subtract current team's losses versus opponent from that opponent's wins
                    opponentModel.AdjustedPythWins = opponentModel.PythWins - opponentModel.LossesVersus;

                    //Subtract current team's wins versus opponent from that opponent's losses
                    opponentModel.AdjustedPythLosses = opponentModel.PythLosses - opponentModel.WinsVersus;

                    //To further complicate things, we must now take into account the number of times a team was played, and
                    //multiply their win/loss record according the times they played them.
                    int timesPlayed = opponentModel.LossesVersus + opponentModel.WinsVersus;

                    opponentModel.AdjustedPythWins   = opponentModel.AdjustedPythWins * timesPlayed;
                    opponentModel.AdjustedPythLosses = opponentModel.AdjustedPythLosses * timesPlayed;

                    //Add adjusted wins and losses together to get totals
                    totalAdjustedOppWins   += opponentModel.AdjustedPythWins;
                    totalAdjustedOppLosses += opponentModel.AdjustedPythLosses;
                }
            }

            PythOpponentsWinPercentage = totalAdjustedOppWins / (totalAdjustedOppWins + totalAdjustedOppLosses);
        }