116 lines
3.5 KiB
TypeScript
116 lines
3.5 KiB
TypeScript
import { registerPuzzle, type PuzzleContext } from './registry';
|
|
|
|
/**
|
|
* Mini-jeu « code couleur » : N boutons qui font défiler des couleurs, un
|
|
* bouton de validation. Paramètres JSON :
|
|
* {
|
|
* "colors": [{ "id": "rouge", "label": "Rouge", "hex": "#d64541" }, ...],
|
|
* "solution": ["jaune", "rouge", "bleu", "vert"],
|
|
* "caption": "texte gravé sur la plaque (optionnel)"
|
|
* }
|
|
*/
|
|
interface ColorDef {
|
|
id: string;
|
|
label: string;
|
|
hex: string;
|
|
}
|
|
interface Params {
|
|
colors: ColorDef[];
|
|
solution: string[];
|
|
caption?: string;
|
|
}
|
|
interface Data {
|
|
/** Index de couleur par emplacement, -1 = éteint. */
|
|
slots: number[];
|
|
}
|
|
|
|
function createColorCode(ctx: PuzzleContext<Data>) {
|
|
const params = ctx.def.params as unknown as Params;
|
|
const n = params.solution.length;
|
|
const slots = ctx.data?.slots?.length === n ? [...ctx.data.slots] : Array<number>(n).fill(-1);
|
|
if (ctx.alreadySolved) {
|
|
params.solution.forEach((id, i) => (slots[i] = params.colors.findIndex((c) => c.id === id)));
|
|
}
|
|
let locked = ctx.alreadySolved;
|
|
|
|
const root = document.createElement('div');
|
|
root.className = 'cc-panel';
|
|
root.innerHTML = `
|
|
<div class="cc-screws"><i></i><i></i><i></i><i></i></div>
|
|
<div class="cc-plate">${params.caption ?? ''}</div>
|
|
<div class="cc-slots"></div>
|
|
<div class="cc-bottom">
|
|
<div class="cc-led"></div>
|
|
<button class="cc-validate">VALIDER</button>
|
|
</div>
|
|
<div class="cc-help">Clic : couleur suivante · Clic droit : couleur précédente</div>`;
|
|
const slotsEl = root.querySelector<HTMLElement>('.cc-slots')!;
|
|
const led = root.querySelector<HTMLElement>('.cc-led')!;
|
|
const validate = root.querySelector<HTMLButtonElement>('.cc-validate')!;
|
|
|
|
const buttons = slots.map((_, i) => {
|
|
const wrap = document.createElement('div');
|
|
wrap.className = 'cc-slot';
|
|
const num = document.createElement('span');
|
|
num.className = 'cc-num';
|
|
num.textContent = String(i + 1);
|
|
const btn = document.createElement('button');
|
|
btn.className = 'cc-button';
|
|
btn.dataset.slot = String(i);
|
|
const cycle = (dir: number) => {
|
|
if (locked) return;
|
|
const count = params.colors.length;
|
|
slots[i] = slots[i] < 0 ? (dir > 0 ? 0 : count - 1) : (slots[i] + dir + count) % count;
|
|
ctx.sound('click');
|
|
led.className = 'cc-led';
|
|
paint();
|
|
ctx.save({ slots: [...slots] });
|
|
};
|
|
btn.onclick = () => cycle(1);
|
|
btn.oncontextmenu = (e) => {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
cycle(-1);
|
|
};
|
|
wrap.append(num, btn);
|
|
slotsEl.appendChild(wrap);
|
|
return btn;
|
|
});
|
|
|
|
function paint() {
|
|
buttons.forEach((btn, i) => {
|
|
const c = params.colors[slots[i]];
|
|
btn.style.setProperty('--c', c ? c.hex : '#2a2a2a');
|
|
btn.classList.toggle('lit', !!c);
|
|
btn.title = c ? c.label : 'Éteint';
|
|
btn.setAttribute('aria-label', `Emplacement ${i + 1} : ${c ? c.label : 'éteint'}`);
|
|
});
|
|
if (locked) {
|
|
led.className = 'cc-led ok';
|
|
validate.disabled = true;
|
|
}
|
|
}
|
|
|
|
validate.onclick = () => {
|
|
if (locked) return;
|
|
const ok = params.solution.every((id, i) => params.colors[slots[i]]?.id === id);
|
|
if (ok) {
|
|
locked = true;
|
|
paint();
|
|
root.classList.add('solved');
|
|
ctx.solve();
|
|
} else {
|
|
ctx.sound('error');
|
|
led.className = 'cc-led ko';
|
|
root.classList.remove('shake');
|
|
void root.offsetWidth;
|
|
root.classList.add('shake');
|
|
}
|
|
};
|
|
|
|
paint();
|
|
return { el: root };
|
|
}
|
|
|
|
registerPuzzle('colorCode', createColorCode);
|