Files
blitzkit/packages/bot/src/core/blitzstars/getStatsInPeriod.ts
T

102 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-09-07 21:45:00 -05:00
import { BlitzStats, emptyAllStats, Region } from '@blitzkit/core';
2024-09-07 23:00:56 -05:00
import { DiffedTankStats } from '@blitzkit/core/src/statistics/filterStats';
2024-03-09 11:48:35 -06:00
import { Locale } from 'discord.js';
2023-06-21 01:52:26 +00:00
import getTankHistories, {
2023-06-23 01:35:15 +00:00
TankHistories,
2023-06-22 18:26:51 +00:00
TankHistoryRaw,
2024-09-07 21:45:00 -05:00
} from './getTankHistories';
2023-05-25 18:20:51 +00:00
2024-03-12 23:36:56 -05:00
const emptyTankHistoryNode: TankHistoryRaw = {
2023-05-25 18:20:51 +00:00
account_id: 0,
battle_life_time: 0,
last_battle_time: 0,
mark_of_mastery: 0,
tank_id: 0,
2023-05-29 17:44:32 +00:00
all: emptyAllStats,
2023-05-25 18:20:51 +00:00
};
2023-09-23 21:51:00 +00:00
// BIG TODO: merge this neatly with diffNormalizedTankStats
2023-10-14 11:25:42 -05:00
export default async function getStatsInPeriod(
server: Region,
2023-05-25 18:20:51 +00:00
id: number,
2023-06-01 18:36:39 +00:00
start: number,
end: number,
2024-03-09 11:48:35 -06:00
locale: Locale,
2023-05-25 18:20:51 +00:00
) {
2024-03-09 11:48:35 -06:00
const history = await getTankHistories(server, id, locale, {
2023-06-21 01:52:26 +00:00
includeLatestHistories: true,
});
2023-05-25 18:20:51 +00:00
// sort them even though most won't be used
2023-06-23 01:35:15 +00:00
const tankSortedHistory: Record<number, TankHistories> = {};
2023-07-09 17:49:11 +00:00
const tanks: number[] = [];
2023-05-25 18:20:51 +00:00
history.forEach((node) => {
if (!tankSortedHistory[node.tank_id]) {
tankSortedHistory[node.tank_id] = [node];
2023-07-09 17:49:11 +00:00
tanks.push(node.tank_id);
2023-05-25 18:20:51 +00:00
} else {
tankSortedHistory[node.tank_id].push(node);
}
});
// find the ones played within the range
2023-07-09 17:49:11 +00:00
const inRangeTanks = tanks.filter((tankId) =>
2023-05-25 18:20:51 +00:00
tankSortedHistory[tankId].some(
(node) => node.last_battle_time > start && node.last_battle_time < end,
),
);
2023-07-09 17:49:11 +00:00
const playedTanks: number[] = [];
2023-05-25 18:20:51 +00:00
// fetch history node right before "start" and right before "end"
2024-08-10 13:29:10 -05:00
const diff: Record<number, BlitzStats> = {};
2023-07-09 17:49:11 +00:00
inRangeTanks.forEach((tankId) => {
2023-05-25 18:20:51 +00:00
const sortedHistory = tankSortedHistory[tankId];
const previousIndex =
sortedHistory.findIndex((node) => node.last_battle_time > start) - 1;
const latestIndex =
sortedHistory.findIndex((node) => node.last_battle_time > end) - 1;
const previous = sortedHistory[previousIndex] ?? emptyTankHistoryNode;
const latest =
sortedHistory[latestIndex] ?? sortedHistory[sortedHistory.length - 1];
2024-08-10 13:29:10 -05:00
function d(get: (allStats: BlitzStats) => number) {
2023-05-25 18:20:51 +00:00
return get(latest.all) - get(previous.all);
}
// check if there was a change in battles as games in rating do update last_battle_time
2023-10-11 18:26:35 -05:00
if (d((a) => a.battles) > 0) {
diff[tankId] = {
battles: d((a) => a.battles),
capture_points: d((a) => a.capture_points),
damage_dealt: d((a) => a.damage_dealt),
damage_received: d((a) => a.damage_received),
dropped_capture_points: d((a) => a.dropped_capture_points),
frags: d((a) => a.frags),
frags8p: d((a) => a.frags8p),
hits: d((a) => a.hits),
losses: d((a) => a.losses),
2023-05-29 17:44:32 +00:00
max_frags: latest.all.max_frags,
max_xp: latest.all.max_xp,
2023-10-11 18:26:35 -05:00
shots: d((a) => a.shots),
spotted: d((a) => a.spotted),
survived_battles: d((a) => a.survived_battles),
win_and_survived: d((a) => a.win_and_survived),
wins: d((a) => a.wins),
xp: d((a) => a.xp),
2023-05-29 17:12:05 +00:00
};
2023-07-09 17:49:11 +00:00
playedTanks.push(tankId);
2023-05-29 17:12:05 +00:00
}
2023-05-25 18:20:51 +00:00
});
2023-07-09 17:49:11 +00:00
const order = playedTanks.sort((a, b) => {
const aHistories = tankSortedHistory[a];
const bHistories = tankSortedHistory[b];
const aLatest = aHistories[aHistories.length - 1];
const bLatest = bHistories[bHistories.length - 1];
return bLatest.last_battle_time - aLatest.last_battle_time;
});
2023-10-11 18:26:35 -05:00
return { diff, order } satisfies DiffedTankStats;
2023-05-25 18:20:51 +00:00
}