Syntax Saga: neues Spiel (server-autoritativ) + 34 Seed-Missionen
Drittes Mindflux-Spiel: interaktive IT-Geschichten, in denen man den fehlenden Code (Schlüsselwörter, Operatoren, ganze Zeilen) in eine kleine Story tippt. Freitext-Eingabe, drei wählbare Stufen (Junior/Professional/ Senior), mehrere Lücken je Codeblock und mehrere gültige Antworten je Lücke (optional case-insensitive). Prüfung und Score ausschließlich serverseitig – die Lösungen verlassen nie das Backend (Anti-Cheat wie TechTacular/MindMatch). Fehler-Mechanik: bis zu 3 Versuche mit Hinweis, dann Auflösung; kein Game-Over. Inhalte DB-gestützt und im Admin pflegbar (/admin/syntaxsaga); 34 Seed- Missionen (Java/SQL/Bash/Git/HTML/Python) aus syntaxsaga/content.json. - Entities SyntaxSagaMission/Gap/Solution + Flyway V7__syntax_saga.sql - SyntaxSagaService (Story ohne Lösung / serverseitige Prüfung / Score-Recompute) - API /api/syntaxsaga (CSRF-ausgenommen) + Admin-CRUD - Frontend syntaxsaga.html/js/css (Prefix ss-, theme.css-Tokens) - Dashboard-Kachel aktiv (vorher "coming soon") Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,247 @@
|
||||
/* === Syntax Saga === Interaktive IT-Geschichten mit Code-Lücken.
|
||||
Farben/Tokens zentral aus theme.css (richtig = Blau, falsch = Rot, Akzent = Amber).
|
||||
Anders als TechTacular ist der Inhalt scrollbar (Story + Codeblock können hoch werden). */
|
||||
|
||||
@keyframes ssFadeInUp {
|
||||
0% { opacity: 0; transform: translateY(16px); }
|
||||
100% { opacity: 1; transform: translateY(0); }
|
||||
}
|
||||
|
||||
body.ss-body {
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.ss-shell {
|
||||
flex: 1;
|
||||
width: 100%;
|
||||
max-width: 980px;
|
||||
margin: 0 auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
padding: 0.7rem 0.9rem 1.4rem;
|
||||
gap: 0.7rem;
|
||||
}
|
||||
|
||||
/* --- Kopf + Leiste --- */
|
||||
.ss-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 0.5rem 1.1rem;
|
||||
background-color: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 12px;
|
||||
box-shadow: var(--g-soft), 0 0 8px var(--glow-w);
|
||||
}
|
||||
.ss-header-tools { display: flex; align-items: center; gap: 0.7rem; }
|
||||
.ss-title { font-size: 1.3rem; color: var(--accent); margin: 0; text-shadow: 0 0 8px var(--glow-s); }
|
||||
.ss-back, .ss-logout { text-decoration: none; font-weight: bold; }
|
||||
.ss-back { color: var(--accent); }
|
||||
.ss-logout { color: var(--danger); }
|
||||
|
||||
.ss-bar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.55rem 0.85rem;
|
||||
}
|
||||
.ss-bar-group { display: flex; align-items: center; flex-wrap: wrap; gap: 0.5rem; }
|
||||
.ss-bar-label { color: var(--muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; }
|
||||
|
||||
.ss-diff {
|
||||
padding: 0.45rem 0.9rem;
|
||||
border: 1px solid var(--border-accent);
|
||||
border-radius: 999px;
|
||||
background: var(--surface-2);
|
||||
color: var(--text);
|
||||
font-weight: 700;
|
||||
font-size: 0.85rem;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
|
||||
}
|
||||
.ss-diff:hover { border-color: var(--accent); }
|
||||
.ss-diff.active {
|
||||
background: linear-gradient(100deg, var(--accent), var(--accent-2));
|
||||
color: var(--accent-ink);
|
||||
box-shadow: 0 0 16px var(--glow-s);
|
||||
}
|
||||
|
||||
/* --- HUD --- */
|
||||
.ss-hud { gap: 0.4rem; }
|
||||
.ss-stat {
|
||||
display: inline-flex;
|
||||
align-items: baseline;
|
||||
gap: 0.4rem;
|
||||
padding: 0.35rem 0.7rem;
|
||||
background: var(--surface-2);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 10px;
|
||||
font-size: 0.7rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
color: var(--muted);
|
||||
white-space: nowrap;
|
||||
}
|
||||
.ss-stat b { font-size: 1.05rem; color: var(--accent); }
|
||||
|
||||
/* --- Bühne / Szene --- */
|
||||
.ss-stage { flex: 1; display: flex; flex-direction: column; }
|
||||
.ss-idle { margin: auto; color: var(--muted); text-align: center; padding: 2.4rem 1rem; max-width: 540px; line-height: 1.6; }
|
||||
|
||||
.ss-scene {
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border-accent);
|
||||
border-radius: 16px;
|
||||
padding: 1.3rem 1.5rem 1.5rem;
|
||||
box-shadow: 0 0 24px var(--glow-w);
|
||||
animation: ssFadeInUp 0.35s ease-out;
|
||||
}
|
||||
|
||||
.ss-scene-head { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; margin-bottom: 0.5rem; }
|
||||
.ss-mission-no { color: var(--muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.07em; font-weight: 700; }
|
||||
.ss-badge {
|
||||
font-size: 0.66rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
padding: 0.16rem 0.5rem;
|
||||
border-radius: 7px;
|
||||
border: 1px solid var(--border-accent);
|
||||
}
|
||||
.ss-badge-diff { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); }
|
||||
.ss-badge-lang { background: color-mix(in srgb, var(--secondary) 16%, transparent); color: var(--secondary); border-color: var(--secondary); font-family: monospace; }
|
||||
|
||||
.ss-mission-title { color: var(--heading); font-size: 1.4rem; font-weight: 800; margin: 0.1rem 0 0.6rem; }
|
||||
.ss-narrative { color: var(--text); line-height: 1.6; margin-bottom: 0.6rem; }
|
||||
.ss-dialog {
|
||||
color: var(--accent);
|
||||
font-style: italic;
|
||||
border-left: 3px solid var(--accent);
|
||||
padding-left: 0.8rem;
|
||||
margin-bottom: 0.9rem;
|
||||
text-shadow: 0 0 10px var(--glow-w);
|
||||
}
|
||||
|
||||
/* --- Codeblock mit Lücken --- */
|
||||
.ss-code {
|
||||
background: var(--surface-2);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 12px;
|
||||
padding: 1rem 1.1rem;
|
||||
margin: 0.4rem 0 0.9rem;
|
||||
overflow-x: auto;
|
||||
box-shadow: inset 0 0 22px var(--glow-w);
|
||||
}
|
||||
.ss-code code {
|
||||
font-family: "JetBrains Mono", "Fira Code", "Consolas", "Courier New", monospace;
|
||||
font-size: 0.95rem;
|
||||
line-height: 2;
|
||||
white-space: pre;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.ss-gap {
|
||||
font-family: inherit;
|
||||
font-size: inherit;
|
||||
color: var(--accent);
|
||||
background: color-mix(in srgb, var(--accent) 10%, transparent);
|
||||
border: 1px solid var(--border-accent);
|
||||
border-radius: 5px;
|
||||
padding: 0.05rem 0.35rem;
|
||||
margin: 0 0.12rem;
|
||||
min-width: 3ch;
|
||||
vertical-align: baseline;
|
||||
transition: border-color 0.2s ease, background 0.2s ease, color 0.2s ease;
|
||||
}
|
||||
.ss-gap::placeholder { color: var(--muted); opacity: 0.7; }
|
||||
.ss-gap:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px var(--glow-w); background: color-mix(in srgb, var(--accent) 16%, transparent); }
|
||||
.ss-gap.correct { color: var(--success); border-color: var(--success); background: color-mix(in srgb, var(--success) 16%, transparent); }
|
||||
.ss-gap.wrong { color: var(--danger); border-color: var(--danger); background: color-mix(in srgb, var(--danger) 16%, transparent); }
|
||||
.ss-gap.revealed { color: var(--secondary); border-color: var(--secondary); background: color-mix(in srgb, var(--secondary) 14%, transparent); }
|
||||
.ss-gap:disabled { cursor: default; }
|
||||
|
||||
/* --- Feedback / Erfolg --- */
|
||||
.ss-feedback { min-height: 1.3rem; font-weight: 700; margin-bottom: 0.5rem; line-height: 1.5; }
|
||||
.ss-feedback.correct { color: var(--success); }
|
||||
.ss-feedback.correct::before { content: "\2713 "; font-weight: 800; }
|
||||
.ss-feedback.wrong { color: var(--danger); }
|
||||
.ss-feedback.wrong::before { content: "\2717 "; font-weight: 800; }
|
||||
.ss-feedback.revealed { color: var(--secondary); }
|
||||
|
||||
.ss-success {
|
||||
color: var(--secondary);
|
||||
background: color-mix(in srgb, var(--secondary) 10%, transparent);
|
||||
border-left: 3px solid var(--secondary);
|
||||
border-radius: 6px;
|
||||
padding: 0.7rem 0.9rem;
|
||||
margin-bottom: 0.8rem;
|
||||
line-height: 1.55;
|
||||
}
|
||||
.ss-success[hidden] { display: none; }
|
||||
|
||||
/* --- Buttons --- */
|
||||
.ss-scene-actions { display: flex; gap: 0.7rem; flex-wrap: wrap; }
|
||||
.ss-btn {
|
||||
padding: 0.55rem 1.2rem;
|
||||
border: 0;
|
||||
border-radius: 9px;
|
||||
background: var(--accent);
|
||||
color: var(--accent-ink);
|
||||
font-weight: 800;
|
||||
font-size: 0.9rem;
|
||||
cursor: pointer;
|
||||
text-decoration: none;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
box-shadow: 0 0 16px var(--glow-w);
|
||||
transition: filter 0.2s ease, transform 0.15s ease;
|
||||
}
|
||||
.ss-btn:hover { filter: brightness(1.07); transform: translateY(-2px); }
|
||||
.ss-btn[hidden] { display: none; }
|
||||
.ss-btn-secondary { background: var(--surface-2); border: 1px solid var(--border-accent); color: var(--text); box-shadow: none; }
|
||||
.ss-btn-ghost { background: transparent; border: 1px solid var(--border-accent); color: var(--accent); box-shadow: none; }
|
||||
.ss-btn-ghost:hover { background: var(--surface-2); }
|
||||
|
||||
/* --- Ergebnis-Overlay --- */
|
||||
.ss-overlay {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: var(--bg-glow), rgba(0, 0, 0, 0.55);
|
||||
backdrop-filter: blur(4px);
|
||||
z-index: 1000;
|
||||
padding: 1rem;
|
||||
}
|
||||
.ss-overlay[hidden] { display: none; }
|
||||
.ss-result-card {
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border-accent);
|
||||
border-radius: 16px;
|
||||
padding: 1.6rem 1.8rem;
|
||||
width: min(440px, 94vw);
|
||||
text-align: center;
|
||||
box-shadow: 0 0 30px var(--glow-w);
|
||||
animation: ssFadeInUp 0.4s ease-out;
|
||||
}
|
||||
.ss-result-card h2 { color: var(--accent); margin-bottom: 0.4rem; }
|
||||
.ss-result-stars { font-size: 2rem; color: var(--accent); letter-spacing: 0.2rem; margin-bottom: 0.5rem; text-shadow: 0 0 12px var(--glow-s); }
|
||||
.ss-result-score { font-size: 1.4rem; margin-bottom: 0.4rem; color: var(--text); }
|
||||
.ss-result-score strong { color: var(--accent); }
|
||||
.ss-result-detail { color: var(--muted); margin-bottom: 0.6rem; }
|
||||
.ss-result-high { color: var(--accent); font-weight: 800; margin-bottom: 1rem; }
|
||||
.ss-result-high[hidden] { display: none; }
|
||||
.ss-result-actions { display: flex; flex-wrap: wrap; gap: 0.7rem; justify-content: center; margin-top: 1rem; }
|
||||
|
||||
@media (max-width: 600px) {
|
||||
.ss-title { font-size: 1.15rem; }
|
||||
.ss-stat { font-size: 0.62rem; padding: 0.3rem 0.5rem; }
|
||||
.ss-scene { padding: 1.1rem 1rem 1.2rem; }
|
||||
.ss-mission-title { font-size: 1.2rem; }
|
||||
.ss-code code { font-size: 0.85rem; }
|
||||
}
|
||||
@@ -0,0 +1,434 @@
|
||||
/* Syntax Saga – interaktive IT-Geschichten mit Code-Lücken.
|
||||
Story kommt aus /api/syntaxsaga/story (ohne Lösung), Prüfung + Score serverseitig. */
|
||||
(() => {
|
||||
'use strict';
|
||||
|
||||
const GAME = 'syntax-saga';
|
||||
const MAX_ATTEMPTS = 3;
|
||||
|
||||
const storyEl = document.getElementById('ss-story');
|
||||
const diffBtns = Array.from(document.querySelectorAll('.ss-diff'));
|
||||
const scoreEl = document.getElementById('ss-score');
|
||||
const missionEl = document.getElementById('ss-mission');
|
||||
const timeEl = document.getElementById('ss-time');
|
||||
const bestEl = document.getElementById('ss-best');
|
||||
|
||||
const resultEl = document.getElementById('ss-result');
|
||||
const resultStars = document.getElementById('ss-result-stars');
|
||||
const resultScore = document.getElementById('ss-result-score');
|
||||
const resultMax = document.getElementById('ss-result-max');
|
||||
const resultDetail = document.getElementById('ss-result-detail');
|
||||
const resultHigh = document.getElementById('ss-result-high');
|
||||
const againBtn = document.getElementById('ss-again');
|
||||
const chooseBtn = document.getElementById('ss-choose');
|
||||
|
||||
const DIFF_LABEL = { junior: 'Junior', professional: 'Professional', senior: 'Senior' };
|
||||
|
||||
let story = null; // StoryView vom Server
|
||||
let missionIndex = 0;
|
||||
let finishGaps = []; // gesammelte Lücken über alle Missionen -> /finish
|
||||
let score = 0; // laufender Client-Schätzwert (spiegelt die Serverformel)
|
||||
let seconds = 0;
|
||||
let timerId = null;
|
||||
let running = false;
|
||||
|
||||
let cur = null; // Laufzeitzustand der aktuellen Mission
|
||||
let curInputs = {}; // position -> <input>
|
||||
let curGaps = {}; // position -> Gap-Daten (points/hint/placeholder)
|
||||
|
||||
// ---------- Helfer ----------
|
||||
|
||||
function formatTime(s) {
|
||||
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
function startTimer() {
|
||||
if (timerId) return;
|
||||
timerId = setInterval(() => { seconds++; timeEl.textContent = formatTime(seconds); }, 1000);
|
||||
}
|
||||
function stopTimer() { clearInterval(timerId); timerId = null; }
|
||||
|
||||
/** Punkte aus Versuch (Erstversuch voll, danach weniger) – identisch zur Serverformel. */
|
||||
function awarded(points, attempts) {
|
||||
const a = Math.max(1, attempts);
|
||||
const factor = a === 1 ? 1.0 : a === 2 ? 0.6 : 0.3;
|
||||
return Math.round(points * factor);
|
||||
}
|
||||
|
||||
function setIdle(message) {
|
||||
storyEl.innerHTML = `<p class="ss-idle">${message}</p>`;
|
||||
}
|
||||
|
||||
function updateHud() {
|
||||
scoreEl.textContent = String(score);
|
||||
if (story && story.missions.length) {
|
||||
missionEl.textContent = `${Math.min(missionIndex + 1, story.missions.length)} / ${story.missions.length}`;
|
||||
} else {
|
||||
missionEl.textContent = '– / –';
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- Code-Rendering ----------
|
||||
|
||||
/** Baut <code>-Inhalt: Textsegmente als Textknoten, {{n}} als Eingabefelder. */
|
||||
function renderCode(codeEl, template) {
|
||||
codeEl.textContent = '';
|
||||
curInputs = {};
|
||||
const regex = /\{\{(\d+)\}\}/g;
|
||||
let last = 0;
|
||||
let m;
|
||||
while ((m = regex.exec(template)) !== null) {
|
||||
if (m.index > last) {
|
||||
codeEl.appendChild(document.createTextNode(template.slice(last, m.index)));
|
||||
}
|
||||
const pos = parseInt(m[1], 10);
|
||||
const gap = curGaps[pos];
|
||||
const input = document.createElement('input');
|
||||
input.type = 'text';
|
||||
input.className = 'ss-gap';
|
||||
input.dataset.pos = String(pos);
|
||||
input.autocomplete = 'off';
|
||||
input.autocapitalize = 'off';
|
||||
input.spellcheck = false;
|
||||
input.setAttribute('autocorrect', 'off');
|
||||
if (gap && gap.placeholder) input.placeholder = gap.placeholder;
|
||||
input.size = Math.max(6, gap && gap.placeholder ? gap.placeholder.length : 8);
|
||||
input.addEventListener('input', () => {
|
||||
input.size = Math.max(4, input.value.length + 1);
|
||||
});
|
||||
input.addEventListener('keydown', (e) => {
|
||||
if (e.key === 'Enter') { e.preventDefault(); onCheck(false); }
|
||||
});
|
||||
curInputs[pos] = input;
|
||||
codeEl.appendChild(input);
|
||||
last = m.index + m[0].length;
|
||||
}
|
||||
if (last < template.length) {
|
||||
codeEl.appendChild(document.createTextNode(template.slice(last)));
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- Mission rendern ----------
|
||||
|
||||
function renderMission() {
|
||||
const mission = story.missions[missionIndex];
|
||||
cur = { mission, attempts: {}, revealed: {}, solved: {}, scored: {} };
|
||||
curGaps = {};
|
||||
(mission.gaps || []).forEach((g) => { curGaps[g.position] = g; });
|
||||
|
||||
storyEl.innerHTML = '';
|
||||
const scene = document.createElement('div');
|
||||
scene.className = 'ss-scene';
|
||||
|
||||
// Kopf
|
||||
const head = document.createElement('div');
|
||||
head.className = 'ss-scene-head';
|
||||
const no = document.createElement('span');
|
||||
no.className = 'ss-mission-no';
|
||||
no.textContent = `Mission ${missionIndex + 1} / ${story.missions.length}`;
|
||||
head.appendChild(no);
|
||||
const diff = document.createElement('span');
|
||||
diff.className = 'ss-badge ss-badge-diff';
|
||||
diff.textContent = DIFF_LABEL[mission.difficulty] || mission.difficulty;
|
||||
head.appendChild(diff);
|
||||
if (mission.language) {
|
||||
const lang = document.createElement('span');
|
||||
lang.className = 'ss-badge ss-badge-lang';
|
||||
lang.textContent = mission.language;
|
||||
head.appendChild(lang);
|
||||
}
|
||||
scene.appendChild(head);
|
||||
|
||||
// Titel + Story
|
||||
const title = document.createElement('h2');
|
||||
title.className = 'ss-mission-title';
|
||||
title.textContent = mission.title;
|
||||
scene.appendChild(title);
|
||||
|
||||
if (mission.narrative) {
|
||||
const nar = document.createElement('p');
|
||||
nar.className = 'ss-narrative';
|
||||
nar.textContent = mission.narrative;
|
||||
scene.appendChild(nar);
|
||||
}
|
||||
if (mission.dialog) {
|
||||
const dlg = document.createElement('p');
|
||||
dlg.className = 'ss-dialog';
|
||||
dlg.textContent = `„${mission.dialog}“`;
|
||||
scene.appendChild(dlg);
|
||||
}
|
||||
|
||||
// Codeblock mit Lücken
|
||||
const pre = document.createElement('pre');
|
||||
pre.className = 'ss-code';
|
||||
const code = document.createElement('code');
|
||||
renderCode(code, mission.codeTemplate || '');
|
||||
pre.appendChild(code);
|
||||
scene.appendChild(pre);
|
||||
|
||||
// Feedback
|
||||
const feedback = document.createElement('p');
|
||||
feedback.className = 'ss-feedback';
|
||||
scene.appendChild(feedback);
|
||||
|
||||
// Erfolgstext (nach Lösen)
|
||||
const success = document.createElement('p');
|
||||
success.className = 'ss-success';
|
||||
success.hidden = true;
|
||||
scene.appendChild(success);
|
||||
|
||||
// Aktionen
|
||||
const actions = document.createElement('div');
|
||||
actions.className = 'ss-scene-actions';
|
||||
const checkBtn = document.createElement('button');
|
||||
checkBtn.type = 'button';
|
||||
checkBtn.className = 'ss-btn';
|
||||
checkBtn.textContent = 'Prüfen';
|
||||
checkBtn.addEventListener('click', () => onCheck(false));
|
||||
const revealBtn = document.createElement('button');
|
||||
revealBtn.type = 'button';
|
||||
revealBtn.className = 'ss-btn ss-btn-ghost';
|
||||
revealBtn.textContent = 'Auflösung anzeigen';
|
||||
revealBtn.hidden = true;
|
||||
revealBtn.addEventListener('click', () => onCheck(true));
|
||||
const nextBtn = document.createElement('button');
|
||||
nextBtn.type = 'button';
|
||||
nextBtn.className = 'ss-btn';
|
||||
nextBtn.textContent = missionIndex + 1 < story.missions.length ? 'Weiter' : 'Saga abschließen';
|
||||
nextBtn.hidden = true;
|
||||
nextBtn.addEventListener('click', nextMission);
|
||||
actions.appendChild(checkBtn);
|
||||
actions.appendChild(revealBtn);
|
||||
actions.appendChild(nextBtn);
|
||||
scene.appendChild(actions);
|
||||
|
||||
cur.els = { scene, feedback, success, checkBtn, revealBtn, nextBtn };
|
||||
|
||||
storyEl.appendChild(scene);
|
||||
updateHud();
|
||||
const first = Object.values(curInputs)[0];
|
||||
if (first) first.focus();
|
||||
}
|
||||
|
||||
// ---------- Prüfen / Auflösen ----------
|
||||
|
||||
async function onCheck(reveal) {
|
||||
if (!running || !cur) return;
|
||||
const gaps = cur.mission.gaps || [];
|
||||
|
||||
if (!reveal) {
|
||||
gaps.forEach((g) => {
|
||||
if (!cur.solved[g.position] && !cur.revealed[g.position]) {
|
||||
cur.attempts[g.position] = (cur.attempts[g.position] || 0) + 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const answers = gaps.map((g) => ({
|
||||
position: g.position,
|
||||
text: (curInputs[g.position] && curInputs[g.position].value) || ''
|
||||
}));
|
||||
|
||||
let data;
|
||||
try {
|
||||
const res = await fetch('/api/syntaxsaga/check', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ missionId: cur.mission.missionId, reveal, answers })
|
||||
});
|
||||
data = await res.json();
|
||||
} catch (err) {
|
||||
cur.els.feedback.textContent = 'Antwort konnte nicht geprüft werden.';
|
||||
cur.els.feedback.className = 'ss-feedback wrong';
|
||||
console.error(err);
|
||||
return;
|
||||
}
|
||||
|
||||
applyVerdicts(data, reveal);
|
||||
refreshSceneState();
|
||||
}
|
||||
|
||||
function applyVerdicts(data, reveal) {
|
||||
(data.results || []).forEach((v) => {
|
||||
const input = curInputs[v.position];
|
||||
if (!input) return;
|
||||
if (cur.solved[v.position] || cur.revealed[v.position]) return;
|
||||
|
||||
if (v.correct) {
|
||||
input.classList.add('correct');
|
||||
input.classList.remove('wrong');
|
||||
input.disabled = true;
|
||||
cur.solved[v.position] = true;
|
||||
if (!cur.scored[v.position]) {
|
||||
cur.scored[v.position] = true;
|
||||
const gap = curGaps[v.position];
|
||||
if (gap) { score += awarded(gap.points, cur.attempts[v.position] || 1); }
|
||||
}
|
||||
} else if (reveal && v.solution) {
|
||||
input.value = v.solution;
|
||||
input.classList.add('revealed');
|
||||
input.classList.remove('wrong');
|
||||
input.disabled = true;
|
||||
cur.revealed[v.position] = true;
|
||||
} else {
|
||||
input.classList.add('wrong');
|
||||
}
|
||||
});
|
||||
updateHud();
|
||||
}
|
||||
|
||||
function refreshSceneState() {
|
||||
const gaps = cur.mission.gaps || [];
|
||||
const open = gaps.filter((g) => !cur.solved[g.position] && !cur.revealed[g.position]);
|
||||
const fb = cur.els.feedback;
|
||||
|
||||
if (open.length === 0) {
|
||||
// Mission gelöst (oder aufgelöst)
|
||||
cur.els.checkBtn.hidden = true;
|
||||
cur.els.revealBtn.hidden = true;
|
||||
cur.els.nextBtn.hidden = false;
|
||||
const anyRevealed = gaps.some((g) => cur.revealed[g.position]);
|
||||
fb.textContent = anyRevealed ? 'Aufgelöst – weiter geht die Geschichte.' : 'Richtig! Sauber gelöst.';
|
||||
fb.className = anyRevealed ? 'ss-feedback revealed' : 'ss-feedback correct';
|
||||
if (cur.mission.successText) {
|
||||
cur.els.success.textContent = cur.mission.successText;
|
||||
cur.els.success.hidden = false;
|
||||
}
|
||||
cur.els.nextBtn.focus();
|
||||
return;
|
||||
}
|
||||
|
||||
// noch offene Lücken
|
||||
const hints = open.map((g) => curGaps[g.position] && curGaps[g.position].hint).filter(Boolean);
|
||||
fb.className = 'ss-feedback wrong';
|
||||
fb.textContent = hints.length ? `Noch nicht ganz. Tipp: ${hints.join(' · ')}` : 'Noch nicht ganz – versuch es noch einmal.';
|
||||
|
||||
const exhausted = open.some((g) => (cur.attempts[g.position] || 0) >= MAX_ATTEMPTS);
|
||||
cur.els.revealBtn.hidden = !exhausted;
|
||||
if (exhausted) {
|
||||
fb.textContent = hints.length
|
||||
? `Versuche aufgebraucht. Tipp: ${hints.join(' · ')} – oder zeig dir die Auflösung.`
|
||||
: 'Versuche aufgebraucht – zeig dir die Auflösung.';
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- Navigation ----------
|
||||
|
||||
function collectCurrentMission() {
|
||||
(cur.mission.gaps || []).forEach((g) => {
|
||||
finishGaps.push({
|
||||
missionId: cur.mission.missionId,
|
||||
position: g.position,
|
||||
text: (curInputs[g.position] && curInputs[g.position].value) || '',
|
||||
attempts: cur.attempts[g.position] || 1,
|
||||
revealed: !!cur.revealed[g.position]
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function nextMission() {
|
||||
collectCurrentMission();
|
||||
missionIndex++;
|
||||
if (missionIndex < story.missions.length) {
|
||||
renderMission();
|
||||
} else {
|
||||
finishRun();
|
||||
}
|
||||
}
|
||||
|
||||
async function finishRun() {
|
||||
running = false;
|
||||
stopTimer();
|
||||
try {
|
||||
const res = await fetch('/api/syntaxsaga/finish', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
difficulty: story.difficulty,
|
||||
durationSeconds: seconds,
|
||||
gaps: finishGaps
|
||||
})
|
||||
});
|
||||
const data = await res.json();
|
||||
score = data.score;
|
||||
updateHud();
|
||||
const stars = Math.max(0, Math.min(3, data.stars));
|
||||
resultStars.textContent = '★'.repeat(stars) + '☆'.repeat(3 - stars);
|
||||
resultScore.textContent = String(data.score);
|
||||
resultMax.textContent = String(data.maxScore);
|
||||
resultDetail.textContent =
|
||||
`${data.solvedGaps} von ${data.totalGaps} Lücken auf Anhieb · ${formatTime(seconds)}`;
|
||||
resultHigh.hidden = !data.newHighscore;
|
||||
bestEl.textContent = String(data.highscore);
|
||||
resultEl.hidden = false;
|
||||
} catch (err) {
|
||||
console.error('Saga konnte nicht gespeichert werden:', err);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- Start / Stats ----------
|
||||
|
||||
async function loadStats() {
|
||||
try {
|
||||
const res = await fetch(`/api/games/${GAME}/stats`);
|
||||
if (!res.ok) return;
|
||||
const stats = await res.json();
|
||||
bestEl.textContent = stats.highscore > 0 ? String(stats.highscore) : '–';
|
||||
} catch (err) { /* egal */ }
|
||||
}
|
||||
|
||||
async function startRun(difficulty) {
|
||||
resultEl.hidden = true;
|
||||
stopTimer();
|
||||
seconds = 0; score = 0; missionIndex = 0; finishGaps = [];
|
||||
timeEl.textContent = '0:00';
|
||||
diffBtns.forEach((b) => b.classList.toggle('active', b.dataset.diff === difficulty));
|
||||
setIdle('Lade Mission …');
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/syntaxsaga/story?difficulty=' + encodeURIComponent(difficulty));
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
story = await res.json();
|
||||
} catch (err) {
|
||||
console.error('Story konnte nicht geladen werden:', err);
|
||||
setIdle('Story konnte nicht geladen werden.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!story.missions || !story.missions.length) {
|
||||
setIdle('Für diese Stufe gibt es noch keine Missionen. Ein Admin kann sie unter „Syntax Saga verwalten“ anlegen.');
|
||||
running = false;
|
||||
updateHud();
|
||||
return;
|
||||
}
|
||||
|
||||
running = true;
|
||||
renderMission();
|
||||
startTimer();
|
||||
}
|
||||
|
||||
diffBtns.forEach((btn) => {
|
||||
btn.addEventListener('click', () => {
|
||||
if (running && finishGaps.length === 0 && missionIndex === 0 && cur &&
|
||||
Object.keys(cur.solved).length === 0) {
|
||||
// noch nichts gespielt -> einfach wechseln
|
||||
} else if (running && !window.confirm('Aktuelle Saga abbrechen und neu starten?')) {
|
||||
return;
|
||||
}
|
||||
startRun(btn.dataset.diff);
|
||||
});
|
||||
});
|
||||
|
||||
againBtn.addEventListener('click', () => startRun(story ? story.difficulty : 'junior'));
|
||||
chooseBtn.addEventListener('click', () => {
|
||||
resultEl.hidden = true;
|
||||
running = false;
|
||||
story = null;
|
||||
diffBtns.forEach((b) => b.classList.remove('active'));
|
||||
setIdle('Wähle oben eine Stufe, um deine Saga zu starten.');
|
||||
updateHud();
|
||||
});
|
||||
|
||||
loadStats();
|
||||
setIdle('Wähle oben eine Stufe – Junior, Professional oder Senior – um deine Saga zu starten.');
|
||||
})();
|
||||
Reference in New Issue
Block a user