gasken bossku
This commit is contained in:
parent
044095e267
commit
e7d2e7bccb
@ -1,5 +1,14 @@
|
||||
<?php
|
||||
include "koneksi.php";
|
||||
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_destroy();
|
||||
header('Location: loginn.php');
|
||||
|
||||
32
ody git.js
32
ody git.js
@ -4,6 +4,25 @@ function setMessage(text, status) {
|
||||
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
|
||||
const SUITS = ['♠', '♥', '♦', '♣'];
|
||||
const RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'];
|
||||
@ -110,6 +129,7 @@ function startRound() {
|
||||
|
||||
currentBet = bet;
|
||||
balance -= bet;
|
||||
updateBalanceToServer(balance); // Update to server
|
||||
inRound = true;
|
||||
dealerHidden = true;
|
||||
|
||||
@ -129,10 +149,12 @@ function startRound() {
|
||||
|
||||
if (dealerVal === 21) {
|
||||
balance += currentBet;
|
||||
updateBalanceToServer(balance); // Update to server
|
||||
setMessage('Tie (seri).', '');
|
||||
} else {
|
||||
const payout = Math.floor(currentBet * 2.5);
|
||||
balance += payout;
|
||||
updateBalanceToServer(balance); // Update to server
|
||||
setMessage('Blackjack! You Win!', 'win');
|
||||
}
|
||||
|
||||
@ -171,10 +193,12 @@ function playerStand() {
|
||||
|
||||
if (dv > 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 = [];
|
||||
|
||||
36
update_balance.php
Normal file
36
update_balance.php
Normal 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']);
|
||||
}
|
||||
?>
|
||||
Loading…
x
Reference in New Issue
Block a user