mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: finish script
This commit is contained in:
1 parent
33012f9f21
commit
ffaa368f81
3 files changed
+103
-70
No files matched your search
+92
-53
@@ -1,7 +1,8 @@
|
|||||||
import { times } from 'lodash';
|
import { times } from 'lodash';
|
||||||
import { argv } from 'process';
|
import { argv } from 'process';
|
||||||
import { Region, REGIONS } from '../src/constants/regions';
|
import { Region, REGIONS } from '../src/constants/regions';
|
||||||
import { blitzFetchQueueAvailableEvent } from '../src/core/blitz/fetchBlitz';
|
import { getAccountInfo } from '../src/core/blitz/getAccountInfo';
|
||||||
|
import getTankStats from '../src/core/blitz/getTankStats';
|
||||||
import { idToRegion } from '../src/core/blitz/idToRegion';
|
import { idToRegion } from '../src/core/blitz/idToRegion';
|
||||||
import {
|
import {
|
||||||
AverageDefinitions,
|
AverageDefinitions,
|
||||||
@@ -10,9 +11,7 @@ import {
|
|||||||
AverageDefinitionsEntrySubPartial,
|
AverageDefinitionsEntrySubPartial,
|
||||||
} from '../src/core/blitzkit/averageDefinitions';
|
} from '../src/core/blitzkit/averageDefinitions';
|
||||||
import { averageDefinitionsAllStatsKeys } from '../src/core/blitzkit/averageDefinitions/constants';
|
import { averageDefinitionsAllStatsKeys } from '../src/core/blitzkit/averageDefinitions/constants';
|
||||||
import { commitAssets } from '../src/core/blitzkit/commitAssets';
|
|
||||||
import { fetchPreDiscoveredIds } from '../src/core/blitzkit/fetchPreDiscoveredIds';
|
import { fetchPreDiscoveredIds } from '../src/core/blitzkit/fetchPreDiscoveredIds';
|
||||||
import { superCompress } from '../src/core/blitzkit/superCompress';
|
|
||||||
|
|
||||||
interface DataPoint {
|
interface DataPoint {
|
||||||
x: number;
|
x: number;
|
||||||
@@ -20,9 +19,13 @@ interface DataPoint {
|
|||||||
w: number;
|
w: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const RUN_TIME = 1000 * 60 * 60;
|
const MAX_ACTIVITY_TIME = 1000 * 60 * 60 * 24 * 120;
|
||||||
const MAX_REQUESTS = 10;
|
const MIN_BATTLES = 5000;
|
||||||
|
const RUN_TIME = 1000 * 60;
|
||||||
|
const THREADS = 10;
|
||||||
|
const PLAYER_IDS_PER_CALL = 100;
|
||||||
|
|
||||||
|
const startTime = Date.now();
|
||||||
const production = argv.includes('--production');
|
const production = argv.includes('--production');
|
||||||
const preDiscoveredIds = await fetchPreDiscoveredIds(!production);
|
const preDiscoveredIds = await fetchPreDiscoveredIds(!production);
|
||||||
const playerIds: Record<Region, number[]> = {
|
const playerIds: Record<Region, number[]> = {
|
||||||
@@ -31,54 +34,88 @@ const playerIds: Record<Region, number[]> = {
|
|||||||
eu: preDiscoveredIds.filter((id) => idToRegion(id) === 'eu'),
|
eu: preDiscoveredIds.filter((id) => idToRegion(id) === 'eu'),
|
||||||
};
|
};
|
||||||
const regionalIndices: Record<Region, number> = {
|
const regionalIndices: Record<Region, number> = {
|
||||||
asia: playerIds.asia.length - 1,
|
asia: 0,
|
||||||
com: playerIds.com.length - 1,
|
com: 0,
|
||||||
eu: playerIds.eu.length - 1,
|
eu: 0,
|
||||||
};
|
};
|
||||||
let regionIndex = 0;
|
let regionIndex = 0;
|
||||||
|
let playerCount = 0;
|
||||||
const availableRegions = [...REGIONS];
|
const availableRegions = [...REGIONS];
|
||||||
|
const tankIds: number[] = [];
|
||||||
|
const tanksSorted: Record<number, AverageDefinitionsAllStats[]> = {};
|
||||||
|
let postWorkRequested = false;
|
||||||
|
|
||||||
times(MAX_REQUESTS, () => {
|
times(THREADS, async () => {
|
||||||
validate();
|
while (availableRegions.length > 0 && startTime + RUN_TIME > Date.now()) {
|
||||||
blitzFetchQueueAvailableEvent.on(validate);
|
const region = availableRegions[regionIndex];
|
||||||
});
|
const idIndex = regionalIndices[region];
|
||||||
|
const ids = playerIds[region].slice(idIndex, idIndex + PLAYER_IDS_PER_CALL);
|
||||||
|
const accountInfo = await getAccountInfo(region, ids, undefined, {
|
||||||
|
fields: 'last_battle_time,statistics.all.battles',
|
||||||
|
});
|
||||||
|
|
||||||
function validate() {
|
/**
|
||||||
const region = availableRegions[regionIndex];
|
* Pass rates
|
||||||
|
* none: 1000 / 1000
|
||||||
|
* activity: 55 / 1000
|
||||||
|
* battles: 30 / 1000
|
||||||
|
* both: 23 / 1000
|
||||||
|
*/
|
||||||
|
|
||||||
regionIndex = (regionIndex + 1) % availableRegions.length;
|
const filteredIds = ids.filter((_, index) => {
|
||||||
}
|
const info = accountInfo[index];
|
||||||
|
return (
|
||||||
|
info !== null &&
|
||||||
|
Date.now() - info.last_battle_time * 1000 <= MAX_ACTIVITY_TIME &&
|
||||||
|
info.statistics.all.battles > MIN_BATTLES
|
||||||
|
);
|
||||||
|
});
|
||||||
|
playerCount += filteredIds.length;
|
||||||
|
const players = await Promise.all(
|
||||||
|
filteredIds.map((id) => getTankStats(region, id)),
|
||||||
|
);
|
||||||
|
|
||||||
function postWork() {
|
players.forEach((tanks) => {
|
||||||
const tankIds: number[] = [];
|
tanks?.forEach((tank) => {
|
||||||
const sorted: Record<number, AverageDefinitionsAllStats[]> = {};
|
if (tank.all.battles === 0) return;
|
||||||
|
|
||||||
console.log('Sorting tanks...');
|
if (!tankIds.includes(tank.tank_id)) {
|
||||||
|
tankIds.push(tank.tank_id);
|
||||||
|
tanksSorted[tank.tank_id] = [];
|
||||||
|
}
|
||||||
|
|
||||||
players.forEach((tanks) => {
|
tanksSorted[tank.tank_id].push({
|
||||||
tanks.forEach((tank) => {
|
...tank.all,
|
||||||
if (tank.all.battles === 0) return;
|
battle_life_time: tank.battle_life_time,
|
||||||
|
});
|
||||||
if (!tankIds.includes(tank.tank_id)) {
|
|
||||||
tankIds.push(tank.tank_id);
|
|
||||||
sorted[tank.tank_id] = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
sorted[tank.tank_id].push({
|
|
||||||
...tank.all,
|
|
||||||
battle_life_time: tank.battle_life_time,
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
|
regionalIndices[region] += PLAYER_IDS_PER_CALL;
|
||||||
|
if (
|
||||||
|
regionalIndices[region] >= playerIds[region].length - 1 &&
|
||||||
|
availableRegions.includes(region)
|
||||||
|
) {
|
||||||
|
availableRegions.splice(availableRegions.indexOf(region), 1);
|
||||||
|
}
|
||||||
|
regionIndex = (regionIndex + 1) % availableRegions.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!postWorkRequested) {
|
||||||
|
postWorkRequested = true;
|
||||||
|
postWork();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
async function postWork() {
|
||||||
console.log(
|
console.log(
|
||||||
`Generating statistics based off ${players.length} players and ${tankIds.length} tanks...`,
|
`Generating statistics based off ${playerCount} players and ${tankIds.length} tanks...`,
|
||||||
);
|
);
|
||||||
|
|
||||||
const averages: AverageDefinitions = {};
|
const averages: AverageDefinitions = {};
|
||||||
|
|
||||||
tankIds.forEach((id) => {
|
tankIds.forEach((id) => {
|
||||||
const tanks = sorted[id];
|
const tanks = tanksSorted[id];
|
||||||
const samples = tanks.length;
|
const samples = tanks.length;
|
||||||
const entry: AverageDefinitionsEntrySubPartial = {
|
const entry: AverageDefinitionsEntrySubPartial = {
|
||||||
samples,
|
samples,
|
||||||
@@ -94,6 +131,8 @@ function postWork() {
|
|||||||
y: tank.wins / tank.battles,
|
y: tank.wins / tank.battles,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
if (data.length === 0) return;
|
||||||
|
|
||||||
function sum(slicer: (data: DataPoint) => number) {
|
function sum(slicer: (data: DataPoint) => number) {
|
||||||
return data.reduce(
|
return data.reduce(
|
||||||
(accumulator, data) => accumulator + slicer(data),
|
(accumulator, data) => accumulator + slicer(data),
|
||||||
@@ -104,16 +143,16 @@ function postWork() {
|
|||||||
const sum_w = sum(({ w }) => w);
|
const sum_w = sum(({ w }) => w);
|
||||||
const sum_wx = sum(({ w, x }) => w * x);
|
const sum_wx = sum(({ w, x }) => w * x);
|
||||||
const sum_wy = sum(({ w, y }) => w * y);
|
const sum_wy = sum(({ w, y }) => w * y);
|
||||||
const mu_x = sum_wx / sum_w;
|
const x_bar = sum_wx / sum_w;
|
||||||
const mu_y = sum_wy / sum_w;
|
const y_bar = sum_wy / sum_w;
|
||||||
const mu = mu_x;
|
const mu = x_bar;
|
||||||
|
|
||||||
const sigma_numerator = sum(({ w, x }) => w * (x - mu_x) ** 2);
|
const sigma_numerator = sum(({ w, x }) => w * (x - x_bar) ** 2);
|
||||||
const sigma = Math.sqrt(sigma_numerator / sum_w);
|
const sigma = Math.sqrt(sigma_numerator / sum_w);
|
||||||
|
|
||||||
const r_numerator = sum(({ w, x, y }) => w * (x - mu_x) * (y - mu_y));
|
const r_numerator = sum(({ w, x, y }) => w * (x - x_bar) * (y - y_bar));
|
||||||
const r_denominator_x = sum(({ w, x }) => w * (x - mu_x) ** 2);
|
const r_denominator_x = sum(({ w, x }) => w * (x - x_bar) ** 2);
|
||||||
const r_denominator_y = sum(({ w, y }) => w * (y - mu_y) ** 2);
|
const r_denominator_y = sum(({ w, y }) => w * (y - y_bar) ** 2);
|
||||||
const r = r_numerator / Math.sqrt(r_denominator_x * r_denominator_y);
|
const r = r_numerator / Math.sqrt(r_denominator_x * r_denominator_y);
|
||||||
|
|
||||||
entry.mu[key] = mu;
|
entry.mu[key] = mu;
|
||||||
@@ -124,15 +163,15 @@ function postWork() {
|
|||||||
averages[id] = entry as AverageDefinitionsEntry;
|
averages[id] = entry as AverageDefinitionsEntry;
|
||||||
});
|
});
|
||||||
|
|
||||||
commitAssets(
|
// commitAssets(
|
||||||
'averages',
|
// 'averages',
|
||||||
[
|
// [
|
||||||
{
|
// {
|
||||||
path: 'definitions/averages.cdon.lz4',
|
// path: 'definitions/averages.cdon.lz4',
|
||||||
content: superCompress(averages),
|
// content: superCompress(averages),
|
||||||
encoding: 'base64',
|
// encoding: 'base64',
|
||||||
},
|
// },
|
||||||
],
|
// ],
|
||||||
production,
|
// production,
|
||||||
);
|
// );
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ async function manageQueue() {
|
|||||||
if (queue.length > 0 && inProgress < MAX_CALLS_PER_SECOND) {
|
if (queue.length > 0 && inProgress < MAX_CALLS_PER_SECOND) {
|
||||||
inProgress++;
|
inProgress++;
|
||||||
|
|
||||||
const timeout = setTimeout(() => decrement, 1000);
|
const timeout = setTimeout(decrement, 1000);
|
||||||
const request = queue.shift()!;
|
const request = queue.shift()!;
|
||||||
const data = (await patientFetch(request.url)
|
const data = (await patientFetch(request.url)
|
||||||
.then((response) => response.json())
|
.then((response) => response.json())
|
||||||
@@ -45,10 +45,6 @@ async function manageQueue() {
|
|||||||
request.resolve(data.data);
|
request.resolve(data.data);
|
||||||
} else {
|
} else {
|
||||||
if (RETRY_ERRORS.includes(data.error.message)) {
|
if (RETRY_ERRORS.includes(data.error.message)) {
|
||||||
console.log(
|
|
||||||
`Encountered retry-able error ${data.error}, putting "${request.url}" back in queue...`,
|
|
||||||
);
|
|
||||||
|
|
||||||
retryAbleBlitzFetchEvent.emit(undefined);
|
retryAbleBlitzFetchEvent.emit(undefined);
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
|||||||
@@ -1,21 +1,23 @@
|
|||||||
import { times } from 'lodash';
|
import { times } from 'lodash';
|
||||||
import { decompress } from 'lz4js';
|
import { decompress } from 'lz4js';
|
||||||
|
import ProgressBar from 'progress';
|
||||||
import { DiscoverIdsManifest } from '../../../scripts/discoverIds';
|
import { DiscoverIdsManifest } from '../../../scripts/discoverIds';
|
||||||
import { DidsReadStream } from '../streams/dids';
|
import { DidsReadStream } from '../streams/dids';
|
||||||
import { asset } from './asset';
|
import { asset } from './asset';
|
||||||
|
|
||||||
export async function fetchPreDiscoveredIds(dev: boolean) {
|
export async function fetchPreDiscoveredIds(dev: boolean) {
|
||||||
const idChunks: number[][] = [];
|
const idChunks: number[][] = [];
|
||||||
const preDiscoveredManifest = (await fetch(
|
const manifest = (await fetch(asset('ids/manifest.json', dev)).then(
|
||||||
asset('ids/manifest.json', dev),
|
(response) => response.json(),
|
||||||
).then((response) => response.json())) as DiscoverIdsManifest;
|
)) as DiscoverIdsManifest;
|
||||||
|
const bar = new ProgressBar(
|
||||||
console.log(
|
`Fetching ${manifest.count.toLocaleString()} ids in ${manifest.chunks} chunks :bar`,
|
||||||
`Fetching ${preDiscoveredManifest.chunks} pre-discovered chunks...`,
|
manifest.chunks,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
bar.render();
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
times(preDiscoveredManifest.chunks, async (chunkIndex) => {
|
times(manifest.chunks, async (chunkIndex) => {
|
||||||
const preDiscovered = await fetch(
|
const preDiscovered = await fetch(
|
||||||
asset(`ids/${chunkIndex}.dids.lz4`, dev),
|
asset(`ids/${chunkIndex}.dids.lz4`, dev),
|
||||||
).then(async (response) => {
|
).then(async (response) => {
|
||||||
@@ -24,12 +26,8 @@ export async function fetchPreDiscoveredIds(dev: boolean) {
|
|||||||
return new DidsReadStream(decompressed as ArrayBuffer).dids();
|
return new DidsReadStream(decompressed as ArrayBuffer).dids();
|
||||||
});
|
});
|
||||||
|
|
||||||
// no spread syntax: https://github.com/oven-sh/bun/issues/11734
|
|
||||||
idChunks.push(preDiscovered);
|
idChunks.push(preDiscovered);
|
||||||
|
bar.tick();
|
||||||
console.log(
|
|
||||||
`Pre-discovered ${preDiscovered.length} ids (chunk ${chunkIndex})`,
|
|
||||||
);
|
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user