From 43b6ab58ae77611b561af08efaf30f5d542b0ebc Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 17 Dec 2025 15:34:22 +0700 Subject: [PATCH] adding score --- assets/gameboard-easy.js | 19 +++++++++++++++++++ score.php | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 score.php diff --git a/assets/gameboard-easy.js b/assets/gameboard-easy.js index 37ce7a1..264f1a0 100644 --- a/assets/gameboard-easy.js +++ b/assets/gameboard-easy.js @@ -99,6 +99,7 @@ function startTimer() { }, 1000); } +// --- FUNGSI END SCREEN (YANG DI-UPDATE) --- function showEndScreen(isWin) { clearInterval(countdown); bgMusic.pause(); @@ -117,6 +118,24 @@ function showEndScreen(isWin) { document.getElementById("timeBonusEnd").textContent = "+" + (isWin ? timeBonus : 0); document.getElementById("moveBonusEnd").textContent = "+" + (isWin ? moveBonus : 0); document.getElementById("totalScoreEnd").textContent = isWin ? total : baseScore; + + // --- KIRIM SCORE KE DATABASE --- + if (isWin) { + // Bungkus data score + let formData = new FormData(); + formData.append('score', total); + + // Kirim ke score.php (tanpa reload halaman) + fetch('score.php', { + method: 'POST', + body: formData + }) + .then(response => response.text()) + .then(result => console.log("Status Simpan Score: " + result)) + .catch(error => console.error('Error:', error)); + } + // ------------------------------ + document.getElementById("endScreen").style.display = "flex"; } diff --git a/score.php b/score.php new file mode 100644 index 0000000..f1ed43f --- /dev/null +++ b/score.php @@ -0,0 +1,20 @@ +prepare("INSERT INTO scores (user_id, score) VALUES (?, ?)"); + $stmt->bind_param("ii", $user_id, $score); + + if ($stmt->execute()) { + echo "Berhasil"; + } else { + echo "Gagal"; + } +} +?> \ No newline at end of file