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

252 lines
6.1 KiB
TypeScript
Raw Normal View History

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 14:09:48 -05:00
import { exit } from "node:process";
2026-08-09 20:26:39 -05:00
import { GitObjectStorage } from "../core/blitzkit/gitObjectStorage";
import { IdArray } from "../core/blitzkit/idArray";
import {
ACCOMMODATED_ID_COUNT,
MAX_BYTES_PER_CHUNK,
MAX_DRY_STREAK,
MAX_IDS_PER_CALL,
} from "./constants";
2026-08-11 20:03:57 -05:00
import { sleep, withinTimeLimit } from "./time";
import { IdsManifest, QuickInfo, RegionDescriptor } from "./types";
2026-08-08 12:28:45 -05:00
2026-08-09 20:26:39 -05:00
const storage = await new GitObjectStorage(
2026-08-11 20:03:57 -05:00
"blitzkit/data-ids",
"../../temp/ids",
2026-08-09 20:26:39 -05:00
import.meta.env.GH_TOKEN,
).init();
2026-08-08 13:30:49 -05:00
2026-08-09 20:26:39 -05:00
await storage.mkdir("chunks");
2026-08-08 12:28:45 -05:00
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
2026-08-11 20:03:57 -05:00
const manifest = await storage.json<IdsManifest>("manifest.json");
2026-08-09 13:55:15 -05:00
for (let i = 0; i < manifest.chunks; i++) {
2026-08-09 20:26:39 -05:00
const bytes = await storage.bytes(`chunks/chunk-${i}.dat.lz4`);
2026-08-08 13:15:04 -05:00
const uncompressed = decompress(bytes);
const ids = IdArray.fromBytes(uncompressed);
2026-08-08 12:28:45 -05:00
discoveredChunks.push(ids);
}
2026-08-09 13:55:15 -05:00
console.log(`Found ${manifest.chunks} pre-existing chunks`);
manifest.chunks = Math.ceil(
(ACCOMMODATED_ID_COUNT * Uint32Array.BYTES_PER_ELEMENT) / MAX_BYTES_PER_CHUNK,
);
2026-08-08 13:01:29 -05:00
2026-08-08 12:28:45 -05:00
let chunks: IdArray[] = [];
2026-08-09 13:55:15 -05:00
if (discoveredChunks.length === manifest.chunks) {
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-09 13:55:15 -05:00
chunks = Array.from({ length: manifest.chunks }).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);
2026-08-09 13:55:15 -05:00
const chunk = chunks[id % manifest.chunks];
2026-08-08 13:01:29 -05:00
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[] = [
2026-08-09 16:12:26 -05:00
{ domain: "eu", seed: 5e8, max: 10e8 - 1, dry_streak: 0 },
{ domain: "com", seed: 10e8, max: 20e8 - 1, dry_streak: 0 },
{ domain: "asia", seed: 20e8, max: 31e8 - 1, dry_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...");
2026-08-09 13:55:15 -05:00
for (let i = 0; i < manifest.chunks; i++) {
2026-08-08 13:15:04 -05:00
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 14:09:48 -05:00
let isSaving = false;
async function save() {
if (isSaving) return;
isSaving = true;
console.log("Saving chunks...");
2026-08-09 20:26:39 -05:00
await storage.rm("chunks/*");
2026-08-08 14:09:48 -05:00
let totalFound = 0;
for (let i = 0; i < chunks.length; i++) {
const chunk = chunks[i];
const bytes = chunk.toBytes();
const compressed = compress(bytes);
totalFound += chunk.size();
2026-08-09 20:26:39 -05:00
await storage.write(`chunks/chunk-${i}.dat.lz4`, compressed);
2026-08-08 14:09:48 -05:00
}
2026-08-09 14:06:40 -05:00
manifest.total_count = totalFound;
2026-08-09 20:26:39 -05:00
await storage.write("manifest.json", JSON.stringify(manifest, null, 2));
2026-08-09 13:55:15 -05:00
2026-08-09 20:26:39 -05:00
await storage.commit();
2026-08-08 14:09:48 -05:00
console.log(
2026-08-09 13:55:15 -05:00
`Committed ${totalFound.toLocaleString()} total ids across ${manifest.chunks} chunks`,
2026-08-08 14:09:48 -05:00
);
exit(0);
}
2026-08-08 13:30:49 -05:00
2026-08-08 14:08:35 -05:00
process.on("SIGINT", save);
process.on("SIGTERM", save);
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;
2026-08-09 16:12:26 -05:00
while (regions.length > 0) {
regionI = (regionI + 1) % regions.length;
const region = regions[regionI];
if (region.dry_streak >= MAX_DRY_STREAK) {
console.log(
`Removing region ${region.domain} removed due to ${region.dry_streak} dry streak...`,
);
regions.splice(regionI, 1);
continue;
}
const id0 = region.seed;
const id1 = Math.min(id0 + MAX_IDS_PER_CALL - 1, region.max);
const idRange = range(id0, id1 + 1);
region.seed += idRange.length;
const accountIds = idRange.join(",");
const url = `https://api.wotblitz.${
region.domain
}/wotb/account/info/?application_id=${applicationId}&fields=${
fields
}&account_id=${accountIds}`;
queue.push({ region, url, size: idRange.length });
if (idRange.length < MAX_IDS_PER_CALL) {
console.log(
`Removing region ${region.domain} removed due to ID range exhaustion...`,
);
regions.splice(regionI, 1);
}
2026-08-08 13:30:49 -05:00
2026-08-08 13:42:16 -05:00
return;
}
2026-08-08 13:30:49 -05:00
}
console.log("Starting discovery loop...");
while (withinTimeLimit() && regions.length > 0) {
2026-08-08 13:30:49 -05:00
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);
2026-08-09 13:55:15 -05:00
const chunkIndex = id % manifest.chunks;
2026-08-08 13:30:49 -05:00
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-09 16:12:26 -05:00
request.region.dry_streak += request.size;
2026-08-08 13:57:35 -05:00
} else {
2026-08-09 16:12:26 -05:00
request.region.dry_streak = 0;
2026-08-08 13:57:35 -05:00
// 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
}
// console.log("Starting verification loop...");
// while (withinTimeLimit()) {
// const verificationIndex = (manifest.last_verified + 1) % manifest.chunks;
// manifest.last_verified = verificationIndex;
// const chunk = chunks[verificationIndex];
// const size = chunk.size();
// let i0 = 0;
// while (withinTimeLimit() && i0 < size) {
// await sleep();
// const i1 = Math.min(i0 + MAX_IDS_PER_CALL - 1, size - 1);
// const indices = range(i0, i1 + 1);
// }
// console.log(`Verification complete for chunk ${verificationIndex}`);
// }
2026-08-08 14:08:35 -05:00
await save();