update html login js proses upbal sql json
This commit is contained in:
parent
0e1543374f
commit
1d5b72fe83
4
.vscode/settings.json
vendored
Normal file
4
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"python-envs.defaultEnvManager": "ms-python.python:system",
|
||||||
|
"python-envs.pythonProjects": []
|
||||||
|
}
|
||||||
72
html.php
72
html.php
@ -1,4 +1,76 @@
|
|||||||
<?php
|
<?php
|
||||||
|
session_start();
|
||||||
|
include "koneksi.php";
|
||||||
|
|
||||||
|
// Redirect ke login jika belum login
|
||||||
|
if (!isset($_SESSION['user_id'])) {
|
||||||
|
header("Location: loginn.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ambil data user dari database
|
||||||
|
$user_id = $_SESSION['user_id'];
|
||||||
|
$sql = "SELECT username, balance FROM users WHERE id = ?";
|
||||||
|
$stmt = mysqli_prepare($conn, $sql);
|
||||||
|
mysqli_stmt_bind_param($stmt, "i", $user_id);
|
||||||
|
mysqli_stmt_execute($stmt);
|
||||||
|
$result = mysqli_stmt_get_result($stmt);
|
||||||
|
$user = mysqli_fetch_assoc($result);
|
||||||
|
|
||||||
|
$username = $_SESSION['username'];
|
||||||
|
$balance = $_SESSION['balance'];
|
||||||
|
?>
|
||||||
|
|
||||||
|
<!doctype html>
|
||||||
|
<html lang="id">
|
||||||
|
<head>
|
||||||
|
<!-- ... meta tags tetap sama ... -->
|
||||||
|
<title>Blackjack [21] - OCA GameHub</title>
|
||||||
|
|
||||||
|
<!-- Tambah PHP untuk dynamic styling -->
|
||||||
|
<style>
|
||||||
|
/* ... semua CSS sebelumnya tetap ... */
|
||||||
|
|
||||||
|
/* Tambahan untuk user info */
|
||||||
|
.user-info {
|
||||||
|
display: flex;
|
||||||
|
gap: 15px;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 10px;
|
||||||
|
padding: 10px;
|
||||||
|
background: rgba(0, 50, 0, 0.2);
|
||||||
|
border-radius: 8px;
|
||||||
|
border: 1px solid rgba(0, 255, 0, 0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.balance-display {
|
||||||
|
font-size: 1.2em;
|
||||||
|
color: #00FF00;
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="table" role="application" aria-label="Blackjack tanpa iklan">
|
||||||
|
<header>
|
||||||
|
<h1> Blackjack [21] </h1>
|
||||||
|
<div class="controls">
|
||||||
|
<!-- Ganti bagian ini -->
|
||||||
|
<div class="user-info">
|
||||||
|
<div>Signed in as: <strong id="username-display"><?php echo htmlspecialchars($username); ?></strong></div>
|
||||||
|
<button id="topup-btn">Top Up</button>
|
||||||
|
<a href="logout.php" onclick="return confirm('Yakin ingin logout?')">Logout</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="info">
|
||||||
|
<div class="chip">Bank: <span id="balance"><?php echo number_format($balance, 0, ',', '.'); ?></span></div>
|
||||||
|
<div class="chip">Taruhan: <span id="current-bet">0</span></div>
|
||||||
|
</div>
|
||||||
|
<!-- ... bagian bet tetap sama ... -->
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
<!-- ... sisanya tetap sama ... -->
|
||||||
|
|
||||||
session_start();
|
session_start();
|
||||||
// If not logged in, redirect to login
|
// If not logged in, redirect to login
|
||||||
if (!isset($_SESSION['username'])) {
|
if (!isset($_SESSION['username'])) {
|
||||||
|
|||||||
89
loginn.php
89
loginn.php
@ -3,64 +3,101 @@ include "koneksi.php";
|
|||||||
session_start();
|
session_start();
|
||||||
|
|
||||||
$error = '';
|
$error = '';
|
||||||
if(isset($_POST['login'])){
|
$success = '';
|
||||||
$username = $_POST['username'];
|
|
||||||
|
if(isset($_POST['register'])){
|
||||||
|
$username = mysqli_real_escape_string($conn, $_POST['username']);
|
||||||
$password = $_POST['password'];
|
$password = $_POST['password'];
|
||||||
|
$confirm_password = $_POST['confirm_password'];
|
||||||
|
|
||||||
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
|
// Validasi
|
||||||
$result = mysqli_query($conn, $sql);
|
if(empty($username) || empty($password)) {
|
||||||
|
$error = 'All fields are required.';
|
||||||
|
} elseif($password !== $confirm_password) {
|
||||||
|
$error = 'Passwords do not match.';
|
||||||
|
} elseif(strlen($password) < 6) {
|
||||||
|
$error = 'Password must be at least 6 characters.';
|
||||||
|
} else {
|
||||||
|
// Cek username sudah ada
|
||||||
|
$check_sql = "SELECT id FROM users WHERE username = ?";
|
||||||
|
$check_stmt = mysqli_prepare($conn, $check_sql);
|
||||||
|
mysqli_stmt_bind_param($check_stmt, "s", $username);
|
||||||
|
mysqli_stmt_execute($check_stmt);
|
||||||
|
mysqli_stmt_store_result($check_stmt);
|
||||||
|
|
||||||
|
if(mysqli_stmt_num_rows($check_stmt) > 0) {
|
||||||
|
$error = 'Username already exists.';
|
||||||
|
} else {
|
||||||
|
// Password hashing untuk keamanan
|
||||||
|
// UNTUK TESTING: simpan plain text (tidak direkomendasikan)
|
||||||
|
$hashed_password = $password; // HAPUS INI DI PRODUKSI
|
||||||
|
|
||||||
|
// UNTUK PRODUKSI: gunakan password_hash()
|
||||||
|
// $hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
||||||
|
|
||||||
|
// Insert user baru dengan saldo awal
|
||||||
|
$insert_sql = "INSERT INTO users (username, password, balance, created_at)
|
||||||
|
VALUES (?, ?, 1000, NOW())";
|
||||||
|
$insert_stmt = mysqli_prepare($conn, $insert_sql);
|
||||||
|
mysqli_stmt_bind_param($insert_stmt, "ss", $username, $hashed_password);
|
||||||
|
|
||||||
|
if(mysqli_stmt_execute($insert_stmt)) {
|
||||||
|
$success = 'Registration successful! You can now login.';
|
||||||
|
// Auto login setelah register (opsional)
|
||||||
|
$user_id = mysqli_insert_id($conn);
|
||||||
|
$_SESSION['user_id'] = $user_id;
|
||||||
|
$_SESSION['username'] = $username;
|
||||||
|
$_SESSION['balance'] = 1000;
|
||||||
|
|
||||||
if (mysqli_num_rows($result) > 0) {
|
|
||||||
$user = mysqli_fetch_assoc($result);
|
|
||||||
$_SESSION['username'] = $user['username'];
|
|
||||||
// Initialize balance in session (default 1000 if not set)
|
|
||||||
$_SESSION['balance'] = isset($user['balance']) ? (int)$user['balance'] : 1000;
|
|
||||||
header("Location: html.php");
|
header("Location: html.php");
|
||||||
exit;
|
exit;
|
||||||
} else {
|
} else {
|
||||||
$error = 'Invalid username or password.';
|
$error = 'Registration failed. Please try again.';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<title>Register</title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Login</title>
|
|
||||||
<link rel="stylesheet" href="login.css">
|
<link rel="stylesheet" href="login.css">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<div class="logo">
|
<div class="logo">
|
||||||
<h1>OCAGamingHub</h1>
|
<h1>OCAGamingHub</h1>
|
||||||
<p>Welcome back — sign in to continue</p>
|
<p>Create your account</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-container">
|
<div class="form-container">
|
||||||
<div class="card-icon">♠</div>
|
|
||||||
|
|
||||||
<?php if(!empty($error)): ?>
|
<?php if(!empty($error)): ?>
|
||||||
<div class="error-message show"><?=htmlspecialchars($error)?></div>
|
<div class="error-message show"><?=htmlspecialchars($error)?></div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
<form action="loginn.php" method="POST">
|
<?php if(!empty($success)): ?>
|
||||||
|
<div class="success-message show"><?=htmlspecialchars($success)?></div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<form method="POST">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="username">Username</label>
|
<label>Username</label>
|
||||||
<input id="username" name="username" type="text" placeholder="Enter username" required>
|
<input type="text" name="username" required>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="password">Password</label>
|
<label>Password</label>
|
||||||
<input id="password" name="password" type="password" placeholder="Enter password" required>
|
<input type="password" name="password" required>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="button-group">
|
<div class="form-group">
|
||||||
<button type="submit" name="login" class="btn btn-signin">Sign In</button>
|
<label>Confirm Password</label>
|
||||||
<a href="register.php" class="btn btn-signup">Sign Up</a>
|
<input type="password" name="confirm_password" required>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<button type="submit" name="register" class="btn btn-signin">Register</button>
|
||||||
|
<a href="loginn.php" class="btn btn-signup">Back to Login</a>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
454
ody git.js
454
ody git.js
@ -1,14 +1,23 @@
|
|||||||
// SET MESSAGE (WIN/LOSE)
|
|
||||||
|
// =========================
|
||||||
|
// SET MESSAGE
|
||||||
|
// =========================
|
||||||
function setMessage(text, status) {
|
function setMessage(text, status) {
|
||||||
|
if (messageEl) {
|
||||||
messageEl.textContent = text;
|
messageEl.textContent = text;
|
||||||
messageEl.className = status;
|
messageEl.className = status;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple Blackjack implementation
|
// =========================
|
||||||
|
// KONSTANTA KARTU
|
||||||
|
// =========================
|
||||||
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'];
|
||||||
|
|
||||||
// DOM
|
// =========================
|
||||||
|
// DOM ELEMENTS
|
||||||
|
// =========================
|
||||||
const dealerHandEl = document.getElementById('dealer-hand');
|
const dealerHandEl = document.getElementById('dealer-hand');
|
||||||
const playerHandEl = document.getElementById('player-hand');
|
const playerHandEl = document.getElementById('player-hand');
|
||||||
const dealerValueEl = document.getElementById('dealer-value');
|
const dealerValueEl = document.getElementById('dealer-value');
|
||||||
@ -23,16 +32,16 @@ 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
|
// TOPUP DOM
|
||||||
// const topUpBtn = document.getElementById('top-up-btn');
|
const topupBtn = document.getElementById('topup-btn');
|
||||||
// const topUpModal = document.getElementById('top-up-modal');
|
const topupModal = document.getElementById('topup-modal');
|
||||||
// const topUpClose = document.getElementById('top-up-close');
|
const topupForm = document.getElementById('topup-form');
|
||||||
// const topUpInput = document.getElementById('top-up-input');
|
const topupMessage = document.getElementById('topup-message');
|
||||||
// const topUpConfirm = document.getElementById('top-up-confirm');
|
const modalAmount = document.getElementById('modal-amount');
|
||||||
// const topUpHistoryEl = document.getElementById('top-up-history');
|
|
||||||
// const topUpBalanceEl = document.getElementById('top-up-balance');
|
|
||||||
|
|
||||||
// Game variables
|
// =========================
|
||||||
|
// VARIABEL GAME
|
||||||
|
// =========================
|
||||||
let deck = [];
|
let deck = [];
|
||||||
let dealer = [];
|
let dealer = [];
|
||||||
let player = [];
|
let player = [];
|
||||||
@ -41,23 +50,80 @@ let balance = 0;
|
|||||||
let currentBet = 0;
|
let currentBet = 0;
|
||||||
let inRound = false;
|
let inRound = false;
|
||||||
|
|
||||||
// TOP UP variables - HANDLED IN HTML.PHP
|
// =========================================
|
||||||
// let topUpAmount = 0;
|
// AMBIL VALUE DARI PHP (HIDDEN INPUT)
|
||||||
// let topUpHistory = [];
|
// =========================================
|
||||||
|
let gameBalance = parseInt(document.getElementById('php-balance').value);
|
||||||
|
let userId = parseInt(document.getElementById('php-userid').value);
|
||||||
|
|
||||||
// Initialize balance from global gameBalance
|
// =========================
|
||||||
if (typeof gameBalance !== 'undefined') {
|
// UPDATE BALANCE DISPLAY
|
||||||
balance = gameBalance;
|
// =========================
|
||||||
} else {
|
function updateBalanceDisplay() {
|
||||||
balance = 1000;
|
if (balanceEl) balanceEl.textContent = gameBalance.toLocaleString('id-ID');
|
||||||
|
const modalBalance = document.getElementById('modal-balance');
|
||||||
|
if (modalBalance) modalBalance.textContent = gameBalance.toLocaleString('id-ID');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// SYNC KE SERVER
|
||||||
|
// =========================
|
||||||
|
async function syncBalanceToServer() {
|
||||||
|
try {
|
||||||
|
const response = await fetch('update_balance.php', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ userId, balance: gameBalance })
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
return data.success;
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error:", err);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// UPDATE SESSION PHP
|
||||||
|
// =========================
|
||||||
|
async function updateSessionBalance() {
|
||||||
|
try {
|
||||||
|
await fetch('update_session.php', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ balance: gameBalance })
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Error:", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// UPDATE BALANCE LOGIC
|
||||||
|
// =========================
|
||||||
|
function updateGameBalance(amount, type) {
|
||||||
|
if (type === 'win') gameBalance += amount;
|
||||||
|
else if (type === 'loss') gameBalance -= amount;
|
||||||
|
else if (type === 'bet') gameBalance -= amount;
|
||||||
|
else if (type === 'refund') gameBalance += amount;
|
||||||
|
|
||||||
|
updateBalanceDisplay();
|
||||||
|
syncBalanceToServer();
|
||||||
|
updateSessionBalance();
|
||||||
|
}
|
||||||
|
|
||||||
|
window.updateGameBalance = updateGameBalance;
|
||||||
|
window.gameBalance = gameBalance;
|
||||||
|
window.userId = userId;
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// FUNGSI KARTU
|
||||||
|
// =========================
|
||||||
function makeDeck() {
|
function makeDeck() {
|
||||||
deck = [];
|
deck = [];
|
||||||
for (const s of SUITS) {
|
for (const s of SUITS) {
|
||||||
for (const r of RANKS) {
|
for (const r of RANKS) deck.push({ suit: s, rank: r });
|
||||||
deck.push({ suit: s, rank: r });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,42 +135,45 @@ function shuffle() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function cardValue(card) {
|
function cardValue(card) {
|
||||||
const r = card.rank;
|
if (card.rank === 'A') return [1, 11];
|
||||||
if (r === 'A') return [1, 11];
|
if (['J', 'Q', 'K'].includes(card.rank)) return [10];
|
||||||
if (['J', 'Q', 'K'].includes(r)) return [10];
|
return [parseInt(card.rank)];
|
||||||
return [parseInt(r, 10)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handValues(hand) {
|
function handValues(hand) {
|
||||||
let totals = [0];
|
let totals = [0];
|
||||||
for (const c of hand) {
|
|
||||||
const vals = cardValue(c);
|
for (const card of hand) {
|
||||||
|
const cv = cardValue(card);
|
||||||
const newTotals = [];
|
const newTotals = [];
|
||||||
for (const t of totals) {
|
|
||||||
for (const v of vals) { newTotals.push(t + v); }
|
totals.forEach(t => cv.forEach(v => newTotals.push(t + v)));
|
||||||
}
|
|
||||||
totals = Array.from(new Set(newTotals));
|
totals = [...new Set(newTotals)];
|
||||||
}
|
}
|
||||||
|
|
||||||
const valid = totals.filter(t => t <= 21);
|
const valid = totals.filter(t => t <= 21);
|
||||||
if (valid.length) return Math.max(...valid);
|
return valid.length ? Math.max(...valid) : Math.min(...totals);
|
||||||
return Math.min(...totals);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderHand(el, hand, hideFirst = false) {
|
function renderHand(el, hand, hideFirst = false) {
|
||||||
el.innerHTML = '';
|
el.innerHTML = '';
|
||||||
|
|
||||||
hand.forEach((c, i) => {
|
hand.forEach((c, i) => {
|
||||||
const div = document.createElement('div');
|
const div = document.createElement('div');
|
||||||
div.className = 'card' + (c.suit === '♥' || c.suit === '♦' ? ' red' : '');
|
|
||||||
|
|
||||||
if (hideFirst && i === 0) {
|
if (hideFirst && i === 0) {
|
||||||
div.className = 'card back';
|
div.className = 'card back';
|
||||||
div.textContent = 'TERSEMBUNYI';
|
div.textContent = 'HIDE';
|
||||||
} else {
|
} else {
|
||||||
|
div.className = 'card' + ((c.suit === '♥' || c.suit === '♦') ? ' red' : '');
|
||||||
const top = document.createElement('div');
|
const top = document.createElement('div');
|
||||||
top.textContent = c.rank + ' ' + c.suit;
|
|
||||||
const bot = document.createElement('div');
|
const bot = document.createElement('div');
|
||||||
bot.style.alignSelf = 'flex-end';
|
|
||||||
|
top.textContent = c.rank + ' ' + c.suit;
|
||||||
bot.textContent = c.rank + ' ' + c.suit;
|
bot.textContent = c.rank + ' ' + c.suit;
|
||||||
|
bot.style.alignSelf = 'flex-end';
|
||||||
|
|
||||||
div.appendChild(top);
|
div.appendChild(top);
|
||||||
div.appendChild(bot);
|
div.appendChild(bot);
|
||||||
}
|
}
|
||||||
@ -113,88 +182,64 @@ function renderHand(el, hand, hideFirst = false) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// UPDATE UI
|
||||||
|
// =========================
|
||||||
function updateUI() {
|
function updateUI() {
|
||||||
renderHand(dealerHandEl, dealer, dealerHidden);
|
renderHand(dealerHandEl, dealer, dealerHidden);
|
||||||
renderHand(playerHandEl, player, false);
|
renderHand(playerHandEl, player);
|
||||||
|
|
||||||
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.toLocaleString('id-ID');
|
updateBalanceDisplay();
|
||||||
currentBetEl.textContent = currentBet.toLocaleString('id-ID');
|
|
||||||
// updateTopUpBalance(); // Redundant
|
if (currentBetEl) currentBetEl.textContent = currentBet.toLocaleString('id-ID');
|
||||||
}
|
|
||||||
|
|
||||||
function updateBalanceOnServer() {
|
|
||||||
fetch('html.php', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
|
||||||
},
|
|
||||||
body: 'update_balance=' + balance
|
|
||||||
})
|
|
||||||
.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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// START ROUND
|
||||||
|
// =========================
|
||||||
function startRound() {
|
function startRound() {
|
||||||
if (inRound) return;
|
if (inRound) return;
|
||||||
|
|
||||||
const bet = Number(betInput.value) || 0;
|
const bet = Number(betInput.value);
|
||||||
|
if (bet <= 0) return alert("Masukkan jumlah taruhan!");
|
||||||
if (bet <= 0 || bet > balance) {
|
if (bet > gameBalance) return alert("BANK TIDAK CUKUP!");
|
||||||
alert('BANK TIDAK CUKUP!');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
currentBet = bet;
|
currentBet = bet;
|
||||||
balance -= bet;
|
updateGameBalance(bet, 'bet');
|
||||||
gameBalance = balance;
|
|
||||||
inRound = true;
|
inRound = true;
|
||||||
dealerHidden = true;
|
dealerHidden = true;
|
||||||
|
setMessage("", "");
|
||||||
setMessage('', '');
|
|
||||||
|
|
||||||
makeDeck();
|
makeDeck();
|
||||||
shuffle();
|
shuffle();
|
||||||
|
|
||||||
dealer = [deck.pop(), deck.pop()];
|
dealer = [deck.pop(), deck.pop()];
|
||||||
player = [deck.pop(), deck.pop()];
|
player = [deck.pop(), deck.pop()];
|
||||||
|
|
||||||
updateUI();
|
updateUI();
|
||||||
|
|
||||||
// NATURAL BLACKJACK
|
// NATURAL BLACKJACK
|
||||||
if (handValues(player) === 21) {
|
if (handValues(player) === 21) {
|
||||||
dealerHidden = false;
|
dealerHidden = false;
|
||||||
updateUI();
|
updateUI();
|
||||||
const dealerVal = handValues(dealer);
|
|
||||||
|
|
||||||
if (dealerVal === 21) {
|
if (handValues(dealer) === 21) {
|
||||||
balance += currentBet;
|
updateGameBalance(currentBet, 'refund');
|
||||||
gameBalance = balance;
|
setMessage("Tie!", "");
|
||||||
setMessage('Tie (seri).', '');
|
|
||||||
} else {
|
} else {
|
||||||
const payout = Math.floor(currentBet * 2.5);
|
updateGameBalance(Math.floor(currentBet * 2.5), 'win');
|
||||||
balance += payout;
|
setMessage("Blackjack! You win!", "win");
|
||||||
gameBalance = balance;
|
|
||||||
setMessage('Blackjack! You Win!', 'win');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inRound = false;
|
inRound = false;
|
||||||
currentBet = 0;
|
currentBet = 0;
|
||||||
updateUI();
|
|
||||||
updateBalanceOnServer();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// HIT
|
||||||
|
// =========================
|
||||||
function playerHit() {
|
function playerHit() {
|
||||||
if (!inRound) return;
|
if (!inRound) return;
|
||||||
|
|
||||||
@ -203,56 +248,49 @@ function playerHit() {
|
|||||||
|
|
||||||
if (handValues(player) > 21) {
|
if (handValues(player) > 21) {
|
||||||
dealerHidden = false;
|
dealerHidden = false;
|
||||||
setMessage('Bust! You Lose!', 'lose');
|
setMessage("Bust! You Lose!", "lose");
|
||||||
inRound = false;
|
inRound = false;
|
||||||
currentBet = 0;
|
currentBet = 0;
|
||||||
gameBalance = balance;
|
|
||||||
updateUI();
|
|
||||||
updateBalanceOnServer();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// STAND
|
||||||
|
// =========================
|
||||||
function playerStand() {
|
function playerStand() {
|
||||||
if (!inRound) return;
|
if (!inRound) return;
|
||||||
|
|
||||||
dealerHidden = false;
|
dealerHidden = false;
|
||||||
|
|
||||||
while (handValues(dealer) < 17) {
|
while (handValues(dealer) < 17) dealer.push(deck.pop());
|
||||||
dealer.push(deck.pop());
|
|
||||||
}
|
updateUI();
|
||||||
|
|
||||||
const pv = handValues(player);
|
const pv = handValues(player);
|
||||||
const dv = handValues(dealer);
|
const dv = handValues(dealer);
|
||||||
|
|
||||||
if (dv > 21 || pv > dv) {
|
if (dv > 21 || pv > dv) {
|
||||||
balance += currentBet * 2;
|
updateGameBalance(currentBet * 2, 'win');
|
||||||
gameBalance = balance;
|
setMessage("You Win!", "win");
|
||||||
setMessage('You Win!', 'win');
|
|
||||||
|
|
||||||
} else if (pv === dv) {
|
} else if (pv === dv) {
|
||||||
balance += currentBet;
|
updateGameBalance(currentBet, 'refund');
|
||||||
gameBalance = balance;
|
setMessage("Tie!", "");
|
||||||
setMessage('Tie (seri).', '');
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
setMessage('You Lose!', 'lose');
|
setMessage("You Lose!", "lose");
|
||||||
}
|
}
|
||||||
|
|
||||||
inRound = false;
|
inRound = false;
|
||||||
currentBet = 0;
|
currentBet = 0;
|
||||||
updateUI();
|
|
||||||
updateBalanceOnServer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// DOUBLE
|
||||||
|
// =========================
|
||||||
function playerDouble() {
|
function playerDouble() {
|
||||||
if (!inRound) return;
|
if (!inRound) return;
|
||||||
if (balance < currentBet) {
|
if (gameBalance < currentBet) return alert("Bank tidak cukup untuk double.");
|
||||||
alert('Bank tidak cukup untuk double.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
balance -= currentBet;
|
updateGameBalance(currentBet, 'bet');
|
||||||
gameBalance = balance;
|
|
||||||
currentBet *= 2;
|
currentBet *= 2;
|
||||||
|
|
||||||
player.push(deck.pop());
|
player.push(deck.pop());
|
||||||
@ -260,151 +298,103 @@ function playerDouble() {
|
|||||||
|
|
||||||
if (handValues(player) > 21) {
|
if (handValues(player) > 21) {
|
||||||
dealerHidden = false;
|
dealerHidden = false;
|
||||||
setMessage('Bust! You Lose!', 'lose');
|
setMessage("Bust! You Lose!", "lose");
|
||||||
inRound = false;
|
inRound = false;
|
||||||
currentBet = 0;
|
currentBet = 0;
|
||||||
updateUI();
|
|
||||||
updateBalanceOnServer();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
playerStand();
|
playerStand();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TOP UP FUNCTIONS - HANDLED IN HTML.PHP
|
// =========================
|
||||||
/*
|
// TOP UP MODAL
|
||||||
function showTopUpModal() {
|
// =========================
|
||||||
topUpModal.style.display = 'block';
|
topupBtn.addEventListener('click', () => {
|
||||||
if(topUpInput) topUpInput.value = '';
|
topupModal.style.display = 'flex';
|
||||||
updateTopUpHistory();
|
topupForm.reset();
|
||||||
}
|
topupMessage.style.display = 'none';
|
||||||
|
updateBalanceDisplay();
|
||||||
function hideTopUpModal() {
|
|
||||||
topUpModal.style.display = 'none';
|
|
||||||
}
|
|
||||||
|
|
||||||
function processTopUp() {
|
|
||||||
const amount = Number(topUpInput.value) || 0;
|
|
||||||
|
|
||||||
if (amount <= 0) {
|
|
||||||
alert('Masukkan jumlah top up yang valid!');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (amount > 1000000) {
|
|
||||||
alert('Maksimal top up adalah 1.000.000!');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
balance += amount;
|
|
||||||
gameBalance = balance;
|
|
||||||
topUpAmount += amount;
|
|
||||||
|
|
||||||
topUpHistory.push({
|
|
||||||
amount: amount,
|
|
||||||
date: new Date().toLocaleString('id-ID'),
|
|
||||||
balanceAfter: balance
|
|
||||||
});
|
|
||||||
|
|
||||||
updateUI();
|
|
||||||
updateTopUpHistory();
|
|
||||||
|
|
||||||
setMessage(`Top up berhasil! +${amount.toLocaleString('id-ID')}`, 'win');
|
|
||||||
|
|
||||||
hideTopUpModal();
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateTopUpHistory() {
|
|
||||||
if (!topUpHistoryEl) return;
|
|
||||||
|
|
||||||
topUpHistoryEl.innerHTML = '';
|
|
||||||
|
|
||||||
if (topUpHistory.length === 0) {
|
|
||||||
topUpHistoryEl.innerHTML = '<p class="no-history">Belum ada riwayat top up</p>';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const recentHistory = topUpHistory.slice(-5).reverse();
|
|
||||||
|
|
||||||
recentHistory.forEach(record => {
|
|
||||||
const historyItem = document.createElement('div');
|
|
||||||
historyItem.className = 'history-item';
|
|
||||||
historyItem.innerHTML = `
|
|
||||||
<div class="history-amount">+${record.amount.toLocaleString('id-ID')}</div>
|
|
||||||
<div class="history-date">${record.date}</div>
|
|
||||||
<div class="history-balance">Saldo: ${record.balanceAfter.toLocaleString('id-ID')}</div>
|
|
||||||
`;
|
|
||||||
topUpHistoryEl.appendChild(historyItem);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateTopUpBalance() {
|
|
||||||
if (topUpBalanceEl) {
|
|
||||||
topUpBalanceEl.textContent = `Total Top Up: ${topUpAmount.toLocaleString('id-ID')}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// EVENT LISTENERS
|
|
||||||
betBtn.addEventListener('click', function (e) {
|
|
||||||
startRound();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function closeTopUpModal() {
|
||||||
|
topupModal.style.display = 'none';
|
||||||
|
topupMessage.style.display = 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('click', e => {
|
||||||
|
if (e.target === topupModal) closeTopUpModal();
|
||||||
|
});
|
||||||
|
|
||||||
|
function setModalAmount(amount) {
|
||||||
|
modalAmount.value = amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
function showTopUpMessage(msg, type) {
|
||||||
|
topupMessage.textContent = msg;
|
||||||
|
topupMessage.className = 'topup-message ' + type;
|
||||||
|
topupMessage.style.display = 'block';
|
||||||
|
}
|
||||||
|
|
||||||
|
topupForm.addEventListener('submit', e => {
|
||||||
|
e.preventDefault();
|
||||||
|
|
||||||
|
const bankMethod = document.querySelector('input[name="bank_method"]:checked');
|
||||||
|
const amount = parseInt(modalAmount.value);
|
||||||
|
|
||||||
|
if (!bankMethod) return showTopUpMessage("Pilih metode pembayaran!", "error");
|
||||||
|
if (amount <= 0 || amount > 1000000) return showTopUpMessage("Jumlah harus 1–1.000.000", "error");
|
||||||
|
|
||||||
|
const formData = new FormData(topupForm);
|
||||||
|
formData.append('userId', userId);
|
||||||
|
|
||||||
|
fetch('process_topup.php', { method: 'POST', body: formData })
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(data => {
|
||||||
|
if (data.success) {
|
||||||
|
gameBalance = data.new_balance;
|
||||||
|
updateBalanceDisplay();
|
||||||
|
showTopUpMessage("✓ Top up berhasil!", "success");
|
||||||
|
setTimeout(closeTopUpModal, 2000);
|
||||||
|
} else {
|
||||||
|
showTopUpMessage(data.message, "error");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => showTopUpMessage("Terjadi kesalahan.", "error"));
|
||||||
|
});
|
||||||
|
|
||||||
|
// =========================
|
||||||
|
// EVENT LISTENERS
|
||||||
|
// =========================
|
||||||
|
betBtn.addEventListener('click', startRound);
|
||||||
hitBtn.addEventListener('click', playerHit);
|
hitBtn.addEventListener('click', playerHit);
|
||||||
standBtn.addEventListener('click', playerStand);
|
standBtn.addEventListener('click', playerStand);
|
||||||
doubleBtn.addEventListener('click', playerDouble);
|
doubleBtn.addEventListener('click', playerDouble);
|
||||||
|
|
||||||
newRoundBtn.addEventListener('click', function () {
|
newRoundBtn.addEventListener('click', () => {
|
||||||
if (inRound && !confirm('Masih dalam ronde. Reset?')) return;
|
if (inRound && !confirm("Masih dalam ronde. Reset?")) return;
|
||||||
balance = 1000;
|
|
||||||
gameBalance = 1000;
|
|
||||||
currentBet = 0;
|
|
||||||
inRound = false;
|
|
||||||
dealer = [];
|
dealer = [];
|
||||||
player = [];
|
player = [];
|
||||||
deck = [];
|
deck = [];
|
||||||
|
inRound = false;
|
||||||
dealerHidden = true;
|
dealerHidden = true;
|
||||||
setMessage('Bank di-reset.', '');
|
currentBet = 0;
|
||||||
|
|
||||||
|
setMessage("Game di-reset.", "");
|
||||||
updateUI();
|
updateUI();
|
||||||
updateBalanceOnServer();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// TOP UP EVENT LISTENERS - HANDLED IN HTML.PHP
|
|
||||||
/*
|
|
||||||
if (topUpBtn) {
|
|
||||||
topUpBtn.addEventListener('click', showTopUpModal);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (topUpClose) {
|
|
||||||
topUpClose.addEventListener('click', hideTopUpModal);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (topUpConfirm) {
|
|
||||||
topUpConfirm.addEventListener('click', processTopUp);
|
|
||||||
}
|
|
||||||
|
|
||||||
window.addEventListener('click', (e) => {
|
|
||||||
if (e.target === topUpModal) {
|
|
||||||
hideTopUpModal();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
|
|
||||||
// KEYBOARD
|
|
||||||
window.addEventListener('keydown', e => {
|
window.addEventListener('keydown', e => {
|
||||||
if (e.key === 'h') playerHit();
|
if (e.key === 'h') playerHit();
|
||||||
if (e.key === 's') playerStand();
|
if (e.key === 's') playerStand();
|
||||||
if (e.key === 'd') playerDouble();
|
if (e.key === 'd') playerDouble();
|
||||||
if (e.key === 'Enter') startRound();
|
if (e.key === 'Enter') startRound();
|
||||||
/*
|
|
||||||
if (e.ctrlKey && e.key === 't') {
|
|
||||||
e.preventDefault();
|
|
||||||
showTopUpModal();
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// INITIALIZATION
|
// =========================
|
||||||
updateUI();
|
// INIT
|
||||||
// updateTopUpHistory();
|
// =========================
|
||||||
// updateTopUpBalance();
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
|
updateBalanceDisplay();
|
||||||
|
});
|
||||||
|
|||||||
54
prosestopup.php
Normal file
54
prosestopup.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
include "koneksi.php";
|
||||||
|
|
||||||
|
if (!isset($_SESSION['user_id'])) {
|
||||||
|
echo json_encode(['success' => false, 'message' => 'Not logged in']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
||||||
|
$user_id = $_SESSION['user_id'];
|
||||||
|
$amount = intval($_POST['amount']);
|
||||||
|
$bank_method = $_POST['bank_method'];
|
||||||
|
|
||||||
|
// Validasi
|
||||||
|
if ($amount <= 0 || $amount > 1000000) {
|
||||||
|
echo json_encode(['success' => false, 'message' => 'Invalid amount']);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update balance di database
|
||||||
|
$sql = "UPDATE users SET balance = balance + ? WHERE id = ?";
|
||||||
|
$stmt = mysqli_prepare($conn, $sql);
|
||||||
|
mysqli_stmt_bind_param($stmt, "ii", $amount, $user_id);
|
||||||
|
|
||||||
|
if (mysqli_stmt_execute($stmt)) {
|
||||||
|
// Get new balance
|
||||||
|
$sql2 = "SELECT balance FROM users WHERE id = ?";
|
||||||
|
$stmt2 = mysqli_prepare($conn, $sql2);
|
||||||
|
mysqli_stmt_bind_param($stmt2, "i", $user_id);
|
||||||
|
mysqli_stmt_execute($stmt2);
|
||||||
|
$result = mysqli_stmt_get_result($stmt2);
|
||||||
|
$user = mysqli_fetch_assoc($result);
|
||||||
|
|
||||||
|
// Update session
|
||||||
|
$_SESSION['balance'] = $user['balance'];
|
||||||
|
|
||||||
|
// Log transaction
|
||||||
|
$log_sql = "INSERT INTO transactions (user_id, type, amount, description)
|
||||||
|
VALUES (?, 'topup', ?, 'Top up via $bank_method')";
|
||||||
|
$log_stmt = mysqli_prepare($conn, $log_sql);
|
||||||
|
mysqli_stmt_bind_param($log_stmt, "ii", $user_id, $amount);
|
||||||
|
mysqli_stmt_execute($log_stmt);
|
||||||
|
|
||||||
|
echo json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'new_balance' => $user['balance'],
|
||||||
|
'message' => 'Top up successful'
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
echo json_encode(['success' => false, 'message' => 'Database error']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
27
upbalance.php
Normal file
27
upbalance.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
include "koneksi.php";
|
||||||
|
|
||||||
|
if (!isset($_SESSION['user_id'])) {
|
||||||
|
echo json_encode(['success' => false]);
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
$user_id = $_SESSION['user_id'];
|
||||||
|
$balance = intval($data['balance']);
|
||||||
|
|
||||||
|
// Update balance di database
|
||||||
|
$sql = "UPDATE users SET balance = ? WHERE id = ?";
|
||||||
|
$stmt = mysqli_prepare($conn, $sql);
|
||||||
|
mysqli_stmt_bind_param($stmt, "ii", $balance, $user_id);
|
||||||
|
|
||||||
|
if (mysqli_stmt_execute($stmt)) {
|
||||||
|
// Update session
|
||||||
|
$_SESSION['balance'] = $balance;
|
||||||
|
|
||||||
|
echo json_encode(['success' => true]);
|
||||||
|
} else {
|
||||||
|
echo json_encode(['success' => false]);
|
||||||
|
}
|
||||||
|
?>
|
||||||
20
users.sql
20
users.sql
@ -68,3 +68,23 @@ COMMIT;
|
|||||||
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
|
||||||
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
|
||||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||||
|
|
||||||
|
-- Tabel users
|
||||||
|
CREATE TABLE users (
|
||||||
|
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
username VARCHAR(50) UNIQUE NOT NULL,
|
||||||
|
password VARCHAR(255) NOT NULL,
|
||||||
|
balance DECIMAL(10,2) DEFAULT 0.00,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Tabel transaction history (opsional)
|
||||||
|
CREATE TABLE transactions (
|
||||||
|
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
user_id INT,
|
||||||
|
type ENUM('topup', 'win', 'loss', 'bet'),
|
||||||
|
amount DECIMAL(10,2),
|
||||||
|
description VARCHAR(255),
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id)
|
||||||
|
);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user