Files
blitzkit/scripts/test.ts
T

96 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-04-13 22:13:57 -05:00
import { chunk, times } from 'lodash';
import { compress } from 'lz4js';
import { argv } from 'process';
import { Region, REGIONS } from '../src/constants/regions';
2024-04-13 18:20:49 -05:00
import getTankStats from '../src/core/blitz/getTankStats';
import { idToRegion } from '../src/core/blitz/idToRegion';
2024-04-23 09:57:59 -05:00
import { commitAssets } from '../src/core/blitzkit/commitAssets';
import { FileChange } from '../src/core/blitzkit/commitMultipleFiles';
2024-04-13 22:13:57 -05:00
import {
RtscWritePlayer,
RtscWriteStream,
RtsmWriteStream,
} from '../src/core/streams/rts';
2024-04-13 18:20:49 -05:00
import usersRaw from '../test.users.json';
2024-04-13 22:13:57 -05:00
const PLAYERS_PER_CHUNK = 256;
const CHUNKS_PER_BATCH = 128;
const production = argv.includes('--production');
2024-04-15 09:27:59 -05:00
const region = argv
.find((argument) => argument.startsWith('--region'))
?.split('=')[1] as Region | undefined;
2024-04-13 22:13:57 -05:00
2024-04-15 09:27:59 -05:00
if (!region || !REGIONS.includes(region)) {
throw new Error('No valid region specified');
}
2024-04-13 18:20:49 -05:00
2024-04-13 22:13:57 -05:00
const today = new Date();
2024-04-16 09:28:01 -05:00
const todayPath = `${today.getUTCFullYear()}/${today.getUTCMonth() + 1}/${today.getUTCDate()}`;
2024-04-15 09:27:59 -05:00
const users = usersRaw.filter(({ blitz }) => idToRegion(blitz) === region);
const base = Math.ceil(users.length / PLAYERS_PER_CHUNK);
const chunks = times(base, (offset) => ({
offset,
users: users.filter((user) => user.blitz % base === offset),
}));
2024-04-13 22:13:57 -05:00
const batches = chunk(chunks, CHUNKS_PER_BATCH);
2024-04-15 09:27:59 -05:00
const manifestWriteStream = new RtsmWriteStream();
2024-04-13 22:13:57 -05:00
2024-04-16 09:35:10 -05:00
console.log(`Found ${users.length} players in ${region}`);
2024-04-13 22:13:57 -05:00
await commitAssets(
`regular tank stats manifests ${todayPath}`,
2024-04-15 09:27:59 -05:00
[
{
encoding: 'base64',
path: `regions/${region}/regular/tanks/${todayPath}/manifest.rtsm`,
content: Buffer.from(
compress(manifestWriteStream.rtsm(base).uint8Array),
).toString('base64'),
},
],
2024-04-13 22:13:57 -05:00
production,
);
let batchesDone = 0;
for (const batch of batches) {
console.log(`Batch ${++batchesDone} of ${batches.length}`);
const changes: FileChange[] = [];
let chunksDone = 0;
for (const chunk of batch) {
console.log(`Chunk ${++chunksDone} of ${chunks.length}`);
let playersDone = 0;
const players = await Promise.all(
chunk.users.map(async (user) => {
2024-04-15 13:25:35 -05:00
const tanks = await getTankStats(region, user.blitz);
2024-04-13 22:13:57 -05:00
console.log(`Player ${++playersDone} of ${chunk.users.length}`);
if (tanks === null) return null;
return {
id: user.blitz,
tanks,
} satisfies RtscWritePlayer;
}),
).then((players) => players.filter((player) => player !== null));
const writeStream = new RtscWriteStream();
writeStream.rtsc(players);
changes.push({
encoding: 'base64',
content: Buffer.from(compress(writeStream.uint8Array)).toString('base64'),
2024-04-15 09:27:59 -05:00
path: `regions/${region}/regular/tanks/${todayPath}/${chunk.offset}.rtsc.lz4`,
2024-04-13 22:13:57 -05:00
});
}
// do not await; commit in background
commitAssets(
`regular tank stats ${todayPath} batch ${batches.indexOf(batch) + 1}`,
changes,
production,
);
}