/
nickaen
/
project_RKSP
Обзор
Документация
Войти
/
nickaen
/
project_RKSP
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
createCard.js
177 строк
6 KB
nickaen
upload files
07 июн 2025, 16:32
07 июн 2025, 16:32
a69cf26
Код
Авторство
О чём код?
document.addEventListener('DOMContentLoaded', fetchCards); const cardBox = document.getElementById('cards'); let cards; const deckId = new URLSearchParams(window.location.search).get('deckId'); async function fetchCards() { try { const response = await fetch('fetchCards.php?deckId=' + deckId); if (!response.ok) { throw new Error('Network response was not ok'); } cards = await response.json(); console.log(cards); cards.forEach(card => { let btn = document.createElement('button'); btn.classList.add('card'); btn.textContent = card.front; btn.onclick = function() { if (btn.textContent === card.front) { btn.textContent = card.back; } else { btn.textContent = card.front; } }; cardBox.appendChild(btn); }); let btn = document.createElement('button'); btn.classList.add('card'); btn.textContent = '+'; let btnPress = 0; btn.onclick = function() { if (btnPress == 0) { const form = document.createElement('form'); form.action = 'createCard.php'; form.method = 'post'; form.id = 'createCardForm'; const front = document.createElement('input'); front.type = 'text'; front.id = 'front'; front.name = 'front'; front.placeholder = 'Введите термин/слово'; front.required = true; form.appendChild(front); const back = document.createElement('input'); back.type = 'text'; back.id = 'back'; back.name = 'back'; back.required = true; back.placeholder = 'Введите описание/значение'; form.appendChild(back); const inputbtn = document.createElement('input'); inputbtn.type = 'submit'; inputbtn.name = 'createCard'; inputbtn.value = 'Создать'; form.appendChild(inputbtn); const deckID = document.createElement('input'); deckID.style.display = 'none'; deckID.name = 'deckId'; deckID.id = 'deckId'; deckID.value = deckId; deckID.type = 'text'; deckID.textContent = deckId; form.appendChild(deckID); // Добавляем форму после кнопки cardBox.appendChild(form); btnPress = 1; } } cardBox.appendChild(btn); } catch (error) { console.error('Ошибка при загрузке колод:', error); return []; } } const gameButton = document.getElementById('playBtn'); const gameBox = document.getElementById('gameBox'); const winMsg = document.getElementById("winMessage"); const deckCount = document.getElementById("countDeckAmount"); const gameCard = document.getElementById("gameCard"); const CWButtons = document.getElementById("gameButtons"); let cardDeck = []; let isStarted = false let currentCard; function startGame(){ if (!isStarted){ cardDeck = []; console.log(cards); cards.forEach(card => { front = card.front; back = card.back; cardDeck.push({F: front, B: back}); }); cardBox.style.display = 'none'; gameBox.style.display = 'flex'; gameButton.textContent = 'Закончить игру'; CWButtons.style.display = 'block'; gameCard.style.display = 'block'; gameButton.onclick = endGame; deckCount.textContent = cardDeck.length; shuffleArray(cardDeck); currentCard = cardDeck[0]; gameCard.textContent = currentCard.F; isStarted = true; } } function correctGuess(){ if (isStarted){ cardDeck.shift(); if (cardDeck.length != 0){ currentCard = cardDeck[0]; gameCard.textContent = currentCard.F; deckCount.textContent -= 1; } else { gameCard.style.display = 'none'; deckCount.textContent = 0; winMsg.style.display = "block"; CWButtons.style.display = 'none'; } } } function changeSide(){ if (gameCard.textContent === currentCard.F) { gameCard.textContent = currentCard.B; } else { gameCard.textContent = currentCard.F; } } function wrongGuess(){ if (isStarted){ cardDeck.push(currentCard); deckCount.textContent = Number(deckCount.textContent) + 1 ; shuffleArray(cardDeck); currentCard = cardDeck[0]; gameCard.textContent = currentCard.F; } } function endGame(){ if (isStarted){ cardBox.style.display = 'flex'; gameBox.style.display = 'none'; gameButton.textContent = 'Начать запоминание'; gameButton.onclick = startGame; winMsg.display = 'none'; isStarted = false; } } function shuffleArray(array){ for(let i = array.length - 1; i>0; i--){ const random = Math.floor(Math.random() * (i+1)); [array[i], array[random]] = [array[random], array[i]] } }