Skip to content

Commit

Permalink
Use consistent double quotes for strings in JavaScript.
Browse files Browse the repository at this point in the history
Refactored the code to replace single quotes with double quotes for string consistency throughout the file. Minor updates include optional comma corrections and improved readability by adding proper spacing and addressing small logical clarifications.
  • Loading branch information
waketraindev committed Dec 12, 2024
1 parent 1a7d81f commit 484a907
Showing 1 changed file with 66 additions and 59 deletions.
125 changes: 66 additions & 59 deletions src/main/resources/static/js/main.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
/* jshint esversion: 6 */
(function () {
"use strict";
const appWindow = document.getElementById('appWindow');
const btnSpin = document.getElementById('btnSpin');
const btnIncBet = document.getElementById('btnIncrementBet');
const btnDecBet = document.getElementById('btnDecrementBet');
const btnDeposit = document.getElementById('btnDeposit');
const btnWithdraw = document.getElementById('btnWithdraw');
const lblBalanceAmount = document.getElementById('lblBalanceAmount');
const lblBetAmount = document.getElementById('lblBetAmount');
const lblDisplay = document.getElementById('lblDisplay');
const lblRollResult = document.getElementById('lblRollResultText');
const lblRollAmount = document.getElementById('lblRollResultAmount');
const lblVersion = document.getElementById('lblVersion');
const blkDisplay = document.getElementById('blkDisplay');
const appWindow = document.getElementById("appWindow");
const btnSpin = document.getElementById("btnSpin");
const btnIncBet = document.getElementById("btnIncrementBet");
const btnDecBet = document.getElementById("btnDecrementBet");
const btnDeposit = document.getElementById("btnDeposit");
const btnWithdraw = document.getElementById("btnWithdraw");
const lblBalanceAmount = document.getElementById("lblBalanceAmount");
const lblBetAmount = document.getElementById("lblBetAmount");
const lblDisplay = document.getElementById("lblDisplay");
const lblRollResult = document.getElementById("lblRollResultText");
const lblRollAmount = document.getElementById("lblRollResultAmount");
const lblVersion = document.getElementById("lblVersion");
const blkDisplay = document.getElementById("blkDisplay");
const lblBetStats = document.getElementById("lblBetStats");
const lblWinStats = document.getElementById("lblWinStats");
const lblRtpStats = document.getElementById("lblRtpStats");

const tb = document.getElementById('payoutTable');
const tb = document.getElementById("payoutTable");
const body = tb.getElementsByTagName("tbody")[0];
const rows = body.getElementsByTagName("tr");

const betRange = [1, 10, 15, 25, 50, 100, 150, 200, 250, 500, 1000, 1500, 2000, 2500, 5000, 10000];
const numFormat = new Intl.NumberFormat('en-US', {});
const betRange = [1, 10, 15, 25, 50, 100, 150, 200, 250, 500, 1000, 1500, 2000, 2500, 5000, 10000,];
const numFormat = new Intl.NumberFormat("en-US", {});

let lastSpin = {winAmount: 0};
let lastSpin = {winAmount: 0,};
let machineState = {
balance: 1, betAmount: 1
balance: 1, betAmount: 1,
};
let betPos = 0;

Expand All @@ -45,10 +45,10 @@
lblDisplay.innerText = prettyNumber(data.result);
lblVersion.innerText = data.version;
btnSpin.disabled = machineState.betAmount > machineState.balance;
setStatusLabel('Balance', prettyNumber(machineState.balance));
setStatusLabel("Balance", prettyNumber(machineState.balance));
refreshStats();

}, '/api/load').then(() => appWindow.classList.remove('d-none'));
}, "/api/load").then(() => appWindow.classList.remove("d-none"));
}

