diff --git a/logout.php b/logout.php index 3f7b77d..e21834c 100644 --- a/logout.php +++ b/logout.php @@ -1,5 +1,14 @@ 21 || pv > dv) { balance += currentBet * 2; + updateBalanceToServer(balance); // Update to server setMessage('You Win!', 'win'); } else if (pv === dv) { balance += currentBet; + updateBalanceToServer(balance); // Update to server setMessage('Tie (seri).', ''); } else { @@ -191,6 +215,7 @@ function playerDouble() { if (balance < currentBet) { alert('Bank tidak cukup untuk double.'); return; } balance -= currentBet; + updateBalanceToServer(balance); // Update to server currentBet *= 2; player.push(deck.pop()); @@ -216,7 +241,7 @@ doubleBtn.addEventListener('click', playerDouble); newRoundBtn.addEventListener('click', () => { if (inRound && !confirm('Masih dalam ronde. Reset?')) return; - // Do NOT reset balance to 1000. Balance persists. + // Do NOT reset balance. Balance persists. currentBet = 0; inRound = false; dealer = []; @@ -279,6 +304,7 @@ function processTopUp() { // Simulasi proses top up (dalam implementasi nyata, ini akan terhubung ke payment gateway) balance += amount; + updateBalanceToServer(balance); // Update to server topUpAmount += amount; // Tambahkan ke riwayat top up @@ -372,9 +398,7 @@ const originalNewRound = newRoundBtn.onclick; newRoundBtn.onclick = function () { if (inRound && !confirm('Masih dalam ronde. Reset?')) return; - // Reset saldo tapi pertahankan riwayat top up - const currentTopUpAmount = topUpAmount; - balance = 1000 + currentTopUpAmount; + // Reset game tapi jangan reset balance currentBet = 0; inRound = false; dealer = []; diff --git a/update_balance.php b/update_balance.php new file mode 100644 index 0000000..69f31e1 --- /dev/null +++ b/update_balance.php @@ -0,0 +1,36 @@ + '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']); +} +?> \ No newline at end of file