mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-22 11:17:05 +02:00
114 lines
3.3 KiB
TypeScript
114 lines
3.3 KiB
TypeScript
import { Region } from '../../constants/regions';
|
|
import {
|
|
GetHistoriesOptions,
|
|
Histories,
|
|
History,
|
|
getHistoriesDefaultOptions,
|
|
} from '../../types/histories';
|
|
import getTankStats from '../blitz/getTankStats';
|
|
import { emptyAllStats } from '../blitzstars/getStatsInPeriod';
|
|
|
|
export interface TankHistory extends History {
|
|
tank_id: number;
|
|
}
|
|
|
|
export type TankHistories = TankHistory[];
|
|
|
|
export interface TankHistoryRaw extends TankHistory {
|
|
mark_of_mastery: number;
|
|
battle_life_time: number;
|
|
account_id: number;
|
|
}
|
|
|
|
export type TankHistoriesRaw = TankHistoryRaw[];
|
|
|
|
export interface GetTankHistoriesOptions extends GetHistoriesOptions {
|
|
tankId?: number;
|
|
}
|
|
|
|
export const getTankHistoriesDefaultOptions: GetTankHistoriesOptions = {
|
|
...getHistoriesDefaultOptions,
|
|
tankId: undefined,
|
|
};
|
|
|
|
export default async function getTankHistories(
|
|
server: Region,
|
|
id: number,
|
|
options?: Partial<GetTankHistoriesOptions>,
|
|
) {
|
|
const mergedOptions = { ...getTankHistoriesDefaultOptions, ...options };
|
|
const tankHistoriesResponse = await fetch(
|
|
`https://www.blitzstars.com/api/tankhistories/for/${id}/`,
|
|
);
|
|
const tankHistories = (
|
|
(await tankHistoriesResponse.json()) as TankHistoriesRaw
|
|
).map(
|
|
(history) =>
|
|
({
|
|
all: history.all,
|
|
last_battle_time: history.last_battle_time,
|
|
tank_id: history.tank_id,
|
|
}) satisfies TankHistory,
|
|
);
|
|
const compliantTanks: number[] = [];
|
|
const lastBattles: Record<number, number> = {};
|
|
const lastNonCompliantIndex: Record<number, number> = {};
|
|
const hasCompliedOnce: Record<number, boolean> = {};
|
|
const filtered = tankHistories.filter(
|
|
({ last_battle_time, tank_id, all: { battles } }, index) => {
|
|
const complies =
|
|
last_battle_time >= mergedOptions.start &&
|
|
last_battle_time <= mergedOptions.end &&
|
|
(mergedOptions.tankId === undefined ||
|
|
tank_id === mergedOptions.tankId);
|
|
|
|
if (complies && !hasCompliedOnce[tank_id]) {
|
|
hasCompliedOnce[tank_id] = true;
|
|
compliantTanks.push(tank_id);
|
|
}
|
|
if (!hasCompliedOnce[tank_id]) lastNonCompliantIndex[tank_id] = index;
|
|
lastBattles[tank_id] = battles;
|
|
|
|
return complies;
|
|
},
|
|
);
|
|
|
|
const latestNodes: TankHistories = [];
|
|
|
|
if (mergedOptions.includeLatestHistories) {
|
|
(await getTankStats(server, id)).forEach((tankHistory) => {
|
|
if (
|
|
tankHistory.all.battles > (lastBattles[tankHistory.tank_id] ?? 0) &&
|
|
(mergedOptions.tankId === undefined ||
|
|
tankHistory.tank_id === mergedOptions.tankId)
|
|
) {
|
|
if (!hasCompliedOnce[tankHistory.tank_id]) {
|
|
// was played today for the first time... I think???
|
|
hasCompliedOnce[tankHistory.tank_id] = true;
|
|
compliantTanks.push(tankHistory.tank_id);
|
|
}
|
|
latestNodes.push(tankHistory);
|
|
}
|
|
});
|
|
}
|
|
|
|
const previousNodes: TankHistories = [];
|
|
|
|
if (mergedOptions.includePreviousHistories) {
|
|
compliantTanks.forEach((tankId) => {
|
|
if (tankId in lastNonCompliantIndex) {
|
|
previousNodes.push(
|
|
tankHistories[lastNonCompliantIndex[tankId]] ??
|
|
({
|
|
all: emptyAllStats,
|
|
last_battle_time: -Infinity,
|
|
tank_id: tankId,
|
|
} satisfies TankHistory),
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
return [...previousNodes, ...filtered, ...latestNodes] satisfies Histories;
|
|
}
|