forked from rDrama/rDrama
1
0
Fork 0

Merge branch 'frost' of https://github.com/Aevann1/rDrama into frost

master
Aevann1 2022-09-05 00:07:22 +02:00
commit 1470b2c8cc
1 changed files with 240 additions and 241 deletions

View File

@ -1,220 +1,218 @@
// Shared // Shared
function updatePlayerCoins(updated) { function updatePlayerCoins(updated) {
if (updated.coins) { if (updated.coins) {
document.getElementById('user-coins-amount').innerText = updated.coins; document.getElementById("user-coins-amount").innerText = updated.coins;
} }
if (updated.procoins) { if (updated.procoins) {
document.getElementById('user-bux-amount').innerText = updated.procoins; document.getElementById("user-bux-amount").innerText = updated.procoins;
} }
} }
// Slots // Slots
function pullSlots() { function pullSlots() {
const wager = document.getElementById("casinoSlotsBet").value; const wager = document.getElementById("casinoSlotsBet").value;
const currency = document.querySelector( const currency = document.querySelector(
'input[name="casinoSlotsCurrency"]:checked' 'input[name="casinoSlotsCurrency"]:checked'
).value; ).value;
document.getElementById("casinoSlotsBet").disabled = true; document.getElementById("casinoSlotsBet").disabled = true;
document.getElementById("casinoSlotsPull").disabled = true; document.getElementById("casinoSlotsPull").disabled = true;
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open("post", "/casino/slots"); xhr.open("post", "/casino/slots");
xhr.onload = handleSlotsResponse.bind(null, xhr); xhr.onload = handleSlotsResponse.bind(null, xhr);
const form = new FormData(); const form = new FormData();
form.append("formkey", formkey()); form.append("formkey", formkey());
form.append("wager", wager); form.append("wager", wager);
form.append("currency", currency); form.append("currency", currency);
xhr.send(form); xhr.send(form);
} }
function handleSlotsResponse(xhr) { function handleSlotsResponse(xhr) {
let response; let response;
try { try {
response = JSON.parse(xhr.response); response = JSON.parse(xhr.response);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
const succeeded = const succeeded =
xhr.status >= 200 && xhr.status < 300 && response && !response.error; xhr.status >= 200 && xhr.status < 300 && response && !response.error;
const slotsResult = document.getElementById("casinoSlotsResult"); const slotsResult = document.getElementById("casinoSlotsResult");
slotsResult.classList.remove("text-success", "text-danger"); slotsResult.classList.remove("text-success", "text-danger");
if (succeeded) { if (succeeded) {
const { game_state, gambler } = response; const { game_state, gambler } = response;
const state = JSON.parse(game_state); const state = JSON.parse(game_state);
const reels = Array.from(document.querySelectorAll(".reel")); const reels = Array.from(document.querySelectorAll(".reel"));
const symbols = state.symbols.split(","); const symbols = state.symbols.split(",");
for (let i = 0; i < 3; i++) { for (let i = 0; i < 3; i++) {
reels[i].innerHTML = symbols[i]; reels[i].innerHTML = symbols[i];
} }
slotsResult.style.visibility = "visible"; slotsResult.style.visibility = "visible";
slotsResult.innerText = state.text; slotsResult.innerText = state.text;
if (state.text.includes("Won")) { if (state.text.includes("Won")) {
if (state.text.includes("Jackpot")) { if (state.text.includes("Jackpot")) {
slotsResult.classList.add("text-warning"); slotsResult.classList.add("text-warning");
} else { } else {
slotsResult.classList.add("text-success"); slotsResult.classList.add("text-success");
} }
} else if (state.text.includes("Lost")) { } else if (state.text.includes("Lost")) {
slotsResult.classList.add("text-danger"); slotsResult.classList.add("text-danger");
} }
updatePlayerCoins(gambler); updatePlayerCoins(gambler);
} else { } else {
slotsResult.style.visibility = "visible"; slotsResult.style.visibility = "visible";
slotsResult.innerText = response.error; slotsResult.innerText = response.error;
slotsResult.classList.add("text-danger"); slotsResult.classList.add("text-danger");
console.error(response.error); console.error(response.error);
} }
document.getElementById("casinoSlotsBet").disabled = false; document.getElementById("casinoSlotsBet").disabled = false;
document.getElementById("casinoSlotsPull").disabled = false; document.getElementById("casinoSlotsPull").disabled = false;
} }
// Blackjack // Blackjack
// When the casino loads, look up the "blackjack status" of a player to either start a new game or continue an existing game. // When the casino loads, look up the "blackjack status" of a player to either start a new game or continue an existing game.
if ( if (
document.readyState === "complete" || document.readyState === "complete" ||
(document.readyState !== "loading" && !document.documentElement.doScroll) (document.readyState !== "loading" && !document.documentElement.doScroll)
) { ) {
checkBlackjackStatus(); checkBlackjackStatus();
} else { } else {
document.addEventListener("DOMContentLoaded", checkBlackjackStatus); document.addEventListener("DOMContentLoaded", checkBlackjackStatus);
} }
function checkBlackjackStatus() { function checkBlackjackStatus() {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open("get", "/casino/blackjack"); xhr.open("get", "/casino/blackjack");
xhr.onload = handleBlackjackStatusResponse.bind(null, xhr); xhr.onload = handleBlackjackStatusResponse.bind(null, xhr);
xhr.send(); xhr.send();
} }
function handleBlackjackStatusResponse(xhr) { function handleBlackjackStatusResponse(xhr) {
let response; let response;
try { try {
response = JSON.parse(xhr.response); response = JSON.parse(xhr.response);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
const succeeded = const succeeded =
xhr.status >= 200 && xhr.status < 300 && response && !response.error; xhr.status >= 200 && xhr.status < 300 && response && !response.error;
if (succeeded) { if (succeeded) {
if (response.active) { if (response.active) {
updateBlackjack(response.game_state); updateBlackjack(response.game_state);
} }
} else { } else {
console.error("error"); console.error("error");
} }
} }
// When starting a new game or taking an action in an existing one, a new state will be returned, and the DOM must be updated. // When starting a new game or taking an action in an existing one, a new state will be returned, and the DOM must be updated.
function updateBlackjack(state) { function updateBlackjack(state) {
const { player, dealer, status } = state; const { player, dealer, status } = state;
const lettersToSuits = { const lettersToSuits = {
S: "♠️", S: "♠️",
H: "♥️", H: "♥️",
C: "♣️", C: "♣️",
D: "♦️", D: "♦️",
"?": "?", "?": "?",
}; };
const suitsToColors = { const suitsToColors = {
"♠️": "black", "♠️": "black",
"♥️": "red", "♥️": "red",
"♣️": "black", "♣️": "black",
"♦️": "red", "♦️": "red",
"?": "black", "?": "black",
}; };
// Clear everything. // Clear everything.
Array.from(document.querySelectorAll(".playing-card")).forEach((card) => { Array.from(document.querySelectorAll(".playing-card")).forEach((card) => {
card.innerText = ""; card.innerText = "";
card.style.color = "unset"; card.style.color = "unset";
card.classList.remove("dealt"); card.classList.remove("dealt");
}); });
// Show dealer cards. // Show dealer cards.
const dealerSlots = Array.from( const dealerSlots = Array.from(
document.querySelectorAll('.playing-card[data-who="dealer"]') document.querySelectorAll('.playing-card[data-who="dealer"]')
); );
for (let i = 0; i < dealer.length; i++) { for (let i = 0; i < dealer.length; i++) {
const slot = dealerSlots[i]; const slot = dealerSlots[i];
if (slot) { if (slot) {
// Technically, the dealer can use more than 5 cards, though it's rare. // Technically, the dealer can use more than 5 cards, though it's rare.
// In that case, the result message is good enough. // In that case, the result message is good enough.
// Thanks, Carp. 🐠 // Thanks, Carp. 🐠
slot.classList.add("dealt"); slot.classList.add("dealt");
if (i > 0 && status === "active") { if (i > 0 && status === "active") {
break; break;
} }
const rank = dealer[i][0]; const rank = dealer[i][0];
const suit = lettersToSuits[dealer[i][1]]; const suit = lettersToSuits[dealer[i][1]];
const card = rank + suit; const card = rank + suit;
slot.innerText = card; slot.innerText = card;
slot.style.color = suitsToColors[suit]; slot.style.color = suitsToColors[suit];
} }
} }
// Show player cards. // Show player cards.
const playerSlots = Array.from( const playerSlots = Array.from(
document.querySelectorAll('.playing-card[data-who="player"]') document.querySelectorAll('.playing-card[data-who="player"]')
); );
for (let i = 0; i < player.length; i++) { for (let i = 0; i < player.length; i++) {
const slot = playerSlots[i]; const slot = playerSlots[i];
const rank = player[i][0]; const rank = player[i][0];
const suit = lettersToSuits[player[i][1]]; const suit = lettersToSuits[player[i][1]];
const card = rank + suit; const card = rank + suit;
slot.innerText = card; slot.innerText = card;
slot.style.color = suitsToColors[suit]; slot.style.color = suitsToColors[suit];
slot.classList.add("dealt"); slot.classList.add("dealt");
} }
updateBlackjackActions(state); updateBlackjackActions(state);
if (status !== "active") { if (status !== "active") {
revealBlackjackResult(state); revealBlackjackResult(state);
} }
} }
function revealBlackjackResult(state) { function revealBlackjackResult(state) {
const blackjackResult = document.getElementById("casinoBlackjackResult"); const blackjackResult = document.getElementById("casinoBlackjackResult");
const lookup = { const lookup = {
bust: ["Bust. Didn't work out for you, did it?", "danger"], bust: ["Bust. Didn't work out for you, did it?", "danger"],
push: ["Pushed. This whole hand never happened.", "secondary"], push: ["Pushed. This whole hand never happened.", "secondary"],
insured_loss: ["Lost, but at least you had insurance.", "secondary"], insured_loss: ["Lost, but at least you had insurance.", "secondary"],
lost: ["Lost. That was pathetic.", "danger"], lost: ["Lost. That was pathetic.", "danger"],
won: ["Won. This time.", "success"], won: ["Won. This time.", "success"],
blackjack: ["Blackjack! Must be your lucky day.", "warning"], blackjack: ["Blackjack! Must be your lucky day.", "warning"],
}; };
const [resultText, resultClass] = lookup[state.status]; const [resultText, resultClass] = lookup[state.status];
blackjackResult.style.visibility = "visible"; blackjackResult.style.visibility = "visible";
blackjackResult.innerText = resultText; blackjackResult.innerText = resultText;
blackjackResult.classList.add(`text-${resultClass}`); blackjackResult.classList.add(`text-${resultClass}`);
} }
function buildBlackjackAction(id, method, title, fullWidth = false) { function buildBlackjackAction(id, method, title, fullWidth = false) {
return ` return `
<button <button
type="button" type="button"
class="btn btn-${ class="btn btn-${fullWidth ? "primary" : "secondary"} lottery-page--action"
fullWidth ? "primary" : "secondary"
} lottery-page--action"
id="${id}" id="${id}"
onclick="${method}()" onclick="${method}()"
style="${fullWidth ? "width: 100%;" : ""}" style="${fullWidth ? "width: 100%;" : ""}"
@ -225,87 +223,88 @@ function buildBlackjackAction(id, method, title, fullWidth = false) {
} }
function clearBlackjackActions() { function clearBlackjackActions() {
const actionWrapper = document.getElementById("casinoBlackjackActions"); const actionWrapper = document.getElementById("casinoBlackjackActions");
actionWrapper.innerHTML = ""; actionWrapper.innerHTML = "";
} }
function updateBlackjackActions(state) { function updateBlackjackActions(state) {
const actionWrapper = document.getElementById("casinoBlackjackActions"); const actionWrapper = document.getElementById("casinoBlackjackActions");
clearBlackjackActions(); clearBlackjackActions();
if (state.status === "active") { if (state.status === "active") {
document.getElementById("casinoBlackjackWager").style.display = "none"; document.getElementById("casinoBlackjackWager").style.display = "none";
const actionLookup = { const actionLookup = {
hit: buildBlackjackAction("casinoBlackjackHit", "hitBlackjack", "Hit"), hit: buildBlackjackAction("casinoBlackjackHit", "hitBlackjack", "Hit"),
stay: buildBlackjackAction( stay: buildBlackjackAction(
"casinoBlackjackStay", "casinoBlackjackStay",
"stayBlackjack", "stayBlackjack",
"Stay" "Stay"
), ),
double_down: buildBlackjackAction( double_down: buildBlackjackAction(
"casinoBlackjackDouble", "casinoBlackjackDouble",
"doubleBlackjack", "doubleBlackjack",
"Double Down" "Double Down"
), ),
insure: buildBlackjackAction( insure: buildBlackjackAction(
"casinoBlackjackInsure", "casinoBlackjackInsure",
"insureBlackjack", "insureBlackjack",
"Insure" "Insure"
), ),
}; };
const actions = state.actions.map((action) => actionLookup[action]); const actions = state.actions.map((action) => actionLookup[action]);
actionWrapper.innerHTML = actions.join("\n"); actionWrapper.innerHTML = actions.join("\n");
} else { } else {
// Game is over, deal a new game. // Game is over, deal a new game.
document.getElementById("casinoBlackjackWager").style.display = "flex"; document.getElementById("casinoBlackjackWager").style.display = "flex";
document.getElementById("casinoBlackjackBet").disabled = false;
const deal = buildBlackjackAction( const deal = buildBlackjackAction(
"casinoBlackjackDeal", "casinoBlackjackDeal",
"dealBlackjack", "dealBlackjack",
"Deal", "Deal",
true true
); );
actionWrapper.innerHTML = deal; actionWrapper.innerHTML = deal;
} }
} }
function dealBlackjack() { function dealBlackjack() {
const wager = document.getElementById("casinoBlackjackBet").value; const wager = document.getElementById("casinoBlackjackBet").value;
const currency = document.querySelector( const currency = document.querySelector(
'input[name="casinoBlackjackCurrency"]:checked' 'input[name="casinoBlackjackCurrency"]:checked'
).value; ).value;
document.getElementById("casinoBlackjackBet").disabled = true; document.getElementById("casinoBlackjackBet").disabled = true;
document.getElementById("casinoBlackjackDeal").disabled = true; document.getElementById("casinoBlackjackDeal").disabled = true;
document.getElementById("casinoBlackjackWager").style.display = "none"; document.getElementById("casinoBlackjackWager").style.display = "none";
document.getElementById("casinoBlackjackResult").style.visibility = "hidden"; document.getElementById("casinoBlackjackResult").style.visibility = "hidden";
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open("post", "/casino/blackjack"); xhr.open("post", "/casino/blackjack");
xhr.onload = handleBlackjackResponse.bind(null, xhr); xhr.onload = handleBlackjackResponse.bind(null, xhr);
const form = new FormData(); const form = new FormData();
form.append("formkey", formkey()); form.append("formkey", formkey());
form.append("wager", wager); form.append("wager", wager);
form.append("currency", currency); form.append("currency", currency);
xhr.send(form); xhr.send(form);
} }
function takeBlackjackAction(action) { function takeBlackjackAction(action) {
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open("post", "/casino/blackjack/action"); xhr.open("post", "/casino/blackjack/action");
xhr.onload = handleBlackjackResponse.bind(null, xhr); xhr.onload = handleBlackjackResponse.bind(null, xhr);
const form = new FormData(); const form = new FormData();
form.append("formkey", formkey()); form.append("formkey", formkey());
form.append("action", action); form.append("action", action);
xhr.send(form); xhr.send(form);
} }
const hitBlackjack = takeBlackjackAction.bind(null, "hit"); const hitBlackjack = takeBlackjackAction.bind(null, "hit");
@ -314,32 +313,32 @@ const doubleBlackjack = takeBlackjackAction.bind(null, "double_down");
const insureBlackjack = takeBlackjackAction.bind(null, "insure"); const insureBlackjack = takeBlackjackAction.bind(null, "insure");
function handleBlackjackResponse(xhr) { function handleBlackjackResponse(xhr) {
let response; let response;
try { try {
response = JSON.parse(xhr.response); response = JSON.parse(xhr.response);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
const succeeded = const succeeded =
xhr.status >= 200 && xhr.status < 300 && response && !response.error; xhr.status >= 200 && xhr.status < 300 && response && !response.error;
const blackjackResult = document.getElementById("casinoBlackjackResult"); const blackjackResult = document.getElementById("casinoBlackjackResult");
blackjackResult.classList.remove("text-success", "text-danger"); blackjackResult.classList.remove("text-success", "text-danger");
if (succeeded) { if (succeeded) {
if (response.game_state) { if (response.game_state) {
updateBlackjack(response.game_state); updateBlackjack(response.game_state);
} }
if (response.gambler) { if (response.gambler) {
updatePlayerCoins(response.gambler); updatePlayerCoins(response.gambler);
} }
} else { } else {
blackjackResult.style.visibility = "visible"; blackjackResult.style.visibility = "visible";
blackjackResult.innerText = response.error; blackjackResult.innerText = response.error;
blackjackResult.classList.add("text-danger"); blackjackResult.classList.add("text-danger");
console.error(response.error); console.error(response.error);
} }
} }