248 lines
8.5 KiB
JavaScript
248 lines
8.5 KiB
JavaScript
// Куиз логикасы
|
||
(function () {
|
||
"use strict";
|
||
|
||
var ALL = Array.isArray(window.Q) ? window.Q : [];
|
||
var TOPIC = "Қарахан мемлекеті";
|
||
var LETTERS = ["А", "Б", "В", "Г"];
|
||
|
||
var $ = function (id) { return document.getElementById(id); };
|
||
var startScreen = $("quizStart");
|
||
var playScreen = $("quizPlay");
|
||
var resultScreen = $("quizResult");
|
||
|
||
var nameInput = $("nameInput");
|
||
var potokInput = $("potokInput");
|
||
var nameError = $("nameError");
|
||
var potokError = $("potokError");
|
||
var startBtn = $("startBtn");
|
||
var progressBar = $("quizProgressBar");
|
||
var quizCount = $("quizCount");
|
||
var quizQuestion = $("quizQuestion");
|
||
var quizOptions = $("quizOptions");
|
||
var quizExplain = $("quizExplain");
|
||
var nextBtn = $("nextBtn");
|
||
var quizScoreChip = $("quizScoreChip");
|
||
var scoreChip = $("scoreChip");
|
||
var startCount = $("startCount");
|
||
var footCount = $("footCount");
|
||
var quizName = $("quizName");
|
||
|
||
var questions = [];
|
||
var current = 0;
|
||
var score = 0;
|
||
var startedAt = 0;
|
||
var playerName = "";
|
||
var playerPotok = "";
|
||
|
||
function shuffle(arr) {
|
||
var a = arr.slice();
|
||
for (var i = a.length - 1; i > 0; i--) {
|
||
var j = Math.floor(Math.random() * (i + 1));
|
||
var t = a[i]; a[i] = a[j]; a[j] = t;
|
||
}
|
||
return a;
|
||
}
|
||
|
||
function setCounters() {
|
||
if (startCount) startCount.textContent = ALL.length + " сұрақ";
|
||
if (footCount) footCount.textContent = ALL.length + " сұрақ · Қарахан мемлекеті";
|
||
}
|
||
|
||
function fmtMs(ms) {
|
||
var s = Math.max(0, Math.round(ms / 1000));
|
||
var m = Math.floor(s / 60);
|
||
s = s % 60;
|
||
return (m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s;
|
||
}
|
||
|
||
function show(screen) {
|
||
startScreen.hidden = true;
|
||
playScreen.hidden = true;
|
||
resultScreen.hidden = true;
|
||
screen.hidden = false;
|
||
if (screen.scrollIntoView) screen.scrollIntoView({ behavior: "smooth", block: "start" });
|
||
}
|
||
|
||
function loadQuestion() {
|
||
var q = questions[current];
|
||
quizQuestion.textContent = q.q;
|
||
quizCount.textContent = (current + 1) + " / " + questions.length;
|
||
progressBar.style.width = (current / questions.length * 100) + "%";
|
||
if (scoreChip) { scoreChip.textContent = score; scoreChip.hidden = false; }
|
||
quizScoreChip.textContent = "Ұпай: " + score;
|
||
quizName.textContent = playerName;
|
||
|
||
quizOptions.innerHTML = "";
|
||
for (var i = 0; i < q.a.length; i++) {
|
||
var isCorrect = i === q.c;
|
||
var btn = document.createElement("button");
|
||
btn.type = "button";
|
||
btn.className = "quiz-opt";
|
||
btn.dataset.c = isCorrect ? "1" : "0";
|
||
var mark = document.createElement("span");
|
||
mark.className = "mark";
|
||
mark.textContent = LETTERS[i] || (i + 1);
|
||
var label = document.createElement("span");
|
||
label.textContent = (q.a[i] || "").replace(/^[А-Дa-dA-D]\)\s*/i, "");
|
||
btn.appendChild(mark);
|
||
btn.appendChild(label);
|
||
btn.addEventListener("click", onAnswer);
|
||
quizOptions.appendChild(btn);
|
||
}
|
||
|
||
quizExplain.className = "quiz-explain";
|
||
quizExplain.textContent = "";
|
||
nextBtn.hidden = true;
|
||
startBtn.hidden = false;
|
||
}
|
||
|
||
function onAnswer(e) {
|
||
var btn = e.currentTarget;
|
||
var correct = btn.dataset.c === "1";
|
||
Array.prototype.forEach.call(quizOptions.children, function (el) {
|
||
el.disabled = true;
|
||
if (el === btn) {
|
||
el.classList.add(correct ? "correct" : "wrong");
|
||
} else if (el.dataset.c === "1") {
|
||
el.classList.add("correct");
|
||
} else {
|
||
el.classList.add("faded");
|
||
}
|
||
});
|
||
if (correct) {
|
||
score++;
|
||
if (scoreChip) scoreChip.textContent = score;
|
||
quizScoreChip.textContent = "Ұпай: " + score;
|
||
}
|
||
var q = questions[current];
|
||
var correctLetter = LETTERS[q.c] || (q.c + 1);
|
||
var correctText = (q.a[q.c] || "").replace(/^[А-Д]\)\s*/i, "");
|
||
quizExplain.textContent = (correct ? "✅ Дұрыс! " : "❌ Дұрыс жауап: " + correctLetter + ". ") + correctText;
|
||
quizExplain.classList.add("show", correct ? "result-ok" : "result-wrong");
|
||
nextBtn.hidden = false;
|
||
startBtn.hidden = true;
|
||
nextBtn.focus();
|
||
}
|
||
|
||
function nextQuestion() {
|
||
current++;
|
||
if (current < questions.length) {
|
||
loadQuestion();
|
||
} else {
|
||
finish();
|
||
}
|
||
}
|
||
|
||
function verdict(pct) {
|
||
if (pct >= 90) return "Білімің өте жоғары";
|
||
if (pct >= 70) return "Жақсы нәтиже";
|
||
if (pct >= 50) return "Орташа. Кейбір тақырыптарды қайталау керек";
|
||
return "Тақырыпты қайта оқып шыққаның жөн";
|
||
}
|
||
|
||
function finish() {
|
||
var total = questions.length;
|
||
var pct = total ? Math.round(score / total * 100) : 0;
|
||
var now = new Date();
|
||
$("resultScore").textContent = score;
|
||
$("resultTotal").textContent = total;
|
||
$("resultVerdict").textContent = verdict(pct);
|
||
$("playerName").textContent = playerName;
|
||
$("playerPotok").textContent = playerPotok;
|
||
$("completionTime").textContent = fmtMs(Date.now() - startedAt);
|
||
$("finalScore").textContent = score + " / " + total;
|
||
$("percentage").textContent = pct + "%";
|
||
$("completionDate").textContent = now.toLocaleDateString("tr-TR");
|
||
$("completionHours").textContent = now.toLocaleTimeString("tr-TR", { hour: "2-digit", minute: "2-digit" });
|
||
if (scoreChip) { scoreChip.textContent = score; scoreChip.hidden = false; }
|
||
show(resultScreen);
|
||
}
|
||
|
||
function validateStart() {
|
||
var ok = true;
|
||
playerName = (nameInput.value || "").trim();
|
||
playerPotok = (potokInput.value || "").trim();
|
||
nameError.hidden = !!playerName;
|
||
potokError.hidden = !!playerPotok;
|
||
if (!playerName) ok = false;
|
||
if (!playerPotok) ok = false;
|
||
if (!ok) return false;
|
||
return true;
|
||
}
|
||
|
||
function startQuiz() {
|
||
if (!validateStart()) return;
|
||
questions = shuffle(ALL.map(function (_, i) { return i; }))
|
||
.map(function (idx) {
|
||
var src = ALL[idx];
|
||
var pairs = src.a.map(function (label, i) {
|
||
return { label: label, correct: i === src.c };
|
||
});
|
||
pairs = shuffle(pairs);
|
||
return {
|
||
q: src.q,
|
||
a: pairs.map(function (p) { return p.label; }),
|
||
c: pairs.findIndex(function (p) { return p.correct; }),
|
||
e: src.e
|
||
};
|
||
});
|
||
current = 0;
|
||
score = 0;
|
||
startedAt = Date.now();
|
||
if (scoreChip) { scoreChip.textContent = "0"; scoreChip.hidden = false; }
|
||
show(playScreen);
|
||
loadQuestion();
|
||
}
|
||
|
||
function retake() {
|
||
if (scoreChip) scoreChip.hidden = true;
|
||
nextBtn.hidden = true;
|
||
startBtn.hidden = false;
|
||
show(startScreen);
|
||
nameInput.value = playerName;
|
||
potokInput.value = playerPotok;
|
||
nameInput.focus();
|
||
}
|
||
|
||
function share() {
|
||
var total = questions.length;
|
||
var pct = total ? Math.round(score / total * 100) : 0;
|
||
var text = "«" + TOPIC + "» сабағы бойынша куиз-тестті тапсырдым: " +
|
||
score + " / " + total + " (" + pct + "%), " +
|
||
"Аты-жөні: " + playerName + ", Поток: " + playerPotok;
|
||
if (navigator.share) {
|
||
navigator.share({ title: "Куиз нәтижесі", text: text }).catch(function () {});
|
||
return;
|
||
}
|
||
if (navigator.clipboard) {
|
||
navigator.clipboard.writeText(text).then(function () {
|
||
var b = $("shareBtn");
|
||
var old = b.textContent;
|
||
b.textContent = "Көшірілді ✓";
|
||
setTimeout(function () { b.textContent = old; }, 1500);
|
||
});
|
||
}
|
||
}
|
||
|
||
startBtn.addEventListener("click", startQuiz);
|
||
nextBtn.addEventListener("click", nextQuestion);
|
||
$("retakeBtn").addEventListener("click", retake);
|
||
$("shareBtn").addEventListener("click", share);
|
||
nameInput.addEventListener("input", function () { nameError.hidden = !!nameInput.value.trim(); });
|
||
potokInput.addEventListener("input", function () { potokError.hidden = !!potokInput.value.trim(); });
|
||
nameInput.addEventListener("keydown", function (e) { if (e.key === "Enter") potokInput.focus(); });
|
||
potokInput.addEventListener("keydown", function (e) { if (e.key === "Enter") startQuiz(); });
|
||
|
||
if (ALL.length === 0) {
|
||
document.querySelector(".quiz-card").innerHTML =
|
||
'<div class="center" style="padding:32px 0">' +
|
||
'<p style="font-size:16px;font-weight:var(--kt-ai-weight-medium)">Сұрақтар қосылады</p>' +
|
||
'<p style="color:var(--kt-ai-fg-muted);font-size:var(--kt-ai-text-sm)">Тест сұрақтары дайын болса, хабарлаңыз.</p>' +
|
||
"</div>";
|
||
} else {
|
||
setCounters();
|
||
show(startScreen);
|
||
}
|
||
})();
|