[ebf5e04] | 1 | const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK",
|
---|
| 2 | "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
|
---|
| 3 | "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
|
---|
| 4 | "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK" ];
|
---|
| 5 |
|
---|
| 6 | /* We are using 5 decks */
|
---|
| 7 | const deck = singleDeck.concat(singleDeck).concat(singleDeck).concat(singleDeck).concat(singleDeck);
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | * Sample game state (Begining of the game)
|
---|
| 11 | */
|
---|
| 12 | export let game = {
|
---|
| 13 | deck: [...deck],
|
---|
[9bd09b0] | 14 | 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] | 15 | playerCards: [],
|
---|
| 16 | dealerName: 'Lazar',
|
---|
| 17 | dealerCards: [],
|
---|
| 18 | initialBet: 0,
|
---|
| 19 | sideBet: 0,
|
---|
| 20 | sideBetName: '',
|
---|
| 21 | outcome: '',
|
---|
| 22 | earnings: 0,
|
---|
| 23 | sideBetOutcome: '',
|
---|
| 24 | sideBetEarnings: 0,
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Replace deck if empty
|
---|
| 29 | */
|
---|
| 30 | function checkDeckSize(game) {
|
---|
[b13f93b] | 31 | if (game.deck.length === 0) {
|
---|
| 32 | game.deck = [...deck];
|
---|
[ebf5e04] | 33 | }
|
---|
[b13f93b] | 34 | }
|
---|
[ebf5e04] | 35 |
|
---|
| 36 | /**
|
---|
| 37 | * Draw a SINGLE random card
|
---|
| 38 | */
|
---|
| 39 | export function drawASingleCard(room) {
|
---|
| 40 | checkDeckSize(room);
|
---|
| 41 | let idx = Math.floor(Math.random() * room.deck.length);
|
---|
| 42 | let card = room.deck[idx];
|
---|
| 43 |
|
---|
| 44 | room.deck.splice(idx, 1);
|
---|
| 45 |
|
---|
| 46 | return card;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * Deal the initial hand of cards
|
---|
| 51 | */
|
---|
| 52 | export function getInitialCards(room) {
|
---|
| 53 | room.playerCards.push(drawASingleCard(room));
|
---|
| 54 | room.playerCards.push(drawASingleCard(room));
|
---|
| 55 |
|
---|
| 56 | room.dealerCards.push(drawASingleCard(room));
|
---|
| 57 | room.dealerCards.push(drawASingleCard(room));
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Calculate the hand value
|
---|
| 62 | */
|
---|
| 63 | export function calculateHandValue(cards) {
|
---|
| 64 | let value = 0;
|
---|
| 65 | let aces = 0;
|
---|
| 66 | for (let i = 0; i < cards.length; i++) {
|
---|
| 67 | let card = cards[i];
|
---|
| 68 | if (card.substring(1) === 'A') {
|
---|
| 69 | value += 11;
|
---|
| 70 | aces++;
|
---|
| 71 | } else if (card.substring(1) === 'X' || card.substring(1) === 'J' || card.substring(1) === 'Q' || card.substring(1) === 'K') {
|
---|
| 72 | value += 10;
|
---|
| 73 | } else {
|
---|
| 74 | value += parseInt(card.substring(1));
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | while (value > 21 && aces > 0) {
|
---|
| 78 | value -= 10;
|
---|
| 79 | aces--;
|
---|
| 80 | }
|
---|
| 81 | return value;
|
---|
| 82 | } |
---|