Skip to content

Commit ed0e164

Browse files
committed
Get latest version always
1 parent 629c74c commit ed0e164

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

src/services/CurrentTeams.tsx

+32-14
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ async function getCurrentKHLTeams(): Promise<TeamInfo> {
2525
return teams;
2626
}
2727

28-
async function getFiveVFiveSeasons(): Promise<Array<{ name: string; id: number }>> {
28+
async function getFiveVFiveSeasons(version: string): Promise<Array<{ name: string; id: number }>> {
2929
const ans = (
30-
(await get(`https://snokinghockeyleague.com/api/season/all/0?v=1021270`)) as {
30+
(await get(`https://snokinghockeyleague.com/api/season/all/0?v=${version}`)) as {
3131
seasons: [
3232
{
3333
name: string;
@@ -56,9 +56,17 @@ async function getFiveVFiveSeasons(): Promise<Array<{ name: string; id: number }
5656
return ans;
5757
}
5858

59-
async function getFiveVFiveCurrentTeams({ name, id }: { name: string; id: number }): Promise<TeamInfo> {
59+
async function getFiveVFiveCurrentTeams({
60+
name,
61+
id,
62+
version,
63+
}: {
64+
name: string;
65+
id: number;
66+
version: string;
67+
}): Promise<TeamInfo> {
6068
return (
61-
(await get(`https://snokinghockeyleague.com/api/team/list/${id}/0?v=1021270`)) as [
69+
(await get(`https://snokinghockeyleague.com/api/team/list/${id}/0?v=${version}`)) as [
6270
{
6371
name: string;
6472
divisionName: string;
@@ -74,9 +82,9 @@ async function getFiveVFiveCurrentTeams({ name, id }: { name: string; id: number
7482
}));
7583
}
7684

77-
async function getPondSeasons(): Promise<Array<{ name: string; id: number }>> {
85+
async function getPondSeasons(version: string): Promise<Array<{ name: string; id: number }>> {
7886
return (
79-
(await get(`https://snokingpondhockey.com/api/season/all/0?v=1021270`)) as {
87+
(await get(`https://snokingpondhockey.com/api/season/all/0?v=${version}`)) as {
8088
seasons: [{ name: string; id: number }];
8189
}
8290
).seasons.map((x: { name: string; id: number }) => ({
@@ -85,13 +93,13 @@ async function getPondSeasons(): Promise<Array<{ name: string; id: number }>> {
8593
}));
8694
}
8795

88-
async function getPondSeasonCurrentTeams(): Promise<
89-
Array<{ name: string; snokingUrl: string; teamId: string; isSnoking: boolean }>
90-
> {
91-
const { id, name: seasonName } = (await getPondSeasons())[0];
96+
async function getPondSeasonCurrentTeams(
97+
version: string
98+
): Promise<Array<{ name: string; snokingUrl: string; teamId: string; isSnoking: boolean }>> {
99+
const { id, name: seasonName } = (await getPondSeasons(version))[0];
92100

93101
return (
94-
(await get(`https://snokingpondhockey.com/api/team/list/${id}/0?v=1021270`)) as [
102+
(await get(`https://snokingpondhockey.com/api/team/list/${id}/0?v=${version}`)) as [
95103
{ name: string; divisionName: string; teamId: string; seasonId: string }
96104
]
97105
).map((x) => {
@@ -108,6 +116,16 @@ export async function getCurrentTeams(): Promise<{
108116
teams: TeamInfo;
109117
errors: ReactElement[];
110118
}> {
119+
const main = (await get("https://snokinghockeyleague.com/")) as string;
120+
const match = main.match(/meta name=\"version\"\s+content=\"(\d+)\"/);
121+
if (!match || match.length < 2) {
122+
return {
123+
teams: [],
124+
errors: [<>Data is not currently available. Please try again later if you require data for that league.</>],
125+
};
126+
}
127+
128+
const version = match[1];
111129
const safelyGetTeams = async (getter: () => Promise<TeamInfo>, errorMessage: ReactElement) => {
112130
try {
113131
const data = await getter();
@@ -128,12 +146,12 @@ export async function getCurrentTeams(): Promise<{
128146
// we now just show every team that is in a season for this calendar year. This means that we'll at some point have 3 seasons showing
129147
// for the main SKAHL league, but that's less interruptive then new seasons breaking our heuristic.
130148
const currentYear = new Date().getFullYear().toString();
131-
const allSKAHLTeamsSeasons = await getFiveVFiveSeasons();
149+
const allSKAHLTeamsSeasons = await getFiveVFiveSeasons(version);
132150
const allCurrentSKAHLSeasons = allSKAHLTeamsSeasons.filter((season) => season.name.indexOf(currentYear) >= 0);
133151

134152
const dataForNonSKAHLSite = [
135153
safelyGetTeams(
136-
() => getPondSeasonCurrentTeams(),
154+
() => getPondSeasonCurrentTeams(version),
137155
<>
138156
<b>SKAHL Pond</b> data is not currently available. Please try again later if you require data for that
139157
league.
@@ -151,7 +169,7 @@ export async function getCurrentTeams(): Promise<{
151169

152170
const dataForSKAHLSite = allCurrentSKAHLSeasons.map((season) =>
153171
safelyGetTeams(
154-
() => getFiveVFiveCurrentTeams(season),
172+
() => getFiveVFiveCurrentTeams({ ...season, version }),
155173
<>
156174
<b>{season.name}</b> data is not currently available. Please try again later if you require data for
157175
that league.

0 commit comments

Comments
 (0)