Initial commit

This commit is contained in:
Dmitry Bikulov
2025-09-11 13:42:53 +03:00
commit d1bb3559bb
9 changed files with 640 additions and 0 deletions

177
static/app.js Normal file
View File

@@ -0,0 +1,177 @@
(() => {
const startBtn = document.getElementById('startBtn');
const timeEl = document.getElementById('time');
const progressEl = document.getElementById('progress');
const problemEl = document.getElementById('problem');
const optionsEl = document.getElementById('options');
const gameEl = document.getElementById('game');
const statusEl = document.getElementById('status');
const resultEl = document.getElementById('result');
const fxLayer = document.getElementById('fxLayer');
let sessionId = null;
let totalQuestions = 20;
let remaining = 60;
let countdown = null;
let inFlight = false;
function setStatus(msg) {
statusEl.textContent = msg || '';
}
function showGame(show) {
gameEl.classList.toggle('hidden', !show);
}
function showResult(score) {
const { correct, total_answered, total } = score;
resultEl.innerHTML = `
<div class="score">Результат: <strong>${correct}</strong> из <strong>${total}</strong></div>
<div class="small">Отвечено: ${total_answered}</div>
<button class="again" id="againBtn">Сыграть ещё</button>
`;
resultEl.classList.remove('hidden');
document.getElementById('againBtn')?.addEventListener('click', () => {
resultEl.classList.add('hidden');
start();
});
}
function renderQuestion(q) {
if (!q) return;
problemEl.textContent = `${q.a} × ${q.b}`;
optionsEl.innerHTML = '';
q.options.forEach(value => {
const btn = document.createElement('button');
btn.className = 'option-btn';
btn.textContent = String(value);
btn.addEventListener('click', () => answer(value, btn));
optionsEl.appendChild(btn);
});
progressEl.textContent = `${q.index}/${totalQuestions}`;
}
function stopTimer() {
if (countdown) {
clearInterval(countdown);
countdown = null;
}
}
function startTimer(seconds) {
remaining = seconds;
timeEl.textContent = String(remaining);
stopTimer();
countdown = setInterval(() => {
remaining -= 1;
if (remaining <= 0) {
remaining = 0;
timeEl.textContent = '0';
stopTimer();
} else {
timeEl.textContent = String(remaining);
}
}, 1000);
}
async function start() {
if (inFlight) return;
inFlight = true;
setStatus('');
showGame(false);
startBtn.disabled = true;
resultEl.classList.add('hidden');
try {
const res = await fetch('/api/session/start', { method: 'POST' });
if (!res.ok) throw new Error('Не удалось начать сессию');
const data = await res.json();
sessionId = data.session_id;
totalQuestions = data.total_questions;
startTimer(data.remaining_seconds);
renderQuestion(data.question);
showGame(true);
setStatus('Отвечайте как можно быстрее!');
} catch (e) {
console.error(e);
setStatus('Ошибка старта. Попробуйте ещё раз.');
} finally {
startBtn.disabled = false;
inFlight = false;
}
}
function celebrateFromElement(el) {
if (!fxLayer) return;
const rect = el.getBoundingClientRect();
const x = rect.left + rect.width / 2;
const y = rect.top + rect.height / 2;
const count = 40;
for (let i = 0; i < count; i++) {
const p = document.createElement('span');
p.className = 'confetti';
// origin
p.style.left = `${x}px`;
p.style.top = `${y}px`;
// random trajectory
const angle = Math.random() * Math.PI * 2;
const distance = 60 + Math.random() * 80;
const tx = Math.cos(angle) * distance;
const ty = Math.sin(angle) * distance + 40 + Math.random() * 60; // gravity pull
const rot = (Math.random() * 720 - 360).toFixed(0) + 'deg';
p.style.setProperty('--tx', `${tx.toFixed(0)}px`);
p.style.setProperty('--ty', `${ty.toFixed(0)}px`);
p.style.setProperty('--rot', rot);
const hue = Math.floor(Math.random() * 360);
p.style.backgroundColor = `hsl(${hue} 90% 55%)`;
p.style.animation = `confetti ${650 + Math.random() * 400}ms ease-out forwards`;
fxLayer.appendChild(p);
setTimeout(() => p.remove(), 1200);
}
}
async function answer(value, clickedBtn) {
if (!sessionId || inFlight) return;
inFlight = true;
// Disable all buttons to prevent double clicks
const buttons = Array.from(optionsEl.querySelectorAll('button'));
buttons.forEach(b => b.disabled = true);
try {
const res = await fetch(`/api/session/${sessionId}/answer`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ answer: value }),
});
if (!res.ok) throw new Error('Не удалось отправить ответ');
const data = await res.json();
// feedback
if (data.correct) {
clickedBtn.classList.add('correct');
celebrateFromElement(clickedBtn);
} else {
clickedBtn.classList.add('wrong');
}
// Small pause to show feedback
await new Promise(r => setTimeout(r, data.correct ? 350 : 250));
if (data.finished) {
stopTimer();
showGame(false);
const reason = data.reason === 'timeout' ? 'Время вышло.' : 'Завершено!';
setStatus(reason);
showResult(data.score);
} else {
timeEl.textContent = String(data.remaining_seconds);
renderQuestion(data.question);
}
} catch (e) {
console.error(e);
setStatus('Ошибка ответа. Перезапустите сессию.');
} finally {
inFlight = false;
}
}
startBtn.addEventListener('click', start);
})();

