Files
blitzkit/src/core/blitzkrieg/getTankHistories.ts
T

113 lines
3.3 KiB
TypeScript
Raw Normal View History

import { Region } from '../../constants/regions';
2023-06-22 18:26:51 +00:00
import {
GetHistoriesOptions,
Histories,
History,
getHistoriesDefaultOptions,
2023-07-11 19:54:57 +00:00
} from '../../types/histories';
2023-10-14 11:25:42 -05:00
import getTankStats from '../blitz/getTankStats';
import { emptyAllStats } from '../blitzstars/getStatsInPeriod';
2023-06-21 01:52:26 +00:00
2023-06-22 18:26:51 +00:00
export interface TankHistory extends History {
2023-06-21 01:52:26 +00:00
tank_id: number;
}
export type TankHistories = TankHistory[];
2023-06-22 18:26:51 +00:00
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 {
2023-06-21 01:52:26 +00:00
tankId?: number;
}
export const getTankHistoriesDefaultOptions: GetTankHistoriesOptions = {
2023-06-22 18:26:51 +00:00
...getHistoriesDefaultOptions,
tankId: undefined,
2023-06-21 01:52:26 +00:00
};
export default async function getTankHistories(
server: Region,
2023-06-21 01:52:26 +00:00
id: number,
options?: Partial<GetTankHistoriesOptions>,
) {
const mergedOptions = { ...getTankHistoriesDefaultOptions, ...options };
const tankHistoriesResponse = await fetch(
`https://www.blitzstars.com/api/tankhistories/for/${id}/`,
);
2023-06-22 18:26:51 +00:00
const tankHistories = (
(await tankHistoriesResponse.json()) as TankHistoriesRaw
).map(
(history) =>
({
all: history.all,
last_battle_time: history.last_battle_time,
tank_id: history.tank_id,
2023-07-11 19:54:57 +00:00
}) satisfies TankHistory,
2023-06-22 18:26:51 +00:00
);
2023-06-21 01:52:26 +00:00
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(
2023-06-22 18:26:51 +00:00
tankHistories[lastNonCompliantIndex[tankId]] ??
({
all: emptyAllStats,
last_battle_time: -Infinity,
tank_id: tankId,
} satisfies TankHistory),
2023-06-21 01:52:26 +00:00
);
}
});
}
2023-06-23 01:35:15 +00:00
return [...previousNodes, ...filtered, ...latestNodes] satisfies Histories;
2023-06-21 01:52:26 +00:00
}