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; } 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(); 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 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.'); 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); } // 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, hint, 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 };