refactor(loldle): source all champion data from loldle.net

loldle.net's JS bundle ships the complete set of classic-mode axes in
plaintext, so ddragon merging is no longer needed. Scraper now produces
the final schema directly.

Schema changes: drop title, skinCount, image, and genre (ddragon-only).
Replace genre (class tags like Fighter/Mage) with species (Human/Darkin/
Vastayan) — the axis loldle.net actually uses. Promote region to a
multi-value field so multi-region champions compare correctly.

Handlers no longer show "Name — Title" on win/giveup.
This commit is contained in:
2026-04-22 13:19:10 +07:00
parent 9855b4d7d0
commit 0836f02ab8
8 changed files with 1443 additions and 5211 deletions
+19 -75
View File
@@ -1,16 +1,15 @@
#!/usr/bin/env node
/**
* @file scrape-loldle-data — rebuilds src/modules/loldle/champions.json by
* scraping loldle.net for canonical game fields (gender, positions,
* range_type, regions, release_date) and merging with ddragon championFull
* for display fields (title, resource, genre tags, skin count, sprite image).
* @file scrape-loldle-data — rebuilds src/modules/loldle/champions.json from
* loldle.net's JS bundle, the canonical source for the classic-mode axes:
* gender, species, resource, attackType, region, lane, releaseDate.
*
* loldle.net embeds the full champion array in plaintext inside its JS bundle
* at `<script src="js/index.<hash>.js">`, one record per champion with the
* exact shape the bot needs. No CryptoJS decoding, no ddragon merge.
*
* Writes both champions.json (authoring format) and champions-data.js (ESM
* wrapper consumed by the bot). Replaces the hand-run build-loldle-data step.
*
* Source of truth — loldle.net embeds the full champion array in plaintext
* inside its JS bundle at `<script src="js/index.<hash>.js">`, one record per
* champion with the exact shape the bot needs. No CryptoJS decoding needed.
* wrapper consumed by the bot).
*
* Usage:
* node scripts/scrape-loldle-data.js
@@ -22,9 +21,6 @@ import { writeFileSync } from "node:fs";
import { resolve } from "node:path";
const LOLDLE_CLASSIC = "https://loldle.net/classic";
const DDRAGON_VERSIONS = "https://ddragon.leagueoflegends.com/api/versions.json";
const ddragonChampUrl = (v) =>
`https://ddragon.leagueoflegends.com/cdn/${v}/data/en_US/championFull.json`;
const LANE_MAP = {
top: "top",
@@ -37,7 +33,7 @@ const LANE_MAP = {
const GENDER_MAP = { male: "male", female: "female", other: "divers" };
const CHAMPION_RECORD_RX =
/\{_id:"[a-f0-9]+",championId:"[^"]+",championName:"([^"]+)",gender:"([^"]+)",positions:\[([^\]]+)\],species:\[[^\]]+\],resource:"[^"]+",range_type:\[([^\]]+)\],regions:\[([^\]]+)\],release_date:"(\d{4})-\d{2}-\d{2}"\}/g;
/\{_id:"[a-f0-9]+",championId:"[^"]+",championName:"([^"]+)",gender:"([^"]+)",positions:\[([^\]]+)\],species:\[([^\]]+)\],resource:"([^"]+)",range_type:\[([^\]]+)\],regions:\[([^\]]+)\],release_date:"(\d{4})-\d{2}-\d{2}"\}/g;
async function fetchText(url) {
const res = await fetch(url);
@@ -45,12 +41,6 @@ async function fetchText(url) {
return res.text();
}
async function fetchJson(url) {
const res = await fetch(url);
if (!res.ok) throw new Error(`fetch ${url}: ${res.status} ${res.statusText}`);
return res.json();
}
function parseJsArrayStrings(inner) {
return [...inner.matchAll(/"([^"]+)"/g)].map((m) => m[1]);
}
@@ -69,7 +59,7 @@ async function scrapeLoldle() {
const seen = new Set();
const records = [];
for (const m of bundle.matchAll(CHAMPION_RECORD_RX)) {
const [, name, gender, positionsRaw, rangeTypeRaw, regionsRaw, year] = m;
const [, name, gender, positionsRaw, speciesRaw, resource, rangeTypeRaw, regionsRaw, year] = m;
if (seen.has(name)) continue;
seen.add(name);
@@ -77,14 +67,18 @@ async function scrapeLoldle() {
.map((p) => LANE_MAP[p.toLowerCase()])
.filter(Boolean);
const regions = parseJsArrayStrings(regionsRaw).map(normalizeRegion);
const species = parseJsArrayStrings(speciesRaw).map((s) => s.toLowerCase());
const rangeType = parseJsArrayStrings(rangeTypeRaw)[0]?.toLowerCase();
records.push({
id: name,
name,
gender: GENDER_MAP[gender.toLowerCase()] ?? "divers",
species: species.join(","),
resource,
attackType: rangeType === "melee" ? "close" : "range",
lane: lanes.join(","),
region: regions.join(","),
lane: lanes.join(","),
releaseDate: Number(year),
});
}
@@ -94,69 +88,19 @@ async function scrapeLoldle() {
"loldle.net: zero champion records parsed — bundle format changed, update CHAMPION_RECORD_RX",
);
}
records.sort((a, b) => a.name.localeCompare(b.name));
return records;
}
async function fetchDdragon() {
const versions = await fetchJson(DDRAGON_VERSIONS);
const version = versions[0];
const full = await fetchJson(ddragonChampUrl(version));
return { version, champions: full.data };
}
function mergeRecords(loldleRecords, ddragonChampions) {
const byName = new Map(loldleRecords.map((r) => [r.name, r]));
const merged = [];
const missing = [];
for (const champ of Object.values(ddragonChampions)) {
const lol = byName.get(champ.name);
if (!lol) {
missing.push(champ.name);
continue;
}
merged.push({
id: champ.id,
name: champ.name,
title: champ.title,
resource: champ.partype,
genre: champ.tags.join(","),
skinCount: champ.skins.length,
image: champ.image,
gender: lol.gender,
attackType: lol.attackType,
releaseDate: lol.releaseDate,
region: lol.region,
lane: lol.lane,
});
}
if (missing.length > 0) {
console.warn(
`warn: ${missing.length} ddragon champions absent from loldle.net (likely just-released): ${missing.join(", ")}`,
);
}
merged.sort((a, b) => a.name.localeCompare(b.name));
return merged;
}
const root = resolve(import.meta.dirname, "..");
const jsonPath = resolve(root, "src/modules/loldle/champions.json");
const esmPath = resolve(root, "src/modules/loldle/champions-data.js");
console.log("scraping loldle.net…");
const loldleRecords = await scrapeLoldle();
console.log(` parsed ${loldleRecords.length} champions from loldle.net`);
const records = await scrapeLoldle();
console.log(` parsed ${records.length} champions`);
console.log("fetching ddragon championFull…");
const { version, champions } = await fetchDdragon();
console.log(` ddragon ${version}: ${Object.keys(champions).length} champions`);
const merged = mergeRecords(loldleRecords, champions);
console.log(`merged ${merged.length} champions`);
const json = JSON.stringify(merged, null, 4);
const json = JSON.stringify(records, null, 4);
writeFileSync(jsonPath, `${json}\n`);
console.log(`wrote ${jsonPath}`);