gasken bossku

This commit is contained in:
Cliff 2025-12-15 22:31:40 +07:00
parent 044095e267
commit e7d2e7bccb
3 changed files with 73 additions and 4 deletions

View File

@ -1,5 +1,14 @@
<?php <?php
include "koneksi.php";
session_start(); session_start();
// Simpan saldo terakhir ke database sebelum logout
if (isset($_SESSION['username']) && isset($_SESSION['balance'])) {
$username = mysqli_real_escape_string($conn, $_SESSION['username']);
$balance = (int)$_SESSION['balance'];
mysqli_query($conn, "UPDATE users SET balance = $balance WHERE username = '$username'");
}
session_unset(); session_unset();
session_destroy(); session_destroy();
header('Location: loginn.php'); header('Location: loginn.php');

View File

@ -4,6 +4,25 @@ function setMessage(text, status) {
messageEl.className = status; // 'win', 'lose', atau '' (tie) messageEl.className = status; // 'win', 'lose', atau '' (tie)
} }
// Fungsi untuk update balance ke server
async function updateBalanceToServer(newBalance) {
try {
const response = await fetch('update_balance.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ balance: newBalance }),
});
const data = await response.json();
if (!data.success) {
console.error('Failed to update balance:', data.error);
}
} catch (error) {
console.error('Error updating balance:', error);
}
}
// Simple Blackjack implementation // Simple Blackjack implementation
const SUITS = ['♠', '♥', '♦', '♣']; const SUITS = ['♠', '♥', '♦', '♣'];
const RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']; const RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
@ -110,6 +129,7 @@ function startRound() {
currentBet = bet; currentBet = bet;
balance -= bet; balance -= bet;
updateBalanceToServer(balance); // Update to server
inRound = true; inRound = true;
dealerHidden = true; dealerHidden = true;
@ -129,10 +149,12 @@ function startRound() {
if (dealerVal === 21) { if (dealerVal === 21) {
balance += currentBet; balance += currentBet;
updateBalanceToServer(balance); // Update to server
setMessage('Tie (seri).', ''); setMessage('Tie (seri).', '');
} else { } else {
const payout = Math.floor(currentBet * 2.5); const payout = Math.floor(currentBet * 2.5);
balance += payout; balance += payout;
updateBalanceToServer(balance); // Update to server
setMessage('Blackjack! You Win!', 'win'); setMessage('Blackjack! You Win!', 'win');
} }
@ -171,10 +193,12 @@ function playerStand() {
if (dv > 21 || pv > dv) { if (dv > 21 || pv > dv) {
balance += currentBet * 2; balance += currentBet * 2;
updateBalanceToServer(balance); // Update to server
setMessage('You Win!', 'win'); setMessage('You Win!', 'win');
} else if (pv === dv) { } else if (pv === dv) {
balance += currentBet; balance += currentBet;
updateBalanceToServer(balance); // Update to server
setMessage('Tie (seri).', ''); setMessage('Tie (seri).', '');
} else { } else {
@ -191,6 +215,7 @@ function playerDouble() {
if (balance < currentBet) { alert('Bank tidak cukup untuk double.'); return; } if (balance < currentBet) { alert('Bank tidak cukup untuk double.'); return; }
balance -= currentBet; balance -= currentBet;
updateBalanceToServer(balance); // Update to server
currentBet *= 2; currentBet *= 2;
player.push(deck.pop()); player.push(deck.pop());
@ -216,7 +241,7 @@ doubleBtn.addEventListener('click', playerDouble);
newRoundBtn.addEventListener('click', () => { newRoundBtn.addEventListener('click', () => {
if (inRound && !confirm('Masih dalam ronde. Reset?')) return; if (inRound && !confirm('Masih dalam ronde. Reset?')) return;
// Do NOT reset balance to 1000. Balance persists. // Do NOT reset balance. Balance persists.
currentBet = 0; currentBet = 0;
inRound = false; inRound = false;
dealer = []; dealer = [];
@ -279,6 +304,7 @@ function processTopUp() {
// Simulasi proses top up (dalam implementasi nyata, ini akan terhubung ke payment gateway) // Simulasi proses top up (dalam implementasi nyata, ini akan terhubung ke payment gateway)
balance += amount; balance += amount;
updateBalanceToServer(balance); // Update to server
topUpAmount += amount; topUpAmount += amount;
// Tambahkan ke riwayat top up // Tambahkan ke riwayat top up
@ -372,9 +398,7 @@ const originalNewRound = newRoundBtn.onclick;
newRoundBtn.onclick = function () { newRoundBtn.onclick = function () {
if (inRound && !confirm('Masih dalam ronde. Reset?')) return; if (inRound && !confirm('Masih dalam ronde. Reset?')) return;
// Reset saldo tapi pertahankan riwayat top up // Reset game tapi jangan reset balance
const currentTopUpAmount = topUpAmount;
balance = 1000 + currentTopUpAmount;
currentBet = 0; currentBet = 0;
inRound = false; inRound = false;
dealer = []; dealer = [];

36
update_balance.php Normal file
View File

@ -0,0 +1,36 @@
<?php
include 'koneksi.php';
session_start();
if (!isset($_SESSION['username'])) {
http_response_code(401);
echo json_encode(['error' => 'Not logged in']);
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
exit;
}
$input = json_decode(file_get_contents('php://input'), true);
$new_balance = isset($input['balance']) ? (int)$input['balance'] : null;
if ($new_balance === null || $new_balance < 0) {
http_response_code(400);
echo json_encode(['error' => 'Invalid balance']);
exit;
}
$username = mysqli_real_escape_string($conn, $_SESSION['username']);
$query = "UPDATE users SET balance = $new_balance WHERE username = '$username'";
if (mysqli_query($conn, $query)) {
$_SESSION['balance'] = $new_balance;
echo json_encode(['success' => true, 'balance' => $new_balance]);
} else {
http_response_code(500);
echo json_encode(['error' => 'Database update failed']);
}
?>