Files
blitzkit/scripts/discoverIds.ts
T

175 lines
5.0 KiB
TypeScript
Raw Normal View History

2024-06-09 23:49:56 -05:00
import { chunk, times, uniq } from 'lodash';
2024-06-08 19:10:15 -05:00
import { compress, decompress } from 'lz4js';
2024-06-05 18:35:44 -05:00
import { argv } from 'process';
2024-06-08 19:10:15 -05:00
import { REGIONS, Region } from '../src/constants/regions';
import { retryAbleBlitzFetchEvent } from '../src/core/blitz/fetchBlitz';
2024-06-05 18:35:44 -05:00
import { getAccountInfo } from '../src/core/blitz/getAccountInfo';
2024-06-08 19:10:15 -05:00
import { MIN_IDS, idToRegion } from '../src/core/blitz/idToRegion';
2024-06-05 18:35:44 -05:00
import { asset } from '../src/core/blitzkit/asset';
2024-06-06 13:37:50 -05:00
import { commitAssets } from '../src/core/blitzkit/commitAssets';
2024-06-05 18:35:44 -05:00
import { DidsReadStream, DidsWriteStream } from '../src/core/streams/dids';
2024-06-09 17:44:38 -05:00
interface DiscoverIdsManifest {
time: number;
chunks: number;
count: number;
}
const CHUNK_SIZE = 2 ** 21;
2024-06-06 13:37:50 -05:00
const RUN_TIME = 1000 * 60 * 60 * 5.5;
2024-06-08 19:10:15 -05:00
const PROGRESS_UPDATE_FREQUENCY = 1000 * 60;
2024-06-05 18:35:44 -05:00
const MAX_REQUESTS = 10;
const ACCOUNTS_PER_CALL = 100;
2024-06-06 19:02:58 -05:00
const TERMINATION_THRESHOLD = 1000;
2024-06-05 18:35:44 -05:00
const production = argv.includes('--production');
2024-06-06 19:02:58 -05:00
2024-06-06 19:08:29 -05:00
console.log(`Running in ${production ? 'production' : 'development'} mode`);
2024-06-09 17:44:38 -05:00
const startTime = Date.now();
const indexableRegions = [...REGIONS];
2024-06-11 11:02:27 -05:00
let ids: number[] = [];
2024-06-09 17:44:38 -05:00
const preDiscoveredManifest = (await fetch(
asset('ids/manifest.json', !production),
).then((response) => response.json())) as DiscoverIdsManifest;
console.log(
`Fetching ${preDiscoveredManifest.chunks} pre-discovered chunks...`,
);
let chunkIndex = 0;
while (chunkIndex < preDiscoveredManifest.chunks) {
const preDiscovered = await fetch(
asset(`ids/${chunkIndex}.dids.lz4`, !production),
).then(async (response) => {
const buffer = await response.arrayBuffer();
const decompressed = decompress(new Uint8Array(buffer)).buffer;
return new DidsReadStream(decompressed).dids();
});
// no spread syntax: https://github.com/oven-sh/bun/issues/11734
2024-06-11 11:02:27 -05:00
ids = ids.concat(preDiscovered);
2024-06-09 17:44:38 -05:00
console.log(
`Pre-discovered ${preDiscovered.length} ids (chunk ${chunkIndex})`,
);
chunkIndex++;
2024-06-06 19:02:58 -05:00
}
2024-06-06 13:37:50 -05:00
const regionalIdIndex: Record<Region, number> = {
2024-06-14 23:20:08 -05:00
asia: ids.findLast((id) => idToRegion(id) === 'asia') ?? MIN_IDS.asia,
com: ids.findLast((id) => idToRegion(id) === 'com') ?? MIN_IDS.com,
eu: ids.findLast((id) => idToRegion(id) === 'eu') ?? MIN_IDS.eu,
2024-06-06 13:37:50 -05:00
};
const zeroStreak: Record<Region, number> = { asia: 0, com: 0, eu: 0 };
let regionIndex = 0;
let outOfTimeFlagged = false;
2024-06-05 18:35:44 -05:00
async function verify(region: Region, ids: number[]) {
const infos = await getAccountInfo(region, ids, undefined, {
// cheeky way of requesting nothing
fields: 'account_id,-account_id',
});
const filtered = ids.filter((_, index) => {
const info = infos[index];
return info !== null;
});
return filtered;
}
2024-06-06 13:37:50 -05:00
let discardAttempts = 0;
2024-06-08 19:10:15 -05:00
let lastProgressUpdate = 0;
retryAbleBlitzFetchEvent.on(() => {
2024-06-06 13:37:50 -05:00
discardAttempts++;
2024-06-05 18:35:44 -05:00
});
const interval = setInterval(async () => {
2024-06-06 13:37:50 -05:00
if (discardAttempts > 0) return discardAttempts--;
2024-06-05 18:35:44 -05:00
const region = indexableRegions[regionIndex];
const idsToVerify = times(
ACCOUNTS_PER_CALL,
(index) => regionalIdIndex[region] + index,
);
const verified = await verify(region, idsToVerify);
2024-06-08 19:10:15 -05:00
if (Date.now() - lastProgressUpdate > PROGRESS_UPDATE_FREQUENCY) {
lastProgressUpdate = Date.now();
2024-06-09 17:44:38 -05:00
console.log(`Discovered ${ids.length} ids in ${Date.now() - startTime}ms`);
2024-06-08 19:10:15 -05:00
}
2024-06-06 19:02:58 -05:00
2024-06-05 18:35:44 -05:00
if (verified.length === 0) {
zeroStreak[region]++;
2024-06-06 19:02:58 -05:00
if (zeroStreak[region] >= TERMINATION_THRESHOLD) {
2024-06-05 18:35:44 -05:00
const sliceableIndex = indexableRegions.indexOf(region);
if (sliceableIndex !== -1) {
console.log(`Stopped discovery for ${region}`);
indexableRegions.splice(sliceableIndex, 1);
if (indexableRegions.length === 0) {
console.log('All players discovered exhaustively!');
post();
}
}
}
} else {
zeroStreak[region] = 0;
ids.push(...verified);
}
if (Date.now() - startTime > RUN_TIME && !outOfTimeFlagged) {
outOfTimeFlagged = true;
console.log('Out of time');
post();
}
regionIndex = (regionIndex + 1) % indexableRegions.length;
regionalIdIndex[region] += ACCOUNTS_PER_CALL;
}, 1000 / MAX_REQUESTS);
function post() {
clearInterval(interval);
2024-06-11 11:02:27 -05:00
ids = uniq(ids).sort((a, b) => a - b);
2024-06-05 18:35:44 -05:00
console.log(`Uploading ${ids.length} ids...`);
2024-06-11 11:02:27 -05:00
const idsChunked = chunk(ids, CHUNK_SIZE);
const files = idsChunked.map((chunk, index) => {
const didsWriteStream = new DidsWriteStream().dids(chunk);
const buffer = didsWriteStream.uint8Array;
const compressed = compress(buffer);
console.log(
`Chunk ${index} with ${chunk.length} ids (compressed: ${compressed.length}; uncompressed: ${buffer.length})`,
);
2024-06-09 17:44:38 -05:00
const content = Buffer.from(compressed).toString('base64');
return {
content,
encoding: 'base64' as const,
2024-06-11 11:02:27 -05:00
path: `ids/${index}.dids.lz4`,
2024-06-09 17:44:38 -05:00
};
});
2024-06-05 18:35:44 -05:00
2024-06-06 13:37:50 -05:00
commitAssets(
'discovered ids',
[
2024-06-09 17:44:38 -05:00
...files,
2024-06-06 13:37:50 -05:00
{
2024-06-09 17:44:38 -05:00
content: JSON.stringify({
chunks: idsChunked.length,
count: ids.length,
time: Date.now(),
} satisfies DiscoverIdsManifest),
encoding: 'utf-8',
path: 'ids/manifest.json',
2024-06-06 13:37:50 -05:00
},
],
production,
);
2024-06-05 18:35:44 -05:00
}