fix in bug
This commit is contained in:
parent
38914b6389
commit
0e1543374f
17
html.php
17
html.php
@ -386,6 +386,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_balance'])) {
|
|||||||
// Initialize balance from server BEFORE loading game script
|
// 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;
|
||||||
|
// let balance = serverBalance; // Removed to avoid conflict with ody git.js
|
||||||
</script>
|
</script>
|
||||||
<script src="ody git.js"></script>
|
<script src="ody git.js"></script>
|
||||||
<script>
|
<script>
|
||||||
@ -452,17 +453,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_balance'])) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add balance immediately
|
|
||||||
gameBalance += amount;
|
|
||||||
updateBalanceDisplay();
|
|
||||||
|
|
||||||
// Submit form
|
// Submit form
|
||||||
const formData = new FormData(topupForm);
|
const formData = new FormData(topupForm);
|
||||||
fetch('html.php', {
|
fetch('html.php', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: formData
|
body: formData
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => response.text())
|
||||||
|
.then(data => {
|
||||||
|
// Update balance from server response
|
||||||
|
const newBalance = serverBalance + amount;
|
||||||
|
gameBalance = newBalance;
|
||||||
|
balance = newBalance;
|
||||||
|
updateBalanceDisplay();
|
||||||
|
|
||||||
showTopUpMessage('✓ Top up berhasil! Saldo: Rp ' + gameBalance.toLocaleString('id-ID'), 'success');
|
showTopUpMessage('✓ Top up berhasil! Saldo: Rp ' + gameBalance.toLocaleString('id-ID'), 'success');
|
||||||
topupForm.reset();
|
topupForm.reset();
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@ -470,8 +474,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_balance'])) {
|
|||||||
}, 2000);
|
}, 2000);
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
gameBalance -= amount; // Revert if error
|
console.error('Error:', error);
|
||||||
updateBalanceDisplay();
|
|
||||||
showTopUpMessage('Terjadi kesalahan. Coba lagi.', 'error');
|
showTopUpMessage('Terjadi kesalahan. Coba lagi.', 'error');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
188
ody git.js
188
ody git.js
@ -1,7 +1,7 @@
|
|||||||
// SET MESSAGE (WIN/LOSE)
|
// SET MESSAGE (WIN/LOSE)
|
||||||
function setMessage(text, status) {
|
function setMessage(text, status) {
|
||||||
messageEl.textContent = text;
|
messageEl.textContent = text;
|
||||||
messageEl.className = status; // 'win', 'lose', atau '' (tie)
|
messageEl.className = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple Blackjack implementation
|
// Simple Blackjack implementation
|
||||||
@ -23,19 +23,33 @@ const doubleBtn = document.getElementById('double');
|
|||||||
const newRoundBtn = document.getElementById('new-round');
|
const newRoundBtn = document.getElementById('new-round');
|
||||||
const messageEl = document.getElementById('message');
|
const messageEl = document.getElementById('message');
|
||||||
|
|
||||||
|
// TOP UP DOM Elements - HANDLED IN HTML.PHP
|
||||||
|
// const topUpBtn = document.getElementById('top-up-btn');
|
||||||
|
// const topUpModal = document.getElementById('top-up-modal');
|
||||||
|
// const topUpClose = document.getElementById('top-up-close');
|
||||||
|
// const topUpInput = document.getElementById('top-up-input');
|
||||||
|
// const topUpConfirm = document.getElementById('top-up-confirm');
|
||||||
|
// const topUpHistoryEl = document.getElementById('top-up-history');
|
||||||
|
// const topUpBalanceEl = document.getElementById('top-up-balance');
|
||||||
|
|
||||||
|
// Game variables
|
||||||
let deck = [];
|
let deck = [];
|
||||||
let dealer = [];
|
let dealer = [];
|
||||||
let player = [];
|
let player = [];
|
||||||
let dealerHidden = true;
|
let dealerHidden = true;
|
||||||
let balance = 0; // Will be initialized from server
|
let balance = 0;
|
||||||
let currentBet = 0;
|
let currentBet = 0;
|
||||||
let inRound = false;
|
let inRound = false;
|
||||||
|
|
||||||
// Initialize balance from global gameBalance (set in html.php)
|
// TOP UP variables - HANDLED IN HTML.PHP
|
||||||
|
// let topUpAmount = 0;
|
||||||
|
// let topUpHistory = [];
|
||||||
|
|
||||||
|
// Initialize balance from global gameBalance
|
||||||
if (typeof gameBalance !== 'undefined') {
|
if (typeof gameBalance !== 'undefined') {
|
||||||
balance = gameBalance;
|
balance = gameBalance;
|
||||||
} else {
|
} else {
|
||||||
balance = 1000; // Fallback
|
balance = 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeDeck() {
|
function makeDeck() {
|
||||||
@ -106,9 +120,9 @@ function updateUI(){
|
|||||||
playerValueEl.textContent = 'Nilai: ' + handValues(player);
|
playerValueEl.textContent = 'Nilai: ' + handValues(player);
|
||||||
balanceEl.textContent = balance.toLocaleString('id-ID');
|
balanceEl.textContent = balance.toLocaleString('id-ID');
|
||||||
currentBetEl.textContent = currentBet.toLocaleString('id-ID');
|
currentBetEl.textContent = currentBet.toLocaleString('id-ID');
|
||||||
|
// updateTopUpBalance(); // Redundant
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to update balance on server
|
|
||||||
function updateBalanceOnServer() {
|
function updateBalanceOnServer() {
|
||||||
fetch('html.php', {
|
fetch('html.php', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@ -117,23 +131,41 @@ function updateBalanceOnServer() {
|
|||||||
},
|
},
|
||||||
body: 'update_balance=' + balance
|
body: 'update_balance=' + balance
|
||||||
})
|
})
|
||||||
.catch(err => console.log('Balance update sent'));
|
.then(response => {
|
||||||
|
if (response.headers.get('content-type')?.includes('application/json')) {
|
||||||
|
return response.json();
|
||||||
|
}
|
||||||
|
return { status: 'success' };
|
||||||
|
})
|
||||||
|
.then(data => {
|
||||||
|
if (data.balance !== undefined) {
|
||||||
|
balance = data.balance;
|
||||||
|
gameBalance = data.balance;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(err => console.log('Balance update sent', err));
|
||||||
}
|
}
|
||||||
|
|
||||||
function startRound() {
|
function startRound() {
|
||||||
if (inRound) return;
|
if (inRound) return;
|
||||||
|
|
||||||
const bet = Number(betInput.value) || 0;
|
const bet = Number(betInput.value) || 0;
|
||||||
if(bet <=0 || bet > balance){ alert(' BANK TIDAK CUKUP! '); return; }
|
|
||||||
|
if (bet <= 0 || bet > balance) {
|
||||||
|
alert('BANK TIDAK CUKUP!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
currentBet = bet;
|
currentBet = bet;
|
||||||
balance -= bet;
|
balance -= bet;
|
||||||
|
gameBalance = balance;
|
||||||
inRound = true;
|
inRound = true;
|
||||||
dealerHidden = true;
|
dealerHidden = true;
|
||||||
|
|
||||||
setMessage('', ''); // RESET MESSAGE
|
setMessage('', '');
|
||||||
|
|
||||||
makeDeck(); shuffle();
|
makeDeck();
|
||||||
|
shuffle();
|
||||||
dealer = [deck.pop(), deck.pop()];
|
dealer = [deck.pop(), deck.pop()];
|
||||||
player = [deck.pop(), deck.pop()];
|
player = [deck.pop(), deck.pop()];
|
||||||
|
|
||||||
@ -147,10 +179,12 @@ function startRound(){
|
|||||||
|
|
||||||
if (dealerVal === 21) {
|
if (dealerVal === 21) {
|
||||||
balance += currentBet;
|
balance += currentBet;
|
||||||
|
gameBalance = balance;
|
||||||
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;
|
||||||
|
gameBalance = balance;
|
||||||
setMessage('Blackjack! You Win!', 'win');
|
setMessage('Blackjack! You Win!', 'win');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,6 +206,7 @@ function playerHit(){
|
|||||||
setMessage('Bust! You Lose!', 'lose');
|
setMessage('Bust! You Lose!', 'lose');
|
||||||
inRound = false;
|
inRound = false;
|
||||||
currentBet = 0;
|
currentBet = 0;
|
||||||
|
gameBalance = balance;
|
||||||
updateUI();
|
updateUI();
|
||||||
updateBalanceOnServer();
|
updateBalanceOnServer();
|
||||||
}
|
}
|
||||||
@ -191,10 +226,12 @@ function playerStand(){
|
|||||||
|
|
||||||
if (dv > 21 || pv > dv) {
|
if (dv > 21 || pv > dv) {
|
||||||
balance += currentBet * 2;
|
balance += currentBet * 2;
|
||||||
|
gameBalance = balance;
|
||||||
setMessage('You Win!', 'win');
|
setMessage('You Win!', 'win');
|
||||||
|
|
||||||
} else if (pv === dv) {
|
} else if (pv === dv) {
|
||||||
balance += currentBet;
|
balance += currentBet;
|
||||||
|
gameBalance = balance;
|
||||||
setMessage('Tie (seri).', '');
|
setMessage('Tie (seri).', '');
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -209,9 +246,13 @@ function playerStand(){
|
|||||||
|
|
||||||
function playerDouble() {
|
function playerDouble() {
|
||||||
if (!inRound) return;
|
if (!inRound) return;
|
||||||
if(balance < currentBet){ alert('Bank tidak cukup untuk double.'); return; }
|
if (balance < currentBet) {
|
||||||
|
alert('Bank tidak cukup untuk double.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
balance -= currentBet;
|
balance -= currentBet;
|
||||||
|
gameBalance = balance;
|
||||||
currentBet *= 2;
|
currentBet *= 2;
|
||||||
|
|
||||||
player.push(deck.pop());
|
player.push(deck.pop());
|
||||||
@ -230,62 +271,18 @@ function playerDouble(){
|
|||||||
playerStand();
|
playerStand();
|
||||||
}
|
}
|
||||||
|
|
||||||
// EVENTS
|
// TOP UP FUNCTIONS - HANDLED IN HTML.PHP
|
||||||
betBtn.addEventListener('click', startRound);
|
/*
|
||||||
hitBtn.addEventListener('click', playerHit);
|
|
||||||
standBtn.addEventListener('click', playerStand);
|
|
||||||
doubleBtn.addEventListener('click', playerDouble);
|
|
||||||
|
|
||||||
newRoundBtn.addEventListener('click', ()=>{
|
|
||||||
if(inRound && !confirm('Masih dalam ronde. Reset?')) return;
|
|
||||||
balance = 1000;
|
|
||||||
currentBet=0;
|
|
||||||
inRound=false;
|
|
||||||
dealer=[];
|
|
||||||
player=[];
|
|
||||||
deck=[];
|
|
||||||
dealerHidden=true;
|
|
||||||
setMessage('Bank di-reset.', '');
|
|
||||||
updateUI();
|
|
||||||
});
|
|
||||||
|
|
||||||
// KEYBOARD
|
|
||||||
window.addEventListener('keydown', e=>{
|
|
||||||
if(e.key==='h') playerHit();
|
|
||||||
if(e.key==='s') playerStand();
|
|
||||||
if(e.key==='d') playerDouble();
|
|
||||||
if(e.key==='Enter') startRound();
|
|
||||||
});
|
|
||||||
|
|
||||||
updateUI();
|
|
||||||
|
|
||||||
// TOP UP FEATURE
|
|
||||||
// Tambahkan variabel global untuk top up
|
|
||||||
let topUpAmount = 0;
|
|
||||||
let topUpHistory = [];
|
|
||||||
|
|
||||||
// DOM elements untuk top up
|
|
||||||
const topUpBtn = document.getElementById('top-up-btn');
|
|
||||||
const topUpModal = document.getElementById('top-up-modal');
|
|
||||||
const topUpClose = document.getElementById('top-up-close');
|
|
||||||
const topUpInput = document.getElementById('top-up-input');
|
|
||||||
const topUpConfirm = document.getElementById('top-up-confirm');
|
|
||||||
const topUpHistoryEl = document.getElementById('top-up-history');
|
|
||||||
const topUpBalanceEl = document.getElementById('top-up-balance');
|
|
||||||
|
|
||||||
// Fungsi untuk menampilkan modal top up
|
|
||||||
function showTopUpModal() {
|
function showTopUpModal() {
|
||||||
topUpModal.style.display = 'block';
|
topUpModal.style.display = 'block';
|
||||||
topUpInput.value = '';
|
if(topUpInput) topUpInput.value = '';
|
||||||
updateTopUpHistory();
|
updateTopUpHistory();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fungsi untuk menyembunyikan modal top up
|
|
||||||
function hideTopUpModal() {
|
function hideTopUpModal() {
|
||||||
topUpModal.style.display = 'none';
|
topUpModal.style.display = 'none';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fungsi untuk memproses top up
|
|
||||||
function processTopUp() {
|
function processTopUp() {
|
||||||
const amount = Number(topUpInput.value) || 0;
|
const amount = Number(topUpInput.value) || 0;
|
||||||
|
|
||||||
@ -299,29 +296,24 @@ function processTopUp() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simulasi proses top up (dalam implementasi nyata, ini akan terhubung ke payment gateway)
|
|
||||||
balance += amount;
|
balance += amount;
|
||||||
|
gameBalance = balance;
|
||||||
topUpAmount += amount;
|
topUpAmount += amount;
|
||||||
|
|
||||||
// Tambahkan ke riwayat top up
|
|
||||||
topUpHistory.push({
|
topUpHistory.push({
|
||||||
amount: amount,
|
amount: amount,
|
||||||
date: new Date().toLocaleString('id-ID'),
|
date: new Date().toLocaleString('id-ID'),
|
||||||
balanceAfter: balance
|
balanceAfter: balance
|
||||||
});
|
});
|
||||||
|
|
||||||
// Update UI
|
|
||||||
updateUI();
|
updateUI();
|
||||||
updateTopUpHistory();
|
updateTopUpHistory();
|
||||||
|
|
||||||
// Tampilkan pesan sukses
|
|
||||||
setMessage(`Top up berhasil! +${amount.toLocaleString('id-ID')}`, 'win');
|
setMessage(`Top up berhasil! +${amount.toLocaleString('id-ID')}`, 'win');
|
||||||
|
|
||||||
// Sembunyikan modal
|
|
||||||
hideTopUpModal();
|
hideTopUpModal();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fungsi untuk memperbarui riwayat top up
|
|
||||||
function updateTopUpHistory() {
|
function updateTopUpHistory() {
|
||||||
if (!topUpHistoryEl) return;
|
if (!topUpHistoryEl) return;
|
||||||
|
|
||||||
@ -332,7 +324,6 @@ function updateTopUpHistory() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tampilkan maksimal 5 riwayat terbaru
|
|
||||||
const recentHistory = topUpHistory.slice(-5).reverse();
|
const recentHistory = topUpHistory.slice(-5).reverse();
|
||||||
|
|
||||||
recentHistory.forEach(record => {
|
recentHistory.forEach(record => {
|
||||||
@ -347,14 +338,39 @@ function updateTopUpHistory() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fungsi untuk memperbarui tampilan saldo top up
|
|
||||||
function updateTopUpBalance() {
|
function updateTopUpBalance() {
|
||||||
if (topUpBalanceEl) {
|
if (topUpBalanceEl) {
|
||||||
topUpBalanceEl.textContent = `Total Top Up: ${topUpAmount.toLocaleString('id-ID')}`;
|
topUpBalanceEl.textContent = `Total Top Up: ${topUpAmount.toLocaleString('id-ID')}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// Event listeners untuk top up
|
// EVENT LISTENERS
|
||||||
|
betBtn.addEventListener('click', function (e) {
|
||||||
|
startRound();
|
||||||
|
});
|
||||||
|
|
||||||
|
hitBtn.addEventListener('click', playerHit);
|
||||||
|
standBtn.addEventListener('click', playerStand);
|
||||||
|
doubleBtn.addEventListener('click', playerDouble);
|
||||||
|
|
||||||
|
newRoundBtn.addEventListener('click', function () {
|
||||||
|
if (inRound && !confirm('Masih dalam ronde. Reset?')) return;
|
||||||
|
balance = 1000;
|
||||||
|
gameBalance = 1000;
|
||||||
|
currentBet = 0;
|
||||||
|
inRound = false;
|
||||||
|
dealer = [];
|
||||||
|
player = [];
|
||||||
|
deck = [];
|
||||||
|
dealerHidden = true;
|
||||||
|
setMessage('Bank di-reset.', '');
|
||||||
|
updateUI();
|
||||||
|
updateBalanceOnServer();
|
||||||
|
});
|
||||||
|
|
||||||
|
// TOP UP EVENT LISTENERS - HANDLED IN HTML.PHP
|
||||||
|
/*
|
||||||
if (topUpBtn) {
|
if (topUpBtn) {
|
||||||
topUpBtn.addEventListener('click', showTopUpModal);
|
topUpBtn.addEventListener('click', showTopUpModal);
|
||||||
}
|
}
|
||||||
@ -367,46 +383,28 @@ if (topUpConfirm) {
|
|||||||
topUpConfirm.addEventListener('click', processTopUp);
|
topUpConfirm.addEventListener('click', processTopUp);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tutup modal jika klik di luar modal
|
|
||||||
window.addEventListener('click', (e) => {
|
window.addEventListener('click', (e) => {
|
||||||
if (e.target === topUpModal) {
|
if (e.target === topUpModal) {
|
||||||
hideTopUpModal();
|
hideTopUpModal();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
// Keyboard shortcut untuk top up (Ctrl+T)
|
// KEYBOARD
|
||||||
window.addEventListener('keydown', e => {
|
window.addEventListener('keydown', e => {
|
||||||
|
if (e.key === 'h') playerHit();
|
||||||
|
if (e.key === 's') playerStand();
|
||||||
|
if (e.key === 'd') playerDouble();
|
||||||
|
if (e.key === 'Enter') startRound();
|
||||||
|
/*
|
||||||
if (e.ctrlKey && e.key === 't') {
|
if (e.ctrlKey && e.key === 't') {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
showTopUpModal();
|
showTopUpModal();
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
});
|
});
|
||||||
|
|
||||||
// Modifikasi fungsi updateUI untuk menampilkan informasi top up
|
// INITIALIZATION
|
||||||
const originalUpdateUI = updateUI;
|
|
||||||
updateUI = function() {
|
|
||||||
originalUpdateUI();` []`
|
|
||||||
updateTopUpBalance();
|
|
||||||
};
|
|
||||||
|
|
||||||
// Modifikasi fungsi newRound untuk reset saldo yang tidak mempengaruhi riwayat top up
|
|
||||||
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;
|
|
||||||
currentBet = 0;
|
|
||||||
inRound = false;
|
|
||||||
dealer = [];
|
|
||||||
player = [];
|
|
||||||
deck = [];
|
|
||||||
dealerHidden = true;
|
|
||||||
setMessage('Bank di-reset.', '');
|
|
||||||
updateUI();
|
updateUI();
|
||||||
};
|
// updateTopUpHistory();
|
||||||
|
// updateTopUpBalance();
|
||||||
// Inisialisasi
|
|
||||||
updateTopUpHistory();
|
|
||||||
updateTopUpBalance();
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user