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;
|
GameStart = false;
|
||||||
ClearCanvas();
|
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)
|
// Kirim skor ke server hanya jika skor lebih besar dari 0 (atau sesuai kriteria)
|
||||||
if (score > 0) {
|
if (score > 0) {
|
||||||
kirimSkorKeServer(score, modePermainan);
|
kirimSkorKeServer(score, GameMode);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TAMPILKAN POP-UP GAME OVER
|
// TAMPILKAN POP-UP GAME OVER
|
||||||
@ -364,44 +364,40 @@ function InputKeyboard() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function kirimSkorKeServer(skor, modePermainan) {
|
// function kirimSkorKeServer(skor, GameMode) {
|
||||||
console.log(`Mengirim skor ${skor} (Mode: ${modePermainan}) ke server...`);
|
// console.log(`Mengirim skor ${skor} (Mode: ${GameMode}) ke server...`);
|
||||||
|
|
||||||
// Ganti 'score.php' sesuai dengan path di server Anda
|
// fetch('score.php', {
|
||||||
fetch('score.php', {
|
// method: 'POST',
|
||||||
method: 'POST',
|
// headers: {
|
||||||
headers: {
|
// 'Content-Type': 'application/json',
|
||||||
'Content-Type': 'application/json',
|
// },
|
||||||
},
|
// body: JSON.stringify({
|
||||||
body: JSON.stringify({
|
// score: skor,
|
||||||
score: skor,
|
// mode: GameMode
|
||||||
mode: modePermainan
|
// }),
|
||||||
}),
|
// })
|
||||||
})
|
// .then(response => {
|
||||||
.then(response => {
|
// if (!response.ok) {
|
||||||
if (!response.ok) {
|
// return response.json().then(error => { throw new Error(error.message || 'Gagal menyimpan skor'); });
|
||||||
return response.json().then(error => { throw new Error(error.message || 'Gagal menyimpan skor'); });
|
// }
|
||||||
}
|
// return response.json();
|
||||||
return response.json();
|
// })
|
||||||
})
|
// .then(data => {
|
||||||
.then(data => {
|
// if (data.status === 'success') {
|
||||||
if (data.status === 'success') {
|
// console.log('✅ Berhasil:', data.message);
|
||||||
console.log('✅ Berhasil:', data.message);
|
// if (data.message.includes('Highscore baru')) {
|
||||||
// Opsional: Perbarui highscore lokal dengan skor yang baru jika ini highscore
|
// highscore = skor;
|
||||||
if (data.message.includes('Highscore baru')) {
|
// UpdateScore(0);
|
||||||
highscore = skor;
|
// }
|
||||||
UpdateScore(0); // Update tampilan score/highscore
|
// } else {
|
||||||
}
|
// console.error('❌ Error Server:', data.message);
|
||||||
} else {
|
// }
|
||||||
console.error('❌ Error Server:', data.message);
|
// })
|
||||||
}
|
// .catch((error) => {
|
||||||
})
|
// console.error('⚠️ Error Jaringan atau Proses:', error.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.");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
InputKeyboard();
|
InputKeyboard();
|
||||||
gameLoop();
|
gameLoop();
|
||||||
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
session_start();
|
session_start();
|
||||||
require_once "koneksi.php"; // Menggunakan koneksi MySQLi
|
require_once "koneksi.php";
|
||||||
|
|
||||||
if (isset($_SESSION['username'])) {
|
if (isset($_SESSION['username'])) {
|
||||||
$nama = $_SESSION['username'];
|
$nama = $_SESSION['username'];
|
||||||
@ -10,9 +10,7 @@ if (isset($_SESSION['username'])) {
|
|||||||
|
|
||||||
$score = 0;
|
$score = 0;
|
||||||
|
|
||||||
// 1. Ambil Skor User yang Login (Untuk Tampilan Individual, jika diperlukan)
|
|
||||||
if (!empty($nama)) {
|
if (!empty($nama)) {
|
||||||
// ⚠️ Perlu DITINGKATKAN ke prepared statement untuk keamanan
|
|
||||||
$getScore = "SELECT score FROM users WHERE username = '$nama'";
|
$getScore = "SELECT score FROM users WHERE username = '$nama'";
|
||||||
$resultMe = mysqli_query($koneksi, $getScore);
|
$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";
|
$sql = "SELECT username, score FROM users ORDER BY score DESC LIMIT 10";
|
||||||
$result = mysqli_query($koneksi, $sql);
|
$result = mysqli_query($koneksi, $sql);
|
||||||
$leaderboard = [];
|
$leaderboard = [];
|
||||||
|
|
||||||
if($result) {
|
if($result) {
|
||||||
// Ambil semua hasil
|
|
||||||
$leaderboard = mysqli_fetch_all($result, MYSQLI_ASSOC);
|
$leaderboard = mysqli_fetch_all($result, MYSQLI_ASSOC);
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="id">
|
<html lang="id">
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
background: linear-gradient(to bottom right, #A3D438, #004D40);
|
background: linear-gradient(to bottom right, #A3D438, #004D40);
|
||||||
height: 100vh; /* Menggunakan vh agar mencakup seluruh tinggi layar */
|
height: 568px;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Leaderboard</title>
|
<title>Leaderboard</title>
|
||||||
|
<link rel="stylesheet" href="">
|
||||||
|
<link rel="stylesheet" href="">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<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>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@ -97,7 +64,9 @@ if($result) {
|
|||||||
<tr>
|
<tr>
|
||||||
<td><?php echo $peringkat; ?></td>
|
<td><?php echo $peringkat; ?></td>
|
||||||
<td><?php echo htmlspecialchars($pemain['username']); ?></td>
|
<td><?php echo htmlspecialchars($pemain['username']); ?></td>
|
||||||
|
|
||||||
<td><?php echo $pemain['score']; ?> PTS</td>
|
<td><?php echo $pemain['score']; ?> PTS</td>
|
||||||
|
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
@ -106,6 +75,7 @@ if($result) {
|
|||||||
} else {
|
} else {
|
||||||
echo '<tr><td colspan="3" style="text-align: center;">Belum ada Pemain</td></tr>';
|
echo '<tr><td colspan="3" style="text-align: center;">Belum ada Pemain</td></tr>';
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
</tbody>
|
</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