[55701f0] | 1 | import { rooms } from "../postgre";
|
---|
| 2 |
|
---|
[ebf5e04] | 3 | const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK",
|
---|
| 4 | "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
|
---|
| 5 | "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
|
---|
| 6 | "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK" ];
|
---|
| 7 |
|
---|
| 8 | /* We are using 5 decks */
|
---|
| 9 | const deck = singleDeck.concat(singleDeck).concat(singleDeck).concat(singleDeck).concat(singleDeck);
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * Sample game state (Begining of the game)
|
---|
| 13 | */
|
---|
| 14 | export let game = {
|
---|
[55701f0] | 15 | credits: -1,
|
---|
[e007fcd] | 16 | username: '',
|
---|
| 17 | displayName: '',
|
---|
[ebf5e04] | 18 | deck: [...deck],
|
---|
[9bd09b0] | 19 | status: '_1_room_created', // statuses: _1_room_created, _2_made_initial_bet, _3_made_side_bet, _4_cards_on_the_table, _5_game_over
|
---|
[ebf5e04] | 20 | playerCards: [],
|
---|
| 21 | dealerName: 'Lazar',
|
---|
| 22 | dealerCards: [],
|
---|
| 23 | initialBet: 0,
|
---|
| 24 | sideBet: 0,
|
---|
| 25 | sideBetName: '',
|
---|
| 26 | outcome: '',
|
---|
| 27 | earnings: 0,
|
---|
| 28 | sideBetOutcome: '',
|
---|
| 29 | sideBetEarnings: 0,
|
---|
[55701f0] | 30 | messageTitle: '',
|
---|
| 31 | messageDescription: '',
|
---|
| 32 | betOutcomeMessageShown: true,
|
---|
| 33 | sideBetOutcomeMessageShown: true,
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | export function getGame(session_id) {
|
---|
| 37 | if (rooms[session_id] !== undefined) {
|
---|
| 38 | return {
|
---|
| 39 | success: true,
|
---|
| 40 | game: rooms[session_id],
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | return {
|
---|
| 45 | success: false,
|
---|
| 46 | game: {...game, playerCards: [...game.playerCards], dealerCards: [...game.dealerCards]},
|
---|
| 47 | };
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | export function getRestrictedGameObject(session_id) {
|
---|
| 51 | const { success, game } = getGame(session_id)
|
---|
| 52 |
|
---|
| 53 | return {
|
---|
| 54 | ...game,
|
---|
| 55 | dealerCards: game.status.includes('_5_') ? game.dealerCards : game.dealerCards.length > 0 ? [game.dealerCards[0]].concat(['back']) : [],
|
---|
| 56 | }
|
---|
[ebf5e04] | 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * Replace deck if empty
|
---|
| 61 | */
|
---|
| 62 | function checkDeckSize(game) {
|
---|
[b13f93b] | 63 | if (game.deck.length === 0) {
|
---|
| 64 | game.deck = [...deck];
|
---|
[ebf5e04] | 65 | }
|
---|
[b13f93b] | 66 | }
|
---|
[ebf5e04] | 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Draw a SINGLE random card
|
---|
| 70 | */
|
---|
| 71 | export function drawASingleCard(room) {
|
---|
| 72 | checkDeckSize(room);
|
---|
| 73 | let idx = Math.floor(Math.random() * room.deck.length);
|
---|
| 74 | let card = room.deck[idx];
|
---|
| 75 |
|
---|
| 76 | room.deck.splice(idx, 1);
|
---|
| 77 |
|
---|
| 78 | return card;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * Deal the initial hand of cards
|
---|
| 83 | */
|
---|
| 84 | export function getInitialCards(room) {
|
---|
| 85 | room.playerCards.push(drawASingleCard(room));
|
---|
| 86 | room.playerCards.push(drawASingleCard(room));
|
---|
| 87 |
|
---|
| 88 | room.dealerCards.push(drawASingleCard(room));
|
---|
| 89 | room.dealerCards.push(drawASingleCard(room));
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /**
|
---|
| 93 | * Calculate the hand value
|
---|
| 94 | */
|
---|
| 95 | export function calculateHandValue(cards) {
|
---|
| 96 | let value = 0;
|
---|
| 97 | let aces = 0;
|
---|
| 98 | for (let i = 0; i < cards.length; i++) {
|
---|
| 99 | let card = cards[i];
|
---|
| 100 | if (card.substring(1) === 'A') {
|
---|
| 101 | value += 11;
|
---|
| 102 | aces++;
|
---|
| 103 | } else if (card.substring(1) === 'X' || card.substring(1) === 'J' || card.substring(1) === 'Q' || card.substring(1) === 'K') {
|
---|
| 104 | value += 10;
|
---|
| 105 | } else {
|
---|
| 106 | value += parseInt(card.substring(1));
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | while (value > 21 && aces > 0) {
|
---|
| 110 | value -= 10;
|
---|
| 111 | aces--;
|
---|
| 112 | }
|
---|
| 113 | return value;
|
---|
| 114 | } |
---|