@@ -30,69 +30,84 @@ statsPeriods.forEach(periodInfo => {
3030 statsData[periodInfo .period ] = periodInfo .data ;
3131});
3232
33- // Calculate period-by-period stats (current period - previous period)
34- const playerStats = {};
35-
36- // Get all available periods
33+ // Get all available periods for the selector
3734const availablePeriods = Object .keys (statsData).map (Number ).sort ((a , b ) => a - b);
35+ const periodOptions = [" Overall" , ... availablePeriods .map (p => ` Period ${ p} ` )];
3836
39- availablePeriods .forEach (period => {
40- const currentPeriodData = statsData[period];
41- const previousPeriodData = period > 1 ? statsData[period - 1 ] : null ;
42-
43- // Create a map of previous period stats for quick lookup
44- const previousStats = {};
45- if (previousPeriodData) {
46- previousPeriodData .forEach (stat => {
47- previousStats[stat .hockeyRef ] = stat;
48- });
49- }
37+ // Get unique teams for the selector
38+ const teams = teamInfo .map (team => team .ABBR ).sort ();
39+ ```
40+
41+ ``` js
42+ const teamSelector = Inputs .select (teams, {label: " Select Team:" , value: teams[0 ]});
43+ const selectedTeam = Generators .input (teamSelector);
44+
45+ const periodSelector = Inputs .select (periodOptions, {label: " Select Period:" , value: " Overall" });
46+ const selectedPeriod = Generators .input (periodSelector);
47+ ```
48+
49+ ${teamSelector}
50+ ${periodSelector}
51+
52+ ``` js
53+ // Calculate stats for the selected period
54+ const playerStats = {};
55+
56+ if (selectedPeriod === " Overall" ) {
57+ // Show cumulative stats from the latest period (no subtraction)
58+ const latestPeriod = Math .max (... availablePeriods);
59+ const latestPeriodData = statsData[latestPeriod];
5060
51- // Process current period data
52- currentPeriodData .forEach (currentStat => {
53- const id = currentStat .hockeyRef ;
54- const prevStat = previousStats[id];
55-
56- // Calculate period stats (current - previous, or current if no previous)
57- const periodStats = {
61+ latestPeriodData .forEach (stat => {
62+ const id = stat .hockeyRef ;
63+ playerStats[id] = {
5864 hockeyRef: id,
59- team: currentStat .team ,
60- pos: currentStat .pos ,
61- goals: (currentStat [" stats/goals" ] || 0 ) - (prevStat ? (prevStat[ " stats/goals " ] || 0 ) : 0 ) ,
62- assists: (currentStat [" stats/assists" ] || 0 ) - (prevStat ? (prevStat[ " stats/assists " ] || 0 ) : 0 ) ,
63- toughness: (currentStat [" stats/toughness" ] || 0 ) - (prevStat ? (prevStat[ " stats/toughness " ] || 0 ) : 0 ) ,
64- dstat: (currentStat [" stats/dstat" ] || 0 ) - (prevStat ? (prevStat[ " stats/dstat " ] || 0 ) : 0 ) ,
65- gstat: (currentStat [" stats/gstat" ] || 0 ) - (prevStat ? (prevStat[ " stats/gstat " ] || 0 ) : 0 ) ,
66- games_played: (currentStat [" stats/gp" ] || 0 ) - (prevStat ? (prevStat[ " stats/gp " ] || 0 ) : 0 )
65+ team: stat .team ,
66+ pos: stat .pos ,
67+ goals: stat [" stats/goals" ] || 0 ,
68+ assists: stat [" stats/assists" ] || 0 ,
69+ toughness: stat [" stats/toughness" ] || 0 ,
70+ dstat: stat [" stats/dstat" ] || 0 ,
71+ gstat: stat [" stats/gstat" ] || 0 ,
72+ games_played: stat [" stats/gp" ] || 0
6773 };
74+ });
75+ } else {
76+ // Show period-specific stats (current - previous)
77+ const selectedPeriodNum = parseInt (selectedPeriod .replace (' Period ' , ' ' ));
78+
79+ if (availablePeriods .includes (selectedPeriodNum)) {
80+ const currentPeriodData = statsData[selectedPeriodNum];
81+ const previousPeriodData = selectedPeriodNum > 1 ? statsData[selectedPeriodNum - 1 ] : null ;
6882
69- // Add to or update player's total stats
70- if (! playerStats[id]) {
83+ // Create a map of previous period stats for quick lookup
84+ const previousStats = {};
85+ if (previousPeriodData) {
86+ previousPeriodData .forEach (stat => {
87+ previousStats[stat .hockeyRef ] = stat;
88+ });
89+ }
90+
91+ // Process current period data to get the difference
92+ currentPeriodData .forEach (currentStat => {
93+ const id = currentStat .hockeyRef ;
94+ const prevStat = previousStats[id];
95+
96+ // Calculate period stats (current - previous, or current if no previous)
7197 playerStats[id] = {
7298 hockeyRef: id,
73- team: periodStats .team ,
74- pos: periodStats .pos ,
75- goals: 0 ,
76- assists: 0 ,
77- toughness: 0 ,
78- dstat: 0 ,
79- gstat: 0 ,
80- games_played: 0
99+ team: currentStat .team ,
100+ pos: currentStat .pos ,
101+ goals: (currentStat[ " stats/goals " ] || 0 ) - (prevStat ? (prevStat[ " stats/goals " ] || 0 ) : 0 ) ,
102+ assists: (currentStat[ " stats/assists " ] || 0 ) - (prevStat ? (prevStat[ " stats/assists " ] || 0 ) : 0 ) ,
103+ toughness: (currentStat[ " stats/toughness " ] || 0 ) - (prevStat ? (prevStat[ " stats/toughness " ] || 0 ) : 0 ) ,
104+ dstat: (currentStat[ " stats/dstat " ] || 0 ) - (prevStat ? (prevStat[ " stats/dstat " ] || 0 ) : 0 ) ,
105+ gstat: (currentStat[ " stats/gstat " ] || 0 ) - (prevStat ? (prevStat[ " stats/gstat " ] || 0 ) : 0 ) ,
106+ games_played: (currentStat[ " stats/gp " ] || 0 ) - (prevStat ? (prevStat[ " stats/gp " ] || 0 ) : 0 )
81107 };
82- }
83-
84- // Add this period's stats to the total
85- playerStats[id].goals += periodStats .goals ;
86- playerStats[id].assists += periodStats .assists ;
87- playerStats[id].toughness += periodStats .toughness ;
88- playerStats[id].dstat += periodStats .dstat ;
89- playerStats[id].gstat += periodStats .gstat ;
90- playerStats[id].games_played += periodStats .games_played ;
91-
92- // Update team info in case player was traded
93- playerStats[id].team = periodStats .team ;
94- });
95- });
108+ });
109+ }
110+ }
96111
97112// Convert back to array
98113const combinedStats = Object .values (playerStats);
@@ -175,19 +190,7 @@ const playerStatsData = playerInfo.map(player => {
175190 };
176191}).filter (player => player .Team !== " N/A" );
177192
178- // Get unique teams for the selector
179- const teams = teamInfo .map (team => team .ABBR ).sort ();
180- ```
181-
182- ``` js
183- const teamSelector = Inputs .select (teams, {label: " Select Team:" , value: teams[0 ]});
184- const selectedTeam = Generators .input (teamSelector);
185- ```
186-
187- ${teamSelector}
188-
189- ``` js
190- // Filter data based on selected team
193+ // Filter data based on selected team and sort
191194const filteredContractData = playerContractData .filter (player => player .Team === selectedTeam);
192195const filteredRosterData = playerRosterData
193196 .filter (player => player .Team === selectedTeam)
0 commit comments