adding score

This commit is contained in:
Nathan 2025-12-17 15:34:22 +07:00
parent 8f5b89a32e
commit 43b6ab58ae
2 changed files with 39 additions and 0 deletions

View File

@ -99,6 +99,7 @@ function startTimer() {
}, 1000); }, 1000);
} }
// --- FUNGSI END SCREEN (YANG DI-UPDATE) ---
function showEndScreen(isWin) { function showEndScreen(isWin) {
clearInterval(countdown); clearInterval(countdown);
bgMusic.pause(); bgMusic.pause();
@ -117,6 +118,24 @@ function showEndScreen(isWin) {
document.getElementById("timeBonusEnd").textContent = "+" + (isWin ? timeBonus : 0); document.getElementById("timeBonusEnd").textContent = "+" + (isWin ? timeBonus : 0);
document.getElementById("moveBonusEnd").textContent = "+" + (isWin ? moveBonus : 0); document.getElementById("moveBonusEnd").textContent = "+" + (isWin ? moveBonus : 0);
document.getElementById("totalScoreEnd").textContent = isWin ? total : baseScore; 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"; document.getElementById("endScreen").style.display = "flex";
} }

20
score.php Normal file
View File

@ -0,0 +1,20 @@
<?php
session_start();
include 'Config.php'; // Sambungkan ke database
if (isset($_SESSION['user_id']) && isset($_POST['score'])) {
$user_id = $_SESSION['user_id'];
$score = intval($_POST['score']);
// Masukkan ke database
$stmt = $conn->prepare("INSERT INTO scores (user_id, score) VALUES (?, ?)");
$stmt->bind_param("ii", $user_id, $score);
if ($stmt->execute()) {
echo "Berhasil";
} else {
echo "Gagal";
}
}
?>