let allTimePOMatchupDifferentials = []; let playoffRosterRecords = {}; let playoffWeekRecords = []; let leaguePOWeekRecords = []; let mostPlayoffLongPoints = []; __________ // variables for playoff records let numPOTeams = parseInt(leagueData.settings.playoff_teams); let playoffStart = parseInt(leagueData.settings.playoff_week_start); let playoffLength; let playoffType; let playoffCase; // for determining relevant (ie. PO bracket) matches // before 2020, 1 week/round was only option; in 2020, 2 weeks/rounds added; in 2021, 1 week/round + 2 champ if(year > 2019) { playoffType = parseInt(leagueData.settings.playoff_round_type); } else { playoffType = 0; } // set length of playoffs if(playoffType == 0) { if(numPOTeams == 6) { playoffLength = 3; playoffCase = 1; } else if(numPOTeams == 8) { playoffLength = 3; playoffCase = 2; } else if(numPOTeams == 4) { playoffLength = 2; playoffCase = 3; } } else if(playoffType == 1 && year > 2020) { if(numPOTeams == 6) { playoffLength = 4; playoffCase = 4; } else if(numPOTeams == 8) { playoffLength = 4; playoffCase = 5 } else if(numPOTeams == 4) { playoffLength = 3; playoffCase = 6; } } else if(playoffType == 2 || playoffType == 1 && year == 2020) { if(numPOTeams == 6) { playoffLength = 6; playoffCase = 7; } else if (numPOTeams == 8) { playoffLength = 6; playoffCase = 8; } else if (numPOTeams == 4) { playoffLength = 4; playoffCase = 9; } } POrecordsWeek = playoffStart + playoffLength - 1; __________ if(!playoffRosterRecords[recordManID]) { playoffRosterRecords[recordManID] = { wins: 0, losses: 0, ties: 0, fptsFor: 0, fptsAgainst: 0, potentialPoints: 0, fptspg: 0, POgames: 0, manager: originalManagers[recordManID], years: {}, recordManID, } } playoffRosterRecords[recordManID].years[year] = { wins: 0, losses: 0, ties: 0, fpts: 0, fptsAgainst: 0, potentialPoints: 0, fptspg: 0, POgames: 0, manager: originalManagers[recordManID], year, recordManID, } __________ const POmatchupsPromises = []; let POstartWeek = parseInt(POrecordsWeek); while(POrecordsWeek > playoffStart - 1) { POmatchupsPromises.push(fetch(`https://api.sleeper.app/v1/league/${curSeason}/matchups/${POrecordsWeek}`, {compress: true})) POrecordsWeek--; } const POmatchupsRes = await waitForAll(...POmatchupsPromises).catch((err) => { console.error(err); }); const POmatchupsJsonPromises = []; for(const POmatchupRes of POmatchupsRes) { const POdata = POmatchupRes.json(); POmatchupsJsonPromises.push(POdata) if (!POmatchupRes.ok) { throw new Error(POdata); } } const POmatchupsData = await waitForAll(...POmatchupsJsonPromises).catch((err) => { console.error(err); }); __________ const playoffPointsRecord = []; let POmatchupDifferentials = []; if(startWeek > playoffStart - 1 || leagueData.status == 'complete') { // process all the PLAYOFFS matchups for(const POmatchupWeek of POmatchupsData) { let POmatchups = {}; let POround = POstartWeek - POrecordsWeek; const POentry = { manager: originalManagers[recordManID], fpts: POmatchup.points, week: POstartWeek, year, rosterID: POmatchup.roster_id, recordManID, } // add each entry to the POmatchup object if(!POmatchups[POmatchup.matchup_id]) { POmatchups[POmatchup.matchup_id] = []; } POmatchups[POmatchup.matchup_id].push(POentry); } if(playoffCase == 4 && POstartWeek == POrecordsWeek + playoffLength || // Relevant Match IDs: 1 playoffCase == 5 && POstartWeek == POrecordsWeek + playoffLength || playoffCase == 6 && POstartWeek == POrecordsWeek + playoffLength) { const champMatch = POmatchups[1]; let home = champMatch[0]; let away = champMatch[1]; if(champMatch[0].fpts < champMatch[1].fpts) { home = champMatch[1]; away = champMatch[0]; } const POmatchupDifferential = { year: home.year, week: home.week, home: { manager: home.manager, fpts: home.fpts, recordManID: home.recordManID, }, away: { manager: away.manager, fpts: away.fpts, recordManID: away.recordManID, }, differential: home.fpts - away.fpts } allTimePOMatchupDifferentials.push(POmatchupDifferential); POmatchupDifferentials.push(POmatchupDifferential); for(const key in champMatch) { const opponent = champMatch[key]; playoffRosterRecords[opponent.recordManID].years[year].fpts += opponent.fpts; playoffRosterRecords[opponent.recordManID].years[year].POgames++; const POweekEntry = { manager: opponent.manager, recordManID: opponent.recordManID, rosterID: opponent.rosterID, fpts: opponent.fpts, week: opponent.week, year, } playoffPointsRecord.push(POweekEntry); leaguePOWeekRecords.push(POweekEntry); } playoffRosterRecords[home.recordManID].years[year].fptsAgainst += away.fpts; playoffRosterRecords[away.recordManID].years[year].fptsAgainst += home.fpts; if(POmatchupDifferential.differential == 0) { playoffRosterRecords[home.recordManID].years[year].ties++; playoffRosterRecords[away.recordManID].years[year].ties++; } else { playoffRosterRecords[home.recordManID].years[year].wins++; playoffRosterRecords[away.recordManID].years[year].losses++; } } else if(playoffCase == 3 || // Relevant Match IDs: 1, 2 playoffCase == 9 || playoffCase == 6 && POstartWeek < POrecordsWeek + playoffLength || playoffCase == 1 && POround != 2 || playoffCase == 4 && POround == 1 || playoffCase == 4 && POround == 3 || playoffCase == 7 && POround < 3 || playoffCase == 7 && POround > 4) { for(let i = 1; i < 3; i++) { let home = POmatchups[i][0]; let away = POmatchups[i][1]; if(POmatchups[i][0].fpts < POmatchups[i][1].fpts) { home = POmatchups[i][1]; away = POmatchups[i][0]; } const POmatchupDifferential = { year: home.year, week: home.week, home: { manager: home.manager, fpts: home.fpts, recordManID: home.recordManID, }, away: { manager: away.manager, fpts: away.fpts, recordManID: away.recordManID, }, differential: home.fpts - away.fpts } allTimePOMatchupDifferentials.push(POmatchupDifferential); POmatchupDifferentials.push(POmatchupDifferential); for(const key in POmatchups[i]) { const opponent = POmatchups[i][key]; playoffRosterRecords[opponent.recordManID].years[year].fpts += opponent.fpts; playoffRosterRecords[opponent.recordManID].years[year].POgames++; const POweekEntry = { manager: opponent.manager, recordManID: opponent.recordManID, rosterID: opponent.rosterID, fpts: opponent.fpts, week: opponent.week, year, } playoffPointsRecord.push(POweekEntry); leaguePOWeekRecords.push(POweekEntry); } playoffRosterRecords[home.recordManID].years[year].fptsAgainst += away.fpts; playoffRosterRecords[away.recordManID].years[year].fptsAgainst += home.fpts; if(POmatchupDifferential.differential == 0) { playoffRosterRecords[home.recordManID].years[year].ties++; playoffRosterRecords[away.recordManID].years[year].ties++; } else { playoffRosterRecords[home.recordManID].years[year].wins++; playoffRosterRecords[away.recordManID].years[year].losses++; } } } else if(playoffCase == 7 && 2 < POround < 5 || // Relevant Match IDs: 1, 2, 3 playoffCase == 4 && POround == 2 || playoffCase == 1 && POround == 2) { for(let i = 1; i < 4; i++) { let home = POmatchups[i][0]; let away = POmatchups[i][1]; if(POmatchups[i][0].fpts < POmatchups[i][1].fpts) { home = POmatchups[i][1]; away = POmatchups[i][0]; } const POmatchupDifferential = { year: home.year, week: home.week, home: { manager: home.manager, fpts: home.fpts, recordManID: home.recordManID, }, away: { manager: away.manager, fpts: away.fpts, recordManID: away.recordManID, }, differential: home.fpts - away.fpts } allTimePOMatchupDifferentials.push(POmatchupDifferential); POmatchupDifferentials.push(POmatchupDifferential); for(const key in POmatchups[i]) { const opponent = POmatchups[i][key]; playoffRosterRecords[opponent.recordManID].years[year].fpts += opponent.fpts; playoffRosterRecords[opponent.recordManID].years[year].POgames++; const POweekEntry = { manager: opponent.manager, recordManID: opponent.recordManID, rosterID: opponent.rosterID, fpts: opponent.fpts, week: opponent.week, year, } playoffPointsRecord.push(POweekEntry); leaguePOWeekRecords.push(POweekEntry); } playoffRosterRecords[home.recordManID].years[year].fptsAgainst += away.fpts; playoffRosterRecords[away.recordManID].years[year].fptsAgainst += home.fpts; if(POmatchupDifferential.differential == 0) { playoffRosterRecords[home.recordManID].years[year].ties++; playoffRosterRecords[away.recordManID].years[year].ties++; } else { playoffRosterRecords[home.recordManID].years[year].wins++; playoffRosterRecords[away.recordManID].years[year].losses++; } } } else if(playoffCase == 8 || // Relevant Match IDs: 1, 2, 3, 4 playoffCase == 2 || playoffCase == 5 && POstartWeek < POrecordsWeek + playoffLength) { for(let i = 1; i < 5; i++) { let home = POmatchups[i][0]; let away = POmatchups[i][1]; if(POmatchups[i][0].fpts < POmatchups[i][1].fpts) { home = POmatchups[i][1]; away = POmatchups[i][0]; } const POmatchupDifferential = { year: home.year, week: home.week, home: { manager: home.manager, fpts: home.fpts, recordManID: home.recordManID, }, away: { manager: away.manager, fpts: away.fpts, recordManID: away.recordManID, }, differential: home.fpts - away.fpts } allTimePOMatchupDifferentials.push(POmatchupDifferential); POmatchupDifferentials.push(POmatchupDifferential); for(const key in POmatchups[i]) { const opponent = POmatchups[i][key]; playoffRosterRecords[opponent.recordManID].years[year].fpts += opponent.fpts; playoffRosterRecords[opponent.recordManID].years[year].POgames++; const POweekEntry = { manager: opponent.manager, recordManID: opponent.recordManID, rosterID: opponent.rosterID, fpts: opponent.fpts, week: opponent.week, year, } playoffPointsRecord.push(POweekEntry); leaguePOWeekRecords.push(POweekEntry); } playoffRosterRecords[home.recordManID].years[year].fptsAgainst += away.fpts; playoffRosterRecords[away.recordManID].years[year].fptsAgainst += home.fpts; if(POmatchupDifferential.differential == 0) { playoffRosterRecords[home.recordManID].years[year].ties++; playoffRosterRecords[away.recordManID].years[year].ties++; } else { playoffRosterRecords[home.recordManID].years[year].wins++; playoffRosterRecords[away.recordManID].years[year].losses++; } } } POstartWeek--; } for(const recordManID in playoffRosterRecords) { const playoffRosterRecord = playoffRosterRecords[recordManID]; if(playoffRosterRecord.years[year] && playoffRosterRecord.years[year].POgames > 0) { playoffRosterRecord.fptsFor += playoffRosterRecord.years[year].fpts; playoffRosterRecord.fptsAgainst += playoffRosterRecord.years[year].fptsAgainst; playoffRosterRecord.wins += playoffRosterRecord.years[year].wins; playoffRosterRecord.ties += playoffRosterRecord.years[year].ties; playoffRosterRecord.losses += playoffRosterRecord.years[year].losses; playoffRosterRecord.POgames += playoffRosterRecord.years[year].POgames; playoffRosterRecord.potentialPoints += playoffRosterRecord.years[year].potentialPoints; const fptspg = playoffRosterRecord.years[year].fpts / playoffRosterRecord.years[year].POgames; const POlongEntry = { recordManID, fpts: playoffRosterRecord.years[year].fpts, fptspg, manager: playoffRosterRecord.years[year].manager, year, } mostPlayoffLongPoints.push(POlongEntry); } else { continue; } } POmatchupDifferentials = POmatchupDifferentials.sort((a, b) => b.differential - a.differential); const biggestPOBlowouts = POmatchupDifferentials.slice(0, 10); const closestPOMatchups = []; for(let i = 0; i < 10; i++) { closestPOMatchups.push(POmatchupDifferentials.pop()); } // per-season ranks & records to push thru seasonWeekRecords const interSeasonPOEntry = { year, biggestPOBlowouts, closestPOMatchups, playoffPointsRecords: playoffPointsRecord.sort((a, b) => b.fpts - a.fpts).slice(0, 10), } if(interSeasonPOEntry.playoffPointsRecords.length > 0) { if(!currentYear) { currentYear = year; } playoffWeekRecords.push(interSeasonPOEntry); }; } __________ // Sorting - PLAYOFFS allTimePOMatchupDifferentials = allTimePOMatchupDifferentials.sort((a, b) => b.differential - a.differential); allTimeBiggestPOBlowouts = allTimePOMatchupDifferentials.slice(0, 10); for(let i = 0; i < 10; i++) { allTimeClosestPOMatchups.push(allTimePOMatchupDifferentials.pop()); } leaguePOWeekRecords = leaguePOWeekRecords.sort((a, b) => b.fpts - a.fpts).slice(0, 10); mostPlayoffLongPoints = mostPlayoffLongPoints.sort((a, b) => b.fptspg - a.fptspg).slice(0, 10); // sorting by ppg makes more sense (more accurately reflects top playoff-long performances) due to bye weeks __________ // const recordsData = {..... playoffWeekRecords, playoffRosterRecords, leaguePOWeekRecords, allTimeBiggestPOBlowouts, allTimeClosestPOMatchups, mostPlayoffLongPoints, ____________________________________________________________________________________________________________________________________________ //RECORDMANID stuff is NOT relevant to Playoffs stats //First I added variables to each managers info for "managerID" (1, 2, 3...) and "yearsactive" (2019, 2020, 2021...) // vvvvvv import { leagueID, managers } from '$lib/utils/leagueInfo'; let leagueManagers = {}; let activeManagers = []; for(const managerID in managers) { const manager = managers[managerID]; const entryMan = { managerID: manager.managerID, rosterID: manager.roster, name: manager.name, status: manager.status, yearsactive: manager.yearsactive, } if(!leagueManagers[manager.roster]) { leagueManagers[manager.roster] = []; } leagueManagers[manager.roster].push(entryMan); if(manager.status == "active") { activeManagers.push(manager.managerID); } } __________ //RECORDMANID stuff is NOT relevant to Playoffs stats for(const POmatchup of POmatchupWeek) { let recordManager = leagueManagers[POmatchup.roster_id].filter(m => m.yearsactive.includes(year)); let recordManID = recordManager[0].managerID; __________