32
static/index.html Normal file
View File

@@ -0,0 +1,32 @@
<!doctype html>
<html lang="ru">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Тренажёр таблицы умножения</title>
<link rel="stylesheet" href="/static/style.css" />
</head>
<body>
<div class="app">
<h1>Тренажёр таблицы умножения</h1>
<div class="top-bar">
<div class="timer">Время: <span id="time">60</span> с</div>
<div class="progress">Вопрос: <span id="progress">0/20</span></div>
<button id="startBtn" class="start">Старт</button>
</div>
<div id="status" class="status"></div>
<div id="game" class="game hidden">
<div id="problem" class="problem">×</div>
<div id="options" class="options"></div>
</div>
<div id="result" class="result hidden"></div>
</div>
<div id="fxLayer" class="fx-layer"></div>
<script src="/static/app.js"></script>
</body>
</html>

80
static/style.css Normal file
View File

@@ -0,0 +1,80 @@
:root {
--bg: #f8fafc;
--panel: #ffffff;
--text: #0f172a;
--muted: #475569;
--accent: #16a34a;
--danger: #dc2626;
--brand: #2563eb;
--border: #e2e8f0;
--shadow: 0 10px 20px rgba(2, 6, 23, 0.06), 0 2px 6px rgba(2, 6, 23, 0.05);
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', Arial, sans-serif;
background: radial-gradient(1200px 600px at 10% 0%, #ffffff 0%, #f8fafc 70%);
color: var(--text);
}
.app { max-width: 900px; margin: 0 auto; padding: 24px; }
h1 { margin: 8px 0 20px; font-size: 28px; font-weight: 800; color: #0f172a; }
.top-bar { display: flex; gap: 12px; align-items: center; }
.timer, .progress { background: var(--panel); padding: 10px 14px; border-radius: 10px; color: var(--muted); border: 1px solid var(--border); box-shadow: var(--shadow); }
.start { margin-left: auto; padding: 10px 16px; border-radius: 10px; border: 1px solid var(--border); background: var(--panel); color: #0f172a; cursor: pointer; box-shadow: var(--shadow); }
.start:hover { background: #f1f5f9; }
.start:disabled { opacity: 0.6; cursor: not-allowed; }
.status { min-height: 22px; margin: 10px 0 8px; color: var(--muted); }
.game { background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); }
.problem { text-align: center; font-size: 72px; font-weight: 900; letter-spacing: 1px; margin: 10px 0 24px; color: #0f172a; }
.options { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.option-btn { font-size: 28px; font-weight: 800; padding: 22px; border-radius: 14px; border: 1px solid var(--border); background: #ffffff;
color: #0f172a; cursor: pointer; transition: transform 80ms ease, background 0.2s ease, box-shadow 0.2s ease; box-shadow: var(--shadow); }
.option-btn:hover { transform: translateY(-1px); background: #f8fafc; }
.option-btn:active { transform: translateY(0); }
.option-btn:disabled { opacity: 0.8; cursor: default; transform: none; }
.option-btn.correct { outline: 2px solid var(--accent); box-shadow: 0 0 0 6px rgba(22,163,74,0.12), var(--shadow); animation: pop 280ms ease-out; }
.option-btn.wrong { outline: 2px solid var(--danger); box-shadow: 0 0 0 6px rgba(220,38,38,0.12), var(--shadow); animation: shake 300ms ease-in-out; }
.result { margin-top: 18px; padding: 18px; background: var(--panel); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); }
.result .score { font-size: 20px; margin-bottom: 8px; }
.again { padding: 10px 16px; border-radius: 10px; border: 1px solid var(--border); background: var(--panel); color: #0f172a; cursor: pointer; box-shadow: var(--shadow); }
.again:hover { background: #f1f5f9; }
.hidden { display: none; }
/* Animations */
@keyframes pop {
0% { transform: scale(1); }
50% { transform: scale(1.08); }
100% { transform: scale(1); }
}
@keyframes shake {
0% { transform: translateX(0); }
20% { transform: translateX(-6px); }
40% { transform: translateX(6px); }
60% { transform: translateX(-4px); }
80% { transform: translateX(4px); }
100% { transform: translateX(0); }
}
.fx-layer { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 999; }
.confetti { position: absolute; width: 8px; height: 14px; border-radius: 2px; will-change: transform, opacity; transform: translate(-50%, -50%); }
@keyframes confetti {
0% { opacity: 1; transform: translate(0, 0) rotate(0deg); }
100% { opacity: 0; transform: translate(var(--tx), var(--ty)) rotate(var(--rot)); }
}
@media (max-width: 640px) {
.problem { font-size: 52px; }
.option-btn { font-size: 22px; padding: 18px; }
}