2025-11-26 18:33:09 -06:00

285 lines
6.6 KiB
JavaScript

/**
* ATM Interface
* Handles banking transactions with PIN authentication
*/
// Mock data
const mockData = {
cash: 2500,
bank: 45750,
pin: '1234' // For demo purposes
};
// State
let enteredPin = '';
let currentView = 'welcomeView';
// View Management
function showView(viewId) {
// Hide all views
document.querySelectorAll('.atm-view').forEach(view => {
view.style.display = 'none';
});
// Show selected view
const view = document.getElementById(viewId);
if (view) {
view.style.display = 'flex';
currentView = viewId;
// Update balance displays when showing certain views
if (viewId === 'menuView' || viewId === 'balanceView' || viewId === 'depositView') {
updateBalances();
}
}
}
// PIN Entry
function enterPin(digit) {
if (enteredPin.length < 4) {
enteredPin += digit;
updatePinDisplay();
}
}
function clearPin() {
enteredPin = '';
updatePinDisplay();
}
function updatePinDisplay() {
const dots = document.querySelectorAll('.pin-dot');
dots.forEach((dot, index) => {
if (index < enteredPin.length) {
dot.classList.add('filled');
} else {
dot.classList.remove('filled');
}
});
}
function submitPin() {
if (enteredPin.length !== 4) {
showError('Please enter a 4-digit PIN');
return;
}
// In a real implementation, this would validate with the server
if (enteredPin === mockData.pin) {
enteredPin = '';
updatePinDisplay();
showView('menuView');
} else {
showError('Incorrect PIN');
clearPin();
}
}
// Balance Updates
function updateBalances() {
// Update all balance displays
const cashElements = ['cashBalance', 'cashBalanceDetail', 'availableCash'];
const bankElements = ['bankBalance', 'bankBalanceDetail'];
cashElements.forEach(id => {
const el = document.getElementById(id);
if (el) el.textContent = `$${mockData.cash.toLocaleString()}`;
});
bankElements.forEach(id => {
const el = document.getElementById(id);
if (el) el.textContent = `$${mockData.bank.toLocaleString()}`;
});
const totalEl = document.getElementById('totalBalance');
if (totalEl) {
const total = mockData.cash + mockData.bank;
totalEl.textContent = `$${total.toLocaleString()}`;
}
}
// Withdraw Functions
function withdrawAmount(amount) {
if (amount > mockData.bank) {
showError('Insufficient funds');
return;
}
mockData.bank -= amount;
mockData.cash += amount;
// sendEvent('atm::withdraw', { amount: amount });
showSuccess(`Withdrew $${amount.toLocaleString()}`);
}
function withdrawCustom() {
const input = document.getElementById('withdrawInput');
const amount = parseFloat(input.value);
if (!amount || amount <= 0) {
showError('Please enter a valid amount');
return;
}
if (amount > mockData.bank) {
showError('Insufficient funds');
return;
}
mockData.bank -= amount;
mockData.cash += amount;
// sendEvent('atm::withdraw', { amount: amount });
input.value = '';
showSuccess(`Withdrew $${amount.toLocaleString()}`);
}
// Deposit Functions
function depositAmount() {
const input = document.getElementById('depositInput');
const amount = parseFloat(input.value);
if (!amount || amount <= 0) {
showError('Please enter a valid amount');
return;
}
if (amount > mockData.cash) {
showError('Insufficient cash');
return;
}
mockData.cash -= amount;
mockData.bank += amount;
// sendEvent('atm::deposit', { amount: amount });
input.value = '';
showSuccess(`Deposited $${amount.toLocaleString()}`);
}
function depositAll() {
if (mockData.cash <= 0) {
showError('No cash to deposit');
return;
}
const amount = mockData.cash;
mockData.cash = 0;
mockData.bank += amount;
// sendEvent('atm::deposit', { amount: amount });
showSuccess(`Deposited $${amount.toLocaleString()}`);
}
// Transfer Function
function transferFunds() {
const playerIdInput = document.getElementById('transferPlayerId');
const amountInput = document.getElementById('transferAmount');
const playerId = playerIdInput.value.trim();
const amount = parseFloat(amountInput.value);
if (!playerId) {
showError('Please enter a player ID');
return;
}
if (!amount || amount <= 0) {
showError('Please enter a valid amount');
return;
}
if (amount > mockData.bank) {
showError('Insufficient funds');
return;
}
mockData.bank -= amount;
// sendEvent('atm::transfer', {
// playerId: playerId,
// amount: amount
// });
playerIdInput.value = '';
amountInput.value = '';
showSuccess(`Transferred $${amount.toLocaleString()} to Player ${playerId}`);
}
// Result Screens
function showSuccess(message) {
document.getElementById('successMessage').textContent = message;
showView('successView');
updateBalances();
}
function showError(message) {
document.getElementById('errorMessage').textContent = message;
showView('errorView');
}
// Exit ATM
function exitATM() {
enteredPin = '';
updatePinDisplay();
sendEvent('atm::exit', {});
showView('welcomeView');
}
// Send event to Arma
function sendEvent(event, data) {
if (typeof A3API !== 'undefined') {
A3API.SendAlert(JSON.stringify({
event: event,
data: data
}));
} else {
console.log('Event:', event, 'Data:', data);
}
}
// Update ATM data from external source
function updateATMData(data) {
if (data.cash !== undefined) {
mockData.cash = data.cash;
}
if (data.bank !== undefined) {
mockData.bank = data.bank;
}
updateBalances();
}
// Initialize
function initATM() {
console.log('ATM interface initializing...');
// Show welcome screen
showView('welcomeView');
// Update initial balances
updateBalances();
console.log('ATM interface initialized');
}
// Auto-initialize
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initATM);
} else {
initATM();
}
// Expose functions globally
window.showView = showView;
window.enterPin = enterPin;
window.clearPin = clearPin;
window.submitPin = submitPin;
window.withdrawAmount = withdrawAmount;
window.withdrawCustom = withdrawCustom;
window.depositAmount = depositAmount;
window.depositAll = depositAll;
window.transferFunds = transferFunds;
window.exitATM = exitATM;
window.updateATMData = updateATMData;