Changeset 55701f0 for pages/api/blackjack
- Timestamp:
- 07/16/22 11:18:24 (2 years ago)
- Branches:
- main
- Children:
- e007fcd
- Parents:
- 433e0c5
- Location:
- pages/api/blackjack
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
pages/api/blackjack/gameStates.js
r433e0c5 r55701f0 1 import { rooms } from "../postgre"; 2 1 3 const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK", 2 4 "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK", … … 11 13 */ 12 14 export let game = { 15 credits: -1, 13 16 deck: [...deck], 14 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 … … 23 26 sideBetOutcome: '', 24 27 sideBetEarnings: 0, 28 messageTitle: '', 29 messageDescription: '', 30 betOutcomeMessageShown: true, 31 sideBetOutcomeMessageShown: true, 32 } 33 34 export 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 48 export 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 } 25 55 } 26 56 -
pages/api/blackjack/index.js
r433e0c5 r55701f0 3 3 require('dotenv').config(); 4 4 5 import { game, drawASingleCard, getInitialCards, calculateHandValue } from './gameStates';5 import { game, drawASingleCard, getInitialCards, calculateHandValue, getGame, getRestrictedGameObject } from './gameStates'; 6 6 import { calculateEarnings, calculateSideBetEarnings } from './calculateEarnings'; 7 7 … … 39 39 rooms[session_id] = {...game, playerCards: [...game.playerCards], dealerCards: [...game.dealerCards]}; 40 40 41 res.json({ 42 success: true, 43 game: rooms[session_id], 44 }) 41 rooms[session_id].betOutcomeMessageShown = true; 45 42 46 43 update_rooms_to_database(); 47 48 return ; 49 } 50 51 res.json({ 52 success: false, 53 }) 44 } 45 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(); 62 } 63 64 res.end(); 54 65 } 55 66 … … 88 99 room.earnings = calculateEarnings(room); 89 100 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 90 119 rooms[session_id] = room; 91 120 92 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 => { 93 122 if (postgreRes.data?.success) { 123 rooms[session_id].credits = postgreRes.data?.credits; 124 94 125 res.json({ 95 126 success: true, … … 139 170 room.status = '_5_game_over'; 140 171 room.outcome = 'player_busted'; 141 172 142 173 room.earnings = calculateEarnings(room); 174 175 room.messageTitle = 'You busted!'; 176 room.messageDescription = `You lost $${-1*room.earnings}` 177 178 room.betOutcomeMessageShown = false; 143 179 144 180 rooms[session_id] = room; … … 146 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 => { 147 183 if (postgreRes.data?.success) { 184 rooms[session_id].credits = postgreRes.data?.credits; 185 148 186 res.json({ 149 187 success: true, … … 207 245 room.sideBetOutcome = room.sideBetEarnings > 0 ? 'side_bet_won' : 'side_bet_lost'; 208 246 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 209 258 rooms[session_id] = room; 259 260 update_rooms_to_database(); 210 261 211 262 axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${session_id}&credits=${room.sideBetEarnings}`).then(postgreRes => { 212 263 if (postgreRes.data?.success) { 213 res.json({ 214 success: true, 215 status: rooms[session_id].status, 216 playerCards: rooms[session_id].playerCards, 217 dealerCards: Array(rooms[session_id].dealerCards[0]).concat('back'), 218 sideBetOutcome: rooms[session_id].sideBetOutcome, 219 sideBetEarnings: rooms[session_id].sideBetEarnings, 220 credits: postgreRes.data?.credits, 221 }) 222 264 rooms[session_id].credits = postgreRes.data?.credits; 265 223 266 update_rooms_to_database(); 224 267 } 225 else {226 res.json({227 success: false,228 })229 }230 268 }); 231 269 } 232 else { 233 res.json({ 234 success: true, 235 status: rooms[session_id].status, 236 playerCards: rooms[session_id].playerCards, 237 dealerCards: Array(rooms[session_id].dealerCards[0]).concat('back'), 238 sideBetOutcome: rooms[session_id].sideBetOutcome, 239 sideBetEarnings: rooms[session_id].sideBetEarnings, 240 }) 241 242 update_rooms_to_database(); 243 } 244 245 return ; 246 } 247 248 res.json({ 249 success: false, 250 }) 270 } 271 272 res.end(); 251 273 } 252 274 … … 259 281 * @param betName 260 282 */ 261 283 if (req.query.action === 'make_side_bet' && req.query?.session_id && req.query?.bet && req.query?.betName) { 262 284 const session_id = req.query.session_id; 263 285 … … 269 291 axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => { 270 292 if (postgreRes.data?.success) { 293 rooms[session_id].credits = postgreRes.data?.credits; 294 271 295 const room = rooms[session_id]; 272 296 … … 279 303 res.json({ 280 304 success: true, 281 status: rooms[session_id].status,282 credits: postgreRes.data?.credits,283 305 }) 284 306 285 307 update_rooms_to_database(); 286 308 } 309 else { 310 res.end(); 311 } 287 312 }); 288 289 return ; 290 } 291 292 res.json({ 293 success: false, 294 }) 313 } 295 314 } 296 315 … … 310 329 axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => { 311 330 if (postgreRes.data?.success) { 331 rooms[session_id].credits = postgreRes.data?.credits; 332 312 333 const room = rooms[session_id]; 313 334 … … 316 337 317 338 rooms[session_id] = room; 318 319 res.json({320 success: true,321 status: rooms[session_id].status,322 credits: postgreRes.data?.credits,323 })324 339 325 340 update_rooms_to_database(); 326 341 } 327 else {328 res.json({329 success: false,330 })331 }332 342 }); 333 334 return ; 335 } 336 337 res.json({ 338 success: false, 339 }) 343 } 344 345 res.end(); 340 346 } 341 347 … … 358 364 359 365 update_rooms_to_database(); 366 } 367 368 /** 369 * /---------------------- GET ----------------------/ 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 }) 360 383 } 361 384
Note:
See TracChangeset
for help on using the changeset viewer.