function sendCall(callback, path, options) {
Expand All @@ -58,15 +58,15 @@
}
return rsp;
}).then((response) => response.json())
.then(data => callback(data)).catch(ignored => {
.then((data) => callback(data)).catch((ignored) => {
window.alert(`Error running API call`);
console.log(ignored);
//console.log(ignored);
});
}

function setStatusLabel(label, text, classes) {
if (classes === undefined) {
classes = 'text-bg-info';
classes = "text-bg-info";
}
lblRollResult.className = `badge ${classes}`;
lblRollResult.innerText = label;
Expand All @@ -75,21 +75,24 @@

function refreshStats() {
sendCall((data) => {
let newText = `Bets: ${prettyNumber(data["betStats"]["count"])} `;
newText += `Max: ${prettyNumber(data["betStats"]["max"])} `;
newText += `Sum: ${prettyNumber(data["betStats"]["sum"])} `;
if (data === undefined) {
data = {betStats: {count: 0, max: 0, sum: 0,}, winStats: {count: 0, max: 0, sum: 0,}, rtp: 0,};
}
let newText = `Bets: ${prettyNumber(data.betStats.count)} `;
newText += `Max: ${prettyNumber(data.betStats.max)} `;
newText += `Sum: ${prettyNumber(data.betStats.sum)} `;
lblBetStats.innerText = newText;

newText = `Wins: ${prettyNumber(data["winStats"]["count"])} `;
newText += `Max: ${prettyNumber(data["winStats"]["max"])} `;
newText += `Sum: ${prettyNumber(data["winStats"]["sum"])} `;
newText = `Wins: ${prettyNumber(data.winStats.count)} `;
newText += `Max: ${prettyNumber(data.winStats.max)} `;
newText += `Sum: ${prettyNumber(data.winStats.sum)} `;
lblWinStats.innerText = newText;
lblRtpStats.innerText = `RTP: ${(data["rtp"] * 100.0).toFixed(2)}%`;
lblRtpStats.innerText = `RTP: ${(data.rtp * 100.0).toFixed(2)}%`;
}, "/api/machine-stats", {}).then();
}

function setButtonsState(state) {
[btnSpin, btnIncBet, btnDecBet, btnDeposit, btnWithdraw].forEach((i) => i.disabled = state);
[btnSpin, btnIncBet, btnDecBet, btnDeposit, btnWithdraw,].forEach((i) => i.disabled = state);
}

function isWin() {
Expand All @@ -107,19 +110,20 @@
if (rows.length > 10) {
tabBody.querySelector("tr:last-child").remove();
}
const newRow = document.createElement('tr');
newRow.innerHTML =
`<td>${prettyNumber(state.betAmount)}</td><td>${prettyNumber(state.winAmount)}</td><td>${state.result}</td>`;
newRow.innerHTML +=
`<td><span class="badge ${isWin() ? 'text-bg-success' : 'text-bg-danger'}">${isWin() ? 'Win' :
'Loss'}</span></td>`;
const newRow = document.createElement("tr");
newRow.innerHTML = `<td>${prettyNumber(state.betAmount)}</td>
<td>${prettyNumber(state.winAmount)}</td>
<td>${state.result}</td>`;
newRow.innerHTML += `<td><span class="badge
${isWin() ? "text-bg-success" : "text-bg-danger"}">
${isWin() ? "Win" : "Loss"}</span></td>`;
tabBody.prepend(newRow);

if (isWin() > 0) {
setStatusLabel('WIN', prettyNumber(state.winAmount), 'text-bg-success');
setStatusLabel("WIN", prettyNumber(state.winAmount), "text-bg-success");
blkDisplay.className = "animate-spin-win";
} else {
setStatusLabel('LOSS', prettyNumber(state.betAmount), 'text-bg-danger');
setStatusLabel("LOSS", prettyNumber(state.betAmount), "text-bg-danger");
blkDisplay.className = "animate-spin-loss";
}
setButtonsState(false);
Expand All @@ -131,81 +135,84 @@
setButtonsState(true);
blkDisplay.className = "animate-spin";
const betAmount = machineState.betAmount;
setStatusLabel('Spin', prettyNumber(machineState.betAmount), 'text-bg-warning');
setStatusLabel("Spin", prettyNumber(machineState.betAmount), "text-bg-warning");

let count = 0;
const animateDisplay = setInterval(() => {
if (count++ < 7) {
lblDisplay.innerText = Math.floor(Math.random() * 10).toFixed(0);
} else {
sendCall(data => {
sendCall((data) => {
clearInterval(animateDisplay);
updateMachineState(data);
}, `/api/spin/${betAmount}`, {
method: 'POST'
method: "POST",
}).then();
}
}, 47);
}

function calcBetValues() {
for (let i = 0; i < rows.length; i++) {
let i;
for (i = 0; i < rows.length; i++) {
const cells = rows[i].getElementsByTagName("td");
const value = cells[1];
value.innerText = prettyNumber(((i >= 10) ? 100 : i) * machineState.betAmount);
}
}

function bindListeners() {
btnDeposit.addEventListener('click', () => {
btnDeposit.addEventListener("click", () => {
const value = window.prompt("Enter deposit amount", "1000");
sendCall(data => {
sendCall((data) => {
lblBalanceAmount.innerText = data.balance;
machineState.balance = data.balance;
btnSpin.disabled = machineState.betAmount > machineState.balance;
}, '/api/deposit/' + value, {
method: 'POST'
}, "/api/deposit/" + value, {
method: "POST",
}).then();

});
btnWithdraw.addEventListener('click', () => {
btnWithdraw.addEventListener("click", () => {
const value = window.prompt("Enter withdrawal amount", "1000");
sendCall(data => {
sendCall((data) => {
lblBalanceAmount.innerText = data.balance;
machineState.balance = data.balance;
btnSpin.disabled = machineState.betAmount > machineState.balance;
}, '/api/withdraw/' + value, {
method: 'POST'
}, "/api/withdraw/" + value, {
method: "POST",
}).then();
});
btnSpin.addEventListener('click', () => {
btnSpin.addEventListener("click", () => {
spin();
});
btnIncBet.addEventListener('click', () => {
btnIncBet.addEventListener("click", () => {
betPos = Math.min((betPos + 1), betRange.length - 1);
machineState.betAmount = betRange[betPos];
lblBetAmount.innerText = prettyNumber(machineState.betAmount);
calcBetValues();
btnSpin.disabled = machineState.betAmount > machineState.balance;
});

btnDecBet.addEventListener('click', () => {
btnDecBet.addEventListener("click", () => {
betPos = Math.max((betPos - 1), 0);
machineState.betAmount = betRange[betPos];
lblBetAmount.innerText = prettyNumber(machineState.betAmount);
calcBetValues();
btnSpin.disabled = !(machineState.balance > machineState.betAmount);
const isEnabled = (machineState.balance > machineState.betAmount);
/* This needs negation for the correct behaviour */
btnSpin.disabled = !isEnabled;
});

document.addEventListener('keydown', (e) => {
document.addEventListener("keydown", (e) => {
switch (e.key) {
case 's':
case "s":
btnSpin.click();
break;
case 'a':
case "a":
btnIncBet.click();
break;
case 'd':
case "d":
btnDecBet.click();
break;
}
Expand Down

0 comments on commit 484a907

Please sign in to comment.