Add puzzle hints and per-word descriptions with sources
Hint is revealed on demand below the board while playing. After the game ends, each word's description and source link are listed below the puzzle, and hovering a solved word highlights its entry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4a2309b589
commit
b2f8e879ab
7 changed files with 141 additions and 21 deletions
23
lib.js
23
lib.js
|
|
@ -9,7 +9,11 @@ function isRealDate(s) {
|
|||
return !isNaN(d) && d.toISOString().slice(0, 10) === s;
|
||||
}
|
||||
|
||||
// Returns { errors: [...] } or { game: { title, date, groups } } with cleaned values.
|
||||
function isHttpUrl(s) {
|
||||
try { return ['http:', 'https:'].includes(new URL(s).protocol); } catch { return false; }
|
||||
}
|
||||
|
||||
// Returns { errors: [...] } or { game: { title, date, hint, groups } } with cleaned values.
|
||||
function validateGame(input) {
|
||||
const errors = [];
|
||||
const title = String(input?.title ?? '').trim();
|
||||
|
|
@ -19,6 +23,9 @@ function validateGame(input) {
|
|||
let date = input?.date ? String(input.date).trim() : null;
|
||||
if (date && !isRealDate(date)) errors.push('Date must be a real date (YYYY-MM-DD).');
|
||||
|
||||
const hint = String(input?.hint ?? '').trim();
|
||||
if (hint.length > 200) errors.push('Hint must be 200 characters or fewer.');
|
||||
|
||||
const rawGroups = Array.isArray(input?.groups) ? input.groups : [];
|
||||
if (rawGroups.length !== 4) errors.push('A game needs exactly 4 groups.');
|
||||
|
||||
|
|
@ -40,10 +47,20 @@ function validateGame(input) {
|
|||
if (w && seen.has(w)) errors.push(`"${w}" appears more than once.`);
|
||||
seen.add(w);
|
||||
}
|
||||
return { name, color, words };
|
||||
// Optional per-word description and source link, shown after the puzzle ends.
|
||||
const rawDetails = Array.isArray(g?.details) ? g.details : [];
|
||||
const details = words.map((w, j) => {
|
||||
const description = String(rawDetails[j]?.description ?? '').trim();
|
||||
const source = String(rawDetails[j]?.source ?? '').trim();
|
||||
if (description.length > 500) errors.push(`Description for "${w}" must be 500 characters or fewer.`);
|
||||
if (source.length > 500) errors.push(`Source for "${w}" must be 500 characters or fewer.`);
|
||||
else if (source && !isHttpUrl(source)) errors.push(`Source for "${w}" must be a full http(s) link.`);
|
||||
return { description, source };
|
||||
});
|
||||
return { name, color, words, details };
|
||||
});
|
||||
|
||||
return errors.length ? { errors: [...new Set(errors)] } : { game: { title, date, groups } };
|
||||
return errors.length ? { errors: [...new Set(errors)] } : { game: { title, date, hint, groups } };
|
||||
}
|
||||
|
||||
// Returns { result: 'correct', group } | { result: 'one-away' } | { result: 'wrong' } | { error }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue