sek belom selesai
This commit is contained in:
parent
ea60abb7f1
commit
e8975da670
38
GameLogic.js
38
GameLogic.js
@ -183,6 +183,11 @@ function GameOver() {
|
|||||||
GameStart = false;
|
GameStart = false;
|
||||||
ClearCanvas();
|
ClearCanvas();
|
||||||
|
|
||||||
|
const modePermainan = ModeH ? "Tambahan" : "Normal";
|
||||||
|
if (score >= 0) {
|
||||||
|
kirimSkorKeServer(score, modePermainan);
|
||||||
|
}
|
||||||
|
|
||||||
// TAMPILKAN POP-UP GAME OVER
|
// TAMPILKAN POP-UP GAME OVER
|
||||||
ScoreMain.innerHTML = "Score: " + score;
|
ScoreMain.innerHTML = "Score: " + score;
|
||||||
UpDead.style.display = "flex";
|
UpDead.style.display = "flex";
|
||||||
@ -282,7 +287,7 @@ function InputKeyboard() {
|
|||||||
document.addEventListener("keydown", function (e) {
|
document.addEventListener("keydown", function (e) {
|
||||||
if (!GameStart) return;
|
if (!GameStart) return;
|
||||||
|
|
||||||
// jalan buat ular
|
//jalan buat ular
|
||||||
if (
|
if (
|
||||||
ArahUlar == 0 &&
|
ArahUlar == 0 &&
|
||||||
((e.code == "KeyA" && Ular.dx === 0) ||
|
((e.code == "KeyA" && Ular.dx === 0) ||
|
||||||
@ -326,5 +331,36 @@ function InputKeyboard() {
|
|||||||
: speed;
|
: speed;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function kirimSkorKeServer(skor, modePermainan) {
|
||||||
|
console.log(`Mengirim skor ${skor} (Mode: ${modePermainan}) ke server...`);
|
||||||
|
|
||||||
|
fetch('score.php', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
score: skor,
|
||||||
|
mode: modePermainan
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
.then(response => {
|
||||||
|
if (!response.ok) {
|
||||||
|
return response.json().then(error => { throw new Error(error.message || 'Gagal menyimpan skor'); });
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
if (data.status === 'success') {
|
||||||
|
console.log('✅ Berhasil:', data.message);
|
||||||
|
} else {
|
||||||
|
console.error('❌ Error Server:', data.message);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error('⚠️ Error Jaringan atau Proses:', error.message);
|
||||||
|
});
|
||||||
|
}
|
||||||
InputKeyboard();
|
InputKeyboard();
|
||||||
gameLoop();
|
gameLoop();
|
||||||
|
|||||||
50
score.php
Normal file
50
score.php
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'koneksi.php';
|
||||||
|
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
|
http_response_code(405);
|
||||||
|
echo json_encode(['status' => 'error', 'message' => 'Metode tidak diizinkan.']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($_SESSION['id_user'])) {
|
||||||
|
http_response_code(401);
|
||||||
|
echo json_encode(['status' => 'error', 'message' => 'Anda harus login untuk menyimpan skor.']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$input = json_decode(file_get_contents('php://input'), true);
|
||||||
|
|
||||||
|
$user_id = $_SESSION['id_user'];
|
||||||
|
$final_score = (int)($input['score'] ?? 0);
|
||||||
|
$game_mode = $input['mode'] ?? 'Normal';
|
||||||
|
|
||||||
|
if ($final_score <= 0) {
|
||||||
|
http_response_code(400);
|
||||||
|
echo json_encode(['status' => 'error', 'message' => 'Skor tidak valid atau 0.']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$sql = "INSERT INTO scores (id_user, score_value, mode) VALUES (:id_user, :score_value, :mode)";
|
||||||
|
$stmt = $pdo->prepare($sql);
|
||||||
|
|
||||||
|
$stmt->execute([
|
||||||
|
':id_user' => $user_id,
|
||||||
|
':score_value' => $final_score,
|
||||||
|
':mode' => $game_mode
|
||||||
|
]);
|
||||||
|
|
||||||
|
echo json_encode([
|
||||||
|
'status' => 'success',
|
||||||
|
'message' => 'Skor berhasil disimpan.',
|
||||||
|
'skor_terkirim' => $final_score
|
||||||
|
]);
|
||||||
|
|
||||||
|
} catch (\PDOException $e) {
|
||||||
|
http_response_code(500);
|
||||||
|
echo json_encode(['status' => 'error', 'message' => 'Terjadi kesalahan server saat menyimpan data.']);
|
||||||
|
}
|
||||||
|
?>
|
||||||
Loading…
x
Reference in New Issue
Block a user