Template
1
0

Simplified codes

This commit is contained in:
2025-11-27 23:17:03 +03:00
parent 42fa44c3a2
commit cae0d5b946
23 changed files with 2892 additions and 1546 deletions

View File

@@ -1,104 +1,149 @@
// TODO: Inna - Implement frontend game functionality
// script.js
let currentQuestion = null;
let gameState = {
currentQuestion: null,
optionsDisabled: false
};
// Utility function for making API requests
function apiRequest(url, options = {}) {
return fetch(url, options)
.then(response => response.json())
.catch(error => {
console.error('Error:', error);
throw error;
});
}
// Utility function for updating element text content
function updateElementText(id, text) {
const element = document.getElementById(id);
if (element) {
element.textContent = text;
}
}
// Utility function for showing/hiding elements
function toggleElementVisibility(id, show = true) {
const element = document.getElementById(id);
if (element) {
element.style.display = show ? 'block' : 'none';
}
}
function loadQuestion() {
fetch('/get_question')
.then(response => response.json())
apiRequest('/get_question')
.then(data => {
if (data.error) {
console.error(data.error);
return;
}
if (data.game_over) {
endGame(data.final_score);
return;
}
gameState.currentQuestion = data;
currentQuestion = data;
displayQuestion(data);
})
.catch(error => console.error('Error:', error));
}
function displayQuestion(questionData) {
document.getElementById('question-num').textContent = questionData.question_number;
document.getElementById('question-text').textContent = questionData.question;
function displayQuestion(data) {
updateElementText('q-number', data.question_number);
updateElementText('question-text', data.question);
updateElementText('prize', data.current_prize);
const optionsContainer = document.getElementById('options-container');
const optionsContainer = document.getElementById('options');
optionsContainer.innerHTML = '';
const optionLetters = ['A', 'B', 'C', 'D'];
questionData.options.forEach((option, index) => {
data.options.forEach((option, index) => {
const optionElement = document.createElement('div');
optionElement.className = 'option';
optionElement.textContent = `${optionLetters[index]}. ${option}`;
optionElement.textContent = option;
optionElement.onclick = () => selectAnswer(option);
optionsContainer.appendChild(optionElement);
});
document.getElementById('feedback').style.display = 'none';
gameState.optionsDisabled = false;
// Reset result display
toggleElementVisibility('result', false);
const result = document.getElementById('result');
if (result) {
result.className = 'result';
}
}
function selectAnswer(selectedAnswer) {
if (gameState.optionsDisabled) return;
gameState.optionsDisabled = true;
fetch('/answer', {
function selectAnswer(answer) {
apiRequest('/answer', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ answer: selectedAnswer })
body: JSON.stringify({ answer: answer })
})
.then(response => response.json())
.then(data => {
const feedback = document.getElementById('feedback');
feedback.style.display = 'block';
if (data.correct) {
feedback.textContent = 'Correct! 🎉';
feedback.className = 'feedback correct';
setTimeout(() => {
if (data.next_question) loadQuestion();
else endGame(data.final_score, true);
}, 1500);
} else {
feedback.textContent = `Incorrect! Correct answer: ${data.correct_answer}`;
feedback.className = 'feedback incorrect';
setTimeout(() => endGame(data.final_score), 2000);
const result = document.getElementById('result');
if (result) {
result.style.display = 'block';
if (data.correct) {
result.textContent = 'Correct!';
result.className = 'result correct';
setTimeout(() => {
if (data.game_over) {
endGame(data.final_score);
} else {
loadQuestion();
}
}, 1500);
} else {
result.textContent = `Wrong! Correct answer: ${data.correct_answer}`;
result.className = 'result incorrect';
setTimeout(() => {
endGame(data.final_score);
}, 2000);
}
}
})
.catch(error => console.error('Error:', error));
}
function useLifeline(lifelineName) {
fetch(`/lifeline/${lifelineName}`)
.then(response => response.json())
function useFiftyFifty() {
const lifelineBtn = document.querySelector('.lifeline');
if (lifelineBtn) {
lifelineBtn.disabled = true;
}
apiRequest('/lifeline/fifty_fifty')
.then(data => {
if (data.error) {
alert(data.error);
if (lifelineBtn) {
lifelineBtn.disabled = false;
}
return;
}
document.getElementById('lifeline-result').textContent =
data.hint || data.friend_says || 'Lifeline used!';
// Hide two wrong options
const options = document.querySelectorAll('.option');
data.remove_indices.forEach(index => {
if (options[index]) {
options[index].style.display = 'none';
}
});
})
.catch(error => console.error('Error:', error));
.catch(error => {
console.error('Error:', error);
if (lifelineBtn) {
lifelineBtn.disabled = false;
}
});
}
function endGame(finalScore, isWin = false) {
document.getElementById('final-prize').textContent = finalScore;
document.getElementById('game-over-screen').style.display = 'block';
document.querySelector('.game-area').style.display = 'none';
function endGame(score) {
updateElementText('final-prize', score);
toggleElementVisibility('game-over', true);
toggleElementVisibility('question-box', false);
toggleElementVisibility('lifeline', false);
}
function restartGame() {
window.location.href = '/start';
}
document.addEventListener('DOMContentLoaded', function() {
if (window.location.pathname === '/start') {
loadQuestion();
}
});
// Start the game when page loads
if (window.location.pathname === '/start') {
loadQuestion();
}