source: pages/api/blackjack/gameStates.js@ 189cd8f

main
Last change on this file since 189cd8f was b13f93b, checked in by anastasovv <simon@…>, 2 years ago

Made poker tables system and round 1

  • Property mode set to 100644
File size: 2.2 KB
Line 
1const 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 */
7const deck = singleDeck.concat(singleDeck).concat(singleDeck).concat(singleDeck).concat(singleDeck);
8
9/**
10 * Sample game state (Begining of the game)
11 */
12export let game = {
13 deck: [...deck],
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
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 */
30function checkDeckSize(game) {
31 if (game.deck.length === 0) {
32 game.deck = [...deck];
33 }
34}
35
36/**
37 * Draw a SINGLE random card
38 */
39export 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 */
52export 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 */
63export 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}
Note: See TracBrowser for help on using the repository browser.