/// <summary>Calculates the league PerGame.</summary> /// <param name="tst">The team stats dictionary.</param> /// <param name="statRange">The stat range.</param> /// <returns></returns> public static TeamStats CalculateLeagueAverages(Dictionary<int, TeamStats> tst, Span statRange) { var ls = new TeamStats(-1, "League"); var teamCount = countTeams(tst, statRange); for (var i = 0; i < tst.Count; i++) { ls.AddTeamStats(tst[i], statRange); } ls.CalcMetrics(ls, (statRange == Span.Playoffs)); ls.Record[0] /= teamCount; ls.Record[1] /= teamCount; ls.PlRecord[0] /= teamCount; ls.PlRecord[1] /= teamCount; ls.PerGame[TAbbrPG.Weff] /= teamCount; ls.PlPerGame[TAbbrPG.Weff] /= teamCount; return ls; }
private void updateGraph() { if (cmbGraphStat.SelectedIndex == -1 || cmbTeam.SelectedIndex == -1 || cmbGraphInterval.SelectedIndex == -1) { clearGraph(); return; } var intervalItem = cmbGraphInterval.SelectedItem.ToString(); var yearlyRows = _dtYea.Rows.Cast<DataRow>().Where(dr => dr[0].ToString().StartsWith("Season")).ToList(); var monthlyStats = MainWindow.SplitTeamStats[_curts.ID].Where(pair => pair.Key.StartsWith("M ")).ToList(); var orderedBSEList = _bseList.OrderBy(bse => bse.BS.GameDate).ToList(); Intervals interval; int count; switch (intervalItem) { case "Every Game": interval = Intervals.EveryGame; count = orderedBSEList.Count; break; case "Monthly": interval = Intervals.Monthly; count = monthlyStats.Count; break; case "Yearly": interval = Intervals.Yearly; count = yearlyRows.Count; break; default: throw new ArgumentOutOfRangeException(); } if (count < 2) { clearGraph(); return; } var propToGet = cmbGraphStat.SelectedItem.ToString(); propToGet = propToGet.Replace('3', 'T'); propToGet = propToGet.Replace('%', 'p'); propToGet = propToGet.Replace("TO", "TOS"); double sum = 0; double games = 0; chart.Primitives.Clear(); var cp = new ChartPrimitive { Label = cmbGraphStat.SelectedItem.ToString(), ShowInLegend = false }; switch (interval) { case Intervals.EveryGame: for (var i = 0; i < count; i++) { var bse = orderedBSEList[i]; bse.BS.PrepareForDisplay(_tst, _curts.ID); var isTeamAway = bse.BS.Team1ID == _curts.ID; var propToGetFinal = propToGet; if (propToGet == "PF") { propToGetFinal = isTeamAway ? "PTS1" : "PTS2"; } else if (propToGet == "PA") { propToGetFinal = isTeamAway ? "PTS2" : "PTS1"; } else { propToGetFinal += (isTeamAway ? 1 : 2).ToString(); } var value = bse.BS.GetValue<TeamBoxScore, double>(propToGetFinal); if (double.IsNaN(value)) { continue; } if (propToGet.Contains("p")) { value = Convert.ToDouble(Convert.ToInt32(value * 1000)) / 1000; } cp.AddPoint(i + 1, value); games++; sum += value; } break; case Intervals.Monthly: monthlyStats = monthlyStats.OrderBy(ms => ms.Key).ToList(); if (TeamStatsHelper.TotalsToPerGame.ContainsKey(propToGet)) { propToGet = TeamStatsHelper.TotalsToPerGame[propToGet]; } for (var i = 0; i < count; i++) { var ts = monthlyStats[i].Value; ts.CalcMetrics(new TeamStats()); var tsr = new TeamStatsRow(ts); var value = tsr.GetValue<double>(propToGet); if (double.IsNaN(value)) { continue; } if (propToGet.Contains("p")) { value = Convert.ToDouble(Convert.ToInt32(value * 1000)) / 1000; } cp.AddPoint(i + 1, value); games++; sum += value; } break; case Intervals.Yearly: if (TeamStatsHelper.TotalsToPerGame.ContainsKey(propToGet)) { propToGet = TeamStatsHelper.TotalsToPerGame[propToGet]; } for (var i = 0; i < count; i++) { var ts = new TeamStats(); createTeamStatsFromDataRow(ref ts, _dtYea.Rows.Cast<DataRow>().ToList()[i]); ts.CalcMetrics(new TeamStats()); var tsr = new TeamStatsRow(ts); var value = tsr.GetValue<TeamStatsRow, double>(propToGet); if (double.IsNaN(value)) { continue; } if (propToGet.Contains("p")) { value = Convert.ToDouble(Convert.ToInt32(value * 1000)) / 1000; } cp.AddPoint(i + 1, value); games++; sum += value; } break; default: throw new ArgumentOutOfRangeException(); } if (cp.Points.Count > 0) { chart.Primitives.Add(cp); } if (chart.Primitives.Count > 0 && chart.Primitives.Sum(p => p.Points.Count) > 1) { //var average = sum / games; if (TeamStatsHelper.TotalsToPerGame.ContainsKey(propToGet)) { propToGet = TeamStatsHelper.TotalsToPerGame[propToGet]; } double average; switch (interval) { case Intervals.EveryGame: case Intervals.Monthly: average = _curTSR.GetValue<double>(propToGet); break; case Intervals.Yearly: var ts = new TeamStats(); createTeamStatsFromDataRow(ref ts, _dtYea.Rows.Cast<DataRow>().ToList().Last()); ts.CalcMetrics(new TeamStats()); var tsr = new TeamStatsRow(ts); average = tsr.GetValue<double>(propToGet); break; default: throw new ArgumentOutOfRangeException(); } var cpavg = new ChartPrimitive(); for (var i = 0; i < count; i++) { cpavg.AddPoint(i + 1, average); } cpavg.Color = Color.FromRgb(0, 0, 100); cpavg.Dashed = true; cpavg.ShowInLegend = false; var cp2 = new ChartPrimitive(); cp2.AddPoint(chart.Primitives.First().Points.First().X, 0); cp2.AddPoint(chart.Primitives.Last().Points.Last().X, 1); chart.Primitives.Add(cpavg); chart.RedrawPlotLines(); chart.Primitives.Add(cp2); } else { chart.RedrawPlotLines(); var cp2 = new ChartPrimitive(); cp2.AddPoint(1, 0); cp2.AddPoint(2, 1); chart.Primitives.Add(cp2); } chart.ResetPanAndZoom(); }
/// <summary>Prepares the presentation fields of the class.</summary> /// <param name="teamID">The team.</param> public void PrepareForDisplay(Dictionary<int, TeamStats> tst, int teamID) { FGp1 = (float) FGM1 / FGA1; TPp1 = (float) TPM1 / TPA1; FTp1 = (float) FTM1 / FTA1; FGp2 = (float) FGM2 / FGA2; TPp2 = (float) TPM2 / TPA2; FTp2 = (float) FTM2 / FTA2; DREB1 = (ushort) (REB1 - OREB1); DREB2 = (ushort) (REB2 - OREB2); if (teamID == Team1ID) { DisplayTeam = tst[Team1ID].DisplayName; DisplayOpponent = tst[Team2ID].DisplayName; DisplayLocation = "Away"; DisplayResult = PTS1 > PTS2 ? "W " : "L "; DisplayREB = REB1; DisplayOREB = OREB1; DisplayAST = AST1; DisplayTO = TOS1; DisplayBLK = BLK1; DisplaySTL = STL1; DisplayFOUL = FOUL1; FGp = FGp1; TPp = TPp1; FTp = FTp1; var temp = new TeamStats(); var tempopp = new TeamStats(); TeamStats.AddTeamStatsFromBoxScore(this, ref temp, ref tempopp); temp.CalcMetrics(tempopp); tempopp.CalcMetrics(temp); DisplayGmSc = temp.Metrics["GmSc"]; GmSc1 = temp.Metrics["GmSc"]; GmSc2 = tempopp.Metrics["GmSc"]; } else { DisplayTeam = tst[Team2ID].DisplayName; DisplayOpponent = tst[Team1ID].DisplayName; DisplayLocation = "Home"; DisplayResult = PTS1 < PTS2 ? "W " : "L "; DisplayREB = REB2; DisplayOREB = OREB2; DisplayAST = AST2; DisplayTO = TOS2; DisplayBLK = BLK2; DisplaySTL = STL2; DisplayFOUL = FOUL2; FGp = FGp2; TPp = TPp2; FTp = FTp2; var temp = new TeamStats(); var tempopp = new TeamStats(); TeamStats.AddTeamStatsFromBoxScore(this, ref tempopp, ref temp); temp.CalcMetrics(tempopp); tempopp.CalcMetrics(temp); DisplayGmSc = temp.Metrics["GmSc"]; GmSc2 = temp.Metrics["GmSc"]; GmSc1 = tempopp.Metrics["GmSc"]; } DisplayResult += PTS1 + "-" + PTS2; }
/// <summary>Updates the metric stats for the specified team's players.</summary> /// <param name="team">1 if the away team's players' metric stats should be updated; anything else for the home team.</param> private void updateMetric(int team) { var ts = new TeamStats(-1, team == 1 ? cmbTeam1.SelectedItem.ToString() : cmbTeam2.SelectedItem.ToString()); var tsopp = new TeamStats(-1, team == 1 ? cmbTeam2.SelectedItem.ToString() : cmbTeam1.SelectedItem.ToString()); tryParseBS(); if (!MainWindow.TempBSE_BS.Done) { return; } var bs = MainWindow.TempBSE_BS; if (team == 1) { TeamStats.AddTeamStatsFromBoxScore(bs, ref ts, ref tsopp); } else { TeamStats.AddTeamStatsFromBoxScore(bs, ref tsopp, ref ts); } ts.CalcMetrics(tsopp); tsopp.CalcMetrics(ts); var ls = new TeamStats(-1); ls.AddTeamStats(ts, Span.SeasonAndPlayoffsToSeason); ls.AddTeamStats(tsopp, Span.SeasonAndPlayoffsToSeason); var pmsrList = new List<PlayerStatsRow>(); var pbsList = team == 1 ? pbsAwayList : pbsHomeList; foreach (var pbs in pbsList) { if (pbs.PlayerID == -1) { continue; } var ps = _pst[pbs.PlayerID].BinarySerializationClone(); ps.ResetStats(); ps.AddBoxScore(pbs, bs.IsPlayoff); ps.CalcMetrics(ts, tsopp, ls); pmsrList.Add(new PlayerStatsRow(ps)); } pmsrList.Sort((pmsr1, pmsr2) => pmsr1.GmSc.CompareTo(pmsr2.GmSc)); pmsrList.Reverse(); var tsr = new TeamStatsRow(ts); if (team == 1) { _psrListAway = new List<PlayerStatsRow>(pmsrList); dgvAwayPlayerMetricStats.ItemsSource = _psrListAway; dgvAwayTeamMetricStats.ItemsSource = new[] { tsr }; } else { _psrListHome = new List<PlayerStatsRow>(pmsrList); dgvHomePlayerMetricStats.ItemsSource = _psrListHome; dgvHomeTeamMetricStats.ItemsSource = new[] { tsr }; } }
/// <summary>Calculates the league PerGame.</summary> /// <param name="playerStats">The player stats.</param> /// <param name="teamStats">The team stats.</param> /// <returns></returns> public static PlayerStats CalculateLeagueAverages( Dictionary<int, PlayerStats> playerStats, Dictionary<int, TeamStats> teamStats) { var lps = new PlayerStats(new Player(-1, -1, "League", "Averages", Position.None, Position.None)); foreach (var key in playerStats.Keys) { lps.AddPlayerStats(playerStats[key]); } var ls = new TeamStats(-2, "League"); for (var i = 0; i < teamStats.Count; i++) { ls.AddTeamStats(teamStats[i], Span.Season); ls.AddTeamStats(teamStats[i], Span.Playoffs); } ls.CalcMetrics(ls); ls.CalcMetrics(ls, true); foreach (var name in PlayerStatsHelper.MetricsNames) { try { lps.Metrics[name] = playerStats.Where(ps => !Double.IsNaN(ps.Value.Metrics[name]) && !Double.IsInfinity(ps.Value.Metrics[name])) .Average(ps => ps.Value.Metrics[name]); } catch (InvalidOperationException) { lps.Metrics[name] = Double.NaN; } try { lps.PlMetrics[name] = playerStats.Where(ps => !Double.IsNaN(ps.Value.PlMetrics[name]) && !Double.IsInfinity(ps.Value.Metrics[name])) .Average(ps => ps.Value.PlMetrics[name]); } catch (InvalidOperationException) { lps.Metrics[name] = Double.NaN; } } var playerCount = (uint) playerStats.Count; for (var i = 0; i < lps.Totals.Length; i++) { lps.Totals[i] /= playerCount; lps.PlTotals[i] /= playerCount; } //ps.CalcAvg(); return lps; }
/// <summary>Calculates all metrics.</summary> /// <param name="playerStats">The player stats.</param> /// <param name="teamStats">The team stats.</param> /// <param name="oppStats">The opposing team stats.</param> /// <param name="leagueOv"> /// set to <c>true</c> if calling from the LeagueOverview window. /// </param> /// <param name="playoffs"> /// if set to <c>true</c>, the metrics will be calculated for the playoff stats. /// </param> /// <param name="teamsPerPlayer"> /// if set to <c>true</c>, the team stats dictionary is assumed to be per player. /// </param> public static void CalculateAllMetrics( ref Dictionary<int, PlayerStats> playerStats, Dictionary<int, TeamStats> teamStats, Dictionary<int, TeamStats> oppStats, bool leagueOv = false, bool playoffs = false, bool teamsPerPlayer = false) { var tCount = teamStats.Count; var ls = new TeamStats(); var tKeys = teamStats.Keys.ToList(); for (var i = 0; i < tCount; i++) { var key = tKeys[i]; if (!playoffs) { ls.AddTeamStats(teamStats[key], Span.Season); teamStats[key].CalcMetrics(oppStats[key]); oppStats[key].CalcMetrics(teamStats[key]); } else { ls.AddTeamStats(teamStats[key], Span.Playoffs); teamStats[key].CalcMetrics(oppStats[key], true); oppStats[key].CalcMetrics(teamStats[key], true); } } ls.CalcMetrics(ls, playoffs); double lgAvgPER = 0; double plLgAvgPER = 0; double totalMins = 0; double plTotalMins = 0; foreach (var playerid in playerStats.Keys.ToList()) { if (playerStats[playerid].TeamF == -1) { continue; } var teamid = playerStats[playerid].TeamF; TeamStats ts; TeamStats tsopp; if (!teamsPerPlayer) { ts = teamStats[teamid]; tsopp = oppStats[teamid]; } else { ts = teamStats[playerid]; tsopp = oppStats[playerid]; } playerStats[playerid].CalcMetrics(ts, tsopp, ls, leagueOv, playoffs: playoffs); if (!playoffs) { if (!(Double.IsNaN(playerStats[playerid].Metrics["aPER"])) && !(Double.IsInfinity(playerStats[playerid].Metrics["aPER"]))) { lgAvgPER += playerStats[playerid].Metrics["aPER"] * playerStats[playerid].Totals[PAbbrT.MINS]; totalMins += playerStats[playerid].Totals[PAbbrT.MINS]; } } else { if (!(Double.IsNaN(playerStats[playerid].PlMetrics["aPER"])) && !(Double.IsInfinity(playerStats[playerid].PlMetrics["aPER"]))) { plLgAvgPER += playerStats[playerid].PlMetrics["aPER"] * playerStats[playerid].PlTotals[PAbbrT.MINS]; plTotalMins += playerStats[playerid].PlTotals[PAbbrT.MINS]; } } } if (!playoffs) { lgAvgPER /= totalMins; } else { plLgAvgPER /= plTotalMins; } foreach (var playerid in playerStats.Keys.ToList()) { if (playerStats[playerid].TeamF == -1) { continue; } if (!playoffs) { playerStats[playerid].calcPER(lgAvgPER); } else { playerStats[playerid].calcPER(plLgAvgPER, true); } } }
/// <summary> /// Calculates the league PerGame. /// </summary> /// <param name="playerStats">The player stats.</param> /// <param name="teamStats">The team stats.</param> /// <returns></returns> public static PlayerStats CalculateLeagueAverages(Dictionary<int, PlayerStats> playerStats, Dictionary<int, TeamStats> teamStats) { var lps = new PlayerStats(new Player(-1, -1, "League", "Averages", Position.None, Position.None)); foreach (int key in playerStats.Keys) { lps.AddPlayerStats(playerStats[key]); } var ls = new TeamStats(-2, "League"); for (int i = 0; i < teamStats.Count; i++) { ls.AddTeamStats(teamStats[i], Span.Season); ls.AddTeamStats(teamStats[i], Span.Playoffs); } ls.CalcMetrics(ls); ls.CalcMetrics(ls, true); lps.CalcMetrics(ls, ls, ls, true); lps.CalcMetrics(ls, ls, ls, true, playoffs: true); var playerCount = (uint) playerStats.Count; for (int i = 0; i < lps.Totals.Length; i++) { lps.Totals[i] /= playerCount; lps.PlTotals[i] /= playerCount; } //ps.CalcAvg(); return lps; }
/// <summary> /// Updates the metric stats for the specified team's players. /// </summary> /// <param name="team">1 if the away team's players' metric stats should be updated; anything else for the home team.</param> private void updateMetric(int team) { var ts = new TeamStats(-1, cmbTeam1.SelectedItem.ToString()); var tsopp = new TeamStats(-1, cmbTeam2.SelectedItem.ToString()); tryParseBS(); if (!MainWindow.bs.Done) { return; } TeamBoxScore bs = MainWindow.bs; if (team == 1) { TeamStats.AddTeamStatsFromBoxScore(bs, ref ts, ref tsopp); } else { TeamStats.AddTeamStatsFromBoxScore(bs, ref tsopp, ref ts); } ts.CalcMetrics(tsopp); var pmsrList = new List<PlayerStatsRow>(); SortableBindingList<PlayerBoxScore> pbsList = team == 1 ? pbsAwayList : pbsHomeList; foreach (PlayerBoxScore pbs in pbsList) { if (pbs.PlayerID == -1) { continue; } PlayerStats ps = _pst[pbs.PlayerID].Clone(); ps.ResetStats(); ps.AddBoxScore(pbs, bs.IsPlayoff); ps.CalcMetrics(ts, tsopp, new TeamStats(-1)); pmsrList.Add(new PlayerStatsRow(ps)); } pmsrList.Sort((pmsr1, pmsr2) => pmsr1.GmSc.CompareTo(pmsr2.GmSc)); pmsrList.Reverse(); if (team == 1) { _pmsrListAway = new List<PlayerStatsRow>(pmsrList); dgvMetricAway.ItemsSource = _pmsrListAway; } else { _pmsrListHome = new List<PlayerStatsRow>(pmsrList); dgvMetricHome.ItemsSource = _pmsrListHome; } }