mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-22 03:07:04 +02:00
feat: data driven latest archived season number
This commit is contained in:
1 parent
d1ebb79432
commit
f02db2a2d8
13 files changed
+145
-96
No files matched your search
@@ -1,8 +0,0 @@
|
||||
/**
|
||||
* The first every season that Blitzkrieg tracked :)
|
||||
*/
|
||||
export const FIRST_ARCHIVED_RATINGS_SEASON = 49;
|
||||
|
||||
export async function getLatestArchivedSeasonNumber() {
|
||||
return 51;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
type Context = 'website' | 'bot';
|
||||
type Context = 'website' | 'server';
|
||||
|
||||
export const context: Context =
|
||||
typeof window === 'undefined' ? 'bot' : 'website';
|
||||
typeof window === 'undefined' ? 'server' : 'website';
|
||||
@@ -0,0 +1,25 @@
|
||||
import { DATABASE_REPO } from './getArchivedRatingsMidnightLeaderboard';
|
||||
import { octokit } from './octokit';
|
||||
|
||||
let cachedLatestArchivedSeasonNumber: number | null = null;
|
||||
|
||||
export async function getArchivedLatestSeasonNumber() {
|
||||
if (cachedLatestArchivedSeasonNumber) {
|
||||
return cachedLatestArchivedSeasonNumber;
|
||||
}
|
||||
|
||||
const { data } = await octokit.repos.getContent({
|
||||
...DATABASE_REPO,
|
||||
path: 'com/ratings',
|
||||
});
|
||||
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error('Archived ratings data is malformed');
|
||||
}
|
||||
|
||||
cachedLatestArchivedSeasonNumber = Math.max(
|
||||
...data.map(({ name }) => parseInt(name)),
|
||||
);
|
||||
|
||||
return cachedLatestArchivedSeasonNumber;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
let cachedLatestArchivedSeasonNumber: number | null = null;
|
||||
|
||||
/**
|
||||
* The first every season that Blitzkrieg tracked :)
|
||||
*/
|
||||
export const FIRST_ARCHIVED_RATINGS_SEASON = 49;
|
||||
|
||||
export async function getArchivedLatestSeasonNumberAPI() {
|
||||
if (cachedLatestArchivedSeasonNumber) {
|
||||
return cachedLatestArchivedSeasonNumber;
|
||||
}
|
||||
|
||||
const response = await fetch(`/api/ratings/latest-archived-season-number`);
|
||||
const number = parseInt(await response.text());
|
||||
cachedLatestArchivedSeasonNumber = number;
|
||||
|
||||
return cachedLatestArchivedSeasonNumber;
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import { RatingsInfo } from '../../commands/ratings';
|
||||
import { Region } from '../../constants/regions';
|
||||
import { DATABASE_REPO } from './getMidnightLeaderboard';
|
||||
import { DATABASE_REPO } from './getArchivedRatingsMidnightLeaderboard';
|
||||
import { octokit } from './octokit';
|
||||
|
||||
const ARCHIVED_RATINGS_CACHE: Record<
|
||||
const INFO_CACHE: Record<
|
||||
Region,
|
||||
Record<
|
||||
number,
|
||||
@@ -18,11 +18,11 @@ const ARCHIVED_RATINGS_CACHE: Record<
|
||||
};
|
||||
|
||||
export default async function getArchivedRatingsInfo(
|
||||
season: number,
|
||||
region: Region,
|
||||
season: number,
|
||||
) {
|
||||
if (ARCHIVED_RATINGS_CACHE[region][season]) {
|
||||
return ARCHIVED_RATINGS_CACHE[region][season];
|
||||
if (INFO_CACHE[region][season]) {
|
||||
return INFO_CACHE[region][season];
|
||||
}
|
||||
|
||||
const { data } = await octokit.repos.getContent({
|
||||
@@ -38,7 +38,7 @@ export default async function getArchivedRatingsInfo(
|
||||
detail: undefined;
|
||||
};
|
||||
|
||||
ARCHIVED_RATINGS_CACHE[region][season] = jsonContent;
|
||||
INFO_CACHE[region][season] = jsonContent;
|
||||
|
||||
return jsonContent;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { BlitzkriegRatingsLeaderboard } from '../../../scripts/buildRatingsLeaderboard';
|
||||
import { Region } from '../../constants/regions';
|
||||
import { DATABASE_REPO } from './getArchivedRatingsMidnightLeaderboard';
|
||||
import { octokit } from './octokit';
|
||||
|
||||
const LEADERBOARD_CACHE: Record<
|
||||
Region,
|
||||
Record<number, BlitzkriegRatingsLeaderboard>
|
||||
> = {
|
||||
asia: {},
|
||||
eu: {},
|
||||
com: {},
|
||||
};
|
||||
|
||||
export async function getArchivedRatingsLeaderboard(
|
||||
region: Region,
|
||||
season: number,
|
||||
) {
|
||||
if (LEADERBOARD_CACHE[region][season]) {
|
||||
return LEADERBOARD_CACHE[region][season];
|
||||
}
|
||||
|
||||
const { data } = await octokit.repos.getContent({
|
||||
...DATABASE_REPO,
|
||||
path: `${region}/ratings/${season}/latest.json`,
|
||||
});
|
||||
|
||||
if (Array.isArray(data) || data.type !== 'file') {
|
||||
throw new Error('Archived ratings data is malformed');
|
||||
}
|
||||
|
||||
const response = await fetch(data.download_url!);
|
||||
const json = (await response.json()) as BlitzkriegRatingsLeaderboard;
|
||||
|
||||
LEADERBOARD_CACHE[region][season] = json;
|
||||
|
||||
return json;
|
||||
}
|
||||
+1
-1
@@ -13,7 +13,7 @@ const MIDNIGHT_LEADERBOARD_CACHE: Record<
|
||||
asia: {},
|
||||
};
|
||||
|
||||
export default async function getMidnightLeaderboard(
|
||||
export default async function getArchivedRatingsMidnightLeaderboard(
|
||||
region: Region,
|
||||
season: number,
|
||||
) {
|
||||
@@ -90,7 +90,7 @@ export const tankAverages = fetch(
|
||||
)
|
||||
.then((response) => response.json())
|
||||
.then((json) => {
|
||||
if (context === 'bot') {
|
||||
if (context === 'server') {
|
||||
const partialTankAverages: TankAverages = {};
|
||||
|
||||
(json as IndividualTankAverage[]).forEach((individualTankAverage) => {
|
||||
|
||||
@@ -23,7 +23,7 @@ export const tankopedia = fetch(
|
||||
)
|
||||
.then(async (response) => response.json())
|
||||
.then((wrapperTankopedia) =>
|
||||
context === 'bot'
|
||||
context === 'server'
|
||||
? (wrapperTankopedia as { data: Tankopedia }).data
|
||||
: (wrapperTankopedia as Tankopedia),
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user