NT-Connections/lib.js
Aidan Dona 4a2309b589 Add Connections-style word game with puzzle editor
Express + node:sqlite server, vanilla HTML/JS frontend. Daily puzzle by
assigned date, puzzle list, shuffle, one-away hint, shareable emoji
results, and a password-protected admin page to create and edit puzzles.
Answers are checked server-side and never sent before they are solved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 16:56:34 -05:00

71 lines
2.7 KiB
JavaScript

const COLORS = ['yellow', 'green', 'blue', 'purple'];
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
const norm = (w) => String(w).trim().toUpperCase();
function isRealDate(s) {
if (!DATE_RE.test(s)) return false;
const d = new Date(s + 'T00:00:00Z');
return !isNaN(d) && d.toISOString().slice(0, 10) === s;
}
// Returns { errors: [...] } or { game: { title, date, groups } } with cleaned values.
function validateGame(input) {
const errors = [];
const title = String(input?.title ?? '').trim();
if (!title) errors.push('Title is required.');
if (title.length > 100) errors.push('Title must be 100 characters or fewer.');
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 rawGroups = Array.isArray(input?.groups) ? input.groups : [];
if (rawGroups.length !== 4) errors.push('A game needs exactly 4 groups.');
const seen = new Set();
const colors = new Set();
const groups = rawGroups.slice(0, 4).map((g, i) => {
const n = i + 1;
const name = String(g?.name ?? '').trim();
if (!name) errors.push(`Group ${n} needs a name.`);
if (name.length > 80) errors.push(`Group ${n} name must be 80 characters or fewer.`);
const color = g?.color;
if (!COLORS.includes(color)) errors.push(`Group ${n} needs a color.`);
else if (colors.has(color)) errors.push(`Color ${color} is used more than once.`);
colors.add(color);
const words = (Array.isArray(g?.words) ? g.words : []).map(norm);
if (words.length !== 4 || words.some((w) => !w)) errors.push(`Group ${n} needs 4 words.`);
for (const w of words) {
if (w.length > 30) errors.push(`"${w}" is longer than 30 characters.`);
if (w && seen.has(w)) errors.push(`"${w}" appears more than once.`);
seen.add(w);
}
return { name, color, words };
});
return errors.length ? { errors: [...new Set(errors)] } : { game: { title, date, groups } };
}
// Returns { result: 'correct', group } | { result: 'one-away' } | { result: 'wrong' } | { error }
function checkGuess(groups, guess) {
const words = Array.isArray(guess) ? [...new Set(guess.map(norm))] : [];
if (words.length !== 4) return { error: 'Pick 4 different words.' };
let best = 0;
for (const g of groups) {
const hits = words.filter((w) => g.words.includes(w)).length;
if (hits === 4) return { result: 'correct', group: g };
best = Math.max(best, hits);
}
return { result: best === 3 ? 'one-away' : 'wrong' };
}
function shuffle(arr) {
const a = [...arr];
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
}
module.exports = { COLORS, isRealDate, validateGame, checkGuess, shuffle };