Changeset 55701f0 for pages/api


Ignore:
Timestamp:
07/16/22 11:18:24 (2 years ago)
Author:
anastasovv <simon@…>
Branches:
main
Children:
e007fcd
Parents:
433e0c5
Message:

Added 1 second update_state calls in blackjack

Location:
pages/api
Files:
5 edited

Legend:

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

    r433e0c5 r55701f0  
     1import { rooms } from "../postgre";
     2
    13const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK",
    24                    "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
     
    1113 */
    1214export let game = {
     15  credits: -1,
    1316  deck: [...deck],
    1417  status: '_1_room_created',      // statuses: _1_room_created, _2_made_initial_bet, _3_made_side_bet, _4_cards_on_the_table, _5_game_over
     
    2326  sideBetOutcome: '',
    2427  sideBetEarnings: 0,
     28  messageTitle: '',
     29  messageDescription: '',
     30  betOutcomeMessageShown: true,
     31  sideBetOutcomeMessageShown: true,
     32}
     33
     34export 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
     48export 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  }
    2555}
    2656
  • pages/api/blackjack/index.js

    r433e0c5 r55701f0  
    33require('dotenv').config();
    44
    5 import { game, drawASingleCard, getInitialCards, calculateHandValue } from './gameStates';
     5import { game, drawASingleCard, getInitialCards, calculateHandValue, getGame, getRestrictedGameObject } from './gameStates';
    66import { calculateEarnings, calculateSideBetEarnings } from './calculateEarnings';
    77
     
    3939        rooms[session_id] = {...game, playerCards: [...game.playerCards], dealerCards: [...game.dealerCards]};
    4040
    41         res.json({
    42           success: true,
    43           game: rooms[session_id],
    44         })
     41        rooms[session_id].betOutcomeMessageShown = true;
    4542
    4643        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();
    5465    }
    5566
     
    8899        room.earnings = calculateEarnings(room);
    89100
     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
    90119        rooms[session_id] = room;
    91120
    92121        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 => {
    93122          if (postgreRes.data?.success) {
     123            rooms[session_id].credits = postgreRes.data?.credits;
     124
    94125            res.json({
    95126              success: true,
     
    139170          room.status = '_5_game_over';
    140171          room.outcome = 'player_busted';
    141 
     172         
    142173          room.earnings = calculateEarnings(room);
     174
     175          room.messageTitle = 'You busted!';
     176          room.messageDescription = `You lost $${-1*room.earnings}`
     177         
     178          room.betOutcomeMessageShown = false;
    143179
    144180          rooms[session_id] = room;
     
    146182          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 => {
    147183            if (postgreRes.data?.success) {
     184              rooms[session_id].credits = postgreRes.data?.credits;
     185
    148186              res.json({
    149187                success: true,
     
    207245          room.sideBetOutcome = room.sideBetEarnings > 0 ? 'side_bet_won' : 'side_bet_lost';
    208246
     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         
    209258          rooms[session_id] = room;
     259
     260          update_rooms_to_database();
    210261
    211262          axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${session_id}&credits=${room.sideBetEarnings}`).then(postgreRes => {
    212263            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
    223266              update_rooms_to_database();
    224267            }
    225             else {
    226               res.json({
    227                 success: false,
    228               })
    229             }
    230268          });
    231269        }
    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();
    251273    }
    252274
     
    259281     * @param betName
    260282     */
    261      if (req.query.action === 'make_side_bet' && req.query?.session_id && req.query?.bet && req.query?.betName) {
     283    if (req.query.action === 'make_side_bet' && req.query?.session_id && req.query?.bet && req.query?.betName) {
    262284      const session_id = req.query.session_id;
    263285
     
    269291        axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => {
    270292          if (postgreRes.data?.success) {
     293            rooms[session_id].credits = postgreRes.data?.credits;
     294
    271295            const room = rooms[session_id];
    272296
     
    279303            res.json({
    280304              success: true,
    281               status: rooms[session_id].status,
    282               credits: postgreRes.data?.credits,
    283305            })
    284306           
    285307            update_rooms_to_database();
    286308          }
     309          else {
     310            res.end();
     311          }
    287312        });
    288        
    289         return ;
    290       }
    291 
    292       res.json({
    293         success: false,
    294       })
     313      }
    295314    }
    296315   
     
    310329        axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.bet}`).then(postgreRes => {
    311330          if (postgreRes.data?.success) {
     331            rooms[session_id].credits = postgreRes.data?.credits;
     332
    312333            const room = rooms[session_id];
    313334
     
    316337
    317338            rooms[session_id] = room;
    318 
    319             res.json({
    320               success: true,
    321               status: rooms[session_id].status,
    322               credits: postgreRes.data?.credits,
    323             })
    324339           
    325340            update_rooms_to_database();
    326341          }
    327           else {
    328             res.json({
    329               success: false,
    330             })
    331           }
    332342        });
    333 
    334         return ;
    335       }
    336 
    337       res.json({
    338         success: false,
    339       })
     343      }
     344
     345      res.end();
    340346    }
    341347
     
    358364           
    359365      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      })
    360383    }
    361384
  • pages/api/poker/index.js

    r433e0c5 r55701f0  
    55import { createTable, getRestrictedTablesArray, getRestrictedTableArray, getTable, getTableAndPlayer } from './gameStates';
    66
    7 import { drawASingleCard, setNextPlayerIdx, progressRoundIfNeeded, progressRoundTillTheEnd } from './tableSpecific'
    8 
    9 import { tables, cleanTables, update_tables_to_database, load_tables_from_database } from '../postgre/index'
     7import { drawASingleCard, setNextPlayerIdx, progressRoundIfNeeded } from './tableSpecific'
     8
     9import { update_tables_to_database } from '../postgre/index'
    1010
    1111/**
  • pages/api/postgre/index.js

    r433e0c5 r55701f0  
    324324        return ;
    325325      }
     326      if (parseInt(body?.data?.amount) > 5000) {
     327        res.json({
     328          success: false,
     329          message: 'Failed to deposit. Insufficient credit on card.',
     330        });
     331        return ;
     332      }
    326333
    327334      let session = sessions.find(session => session.id === body?.session_id)
     
    338345              credits: session.credits
    339346            })
     347           
     348            update_sessions_to_database();
    340349          });
    341350        }
     
    415424              credits: session.credits
    416425            })
     426
     427            update_sessions_to_database();
    417428          });
    418429        }
     
    762773}
    763774load_rooms_from_database();
    764  
  • pages/api/roulette/gameStates.js

    r433e0c5 r55701f0  
    8686        })
    8787    }
     88    else {
     89        game.players[game.players.map(e=>e.session_id).indexOf(session_id)].credits = -1;
     90    }
    8891}
    8992
Note: See TracChangeset for help on using the changeset viewer.