Files
quartiles/scripts/build-all-data.mjs
2026-10-30 15:16:17 -04:00

54 lines
2.1 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import { extraWords } from './extra-words.mjs';
import { DICTIONARY_SET } from '../src/data/lexicon.js';
// Build and validate target words pool for Quartiles
export const targetWordsPool = [];
{
const existing = new Set();
let added = 0;
for (const item of extraWords) {
const problems = [];
if (existing.has(item.word)) problems.push('duplicate word');
if (item.tiles.length !== 4) problems.push('not 4 tiles');
if (item.tiles.join('') !== item.word) problems.push(`tiles spell "${item.tiles.join('')}"`);
if (!item.tiles.every(t => t.length >= 2)) problems.push('tile < 2 letters');
if (new Set(item.tiles).size !== item.tiles.length) problems.push('repeated tile within word');
if (!DICTIONARY_SET.has(item.word)) problems.push('not in runtime dictionary');
if (problems.length) {
console.warn(` Skipping target word "${item.word}": ${problems.join(', ')}`);
continue;
}
existing.add(item.word);
targetWordsPool.push(item);
added++;
}
console.log(`Loaded and validated ${added} target words.`);
}
// Validate pool integrity
for (const item of targetWordsPool) {
if (item.tiles.join('') !== item.word) {
throw new Error(`Word spelling mismatch: ${item.word} !== ${item.tiles.join('')}`);
}
if (!item.tiles.every(t => t.length >= 2)) {
throw new Error(`Word contains tile < 2 chars: ${item.word} -> ${JSON.stringify(item.tiles)}`);
}
if (item.tiles.length !== 4) {
throw new Error(`Word does not have 4 tiles: ${item.word}`);
}
}
console.log('Target words pool verified! Total words:', targetWordsPool.length);
// Puzzles are generated from this pool at runtime (src/engine/generator.js):
// daily puzzles are seeded by date, unlimited puzzles are random.
const puzzlesFileContent = `// Target word pool for generated Quartiles puzzles (built by scripts/build-all-data.mjs)
export const TARGET_WORDS_POOL = ${JSON.stringify(targetWordsPool, null, 2)};
`;
fs.mkdirSync('src/data', { recursive: true });
fs.writeFileSync('src/data/puzzles.js', puzzlesFileContent, 'utf8');
console.log('Saved src/data/puzzles.js successfully!');