[87614a5] | 1 | import axios from 'axios';
|
---|
| 2 |
|
---|
| 3 | require('dotenv').config();
|
---|
| 4 |
|
---|
[55701f0] | 5 | import { game, drawASingleCard, getInitialCards, calculateHandValue, getGame, getRestrictedGameObject } from './gameStates';
|
---|
[ebf5e04] | 6 | import { calculateEarnings, calculateSideBetEarnings } from './calculateEarnings';
|
---|
[87614a5] | 7 |
|
---|
[faff334] | 8 | import { rooms, saveGameInHistory, update_rooms_to_database } from '../postgre/index'
|
---|
[87614a5] | 9 |
|
---|
| 10 | /**
|
---|
| 11 | * Set up a room
|
---|
| 12 | */
|
---|
[e007fcd] | 13 | function createARoom(session_id, displayName, username) {
|
---|
[87614a5] | 14 | let room = {
|
---|
[e007fcd] | 15 | ...game, displayName: displayName, username: username, playerCards: [...game.playerCards], dealerCards: [...game.dealerCards],
|
---|
[87614a5] | 16 | }
|
---|
| 17 |
|
---|
| 18 | rooms[session_id] = room;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
| 22 | * ********************* BEGIN OF REQUEST HANDLER *********************
|
---|
| 23 | */
|
---|
| 24 | export default async function handler(req, res) {
|
---|
| 25 | /**
|
---|
| 26 | * GET method
|
---|
| 27 | */
|
---|
| 28 | if (req.method === 'GET') {
|
---|
| 29 | /**
|
---|
| 30 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 31 | * If game status is _5_game_over, restart the room for a new game.
|
---|
[87614a5] | 32 | * @action play_again
|
---|
| 33 | * @param session_id
|
---|
| 34 | */
|
---|
| 35 | if (req.query.action === 'play_again' && req.query?.session_id) {
|
---|
| 36 | const session_id = req.query.session_id;
|
---|
| 37 |
|
---|
| 38 | if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '5') {
|
---|
[faff334] | 39 | rooms[session_id] = {...game, displayName: rooms[session_id].displayName, username: rooms[session_id].username, playerCards: [...game.playerCards], dealerCards: [...game.dealerCards]};
|
---|
[87614a5] | 40 |
|
---|
[55701f0] | 41 | rooms[session_id].betOutcomeMessageShown = true;
|
---|
[87614a5] | 42 |
|
---|
[433e0c5] | 43 | update_rooms_to_database();
|
---|
[55701f0] | 44 | }
|
---|
[433e0c5] | 45 |
|
---|
[55701f0] | 46 | res.end();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * /---------------------- GET ----------------------/
|
---|
| 51 | * If game status is _4_cards_on_the_table, turn off the alert for side bet outcome
|
---|
| 52 | * @action continue_from_side_bet
|
---|
| 53 | * @param session_id
|
---|
| 54 | */
|
---|
| 55 | if (req.query.action === 'continue_from_side_bet' && req.query?.session_id) {
|
---|
| 56 | const session_id = req.query.session_id;
|
---|
| 57 |
|
---|
| 58 | if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '4') {
|
---|
| 59 | rooms[session_id].sideBetOutcomeMessageShown = true;
|
---|
| 60 |
|
---|
| 61 | update_rooms_to_database();
|
---|
[87614a5] | 62 | }
|
---|
| 63 |
|
---|
[55701f0] | 64 | res.end();
|
---|
[87614a5] | 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 69 | * If game status is _4_cards_on_the_table, draw cards for the dealer while handValue < 17, and calculate game outcome and player earnings.
|
---|
| 70 | * Also, update the player's credits and stats in the database through /api/postgre?action=add_credits.
|
---|
[87614a5] | 71 | * @action stand
|
---|
| 72 | * @param session_id
|
---|
| 73 | */
|
---|
| 74 | if (req.query.action === 'stand' && req.query?.session_id) {
|
---|
| 75 | const session_id = req.query.session_id;
|
---|
| 76 |
|
---|
| 77 | if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '4') {
|
---|
| 78 | const room = rooms[session_id];
|
---|
| 79 |
|
---|
| 80 | while (calculateHandValue(room.dealerCards) < 17) {
|
---|
| 81 | room.dealerCards.push(drawASingleCard(room));
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | room.status = '_5_game_over';
|
---|
| 85 |
|
---|
| 86 | if (calculateHandValue(room.dealerCards) > 21) {
|
---|
| 87 | room.outcome = 'dealer_busted';
|
---|
| 88 | }
|
---|
| 89 | else if (calculateHandValue(room.playerCards) > calculateHandValue(room.dealerCards)) {
|
---|
| 90 | room.outcome = 'player_won';
|
---|
| 91 | }
|
---|
| 92 | else if (calculateHandValue(room.playerCards) < calculateHandValue(room.dealerCards)) {
|
---|
| 93 | room.outcome = 'player_lost';
|
---|
| 94 | }
|
---|
| 95 | else {
|
---|
| 96 | room.outcome = 'draw';
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | room.earnings = calculateEarnings(room);
|
---|
| 100 |
|
---|
[55701f0] | 101 | if (room.outcome === 'draw') {
|
---|
| 102 | room.messageTitle = 'Draw!';
|
---|
| 103 | room.messageDescription = `You got your $${room.earnings} back`
|
---|
| 104 | }
|
---|
| 105 | else if (room.outcome === 'player_won') {
|
---|
| 106 | room.messageTitle = 'You won!';
|
---|
| 107 | room.messageDescription = `You won $${room.earnings}`
|
---|
| 108 | }
|
---|
| 109 | else if (room.outcome === 'dealer_busted') {
|
---|
| 110 | room.messageTitle = `Dealer ${room.dealerName} busted!`;
|
---|
| 111 | room.messageDescription = `You won $${room.earnings}`
|
---|
| 112 | }
|
---|
| 113 | else if (room.outcome === 'player_lost') {
|
---|
| 114 | room.messageTitle = 'You lost!';
|
---|
| 115 | room.messageDescription = `You lost $${-1*room.earnings}`
|
---|
| 116 | }
|
---|
| 117 | room.betOutcomeMessageShown = false;
|
---|
| 118 |
|
---|
[87614a5] | 119 | rooms[session_id] = room;
|
---|
| 120 |
|
---|
| 121 | axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${session_id}&credits=${room.earnings}&game=blackjack&outcome=${room.outcome}`).then(postgreRes => {
|
---|
| 122 | if (postgreRes.data?.success) {
|
---|
[55701f0] | 123 | rooms[session_id].credits = postgreRes.data?.credits;
|
---|
| 124 |
|
---|
[87614a5] | 125 | res.json({
|
---|
| 126 | success: true,
|
---|
| 127 | status: rooms[session_id].status,
|
---|
| 128 | playerCards: rooms[session_id].playerCards,
|
---|
| 129 | dealerCards: rooms[session_id].dealerCards,
|
---|
| 130 | outcome: rooms[session_id].outcome,
|
---|
| 131 | earnings: rooms[session_id].earnings,
|
---|
| 132 | credits: postgreRes.data?.credits,
|
---|
| 133 | })
|
---|
[433e0c5] | 134 |
|
---|
| 135 | update_rooms_to_database();
|
---|
[87614a5] | 136 | }
|
---|
| 137 | else {
|
---|
| 138 | res.json({
|
---|
| 139 | success: false,
|
---|
| 140 | })
|
---|
| 141 | }
|
---|
| 142 | });
|
---|
| 143 |
|
---|
[faff334] | 144 | saveGameInHistory('blackjack', rooms[session_id], rooms[session_id].username);
|
---|
| 145 |
|
---|
[87614a5] | 146 | return ;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | res.json({
|
---|
| 150 | success: false,
|
---|
| 151 | })
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 156 | * If game status is _4_cards_on_the_table, draw a card for the player.
|
---|
| 157 | * If player busts, update the player's stats in the database through /api/postgre?action=add_credits.
|
---|
[87614a5] | 158 | * @action hit_a_card
|
---|
| 159 | * @param session_id
|
---|
| 160 | */
|
---|
| 161 | if (req.query.action === 'hit_a_card' && req.query?.session_id) {
|
---|
| 162 | const session_id = req.query.session_id;
|
---|
| 163 |
|
---|
| 164 | if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '4') {
|
---|
| 165 | const room = rooms[session_id];
|
---|
| 166 |
|
---|
| 167 | room.playerCards.push(drawASingleCard(room));
|
---|
| 168 |
|
---|
| 169 | rooms[session_id] = room;
|
---|
| 170 |
|
---|
| 171 | if (calculateHandValue(room.playerCards) > 21) {
|
---|
| 172 | room.status = '_5_game_over';
|
---|
| 173 | room.outcome = 'player_busted';
|
---|
[55701f0] | 174 |
|
---|
[87614a5] | 175 | room.earnings = calculateEarnings(room);
|
---|
| 176 |
|
---|
[55701f0] | 177 | room.messageTitle = 'You busted!';
|
---|
| 178 | room.messageDescription = `You lost $${-1*room.earnings}`
|
---|
| 179 |
|
---|
| 180 | room.betOutcomeMessageShown = false;
|
---|
| 181 |
|
---|
[87614a5] | 182 | rooms[session_id] = room;
|
---|
| 183 |
|
---|
| 184 | axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${session_id}&credits=${room.earnings}&game=blackjack&outcome=${room.outcome}`).then(postgreRes => {
|
---|
| 185 | if (postgreRes.data?.success) {
|
---|
[55701f0] | 186 | rooms[session_id].credits = postgreRes.data?.credits;
|
---|
| 187 |
|
---|
[87614a5] | 188 | res.json({
|
---|
| 189 | success: true,
|
---|
| 190 | status: rooms[session_id].status,
|
---|
| 191 | playerCards: rooms[session_id].playerCards,
|
---|
| 192 | outcome: rooms[session_id].outcome,
|
---|
| 193 | earnings: rooms[session_id].earnings,
|
---|
| 194 | credits: postgreRes.data?.credits,
|
---|
| 195 | })
|
---|
[433e0c5] | 196 |
|
---|
| 197 | update_rooms_to_database();
|
---|
[87614a5] | 198 | }
|
---|
| 199 | else {
|
---|
| 200 | res.json({
|
---|
| 201 | success: false,
|
---|
| 202 | })
|
---|
| 203 | }
|
---|
| 204 | });
|
---|
[faff334] | 205 |
|
---|
| 206 | saveGameInHistory('blackjack', rooms[session_id], rooms[session_id].username);
|
---|
[87614a5] | 207 | }
|
---|
| 208 | else {
|
---|
| 209 | res.json({
|
---|
| 210 | success: true,
|
---|
| 211 | status: rooms[session_id].status,
|
---|
| 212 | playerCards: rooms[session_id].playerCards,
|
---|
| 213 | outcome: rooms[session_id].outcome,
|
---|
| 214 | earnings: rooms[session_id].earnings,
|
---|
| 215 | })
|
---|
[433e0c5] | 216 |
|
---|
| 217 | update_rooms_to_database();
|
---|
[87614a5] | 218 | }
|
---|
| 219 |
|
---|
| 220 | return ;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | res.json({
|
---|
| 224 | success: false,
|
---|
| 225 | })
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | /**
|
---|
| 229 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 230 | * 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).
|
---|
| 231 | * Update the player's stats in the database through /api/postgre?action=add_credits.
|
---|
[87614a5] | 232 | * @action get_initial_cards
|
---|
| 233 | * @param session_id
|
---|
| 234 | */
|
---|
| 235 | if (req.query.action === 'get_initial_cards' && req.query?.session_id) {
|
---|
| 236 | const session_id = req.query.session_id;
|
---|
| 237 |
|
---|
| 238 | if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '3') {
|
---|
| 239 | const room = rooms[session_id];
|
---|
| 240 |
|
---|
| 241 | getInitialCards(room);
|
---|
| 242 |
|
---|
| 243 | room.status = '_4_cards_on_the_table';
|
---|
| 244 |
|
---|
| 245 | rooms[session_id] = room;
|
---|
| 246 |
|
---|
[e007fcd] | 247 | update_rooms_to_database();
|
---|
| 248 |
|
---|
[87614a5] | 249 | if (room.sideBetName !== '' && room.sideBetName !== 'none') {
|
---|
| 250 | room.sideBetEarnings = calculateSideBetEarnings(room);
|
---|
| 251 | room.sideBetOutcome = room.sideBetEarnings > 0 ? 'side_bet_won' : 'side_bet_lost';
|
---|
| 252 |
|
---|
[55701f0] | 253 | if (room.sideBetOutcome === 'side_bet_won') {
|
---|
| 254 | room.messageTitle = `You won the side bet!`;
|
---|
| 255 | room.messageDescription = `You won $${room.sideBetEarnings}`
|
---|
| 256 | }
|
---|
| 257 | else if (room.sideBetOutcome === 'side_bet_lost') {
|
---|
| 258 | room.messageTitle = `You lost the side bet!`;
|
---|
| 259 | room.messageDescription = `You lost $${-1*room.sideBetEarnings}`
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | room.sideBetOutcomeMessageShown = false;
|
---|
| 263 |
|
---|
[87614a5] | 264 | rooms[session_id] = room;
|
---|
| 265 |
|
---|
| 266 | axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${session_id}&credits=${room.sideBetEarnings}`).then(postgreRes => {
|
---|
| 267 | if (postgreRes.data?.success) {
|
---|
[55701f0] | 268 | rooms[session_id].credits = postgreRes.data?.credits;
|
---|
| 269 |
|
---|
[433e0c5] | 270 | update_rooms_to_database();
|
---|
[87614a5] | 271 | }
|
---|
| 272 | });
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[55701f0] | 276 | res.end();
|
---|
[87614a5] | 277 | }
|
---|
| 278 |
|
---|
| 279 | /**
|
---|
| 280 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 281 | * If game status is _2_made_initial_bet, place a side bet if the user has chosen one.
|
---|
[87614a5] | 282 | * @action make_side_bet
|
---|
| 283 | * @param session_id
|
---|
| 284 | * @param bet
|
---|
| 285 | * @param betName
|
---|
| 286 | */
|
---|
[55701f0] | 287 | if (req.query.action === 'make_side_bet' && req.query?.session_id && req.query?.bet && req.query?.betName) {
|
---|
[87614a5] | 288 | const session_id = req.query.session_id;
|
---|
| 289 |
|
---|
| 290 | if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '2') {
|
---|
| 291 | if (req.query?.skip !== 'true' && parseInt(req.query.bet) <= 0) {
|
---|
| 292 | return ;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[285c3cc] | 295 | axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => {
|
---|
| 296 | if (postgreRes.data?.success) {
|
---|
[55701f0] | 297 | rooms[session_id].credits = postgreRes.data?.credits;
|
---|
| 298 |
|
---|
[285c3cc] | 299 | const room = rooms[session_id];
|
---|
[87614a5] | 300 |
|
---|
[285c3cc] | 301 | room.sideBet = parseInt(req.query.bet);
|
---|
| 302 | room.sideBetName = req.query.betName;
|
---|
| 303 | room.status = '_3_made_side_bet';
|
---|
[87614a5] | 304 |
|
---|
[285c3cc] | 305 | rooms[session_id] = room;
|
---|
[87614a5] | 306 |
|
---|
[285c3cc] | 307 | res.json({
|
---|
| 308 | success: true,
|
---|
| 309 | })
|
---|
[433e0c5] | 310 |
|
---|
| 311 | update_rooms_to_database();
|
---|
[285c3cc] | 312 | }
|
---|
[55701f0] | 313 | else {
|
---|
| 314 | res.end();
|
---|
| 315 | }
|
---|
[285c3cc] | 316 | });
|
---|
[87614a5] | 317 | }
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | /**
|
---|
| 321 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 322 | * If game status is _1_room_created, get the initial bet placed by the player.
|
---|
[87614a5] | 323 | * @action make_initial_bet
|
---|
| 324 | * @param session_id
|
---|
| 325 | * @param bet
|
---|
| 326 | */
|
---|
| 327 | if (req.query.action === 'make_initial_bet' && req.query?.session_id && req.query?.bet) {
|
---|
| 328 | const session_id = req.query.session_id;
|
---|
| 329 |
|
---|
| 330 | if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '1') {
|
---|
| 331 | if (parseInt(req.query.bet) <= 0) return ;
|
---|
| 332 |
|
---|
[285c3cc] | 333 | axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => {
|
---|
| 334 | if (postgreRes.data?.success) {
|
---|
[55701f0] | 335 | rooms[session_id].credits = postgreRes.data?.credits;
|
---|
| 336 |
|
---|
[285c3cc] | 337 | const room = rooms[session_id];
|
---|
[87614a5] | 338 |
|
---|
[285c3cc] | 339 | room.initialBet = parseInt(req.query.bet);
|
---|
| 340 | room.status = '_2_made_initial_bet';
|
---|
[87614a5] | 341 |
|
---|
[285c3cc] | 342 | rooms[session_id] = room;
|
---|
[433e0c5] | 343 |
|
---|
| 344 | update_rooms_to_database();
|
---|
[285c3cc] | 345 | }
|
---|
| 346 | });
|
---|
[87614a5] | 347 | }
|
---|
| 348 |
|
---|
[55701f0] | 349 | res.end();
|
---|
[87614a5] | 350 | }
|
---|
| 351 |
|
---|
| 352 | /**
|
---|
| 353 | * /---------------------- GET ----------------------/
|
---|
[55701f0] | 354 | * Updates the state periodically
|
---|
| 355 | * @action update_state
|
---|
| 356 | * @param session_id
|
---|
| 357 | */
|
---|
| 358 | if (req.query.action === 'update_state' && req.query?.session_id) {
|
---|
| 359 | const session_id = req.query.session_id;
|
---|
| 360 |
|
---|
| 361 | const { success, game } = getGame(session_id);
|
---|
| 362 |
|
---|
| 363 | res.json({
|
---|
| 364 | success: true,
|
---|
| 365 | blackjackGame: getRestrictedGameObject(session_id),
|
---|
| 366 | })
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | /**
|
---|
| 370 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 371 | * If the player is not in an existing room, create a room for them.
|
---|
| 372 | * If they are reconnecting, get the room they were in.
|
---|
[87614a5] | 373 | * @action get_player_info_on_enter
|
---|
| 374 | * @param session_id
|
---|
| 375 | */
|
---|
| 376 | if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
|
---|
| 377 | const session_id = req.query.session_id;
|
---|
| 378 |
|
---|
[285c3cc] | 379 | axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
|
---|
| 380 | if (postgreRes.data?.success) {
|
---|
| 381 | if (rooms[session_id] !== undefined) {
|
---|
| 382 | // room exists
|
---|
[87614a5] | 383 | }
|
---|
| 384 | else {
|
---|
[e007fcd] | 385 | createARoom(session_id, postgreRes.data?.displayName, postgreRes.data?.username);
|
---|
[87614a5] | 386 | }
|
---|
[285c3cc] | 387 |
|
---|
| 388 | let dealerCardsTmp = [];
|
---|
| 389 | if (rooms[session_id].status.substr(1, 1) != '5') { // 5 == game_over
|
---|
| 390 | rooms[session_id].dealerCards.forEach((card, i) => {
|
---|
| 391 | if (i === 0) {
|
---|
| 392 | dealerCardsTmp.push(card);
|
---|
| 393 | }
|
---|
| 394 | else {
|
---|
| 395 | dealerCardsTmp.push('back');
|
---|
| 396 | }
|
---|
| 397 | })
|
---|
| 398 | }
|
---|
| 399 | else {
|
---|
| 400 | dealerCardsTmp = rooms[session_id].dealerCards;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | res.json({
|
---|
| 404 | success: true,
|
---|
| 405 | status: rooms[session_id].status,
|
---|
| 406 | initialBet: rooms[session_id].initialBet,
|
---|
| 407 | sideBet: rooms[session_id].sideBet,
|
---|
| 408 | sideBetName: rooms[session_id].sideBetName,
|
---|
| 409 | playerCards: rooms[session_id].playerCards,
|
---|
| 410 | dealerCards: dealerCardsTmp,
|
---|
| 411 | outcome: rooms[session_id].outcome,
|
---|
| 412 | earnings: rooms[session_id].earnings,
|
---|
| 413 | displayName: postgreRes.data?.displayName,
|
---|
| 414 | session_id: postgreRes.data?.session_id,
|
---|
| 415 | credits: postgreRes.data?.credits,
|
---|
| 416 | })
|
---|
[433e0c5] | 417 |
|
---|
| 418 | update_rooms_to_database();
|
---|
[285c3cc] | 419 | }
|
---|
| 420 | else {
|
---|
| 421 | res.json({
|
---|
| 422 | success: false,
|
---|
| 423 | })
|
---|
| 424 | }
|
---|
| 425 | });
|
---|
[87614a5] | 426 | }
|
---|
| 427 | }
|
---|
| 428 | }
|
---|
| 429 | /**
|
---|
| 430 | * ********************* END OF REQUEST HANDLER *********************
|
---|
| 431 | */
|
---|