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

253 lines
6.0 KiB
TypeScript
Raw Normal View History

2026-08-08 12:28:45 -05:00
import { $ } from "bun";
2026-08-08 13:30:49 -05:00
import { range } from "lodash-es";
2026-08-08 13:15:04 -05:00
import { compress, decompress } from "lz4js";
2026-08-08 12:28:45 -05:00
import { mkdir, rm } from "node:fs/promises";
import { IdArray } from "./idArray";
interface ManifestV1 {
version: 1;
last_verified: number;
}
2026-08-08 13:42:16 -05:00
interface RegionDescriptor {
domain: string;
seed: number;
max: number;
streak: number;
}
2026-08-08 13:30:49 -05:00
export interface QuickInfo {
status: string;
data: Record<string, {} | null>;
}
2026-08-08 12:28:45 -05:00
const TEMP = "../../temp";
const WORKING_DIR = `${TEMP}/ids`;
const CHUNKS_DIR = `${WORKING_DIR}/chunks`;
const REPO = "blitzkit/ids";
const ACCOMMODATED_ID_COUNT = 10_000_000_000; // 10B ids
const MAX_BYTES_PER_CHUNK = 100 * 1_000_000; // 100MB
2026-08-08 13:30:49 -05:00
const MAX_IDS_PER_CALL = 100;
const API_RATE = 10; // 10Hz
2026-08-08 13:52:07 -05:00
const MAX_DRY_STREAK = 10_000;
2026-08-08 13:30:49 -05:00
2026-08-08 13:42:16 -05:00
const chunkCount = Math.ceil(
(ACCOMMODATED_ID_COUNT * Uint32Array.BYTES_PER_ELEMENT) / MAX_BYTES_PER_CHUNK,
);
2026-08-08 12:28:45 -05:00
console.log(
`${chunkCount.toLocaleString()} will be needed for ${ACCOMMODATED_ID_COUNT.toLocaleString()} ids`,
);
2026-08-08 13:15:04 -05:00
const t0 = performance.now();
2026-08-08 13:30:49 -05:00
// const maxT = 5 * 60 * 60 * 1000; // 5hr
2026-08-08 13:57:35 -05:00
const maxT = 60 * 1000;
2026-08-08 13:15:04 -05:00
const t1 = t0 + maxT;
2026-08-08 12:28:45 -05:00
await mkdir(TEMP, { recursive: true });
await rm(WORKING_DIR, { recursive: true, force: true });
2026-08-08 13:01:29 -05:00
console.log(`Cloning ${REPO} to ${WORKING_DIR}`);
2026-08-08 12:28:45 -05:00
2026-08-08 13:01:29 -05:00
await $`git clone --depth 1 https://github.com/${REPO} ${WORKING_DIR}`;
2026-08-08 12:28:45 -05:00
await mkdir(CHUNKS_DIR, { recursive: true });
2026-08-08 13:01:29 -05:00
console.log(`Discovering pre-existing chunks...`);
const discoveredChunks: (IdArray | null)[] = [];
2026-08-08 12:28:45 -05:00
for (let i = 0; true; i++) {
const path = `${CHUNKS_DIR}/chunk-${i}.dat.lz4`;
const file = Bun.file(path);
if (!(await file.exists())) break;
const bytes = await file.bytes();
2026-08-08 13:15:04 -05:00
const uncompressed = decompress(bytes);
2026-08-08 12:28:45 -05:00
2026-08-08 13:15:04 -05:00
const ids = IdArray.fromBytes(uncompressed);
2026-08-08 12:28:45 -05:00
discoveredChunks.push(ids);
}
2026-08-08 13:01:29 -05:00
console.log(`Found ${discoveredChunks.length} pre-existing chunks`);
2026-08-08 12:28:45 -05:00
let chunks: IdArray[] = [];
if (discoveredChunks.length === chunkCount) {
2026-08-08 13:01:29 -05:00
console.log("Pre-existing chunking is consistent, passing as-is...");
chunks = discoveredChunks as IdArray[];
2026-08-08 12:28:45 -05:00
} else {
2026-08-08 13:01:29 -05:00
console.log("Pre-existing chunking is not consistent, re-chunking...");
2026-08-08 12:28:45 -05:00
chunks = Array.from({ length: chunkCount }).map(() => new IdArray());
2026-08-08 13:01:29 -05:00
for (let i = 0; i < discoveredChunks.length; i++) {
const ids = discoveredChunks[i];
const size = ids!.size();
for (let j = 0; j < size; j++) {
const id = ids!.get(j);
const chunk = chunks[id % chunkCount];
chunk.push(id);
}
discoveredChunks[i] = null;
}
2026-08-08 13:15:04 -05:00
console.log("Sorting chunks...");
for (let i = 0; i < chunks.length; i++) chunks[i].sort();
2026-08-08 13:01:29 -05:00
}
2026-08-08 13:42:16 -05:00
const regions: RegionDescriptor[] = [
{ domain: "eu", seed: 5e8, max: 10e8 - 1, streak: 0 },
{ domain: "com", seed: 10e8, max: 20e8 - 1, streak: 0 },
{ domain: "asia", seed: 20e8, max: 31e8 - 1, streak: 0 },
2026-08-08 13:15:04 -05:00
];
2026-08-08 13:01:29 -05:00
2026-08-08 13:15:04 -05:00
console.log("Finding new seed ids...");
for (let i = 0; i < chunkCount; i++) {
const chunk = chunks[i];
const size = chunk.size();
for (let j = 0; j < size; j++) {
const id = chunk.get(j);
for (const region of regions) {
if (id <= region.max && id >= region.seed) {
region.seed = id + 1;
}
}
}
}
console.log("Found seeds:");
for (const region of regions) {
console.log(` ${region.domain}: ${region.seed.toLocaleString()}`);
}
2026-08-08 13:01:29 -05:00
2026-08-08 13:30:49 -05:00
console.log("Starting discovery loop...");
2026-08-08 13:42:16 -05:00
const queue: { region: RegionDescriptor; url: string; size: number }[] = [];
2026-08-08 13:30:49 -05:00
const applicationId = import.meta.env.PUBLIC_WARGAMING_APPLICATION_ID;
const fields = "account_id%2C-account_id";
let regionI = 0;
function fillQueue() {
if (queue.length >= 1) return;
regionI = (regionI + 1) % regions.length;
const region = regions[regionI];
2026-08-08 13:42:16 -05:00
if (region.streak >= MAX_DRY_STREAK) {
console.log(
`Removing region ${region.domain} removed due to ${region.streak} dry streak...`,
);
regions.splice(regionI, 1);
return;
}
2026-08-08 13:30:49 -05:00
const id0 = region.seed;
const id1 = Math.min(id0 + MAX_IDS_PER_CALL - 1, region.max);
const idRange = range(id0, id1 + 1);
2026-08-08 13:57:35 -05:00
region.seed += idRange.length;
2026-08-08 13:30:49 -05:00
const accountIds = idRange.join(",");
const url = `https://api.wotblitz.${
region.domain
}/wotb/account/info/?application_id=${applicationId}&fields=${
fields
}&account_id=${accountIds}`;
2026-08-08 13:42:16 -05:00
queue.push({ region, url, size: idRange.length });
2026-08-08 13:30:49 -05:00
if (idRange.length < MAX_IDS_PER_CALL) {
console.log(
`Removing region ${region.domain} removed due to ID range exhaustion...`,
);
regions.splice(regionI, 1);
}
}
function sleep() {
return new Promise((resolve) => setTimeout(resolve, 1000 / API_RATE));
}
while (performance.now() < t1 && regions.length > 0) {
await sleep();
fillQueue();
if (queue.length === 0) {
console.log("Queue empty, ending discovery loop...");
break;
}
2026-08-08 13:42:16 -05:00
const request = queue.shift()!;
const response = await fetch(request.url);
2026-08-08 13:30:49 -05:00
const data = (await response.json()) as QuickInfo;
if (data.status !== "ok") {
console.warn("Failed API call; pushing back into queue...");
2026-08-08 13:42:16 -05:00
queue.push(request);
2026-08-08 13:30:49 -05:00
continue;
}
2026-08-08 13:57:35 -05:00
let foundIds = 0;
2026-08-08 13:42:16 -05:00
2026-08-08 13:30:49 -05:00
for (const key in data.data) {
const value = data.data[key];
if (value === null) continue;
2026-08-08 13:57:35 -05:00
foundIds++;
2026-08-08 13:42:16 -05:00
2026-08-08 13:30:49 -05:00
const id = Number(key);
const chunkIndex = id % chunkCount;
const chunk = chunks[chunkIndex];
chunk.push(id);
}
2026-08-08 13:42:16 -05:00
2026-08-08 13:57:35 -05:00
if (foundIds === 0) {
2026-08-08 13:42:16 -05:00
request.region.streak += request.size;
2026-08-08 13:57:35 -05:00
} else {
// console.log(`Found ${foundIds} ids in ${request.region.domain}`);
2026-08-08 13:42:16 -05:00
}
2026-08-08 13:30:49 -05:00
}
2026-08-08 13:01:29 -05:00
console.log("Saving chunks...");
2026-08-08 13:15:04 -05:00
await rm(`${CHUNKS_DIR}/*`, { force: true });
2026-08-08 13:57:35 -05:00
let totalFound = 0;
2026-08-08 13:01:29 -05:00
for (let i = 0; i < chunks.length; i++) {
const path = `${CHUNKS_DIR}/chunk-${i}.dat.lz4`;
const chunk = chunks[i];
const bytes = chunk.toBytes();
2026-08-08 13:15:04 -05:00
const compressed = compress(bytes);
2026-08-08 13:01:29 -05:00
2026-08-08 13:57:35 -05:00
totalFound += chunk.size();
2026-08-08 13:01:29 -05:00
await Bun.write(path, compressed);
2026-08-08 12:28:45 -05:00
}
2026-08-08 13:42:16 -05:00
const git = $.cwd(WORKING_DIR);
await git`git add .`;
await git`git commit --amend -m "ids update ${new Date().toISOString()}"`;
await git`git push --force-with-lease`;
2026-08-08 13:57:35 -05:00
console.log(
`Committed ${totalFound.toLocaleString()} total ids across ${chunkCount} chunks`,
);