Changeset ebf5e04 for pages


Ignore:
Timestamp:
06/12/22 18:31:03 (3 years ago)
Author:
anastasovv <simon@…>
Branches:
main
Children:
285c3cc
Parents:
fe03f69
Message:

Code cleanings

Location:
pages
Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • pages/api/blackjack/index.js

    rfe03f69 rebf5e04  
    1 import { v4 as uuidv4 } from 'uuid';
    2 
    31import axios from 'axios';
    42
    53require('dotenv').config();
    64
    7 /**
    8  * ********************* BEGIN OF DEALING WITH GAME STATES *********************
    9  */
    10 
    11 const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK",
    12                     "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
    13                     "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
    14                     "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK"    ];
    15 
    16 /* We are using 5 decks */
    17 const deck = singleDeck.concat(singleDeck).concat(singleDeck).concat(singleDeck).concat(singleDeck);
    18 
    19 let game = {
    20   deck: [...deck],
    21   status: '_1_room_created',
    22   playerCards: [],
    23   dealerName: 'Lazar',
    24   dealerCards: [],
    25   initialBet: 0,
    26   sideBet: 0,
    27   sideBetName: '',
    28   outcome: '',
    29   earnings: 0,              // positive for draw, 2x for win, negative for loss.
    30   sideBetOutcome: '',
    31   sideBetEarnings: 0,
    32 }
     5import { game, drawASingleCard, getInitialCards, calculateHandValue } from './gameStates';
     6import { calculateEarnings, calculateSideBetEarnings } from './calculateEarnings';
    337
    348let rooms = []
    35 
    36 /**
    37  * Replace deck if empty
    38  */
    39 function checkDeckSize(game) {
    40   if (game.deck.length === 0) {
    41     game.deck = [...deck];
    42   }
    43 }
    44 
    45 /**
    46  * Draw a SINGLE random card
    47  */
    48 function drawASingleCard(room) {
    49   checkDeckSize(room);
    50   let idx = Math.floor(Math.random() * room.deck.length);
    51   let card = room.deck[idx];
    52 
    53   room.deck.splice(idx, 1);
    54 
    55   return card;
    56 }
    57 
    58 /**
    59  * Deal the initial hand of cards
    60  */
    61 function getInitialCards(room) {
    62   room.playerCards.push(drawASingleCard(room));
    63   room.playerCards.push(drawASingleCard(room));
    64 
    65   room.dealerCards.push(drawASingleCard(room));
    66   room.dealerCards.push(drawASingleCard(room));
    67 }
    68 
    69 function calculateEarnings(room) {
    70   let betEarnings = 0;
    71 
    72   if (room.outcome === 'draw') {
    73     betEarnings = room.initialBet;
    74   }
    75   else if (room.outcome === 'player_won' || room.outcome === 'dealer_busted') {
    76     betEarnings = 2 * room.initialBet;
    77   }
    78   else if (room.outcome === 'player_lost' || room.outcome === 'player_busted') {
    79     betEarnings = -1 * room.initialBet;
    80   }
    81 
    82   return betEarnings;
    83 }
    84 
    85 function calculateSideBetEarnings(room) {
    86   let sideBetEarnings = -1 * room.sideBet;
    87 
    88   if (room.sideBetName != '') {
    89     if (room.sideBetName === 'mixed_pair') {
    90       if (checkIfSameValue(room.playerCards)) {
    91         sideBetEarnings = room.sideBet * 5;
    92       }
    93     }
    94     else if (room.sideBetName === 'coloured_pair') {
    95       if (checkIfSameValue(room.playerCards) && checkIfSameColour(room.playerCards)) {
    96         sideBetEarnings = room.sideBet * 12;
    97       }
    98     }
    99     else if (room.sideBetName === 'perfect_pair') {
    100       if (checkIfSameValue(room.playerCards) && checkIfSameSuit(room.playerCards)) {
    101         sideBetEarnings = room.sideBet * 25;
    102       }
    103     }
    104     else if (room.sideBetName === 'flush') {
    105       const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
    106       if (checkIfSameSuit(tmpCards)) {
    107         sideBetEarnings = room.sideBet * 5;
    108       }
    109     }
    110     else if (room.sideBetName === 'straight') {
    111       const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
    112       if (checkIfStraight(tmpCards)) {
    113         sideBetEarnings = room.sideBet * 10;
    114       }
    115     }
    116     else if (room.sideBetName === 'three_of_a_kind') {
    117       const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
    118       if (checkIfSameValue(tmpCards)) {
    119         sideBetEarnings = room.sideBet * 30;
    120       }
    121     }
    122     else if (room.sideBetName === 'straight_flush') {
    123       const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
    124       if (checkIfStraight(tmpCards) && checkIfSameSuit(tmpCards)) {
    125         sideBetEarnings = room.sideBet * 40;
    126       }
    127     }
    128     else if (room.sideBetName === 'suited_triple') {
    129       const tmpCards = room.playerCards.slice().concat(room.dealerCards[0]);
    130       if (checkIfSameSuit(tmpCards) && checkIfSameValue(tmpCards)) {
    131         sideBetEarnings = room.sideBet * 100;
    132       }
    133     }
    134   }
    135 
    136   return sideBetEarnings;
    137 }
    1389
    13910/**
     
    14718  rooms[session_id] = room;
    14819}
    149 /**
    150  * ********************* END OF DEALING WITH GAME STATES *********************
    151  */
    15220
    15321/**
     
    16129    /**
    16230     * /---------------------- GET ----------------------/
     31     * If game status is _5_game_over, restart the room for a new game.
    16332     * @action play_again
    16433     * @param session_id
     
    18554    /**
    18655     * /---------------------- GET ----------------------/
     56     * If game status is _4_cards_on_the_table, draw cards for the dealer while handValue < 17, and calculate game outcome and player earnings.
     57     * Also, update the player's credits and stats in the database through /api/postgre?action=add_credits.
    18758     * @action stand
    18859     * @param session_id
     
    246117    /**
    247118     * /---------------------- GET ----------------------/
     119     * If game status is _4_cards_on_the_table, draw a card for the player.
     120     * If player busts, update the player's stats in the database through /api/postgre?action=add_credits.
    248121     * @action hit_a_card
    249122     * @param session_id
     
    305178     /**
    306179     * /---------------------- GET ----------------------/
     180     * If game status is _3_made_side_bet, check if the player won the side bet or not (if they placed a side bet of course).
     181     * Update the player's stats in the database through /api/postgre?action=add_credits.
    307182     * @action get_initial_cards
    308183     * @param session_id
     
    366241    /**
    367242     * /---------------------- GET ----------------------/
     243     * If game status is _2_made_initial_bet, place a side bet if the user has chosen one.
    368244     * @action make_side_bet
    369245     * @param session_id
     
    402278    /**
    403279     * /---------------------- GET ----------------------/
     280     * If game status is _1_room_created, get the initial bet placed by the player.
    404281     * @action make_initial_bet
    405282     * @param session_id
     
    434311    /**
    435312     * /---------------------- GET ----------------------/
     313     * Remove a room from the rooms array.
    436314     * @action remove_room
    437315     * @param session_id
     
    451329    /**
    452330     * /---------------------- GET ----------------------/
     331     * If the player is not in an existing room, create a room for them.
     332     * If they are reconnecting, get the room they were in.
    453333     * @action get_player_info_on_enter
    454334     * @param session_id
     
    465345
    466346      let dealerCardsTmp = [];
    467       if (rooms[session_id].status.substr(1, 1) != '1') { // 5 == game_over
     347      if (rooms[session_id].status.substr(1, 1) != '5') { // 5 == game_over
    468348        rooms[session_id].dealerCards.forEach((card, i) => {
    469349          if (i === 0) {
     
    474354          }
    475355        })
     356      }
     357      else {
     358        dealerCardsTmp = rooms[session_id].dealerCards;
    476359      }
    477360
     
    493376 * ********************* END OF REQUEST HANDLER *********************
    494377 */
    495 
    496 /**
    497  * ********************* BEGIN OF FUNCTIONS THAT CHECK CARD COMBINATIONS *********************
    498  */
    499 
    500 function calculateHandValue(cards) {
    501   let value = 0;
    502   let aces = 0;
    503   for (let i = 0; i < cards.length; i++) {
    504     let card = cards[i];
    505     if (card.substring(1) === 'A') {
    506       value += 11;
    507       aces++;
    508     } else if (card.substring(1) === 'X' || card.substring(1) === 'J' || card.substring(1) === 'Q' || card.substring(1) === 'K') {
    509       value += 10;
    510     } else {
    511       value += parseInt(card.substring(1));
    512     }
    513   }
    514   while (value > 21 && aces > 0) {
    515     value -= 10;
    516     aces--;
    517   }
    518   return value;
    519 }
    520 
    521 function checkIfSameValue(cards) {
    522   for (let i = 1; i < cards.length; i++) {
    523     if (cards[i][1] !== cards[i-1][1]) {
    524       return false;
    525     }
    526   }
    527 
    528   return true;
    529 }
    530 
    531 function checkIf2CardsAreSameColour(card1, card2) {
    532   if (card1[0] === card2[0]) return true;
    533   if (card1[0] === 'H' && card2[0] === 'D') return true;
    534   if (card1[0] === 'D' && card2[0] === 'H') return true;
    535   if (card1[0] === 'S' && card2[0] === 'C') return true;
    536   if (card1[0] === 'C' && card2[0] === 'S') return true;
    537   return false;
    538 }
    539 
    540 function checkIfSameColour(cards) {
    541   for (let i = 1; i < cards.length; i++) {
    542     if (!checkIf2CardsAreSameColour(cards[i], cards[i-1])) {
    543       return false;
    544     }
    545   }
    546 
    547   return true;
    548 }
    549 
    550 function checkIfSameSuit(cards) {
    551   for (let i = 1; i < cards.length; i++) {
    552     if (cards[i][0] !== cards[i-1][0]) {
    553       return false;
    554     }
    555   }
    556 
    557   return true;
    558 }
    559 
    560 function checkIfStraight(cards) {
    561   let values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K'];
    562 
    563   let valuesInCards = [];
    564   for (let i = 0; i < cards.length; i++) {
    565     valuesInCards.push(cards[i][1]);
    566   }
    567 
    568   let temp = values.reduce((c, v, i) => Object.assign(c, {[v]: i}), {});
    569 
    570   valuesInCards = valuesInCards.sort((a, b) => temp[a] - temp[b]);
    571 
    572   let idx = values.indexOf(valuesInCards[0]);
    573 
    574   let straight = true;
    575 
    576   for (let i = 0; i < valuesInCards.length; i++) {
    577     if (valuesInCards[i] !== values[idx]) {
    578       straight = false;
    579       break;
    580     }
    581 
    582     idx++;
    583     if (idx >= temp.length) {
    584       straight = false;
    585       break;
    586     }
    587   }
    588 
    589   if (straight) {
    590     return true;
    591   }
    592 
    593   values = ['2', '3', '4', '5', '6', '7', '8', '9', 'X', 'J', 'Q', 'K', 'A'];
    594   temp = values.reduce((c, v, i) => Object.assign(c, {[v]: i}), {});
    595 
    596   valuesInCards = valuesInCards.sort((a, b) => temp[a] - temp[b]);
    597 
    598   idx = values.indexOf(valuesInCards[0]);
    599 
    600   for (let i = 0; i < valuesInCards.length; i++) {
    601     if (valuesInCards[i] !== values[idx]) return false;
    602 
    603     idx++;
    604     if (idx >= temp.length) return false;
    605   }
    606 
    607   return true;
    608 }
    609 /**
    610  * ********************* END OF FUNCTIONS THAT CHECK CARD COMBINATIONS *********************
    611  */
  • pages/api/postgre/index.js

    rfe03f69 rebf5e04  
    1313
    1414const sessions = []
    15 // example session
    16 // const session = {
    17 //   id,
    18 //   displayName,
    19 //   username,
    20 //   credits,
    21 //   lastActivity,
    22 // }
    23 
     15// example session = { id, displayName, username, credits, lastActivity }
    2416
    2517export default function handler(req, res) {
     
    3022    /**
    3123     * /---------------------- GET ----------------------/
     24     * If the player won credits, update them in the database.
     25     * Also, update the stats in the database.
    3226     * @action give_credits
    3327     * @param session_id
     
    9589    /**
    9690     * /---------------------- GET ----------------------/
     91     * The player lost credits, update this in the database.
    9792     * @action take_credits
    9893     * @param session_id
     
    138133    /**
    139134     * /---------------------- GET ----------------------/
     135     * Get stats for the player, so we can display them in the front end.
    140136     * @action get_stats
    141137     * @param session_id
     
    174170    /**
    175171     * /---------------------- GET ----------------------/
     172     * Returns the player's room, if the player was in one.
     173     * Same as the one below, but this one is used in a game-specific context.
    176174     * @action get_player_info_on_enter
    177175     * @param session_id
     
    198196    /**
    199197     * /---------------------- GET ----------------------/
     198     * Returns the player's room, if the player was in one.
     199     * Same as the one above, but this one is used in a general context.
    200200     * @action check_if_logged_in
    201201     * @param session_id
     
    222222    /**
    223223     * /---------------------- GET ----------------------/
     224     * Takes the credits in the player's session, and updates the database.
     225     * Logs the player out and kills the session.
    224226     * @action logout
    225227     * @param session_id
     
    254256    /**
    255257     * /---------------------- POST ----------------------/
     258     * Checks if the entered account info is good, and registers a new user in the database if so.
    256259     * @action register
    257260     * @param username
     
    339342    /**
    340343     * /---------------------- POST ----------------------/
     344     * Checks if the entered account info is good, and logs the user in if so.
    341345     * @action login
    342346     * @param username
     
    433437    }
    434438  }
    435 
    436   /**
    437    * PUT method
    438    */
    439   if (req.method === 'PUT') {
    440 
    441   }
    442439}
  • pages/games/blackjack.js

    rfe03f69 rebf5e04  
    22
    33import Blackjack from '../../components/blackjack/Blackjack'
    4 
    5 import { useEffect } from 'react'
    6 
    7 import axios from 'axios'
    84
    95const blackjack = () => {
  • pages/index.js

    rfe03f69 rebf5e04  
    2121
    2222        <meta name="author" content="ESS" />
    23         <meta name="copyright" content="ESS CORP" />
     23        <meta name="copyright" content="ESS CORP 2022 &copy;" />
    2424
    2525        <title>Caessino</title>
Note: See TracChangeset for help on using the changeset viewer.