source: pages/api/blackjack/gameStates.js@ 55701f0

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

Added 1 second update_state calls in blackjack

  • Property mode set to 100644
File size: 2.9 KB
Line 
1import { rooms } from "../postgre";
2
3const 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 */
9const deck = singleDeck.concat(singleDeck).concat(singleDeck).concat(singleDeck).concat(singleDeck);
10
11/**
12 * Sample game state (Begining of the game)
13 */
14export let game = {
15 credits: -1,
16 deck: [...deck],
17 status: '_1_room_created', // statuses: _1_room_created, _2_made_initial_bet, _3_made_side_bet, _4_cards_on_the_table, _5_game_over
18 playerCards: [],
19 dealerName: 'Lazar',
20 dealerCards: [],
21 initialBet: 0,
22 sideBet: 0,
23 sideBetName: '',
24 outcome: '',
25 earnings: 0,
26 sideBetOutcome: '',
27 sideBetEarnings: 0,
28 messageTitle: '',
29 messageDescription: '',
30 betOutcomeMessageShown: true,
31 sideBetOutcomeMessageShown: true,
32}
33
34export function getGame(session_id) {
35 if (rooms[session_id] !== undefined) {
36 return {
37 success: true,
38 game: rooms[session_id],
39 }
40 }
41
42 return {
43 success: false,
44 game: {...game, playerCards: [...game.playerCards], dealerCards: [...game.dealerCards]},
45 };
46}
47
48export function getRestrictedGameObject(session_id) {
49 const { success, game } = getGame(session_id)
50
51 return {
52 ...game,
53 dealerCards: game.status.includes('_5_') ? game.dealerCards : game.dealerCards.length > 0 ? [game.dealerCards[0]].concat(['back']) : [],
54 }
55}
56
57/**
58 * Replace deck if empty
59 */
60function checkDeckSize(game) {
61 if (game.deck.length === 0) {
62 game.deck = [...deck];
63 }
64}
65
66/**
67 * Draw a SINGLE random card
68 */
69export function drawASingleCard(room) {
70 checkDeckSize(room);
71 let idx = Math.floor(Math.random() * room.deck.length);
72 let card = room.deck[idx];
73
74 room.deck.splice(idx, 1);
75
76 return card;
77}
78
79/**
80 * Deal the initial hand of cards
81 */
82export function getInitialCards(room) {
83 room.playerCards.push(drawASingleCard(room));
84 room.playerCards.push(drawASingleCard(room));
85
86 room.dealerCards.push(drawASingleCard(room));
87 room.dealerCards.push(drawASingleCard(room));
88}
89
90/**
91 * Calculate the hand value
92 */
93export function calculateHandValue(cards) {
94 let value = 0;
95 let aces = 0;
96 for (let i = 0; i < cards.length; i++) {
97 let card = cards[i];
98 if (card.substring(1) === 'A') {
99 value += 11;
100 aces++;
101 } else if (card.substring(1) === 'X' || card.substring(1) === 'J' || card.substring(1) === 'Q' || card.substring(1) === 'K') {
102 value += 10;
103 } else {
104 value += parseInt(card.substring(1));
105 }
106 }
107 while (value > 21 && aces > 0) {
108 value -= 10;
109 aces--;
110 }
111 return value;
112}
Note: See TracBrowser for help on using the repository browser.