[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 |
|
---|
[433e0c5] | 8 | import { rooms, update_rooms_to_database } from '../postgre/index'
|
---|
[87614a5] | 9 |
|
---|
| 10 | /**
|
---|
| 11 | * Set up a room
|
---|
| 12 | */
|
---|
| 13 | function createARoom(session_id) {
|
---|
| 14 | let room = {
|
---|
| 15 | ...game, playerCards: [...game.playerCards], dealerCards: [...game.dealerCards],
|
---|
| 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') {
|
---|
| 39 | rooms[session_id] = {...game, playerCards: [...game.playerCards], dealerCards: [...game.dealerCards]};
|
---|
| 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 |
|
---|
| 144 | return ;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | res.json({
|
---|
| 148 | success: false,
|
---|
| 149 | })
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
| 153 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 154 | * If game status is _4_cards_on_the_table, draw a card for the player.
|
---|
| 155 | * If player busts, update the player's stats in the database through /api/postgre?action=add_credits.
|
---|
[87614a5] | 156 | * @action hit_a_card
|
---|
| 157 | * @param session_id
|
---|
| 158 | */
|
---|
| 159 | if (req.query.action === 'hit_a_card' && req.query?.session_id) {
|
---|
| 160 | const session_id = req.query.session_id;
|
---|
| 161 |
|
---|
| 162 | if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '4') {
|
---|
| 163 | const room = rooms[session_id];
|
---|
| 164 |
|
---|
| 165 | room.playerCards.push(drawASingleCard(room));
|
---|
| 166 |
|
---|
| 167 | rooms[session_id] = room;
|
---|
| 168 |
|
---|
| 169 | if (calculateHandValue(room.playerCards) > 21) {
|
---|
| 170 | room.status = '_5_game_over';
|
---|
| 171 | room.outcome = 'player_busted';
|
---|
[55701f0] | 172 |
|
---|
[87614a5] | 173 | room.earnings = calculateEarnings(room);
|
---|
| 174 |
|
---|
[55701f0] | 175 | room.messageTitle = 'You busted!';
|
---|
| 176 | room.messageDescription = `You lost $${-1*room.earnings}`
|
---|
| 177 |
|
---|
| 178 | room.betOutcomeMessageShown = false;
|
---|
| 179 |
|
---|
[87614a5] | 180 | rooms[session_id] = room;
|
---|
| 181 |
|
---|
| 182 | 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 => {
|
---|
| 183 | if (postgreRes.data?.success) {
|
---|
[55701f0] | 184 | rooms[session_id].credits = postgreRes.data?.credits;
|
---|
| 185 |
|
---|
[87614a5] | 186 | res.json({
|
---|
| 187 | success: true,
|
---|
| 188 | status: rooms[session_id].status,
|
---|
| 189 | playerCards: rooms[session_id].playerCards,
|
---|
| 190 | outcome: rooms[session_id].outcome,
|
---|
| 191 | earnings: rooms[session_id].earnings,
|
---|
| 192 | credits: postgreRes.data?.credits,
|
---|
| 193 | })
|
---|
[433e0c5] | 194 |
|
---|
| 195 | update_rooms_to_database();
|
---|
[87614a5] | 196 | }
|
---|
| 197 | else {
|
---|
| 198 | res.json({
|
---|
| 199 | success: false,
|
---|
| 200 | })
|
---|
| 201 | }
|
---|
| 202 | });
|
---|
| 203 | }
|
---|
| 204 | else {
|
---|
| 205 | res.json({
|
---|
| 206 | success: true,
|
---|
| 207 | status: rooms[session_id].status,
|
---|
| 208 | playerCards: rooms[session_id].playerCards,
|
---|
| 209 | outcome: rooms[session_id].outcome,
|
---|
| 210 | earnings: rooms[session_id].earnings,
|
---|
| 211 | })
|
---|
[433e0c5] | 212 |
|
---|
| 213 | update_rooms_to_database();
|
---|
[87614a5] | 214 | }
|
---|
| 215 |
|
---|
| 216 | return ;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | res.json({
|
---|
| 220 | success: false,
|
---|
| 221 | })
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /**
|
---|
| 225 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 226 | * 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).
|
---|
| 227 | * Update the player's stats in the database through /api/postgre?action=add_credits.
|
---|
[87614a5] | 228 | * @action get_initial_cards
|
---|
| 229 | * @param session_id
|
---|
| 230 | */
|
---|
| 231 | if (req.query.action === 'get_initial_cards' && req.query?.session_id) {
|
---|
| 232 | const session_id = req.query.session_id;
|
---|
| 233 |
|
---|
| 234 | if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '3') {
|
---|
| 235 | const room = rooms[session_id];
|
---|
| 236 |
|
---|
| 237 | getInitialCards(room);
|
---|
| 238 |
|
---|
| 239 | room.status = '_4_cards_on_the_table';
|
---|
| 240 |
|
---|
| 241 | rooms[session_id] = room;
|
---|
| 242 |
|
---|
| 243 | if (room.sideBetName !== '' && room.sideBetName !== 'none') {
|
---|
| 244 | room.sideBetEarnings = calculateSideBetEarnings(room);
|
---|
| 245 | room.sideBetOutcome = room.sideBetEarnings > 0 ? 'side_bet_won' : 'side_bet_lost';
|
---|
| 246 |
|
---|
[55701f0] | 247 | if (room.sideBetOutcome === 'side_bet_won') {
|
---|
| 248 | room.messageTitle = `You won the side bet!`;
|
---|
| 249 | room.messageDescription = `You won $${room.sideBetEarnings}`
|
---|
| 250 | }
|
---|
| 251 | else if (room.sideBetOutcome === 'side_bet_lost') {
|
---|
| 252 | room.messageTitle = `You lost the side bet!`;
|
---|
| 253 | room.messageDescription = `You lost $${-1*room.sideBetEarnings}`
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | room.sideBetOutcomeMessageShown = false;
|
---|
| 257 |
|
---|
[87614a5] | 258 | rooms[session_id] = room;
|
---|
| 259 |
|
---|
[55701f0] | 260 | update_rooms_to_database();
|
---|
| 261 |
|
---|
[87614a5] | 262 | axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${session_id}&credits=${room.sideBetEarnings}`).then(postgreRes => {
|
---|
| 263 | if (postgreRes.data?.success) {
|
---|
[55701f0] | 264 | rooms[session_id].credits = postgreRes.data?.credits;
|
---|
| 265 |
|
---|
[433e0c5] | 266 | update_rooms_to_database();
|
---|
[87614a5] | 267 | }
|
---|
| 268 | });
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[55701f0] | 272 | res.end();
|
---|
[87614a5] | 273 | }
|
---|
| 274 |
|
---|
| 275 | /**
|
---|
| 276 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 277 | * If game status is _2_made_initial_bet, place a side bet if the user has chosen one.
|
---|
[87614a5] | 278 | * @action make_side_bet
|
---|
| 279 | * @param session_id
|
---|
| 280 | * @param bet
|
---|
| 281 | * @param betName
|
---|
| 282 | */
|
---|
[55701f0] | 283 | if (req.query.action === 'make_side_bet' && req.query?.session_id && req.query?.bet && req.query?.betName) {
|
---|
[87614a5] | 284 | const session_id = req.query.session_id;
|
---|
| 285 |
|
---|
| 286 | if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '2') {
|
---|
| 287 | if (req.query?.skip !== 'true' && parseInt(req.query.bet) <= 0) {
|
---|
| 288 | return ;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
[285c3cc] | 291 | axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => {
|
---|
| 292 | if (postgreRes.data?.success) {
|
---|
[55701f0] | 293 | rooms[session_id].credits = postgreRes.data?.credits;
|
---|
| 294 |
|
---|
[285c3cc] | 295 | const room = rooms[session_id];
|
---|
[87614a5] | 296 |
|
---|
[285c3cc] | 297 | room.sideBet = parseInt(req.query.bet);
|
---|
| 298 | room.sideBetName = req.query.betName;
|
---|
| 299 | room.status = '_3_made_side_bet';
|
---|
[87614a5] | 300 |
|
---|
[285c3cc] | 301 | rooms[session_id] = room;
|
---|
[87614a5] | 302 |
|
---|
[285c3cc] | 303 | res.json({
|
---|
| 304 | success: true,
|
---|
| 305 | })
|
---|
[433e0c5] | 306 |
|
---|
| 307 | update_rooms_to_database();
|
---|
[285c3cc] | 308 | }
|
---|
[55701f0] | 309 | else {
|
---|
| 310 | res.end();
|
---|
| 311 | }
|
---|
[285c3cc] | 312 | });
|
---|
[87614a5] | 313 | }
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | /**
|
---|
| 317 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 318 | * If game status is _1_room_created, get the initial bet placed by the player.
|
---|
[87614a5] | 319 | * @action make_initial_bet
|
---|
| 320 | * @param session_id
|
---|
| 321 | * @param bet
|
---|
| 322 | */
|
---|
| 323 | if (req.query.action === 'make_initial_bet' && req.query?.session_id && req.query?.bet) {
|
---|
| 324 | const session_id = req.query.session_id;
|
---|
| 325 |
|
---|
| 326 | if (rooms[session_id] !== undefined && rooms[session_id].status.substr(1, 1) === '1') {
|
---|
| 327 | if (parseInt(req.query.bet) <= 0) return ;
|
---|
| 328 |
|
---|
[285c3cc] | 329 | axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => {
|
---|
| 330 | if (postgreRes.data?.success) {
|
---|
[55701f0] | 331 | rooms[session_id].credits = postgreRes.data?.credits;
|
---|
| 332 |
|
---|
[285c3cc] | 333 | const room = rooms[session_id];
|
---|
[87614a5] | 334 |
|
---|
[285c3cc] | 335 | room.initialBet = parseInt(req.query.bet);
|
---|
| 336 | room.status = '_2_made_initial_bet';
|
---|
[87614a5] | 337 |
|
---|
[285c3cc] | 338 | rooms[session_id] = room;
|
---|
[433e0c5] | 339 |
|
---|
| 340 | update_rooms_to_database();
|
---|
[285c3cc] | 341 | }
|
---|
| 342 | });
|
---|
[87614a5] | 343 | }
|
---|
| 344 |
|
---|
[55701f0] | 345 | res.end();
|
---|
[87614a5] | 346 | }
|
---|
| 347 |
|
---|
| 348 | /**
|
---|
| 349 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 350 | * Remove a room from the rooms array.
|
---|
[87614a5] | 351 | * @action remove_room
|
---|
| 352 | * @param session_id
|
---|
| 353 | */
|
---|
| 354 | if (req.query.action === 'remove_room' && req.query?.session_id) {
|
---|
| 355 | const session_id = req.query.session_id;
|
---|
| 356 |
|
---|
| 357 | if (rooms[session_id] !== undefined) {
|
---|
| 358 | delete rooms[session_id];
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | res.json({
|
---|
| 362 | success: true,
|
---|
| 363 | })
|
---|
[433e0c5] | 364 |
|
---|
| 365 | update_rooms_to_database();
|
---|
[87614a5] | 366 | }
|
---|
| 367 |
|
---|
| 368 | /**
|
---|
| 369 | * /---------------------- GET ----------------------/
|
---|
[55701f0] | 370 | * Updates the state periodically
|
---|
| 371 | * @action update_state
|
---|
| 372 | * @param session_id
|
---|
| 373 | */
|
---|
| 374 | if (req.query.action === 'update_state' && req.query?.session_id) {
|
---|
| 375 | const session_id = req.query.session_id;
|
---|
| 376 |
|
---|
| 377 | const { success, game } = getGame(session_id);
|
---|
| 378 |
|
---|
| 379 | res.json({
|
---|
| 380 | success: true,
|
---|
| 381 | blackjackGame: getRestrictedGameObject(session_id),
|
---|
| 382 | })
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | /**
|
---|
| 386 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 387 | * If the player is not in an existing room, create a room for them.
|
---|
| 388 | * If they are reconnecting, get the room they were in.
|
---|
[87614a5] | 389 | * @action get_player_info_on_enter
|
---|
| 390 | * @param session_id
|
---|
| 391 | */
|
---|
| 392 | if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
|
---|
| 393 | const session_id = req.query.session_id;
|
---|
| 394 |
|
---|
[285c3cc] | 395 | axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
|
---|
| 396 | if (postgreRes.data?.success) {
|
---|
| 397 | if (rooms[session_id] !== undefined) {
|
---|
| 398 | // room exists
|
---|
[87614a5] | 399 | }
|
---|
| 400 | else {
|
---|
[285c3cc] | 401 | createARoom(session_id);
|
---|
[87614a5] | 402 | }
|
---|
[285c3cc] | 403 |
|
---|
| 404 | let dealerCardsTmp = [];
|
---|
| 405 | if (rooms[session_id].status.substr(1, 1) != '5') { // 5 == game_over
|
---|
| 406 | rooms[session_id].dealerCards.forEach((card, i) => {
|
---|
| 407 | if (i === 0) {
|
---|
| 408 | dealerCardsTmp.push(card);
|
---|
| 409 | }
|
---|
| 410 | else {
|
---|
| 411 | dealerCardsTmp.push('back');
|
---|
| 412 | }
|
---|
| 413 | })
|
---|
| 414 | }
|
---|
| 415 | else {
|
---|
| 416 | dealerCardsTmp = rooms[session_id].dealerCards;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | res.json({
|
---|
| 420 | success: true,
|
---|
| 421 | status: rooms[session_id].status,
|
---|
| 422 | initialBet: rooms[session_id].initialBet,
|
---|
| 423 | sideBet: rooms[session_id].sideBet,
|
---|
| 424 | sideBetName: rooms[session_id].sideBetName,
|
---|
| 425 | playerCards: rooms[session_id].playerCards,
|
---|
| 426 | dealerCards: dealerCardsTmp,
|
---|
| 427 | outcome: rooms[session_id].outcome,
|
---|
| 428 | earnings: rooms[session_id].earnings,
|
---|
| 429 | displayName: postgreRes.data?.displayName,
|
---|
| 430 | session_id: postgreRes.data?.session_id,
|
---|
| 431 | credits: postgreRes.data?.credits,
|
---|
| 432 | })
|
---|
[433e0c5] | 433 |
|
---|
| 434 | update_rooms_to_database();
|
---|
[285c3cc] | 435 | }
|
---|
| 436 | else {
|
---|
| 437 | res.json({
|
---|
| 438 | success: false,
|
---|
| 439 | })
|
---|
| 440 | }
|
---|
| 441 | });
|
---|
[87614a5] | 442 | }
|
---|
| 443 | }
|
---|
| 444 | }
|
---|
| 445 | /**
|
---|
| 446 | * ********************* END OF REQUEST HANDLER *********************
|
---|
| 447 | */
|
---|