Skip to content

Commit

Permalink
Added helper functions to handle global variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
weakbox committed Jul 25, 2024
1 parent 9b3ba8f commit 5acf384
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1><strong>Stick & Puck Fantasy Dashboard</strong></h1>

<div class="button-container">
<button id="year-select-2024" class="year-button">2024</button>
<button id="doaktown-year-select-2024" class="year-button">2024 Doaktown League</button>
<button id="doaktown-year-select-2024" class="year-button">2025 UMHL League</button>
<button id="year-select-2025" class="year-button">2025 (Upcoming Season)</button>
</div>

Expand Down
64 changes: 45 additions & 19 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

const result = document.getElementById("result");

const scheduleCache = {};
const stickAndPuckLeagueId = "869377698";
const doaktownLeagueId = "1017266493";

setupYearSelectButton("year-select-2024", 2024, stickAndPuckLeagueId);
setupYearSelectButton("doaktown-year-select-2024", 2024, 0);
setupYearSelectButton("doaktown-year-select-2024", 2025, doaktownLeagueId);
setupYearSelectButton("year-select-2025", 2025, stickAndPuckLeagueId);

let globalYear = 2025;
let scheduleCache = {};
let leagueId = stickAndPuckLeagueId;
let globalYear = 2024;

fetchScheduleData(globalYear, leagueId);

Expand All @@ -27,58 +28,82 @@ function setupYearSelectButton(id, year, leagueId)
return;
}

button.year = year;
button.leagueId = leagueId;
button.year = year;

button.addEventListener("click", (event) =>
{
clearMatchups();

globalYear = event.target.year;
leagueId = event.target.leagueId;
changeGlobalLeagueId(event.target.leagueId);
changeGlobalYear(event.target.year);

fetchScheduleData(globalYear, leagueId);
});
}

function changeGlobalYear(newYear)
{
globalYear = newYear;
}

function changeGlobalLeagueId(newLeagueId)
{
leagueId = newLeagueId;
}

function clearMatchups()
{
result.innerHTML = "";
document.getElementById("result").innerHTML = "";
}

async function fetchScheduleData(year, leagueId)
{
const url = `https://lm-api-reads.fantasy.espn.com/apis/v3/games/fhl/seasons/${year}/segments/0/leagues/${leagueId}?view=modular&view=mNav&view=mMatchupScore&view=mScoreboard&view=mSettings&view=mTopPerformers&view=mTeam`;

// Use cache if data has already been fetched:
if (scheduleCache[`${leagueId}-${year}`])
if (fetchFromCache(leagueId, year))
{
displayScheduleData(scheduleCache[`${leagueId}-${year}`]);
displayScheduleData(fetchFromCache(leagueId, year));
return;
}

try
{
console.log(`Fetching from "${url}"`);
console.log(`Attempting fetch from: ${url}`);

const result = await fetch(url);
const data = await result.json();

scheduleCache[`${leagueId}-${year}`] = data;

console.log("Current cache:", scheduleCache);
updateCache(leagueId, year, data);

getMatchupPeriodLengths(data, year);
displayScheduleData(data);
}
catch (error)
{
console.log(error);
alert(`Something went wrong when trying to fetch the data.`);
alert(`Something went wrong when trying to fetch the data for ${leagueId} ${globalYear}.`);
}
}

// Reload the page to reset from errors:
location.reload();
function updateCache(leagueId, year, data)
{
scheduleCache[`${leagueId}-${year}`] = data;
console.log(`${leagueId}-${year}`);
console.log("Cache updated:", scheduleCache);
}

function fetchFromCache(leagueId, year)
{
const result = scheduleCache[`${leagueId}-${year}`];

if (result)
{
console.log(`Data was found in cache.`);
}
console.log(leagueId, year);
return result;
}

function displayScheduleData(data)
Expand Down Expand Up @@ -469,8 +494,9 @@ function getMatchupPeriodLengths(data, year)

matchupPeriodInfo.push(info);
}

scheduleCache[`${leagueId}-${year}`]['matchupPeriodInfo'] = matchupPeriodInfo;

console.log("For Debug:", scheduleCache);
fetchFromCache(leagueId, year)['matchupPeriodInfo'] = matchupPeriodInfo;
}

function getMinKey(keyArray)
Expand All @@ -487,7 +513,7 @@ function getMaxKey(keyArray)

function populateMissingKeysInMatchupObject(pointsByScoringPeriodObject, matchupPeriodId, year)
{
const { start, end } = scheduleCache[`${leagueId}-${year}`].matchupPeriodInfo[matchupPeriodId];
const { start, end } = fetchFromCache(leagueId, year).matchupPeriodInfo[matchupPeriodId];

for (let i = start; i <= end; i++)
{
Expand Down

0 comments on commit 5acf384

Please sign in to comment.