This commit is contained in:
Cliff 2025-12-01 11:30:34 +07:00
parent c17ddbd203
commit 38914b6389
2 changed files with 41 additions and 6 deletions

View File

@ -28,6 +28,17 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['bank_method'])) {
$topup_success = true; $topup_success = true;
} }
} }
// Handle balance update from game
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_balance'])) {
$new_balance = (int)$_POST['update_balance'];
if ($new_balance >= 0) {
$_SESSION['balance'] = $new_balance;
header('Content-Type: application/json');
echo json_encode(['status' => 'success', 'balance' => $_SESSION['balance']]);
exit;
}
}
?> ?>
<!doctype html> <!doctype html>
<html lang="id"> <html lang="id">
@ -371,12 +382,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['bank_method'])) {
</footer> </footer>
</div> </div>
<script src="ody git.js"></script>
<script> <script>
// Initialize balance from server // Initialize balance from server BEFORE loading game script
const serverBalance = <?php echo (int)$_SESSION['balance']; ?>; const serverBalance = <?php echo (int)$_SESSION['balance']; ?>;
let gameBalance = serverBalance; let gameBalance = serverBalance;
</script>
<script src="ody git.js"></script>
<script>
// Update display on load // Update display on load
function updateBalanceDisplay() { function updateBalanceDisplay() {
document.getElementById('balance').textContent = gameBalance.toLocaleString('id-ID'); document.getElementById('balance').textContent = gameBalance.toLocaleString('id-ID');

View File

@ -27,10 +27,17 @@ let deck = [];
let dealer = []; let dealer = [];
let player = []; let player = [];
let dealerHidden = true; let dealerHidden = true;
let balance = 1000; let balance = 0; // Will be initialized from server
let currentBet = 0; let currentBet = 0;
let inRound = false; let inRound = false;
// Initialize balance from global gameBalance (set in html.php)
if (typeof gameBalance !== 'undefined') {
balance = gameBalance;
} else {
balance = 1000; // Fallback
}
function makeDeck(){ function makeDeck(){
deck = []; deck = [];
for(const s of SUITS){ for(const s of SUITS){
@ -97,8 +104,20 @@ function updateUI(){
renderHand(playerHandEl,player,false); renderHand(playerHandEl,player,false);
dealerValueEl.textContent = dealerHidden ? '??' : 'Nilai: '+handValues(dealer); dealerValueEl.textContent = dealerHidden ? '??' : 'Nilai: '+handValues(dealer);
playerValueEl.textContent = 'Nilai: '+handValues(player); playerValueEl.textContent = 'Nilai: '+handValues(player);
balanceEl.textContent = balance; balanceEl.textContent = balance.toLocaleString('id-ID');
currentBetEl.textContent = currentBet; currentBetEl.textContent = currentBet.toLocaleString('id-ID');
}
// Function to update balance on server
function updateBalanceOnServer() {
fetch('html.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'update_balance=' + balance
})
.catch(err => console.log('Balance update sent'));
} }
function startRound(){ function startRound(){
@ -138,6 +157,7 @@ function startRound(){
inRound=false; inRound=false;
currentBet=0; currentBet=0;
updateUI(); updateUI();
updateBalanceOnServer();
} }
} }
@ -153,6 +173,7 @@ function playerHit(){
inRound=false; inRound=false;
currentBet=0; currentBet=0;
updateUI(); updateUI();
updateBalanceOnServer();
} }
} }
@ -183,6 +204,7 @@ function playerStand(){
inRound=false; inRound=false;
currentBet=0; currentBet=0;
updateUI(); updateUI();
updateBalanceOnServer();
} }
function playerDouble(){ function playerDouble(){
@ -201,6 +223,7 @@ function playerDouble(){
inRound=false; inRound=false;
currentBet=0; currentBet=0;
updateUI(); updateUI();
updateBalanceOnServer();
return; return;
} }