ga jadi ga isa konek database e dek aku
This commit is contained in:
parent
da8b69c174
commit
92cfd08b94
74
GameLogic.js
74
GameLogic.js
@ -203,11 +203,11 @@ function GameOver() {
|
||||
GameStart = false;
|
||||
ClearCanvas();
|
||||
|
||||
const modePermainan = ModeH ? "Tambahan" : "Normal";
|
||||
const GameMode = ModeH ? "Tambahan" : "Normal";
|
||||
|
||||
// Kirim skor ke server hanya jika skor lebih besar dari 0 (atau sesuai kriteria)
|
||||
if (score > 0) {
|
||||
kirimSkorKeServer(score, modePermainan);
|
||||
kirimSkorKeServer(score, GameMode);
|
||||
}
|
||||
|
||||
// TAMPILKAN POP-UP GAME OVER
|
||||
@ -364,44 +364,40 @@ function InputKeyboard() {
|
||||
});
|
||||
}
|
||||
|
||||
function kirimSkorKeServer(skor, modePermainan) {
|
||||
console.log(`Mengirim skor ${skor} (Mode: ${modePermainan}) ke server...`);
|
||||
// function kirimSkorKeServer(skor, GameMode) {
|
||||
// console.log(`Mengirim skor ${skor} (Mode: ${GameMode}) ke server...`);
|
||||
|
||||
// Ganti 'score.php' sesuai dengan path di server Anda
|
||||
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);
|
||||
// Opsional: Perbarui highscore lokal dengan skor yang baru jika ini highscore
|
||||
if (data.message.includes('Highscore baru')) {
|
||||
highscore = skor;
|
||||
UpdateScore(0); // Update tampilan score/highscore
|
||||
}
|
||||
} else {
|
||||
console.error('❌ Error Server:', data.message);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('⚠️ Error Jaringan atau Proses:', error.message);
|
||||
// Tampilkan pesan error ke user jika perlu
|
||||
// alert("Gagal terhubung ke server skor. Pastikan Anda sudah login.");
|
||||
});
|
||||
}
|
||||
// fetch('score.php', {
|
||||
// method: 'POST',
|
||||
// headers: {
|
||||
// 'Content-Type': 'application/json',
|
||||
// },
|
||||
// body: JSON.stringify({
|
||||
// score: skor,
|
||||
// mode: GameMode
|
||||
// }),
|
||||
// })
|
||||
// .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);
|
||||
// if (data.message.includes('Highscore baru')) {
|
||||
// highscore = skor;
|
||||
// UpdateScore(0);
|
||||
// }
|
||||
// } else {
|
||||
// console.error('❌ Error Server:', data.message);
|
||||
// }
|
||||
// })
|
||||
// .catch((error) => {
|
||||
// console.error('⚠️ Error Jaringan atau Proses:', error.message);
|
||||
// });
|
||||
// }
|
||||
|
||||
InputKeyboard();
|
||||
gameLoop();
|
||||
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once "koneksi.php"; // Menggunakan koneksi MySQLi
|
||||
require_once "koneksi.php";
|
||||
|
||||
if (isset($_SESSION['username'])) {
|
||||
$nama = $_SESSION['username'];
|
||||
@ -10,9 +10,7 @@ if (isset($_SESSION['username'])) {
|
||||
|
||||
$score = 0;
|
||||
|
||||
// 1. Ambil Skor User yang Login (Untuk Tampilan Individual, jika diperlukan)
|
||||
if (!empty($nama)) {
|
||||
// ⚠️ Perlu DITINGKATKAN ke prepared statement untuk keamanan
|
||||
$getScore = "SELECT score FROM users WHERE username = '$nama'";
|
||||
$resultMe = mysqli_query($koneksi, $getScore);
|
||||
|
||||
@ -22,63 +20,32 @@ if (!empty($nama)) {
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Ambil 10 Skor Tertinggi
|
||||
$sql = "SELECT username, score FROM users ORDER BY score DESC LIMIT 10";
|
||||
$result = mysqli_query($koneksi, $sql);
|
||||
$leaderboard = [];
|
||||
|
||||
if($result) {
|
||||
// Ambil semua hasil
|
||||
$leaderboard = mysqli_fetch_all($result, MYSQLI_ASSOC);
|
||||
}
|
||||
?>
|
||||
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="id">
|
||||
<style>
|
||||
body {
|
||||
background: linear-gradient(to bottom right, #A3D438, #004D40);
|
||||
height: 100vh; /* Menggunakan vh agar mencakup seluruh tinggi layar */
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding-top: 50px;
|
||||
color: white;
|
||||
font-family: sans-serif;
|
||||
}
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
width: 80%;
|
||||
max-width: 600px;
|
||||
margin-top: 20px;
|
||||
background-color: rgba(0, 0, 0, 0.4);
|
||||
}
|
||||
th, td {
|
||||
border: 1px solid #ddd;
|
||||
padding: 12px;
|
||||
text-align: left;
|
||||
}
|
||||
th {
|
||||
background-color: #004D40;
|
||||
color: white;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
tr:nth-child(even) {
|
||||
background-color: rgba(255, 255, 255, 0.1);
|
||||
height: 568px;
|
||||
}
|
||||
</style>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Leaderboard</title>
|
||||
<link rel="stylesheet" href="">
|
||||
<link rel="stylesheet" href="">
|
||||
</head>
|
||||
<body>
|
||||
<h1>🥇 Leaderboard Top 10 🥈</h1>
|
||||
<?php if (!empty($nama)): ?>
|
||||
<p style="font-size: 1.2em;">Skor Tertinggi Anda (<?php echo htmlspecialchars($nama); ?>): <strong><?php echo $score; ?> PTS</strong></p>
|
||||
<?php else: ?>
|
||||
<p>Silakan <a href="login.php" style="color: yellow;">login</a> untuk melihat skor Anda.</p>
|
||||
<?php endif; ?>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
@ -97,7 +64,9 @@ if($result) {
|
||||
<tr>
|
||||
<td><?php echo $peringkat; ?></td>
|
||||
<td><?php echo htmlspecialchars($pemain['username']); ?></td>
|
||||
|
||||
<td><?php echo $pemain['score']; ?> PTS</td>
|
||||
|
||||
</tr>
|
||||
|
||||
<?php
|
||||
@ -106,6 +75,7 @@ if($result) {
|
||||
} else {
|
||||
echo '<tr><td colspan="3" style="text-align: center;">Belum ada Pemain</td></tr>';
|
||||
}
|
||||
|
||||
?>
|
||||
</tbody>
|
||||
|
||||
|
||||
81
score.php
81
score.php
@ -1,81 +0,0 @@
|
||||
<?php
|
||||
session_start();
|
||||
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']) && !isset($_SESSION['username'])) {
|
||||
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);
|
||||
|
||||
$final_score = (int)($input['score'] ?? 0);
|
||||
|
||||
if ($final_score <= 0) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Skor tidak valid atau 0.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$user_id = null;
|
||||
if (isset($_SESSION['id_user'])) {
|
||||
$user_id = $_SESSION['id_user'];
|
||||
} elseif (isset($_SESSION['username'])) {
|
||||
|
||||
$username = $_SESSION['username'];
|
||||
|
||||
$getID_sql = "SELECT id_user FROM users WHERE username = '$username'";
|
||||
$result_id = mysqli_query($koneksi, $getID_sql);
|
||||
|
||||
if ($result_id && mysqli_num_rows($result_id) > 0) {
|
||||
$row = mysqli_fetch_assoc($result_id);
|
||||
$user_id = $row['id_user'];
|
||||
}
|
||||
}
|
||||
|
||||
if (!$user_id) {
|
||||
http_response_code(401);
|
||||
echo json_encode(['status' => 'error', 'message' => 'ID pengguna tidak ditemukan.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$sql = "UPDATE users SET score = ? WHERE id_user = ? AND score < ?";
|
||||
|
||||
if ($stmt = mysqli_prepare($koneksi, $sql)) {
|
||||
mysqli_stmt_bind_param($stmt, "iii", $final_score, $user_id, $final_score);
|
||||
$exec = mysqli_stmt_execute($stmt);
|
||||
|
||||
if ($exec) {
|
||||
$rows_affected = mysqli_stmt_affected_rows($stmt);
|
||||
|
||||
if ($rows_affected > 0) {
|
||||
$message = 'Skor berhasil diperbarui. Ini adalah Highscore baru!';
|
||||
} else {
|
||||
$message = 'Skor berhasil dikirim, tetapi skor tidak lebih tinggi dari Highscore sebelumnya.';
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'status' => 'success',
|
||||
'message' => $message,
|
||||
'skor_terkirim' => $final_score
|
||||
]);
|
||||
} else {
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Gagal menjalankan kueri update: ' . mysqli_stmt_error($stmt)]);
|
||||
}
|
||||
mysqli_stmt_close($stmt);
|
||||
|
||||
} else {
|
||||
http_response_code(500);
|
||||
echo json_encode(['status' => 'error', 'message' => 'Gagal mempersiapkan statement: ' . mysqli_error($koneksi)]);
|
||||
}
|
||||
?>
|
||||
Loading…
x
Reference in New Issue
Block a user