Files
blitzkit/packages/scripts/src/discoverIds.ts
T

165 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-09-07 21:45:00 -05:00
import {
DidsReadStream,
DidsWriteStream,
2024-09-08 10:42:34 -05:00
DiscoveredIdsDefinitions,
2024-09-07 21:45:00 -05:00
MIN_REGION_IDS,
REGIONS,
Region,
asset,
2024-09-08 10:17:40 -05:00
getAccountInfo,
2024-09-07 21:45:00 -05:00
idToRegion,
2024-09-08 10:17:40 -05:00
retryAbleBlitzFetchEvent,
2025-10-23 21:22:21 -05:00
} from "@blitzkit/core";
import { chunk, times, uniq } from "lodash-es";
import { compress, decompress } from "lz4js";
import { AssetUploader } from "./core/github/assetUploader";
2024-06-05 18:35:44 -05:00
2024-06-09 17:44:38 -05:00
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-09 17:44:38 -05:00
const startTime = Date.now();
const indexableRegions = [...REGIONS];
2024-06-11 11:02:27 -05:00
let ids: number[] = [];
2025-10-23 21:22:21 -05:00
const preDiscoveredManifest = (await fetch(asset("ids/manifest.json")).then(
(response) => response.json()
2024-08-27 14:40:43 -05:00
)) as DiscoveredIdsDefinitions;
2024-06-09 17:44:38 -05:00
console.log(
2025-10-23 21:22:21 -05:00
`Fetching ${preDiscoveredManifest.chunks} pre-discovered chunks...`
2024-06-09 17:44:38 -05:00
);
let chunkIndex = 0;
while (chunkIndex < preDiscoveredManifest.chunks) {
2024-08-27 14:40:43 -05:00
const preDiscovered = await fetch(asset(`ids/${chunkIndex}.dids.lz4`)).then(
async (response) => {
const buffer = await response.arrayBuffer();
const decompressed = decompress(new Uint8Array(buffer)).buffer;
return new DidsReadStream(decompressed as ArrayBuffer).dids();
2025-10-23 21:22:21 -05:00
}
2024-08-27 14:40:43 -05:00
);
2024-06-09 17:44:38 -05:00
// 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(
2025-10-23 21:22:21 -05:00
`Pre-discovered ${preDiscovered.length} ids (chunk ${chunkIndex})`
2024-06-09 17:44:38 -05:00
);
chunkIndex++;
2024-06-06 19:02:58 -05:00
}
2024-06-06 13:37:50 -05:00
const regionalIdIndex: Record<Region, number> = {
2025-10-23 21:22:21 -05:00
asia: ids.findLast((id) => idToRegion(id) === "asia") ?? MIN_REGION_IDS.asia,
com: ids.findLast((id) => idToRegion(id) === "com") ?? MIN_REGION_IDS.com,
eu: ids.findLast((id) => idToRegion(id) === "eu") ?? MIN_REGION_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
2025-10-23 21:22:21 -05:00
fields: "account_id,-account_id",
2024-06-05 18:35:44 -05:00
});
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;
2024-06-18 12:55:48 -05:00
retryAbleBlitzFetchEvent.on(() => 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,
2025-10-23 21:22:21 -05:00
(index) => regionalIdIndex[region] + index
2024-06-05 18:35:44 -05:00
);
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) {
2025-10-23 21:22:21 -05:00
console.log("All players discovered exhaustively!");
await post();
2024-06-05 18:35:44 -05:00
}
}
}
} else {
zeroStreak[region] = 0;
ids.push(...verified);
}
if (Date.now() - startTime > RUN_TIME && !outOfTimeFlagged) {
outOfTimeFlagged = true;
2025-10-23 21:22:21 -05:00
console.log("Out of time");
await post();
2024-06-05 18:35:44 -05:00
}
regionIndex = (regionIndex + 1) % indexableRegions.length;
regionalIdIndex[region] += ACCOUNTS_PER_CALL;
}, 1000 / MAX_REQUESTS);
2025-10-23 21:22:21 -05:00
async function post() {
2024-06-05 18:35:44 -05:00
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...`);
2025-10-23 21:22:21 -05:00
using uploader = new AssetUploader("discovered ids");
2024-06-11 11:02:27 -05:00
const idsChunked = chunk(ids, CHUNK_SIZE);
2025-10-23 21:22:21 -05:00
let index = 0;
for (const chunk of idsChunked) {
2024-06-11 11:02:27 -05:00
const didsWriteStream = new DidsWriteStream().dids(chunk);
const buffer = didsWriteStream.uint8Array;
2025-06-07 12:35:23 -05:00
const content = compress(buffer);
2024-06-11 11:02:27 -05:00
console.log(
2025-10-23 21:22:21 -05:00
`Chunk ${index} with ${chunk.length} ids (compressed: ${content.length}; uncompressed: ${buffer.length})`
2024-06-11 11:02:27 -05:00
);
2025-10-23 21:22:21 -05:00
await uploader.add({
2024-06-09 17:44:38 -05:00
content,
2024-06-11 11:02:27 -05:00
path: `ids/${index}.dids.lz4`,
2025-10-23 21:22:21 -05:00
});
index++;
}
await uploader.add({
content: new TextEncoder().encode(
JSON.stringify({
chunks: idsChunked.length,
count: ids.length,
time: Date.now(),
} satisfies DiscoveredIdsDefinitions)
),
path: "ids/manifest.json",
2024-06-09 17:44:38 -05:00
});
2024-06-05 18:35:44 -05:00
2025-10-23 21:22:21 -05:00
await uploader.flush();
2024-06-05 18:35:44 -05:00
}