diff --git a/index.ts b/index.ts index ebf50cc..cc0d1d5 100644 --- a/index.ts +++ b/index.ts @@ -62,11 +62,11 @@ function fetchCache(endpoint: string) { async function getTBAEndpoint(endpoint: string): Promise { const time = Date.now(); if (tbaCache[endpoint] && tbaCache[endpoint].staleTime > time) { - console.log("Cache hit... " + endpoint); + console.error("Cache hit... " + endpoint); return { data: fetchCache(endpoint).data, status: 200 }; } - console.log("Fetching... " + endpoint); + console.error("Fetching... " + endpoint); const headers = new Headers(authHeaders); if (tbaCache[endpoint]) { headers.append("If-None-Match", tbaCache[endpoint].etag); @@ -89,7 +89,7 @@ async function getTBAEndpoint(endpoint: string): Promise { // Safety: a 304 response indicates out cache is valid based on ETag data = fetchCache(endpoint).data as string; - console.log("Unchanged... " + endpoint); + console.error("Unchanged... " + endpoint); } else { data = await response.text(); } @@ -104,7 +104,66 @@ const response = await getTBAEndpoint( "/team/frc4043/event/2024pncmp/matches/simple", ); -console.log(response.data); +const data: any = JSON.parse(response.data); + +const matchAlliances = data.map((match: any) => match.alliances); + +function filterNerdHerd(teamKeys: string[]) { + return teamKeys.filter((team) => !team.includes("4043")); +} + +const matchups = matchAlliances.map((match: any) => { + const blueAlliance = filterNerdHerd(match.blue["team_keys"]); + const redAlliance = filterNerdHerd(match.red["team_keys"]); + + if (blueAlliance.length > redAlliance.length) { + return { + with: redAlliance, + against: blueAlliance, + }; + } else { + return { + with: blueAlliance, + against: redAlliance, + }; + } +}) as { with: string[]; against: string[] }[]; + +const withOccurences = matchups.reduce( + (acc, matchup) => { + for (const team of matchup.with) { + acc[team] = acc[team] + 1 || 1; + } + + return acc; + }, + {} as { [team: string]: number }, +); + +const againstOccurences = matchups.reduce( + (acc, matchup) => { + for (const team of matchup.against) { + acc[team] = acc[team] + 1 || 1; + } + + return acc; + }, + {} as { [team: string]: number }, +); + +console.log("With:"); +for (const team of Object.entries(withOccurences).toSorted( + (a, b) => b[1] - a[1], +)) { + console.log(team); +} + +console.log("\nAgainst:"); +for (const team of Object.entries(againstOccurences).toSorted( + (a, b) => b[1] - a[1], +)) { + console.log(team); +} writeCache();