- Timestamp:
- 06/12/22 18:31:03 (3 years ago)
- Branches:
- main
- Children:
- 285c3cc
- Parents:
- fe03f69
- Location:
- pages/api
- Files:
-
- 3 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
pages/api/blackjack/index.js
rfe03f69 rebf5e04 1 import { v4 as uuidv4 } from 'uuid';2 3 1 import axios from 'axios'; 4 2 5 3 require('dotenv').config(); 6 4 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 } 5 import { game, drawASingleCard, getInitialCards, calculateHandValue } from './gameStates'; 6 import { calculateEarnings, calculateSideBetEarnings } from './calculateEarnings'; 33 7 34 8 let rooms = [] 35 36 /**37 * Replace deck if empty38 */39 function checkDeckSize(game) {40 if (game.deck.length === 0) {41 game.deck = [...deck];42 }43 }44 45 /**46 * Draw a SINGLE random card47 */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 cards60 */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 }138 9 139 10 /** … … 147 18 rooms[session_id] = room; 148 19 } 149 /**150 * ********************* END OF DEALING WITH GAME STATES *********************151 */152 20 153 21 /** … … 161 29 /** 162 30 * /---------------------- GET ----------------------/ 31 * If game status is _5_game_over, restart the room for a new game. 163 32 * @action play_again 164 33 * @param session_id … … 185 54 /** 186 55 * /---------------------- 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. 187 58 * @action stand 188 59 * @param session_id … … 246 117 /** 247 118 * /---------------------- 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. 248 121 * @action hit_a_card 249 122 * @param session_id … … 305 178 /** 306 179 * /---------------------- 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. 307 182 * @action get_initial_cards 308 183 * @param session_id … … 366 241 /** 367 242 * /---------------------- GET ----------------------/ 243 * If game status is _2_made_initial_bet, place a side bet if the user has chosen one. 368 244 * @action make_side_bet 369 245 * @param session_id … … 402 278 /** 403 279 * /---------------------- GET ----------------------/ 280 * If game status is _1_room_created, get the initial bet placed by the player. 404 281 * @action make_initial_bet 405 282 * @param session_id … … 434 311 /** 435 312 * /---------------------- GET ----------------------/ 313 * Remove a room from the rooms array. 436 314 * @action remove_room 437 315 * @param session_id … … 451 329 /** 452 330 * /---------------------- 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. 453 333 * @action get_player_info_on_enter 454 334 * @param session_id … … 465 345 466 346 let dealerCardsTmp = []; 467 if (rooms[session_id].status.substr(1, 1) != ' 1') { // 5 == game_over347 if (rooms[session_id].status.substr(1, 1) != '5') { // 5 == game_over 468 348 rooms[session_id].dealerCards.forEach((card, i) => { 469 349 if (i === 0) { … … 474 354 } 475 355 }) 356 } 357 else { 358 dealerCardsTmp = rooms[session_id].dealerCards; 476 359 } 477 360 … … 493 376 * ********************* END OF REQUEST HANDLER ********************* 494 377 */ 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 13 13 14 14 const 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 } 24 16 25 17 export default function handler(req, res) { … … 30 22 /** 31 23 * /---------------------- GET ----------------------/ 24 * If the player won credits, update them in the database. 25 * Also, update the stats in the database. 32 26 * @action give_credits 33 27 * @param session_id … … 95 89 /** 96 90 * /---------------------- GET ----------------------/ 91 * The player lost credits, update this in the database. 97 92 * @action take_credits 98 93 * @param session_id … … 138 133 /** 139 134 * /---------------------- GET ----------------------/ 135 * Get stats for the player, so we can display them in the front end. 140 136 * @action get_stats 141 137 * @param session_id … … 174 170 /** 175 171 * /---------------------- 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. 176 174 * @action get_player_info_on_enter 177 175 * @param session_id … … 198 196 /** 199 197 * /---------------------- 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. 200 200 * @action check_if_logged_in 201 201 * @param session_id … … 222 222 /** 223 223 * /---------------------- GET ----------------------/ 224 * Takes the credits in the player's session, and updates the database. 225 * Logs the player out and kills the session. 224 226 * @action logout 225 227 * @param session_id … … 254 256 /** 255 257 * /---------------------- POST ----------------------/ 258 * Checks if the entered account info is good, and registers a new user in the database if so. 256 259 * @action register 257 260 * @param username … … 339 342 /** 340 343 * /---------------------- POST ----------------------/ 344 * Checks if the entered account info is good, and logs the user in if so. 341 345 * @action login 342 346 * @param username … … 433 437 } 434 438 } 435 436 /**437 * PUT method438 */439 if (req.method === 'PUT') {440 441 }442 439 }
Note:
See TracChangeset
for help on using the changeset viewer.