Changeset 55701f0


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

Added 1 second update_state calls in blackjack

Files:
14 edited

Legend:

Unmodified
Added
Removed
  • components/Alert.jsx

    r433e0c5 r55701f0  
    77import axios from 'axios'
    88
    9 import { setBlackjack, setStyle } from '../redux/reducers/styleSlice'
    10 import { setBlackjackGame } from '../redux/reducers/playerSlice'
     9import { setStyle } from '../redux/reducers/styleSlice'
    1110
    1211const Alert = ({ onTop = false }) => {
    13     const playerState = useSelector(state => state.player)
    1412    const styleState = useSelector(state => state.style)
    1513
     
    1917
    2018    function clicked() {
    21         dispatch(setStyle({
    22             ...styleState.style,
    23             alert: {
    24                 ...styleState.style.alert,
    25                 show: false
    26             }
    27         }))
    28 
    2919        if (styleState.style.alert.button.action === 'play_again') {
    30             axios.get(`/api/blackjack?action=play_again&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
    31                 if (res.data?.success && res.data?.game) {
    32                     dispatch(setBlackjackGame({
    33                         ...playerState.blackjackGame,
    34                         status: res.data.game?.status,
    35                         playerCards: res.data.game?.playerCards,
    36                         dealerCards: res.data.game?.dealerCards,
    37                         sideBetName: res.data.game?.sideBetName,
    38                     }))
    39 
    40                     dispatch(setBlackjack({
    41                         ...styleState.blackjack,
    42                         inputControls: {
    43                             ...styleState.blackjack.inputControls,
    44                             initialBet: {
    45                                 ...styleState.blackjack.inputControls.initialBet,
    46                                 chosenCredits: parseInt(playerState.player.credits/2),
    47                             },
    48                             sideBet: {
    49                                 ...styleState.blackjack.inputControls.sideBet,
    50                                 chosenCredits: parseInt(0),
    51                             }
    52                         },
    53                         displays: {
    54                             ...styleState.blackjack.displays,
    55                             initialBet: true,
    56                             hitStand: false,
    57                         }
    58                     }))
    59                 }
    60             });
     20            axios.get(`/api/blackjack?action=play_again&session_id=${localStorage.CAESSINO_SESSION_ID}`);
    6121        }
    6222        else if (styleState.style.alert.button.action === 'continue_from_side_bet') {
    63             dispatch(setBlackjack({
    64                 ...styleState.blackjack,
    65                 inputControls: {
    66                     ...styleState.blackjack.inputControls,
    67                     sideBet: {
    68                         ...styleState.blackjack.inputControls.sideBet,
    69                         chosenCredits: parseInt(0),
    70                     }
    71                 },
     23            axios.get(`/api/blackjack?action=continue_from_side_bet&session_id=${localStorage.CAESSINO_SESSION_ID}`);
     24        }
     25        else {
     26            dispatch(setStyle({
     27                ...styleState.style,
     28                alert: {
     29                    ...styleState.style.alert,
     30                    show: false
     31                }
    7232            }))
    7333        }
  • components/ManageCredits.jsx

    r433e0c5 r55701f0  
    4444            } : action === 'withdraw' ? {
    4545                show: true,
    46                 text: `Withdrawed $${styleState.style.withdrawModalInputs.amount} successfully`,
     46                text: `Withdrew $${Math.min(styleState.style.withdrawModalInputs.amount, playerState.player.credits)} successfully`,
    4747                status: 'success',
    4848            } : {
  • components/blackjack/BlackjackHeader.jsx

    r433e0c5 r55701f0  
    3030        }));
    3131
    32         dispatch(setBlackjack({
    33             ...styleState.blackjack,
    34             inputControls: {
    35                 ...styleState.blackjack.inputControls,
    36                 initialBet: {
    37                     ...styleState.blackjack.inputControls.initialBet,
    38                     chosenCredits: parseInt(playerState.player.credits/2),
    39                 }
    40             }
    41         }));
    42 
     32        let interval = null;
    4333        axios.get(`/api/blackjack?action=get_player_info_on_enter&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
    4434            if (res.data?.success) {
     
    4838                    session_id: res.data?.session_id,
    4939                    credits: res.data?.credits,
    50                 }));
    51 
    52                 dispatch(setBlackjackGame({
    53                     ...playerState.blackjackGame,
    54                     status: res.data?.status,
    55                     playerCards: res.data?.playerCards,
    56                     dealerCards: res.data?.dealerCards,
    5740                }))
    5841
    59                 dispatch(setStyle({
    60                     ...styleState.style,
    61                     displayLoadingScreen: false,
     42                dispatch(setBlackjack({
     43                    ...styleState.blackjack,
     44                    inputControls: {
     45                        ...styleState.blackjack.inputControls,
     46                        initialBet: {
     47                            ...styleState.blackjack.inputControls.initialBet,
     48                            chosenCredits: parseInt(playerState.player.credits/2),
     49                        },
     50                        sideBet: {
     51                            ...styleState.blackjack.inputControls.sideBet,
     52                            chosenCredits: parseInt(playerState.player.credits/2),
     53                        }
     54                    },
    6255                }))
    6356
    64                 if (parseInt(res.data?.status.toString().substr(1, 1)) == 5) {
    65                     dispatch(setBlackjack({
    66                         ...styleState.blackjack,
    67                         inputControls: {
    68                             ...styleState.blackjack.inputControls,
    69                             initialBet: {
    70                                 ...styleState.blackjack.inputControls.initialBet,
    71                                 chosenCredits: parseInt(res.data?.initialBet),
    72                             },
    73                             sideBet: {
    74                                 ...styleState.blackjack.inputControls.sideBet,
    75                                 chosenCredits: 0,
     57                if (interval !== null) clearInterval(interval);
     58               
     59                interval = setInterval(() => {
     60                    axios.get(`/api/blackjack?action=update_state&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(newRes => {
     61                        if (newRes.data?.success) {
     62                            dispatch(setBlackjackGame(newRes.data?.blackjackGame))
     63
     64                            if (newRes.data?.blackjackGame?.credits !== playerState.player.credits && parseInt(newRes.data?.blackjackGame?.credits) > 0) {
     65                                dispatch(setPlayer({
     66                                    ...playerState.player,
     67                                    displayName: res.data?.displayName,
     68                                    session_id: res.data?.session_id,
     69                                    credits: newRes.data?.blackjackGame?.credits,
     70                                }))
    7671                            }
    77                         },
    78                         displays: {
    79                             ...styleState.blackjack.displays,
    80                             initialBet: false,
    81                             hitStand: true,
     72
     73                            dispatch(setStyle({
     74                                ...styleState.style,
     75                                displayLoadingScreen: false,
     76                                notification: {
     77                                    ...styleState.style.notification,
     78                                    show: false,
     79                                },
     80                                lostConnectionInfo: {
     81                                    show: false,
     82                                    message: ''
     83                                },
     84                                alert: (newRes.data?.blackjackGame?.status?.includes('_5_') && !newRes.data?.blackjackGame?.betOutcomeMessageShown) ? {
     85                                    show: true,
     86                                    title: newRes.data?.blackjackGame?.messageTitle,
     87                                    subtitle: newRes.data?.blackjackGame?.messageDescription,
     88                                    button: {
     89                                        text: 'Play Again',
     90                                        action: 'play_again',
     91                                    }
     92                                } : (newRes.data?.blackjackGame?.status?.includes('_4_') && !newRes.data?.blackjackGame?.sideBetOutcomeMessageShown && newRes.data?.blackjackGame?.sideBet > 0) ? {
     93                                    show: true,
     94                                    title: newRes.data?.blackjackGame?.messageTitle,
     95                                    subtitle: newRes.data?.blackjackGame?.messageDescription,
     96                                    button: {
     97                                        text: 'Continue',
     98                                        action: 'continue_from_side_bet',
     99                                    }
     100                                } : {
     101                                    ...styleState.style.alert,
     102                                    show: false,
     103                                },
     104                            }))
    82105                        }
    83                     }))
    84 
    85                     if (res.data?.outcome === 'player_busted') {
     106                    }).catch(error => {
    86107                        dispatch(setStyle({
    87108                            ...styleState.style,
    88                             alert: {
     109                            displayLoadingScreen: false,
     110                            lostConnectionInfo: {
    89111                                show: true,
    90                                 title: 'You busted!',
    91                                 subtitle: `You lost $${-1*res.data?.earnings}`,
    92                                 button: {
    93                                     text: 'Play again',
    94                                     action: 'play_again',
    95                                 }
     112                                message: 'Game will resume upon reconnection to the server.'
    96113                            }
    97114                        }))
    98                     }
    99                     else if (res.data?.outcome === 'dealer_busted') {
    100                         dispatch(setStyle({
    101                             ...styleState.style,
    102                             alert: {
    103                                 show: true,
    104                                 title: 'Dealer busted!',
    105                                 subtitle: `You won $${res.data?.earnings}`,
    106                                 button: {
    107                                     text: 'Play again',
    108                                     action: 'play_again',
    109                                 }
    110                             }
    111                         }))
    112                     }
    113                     else if (res.data?.outcome === 'player_won') {
    114                         dispatch(setStyle({
    115                             ...styleState.style,
    116                             alert: {
    117                                 show: true,
    118                                 title: 'You won!',
    119                                 subtitle: `You won $${res.data?.earnings}`,
    120                                 button: {
    121                                     text: 'Play again',
    122                                     action: 'play_again',
    123                                 }
    124                             }
    125                         }))
    126                     }
    127                     else if (res.data?.outcome === 'player_lost') {
    128                         dispatch(setStyle({
    129                             ...styleState.style,
    130                             alert: {
    131                                 show: true,
    132                                 title: 'You lost!',
    133                                 subtitle: `You lost $${-1*res.data?.earnings}`,
    134                                 button: {
    135                                     text: 'Play again',
    136                                     action: 'play_again',
    137                                 }
    138                             }
    139                         }))
    140                     }
    141                     else if (res.data?.outcome === 'draw') {
    142                         dispatch(setStyle({
    143                             ...styleState.style,
    144                             alert: {
    145                                 show: true,
    146                                 title: 'Draw!',
    147                                 subtitle: `You got your $${res.data?.earnings} back`,
    148                                 button: {
    149                                     text: 'Play again',
    150                                     action: 'play_again',
    151                                 }
    152                             }
    153                         }))
    154                     }
    155                 }
    156 
    157                 if (parseInt(res.data?.status.toString().substr(1, 1)) == 4) {
    158                     dispatch(setBlackjack({
    159                         ...styleState.blackjack,
    160                         inputControls: {
    161                             ...styleState.blackjack.inputControls,
    162                             initialBet: {
    163                                 ...styleState.blackjack.inputControls.initialBet,
    164                                 chosenCredits: parseInt(res.data?.initialBet),
    165                             },
    166                             sideBet: {
    167                                 ...styleState.blackjack.inputControls.sideBet,
    168                                 chosenCredits: 0,
    169                             }
    170                         },
    171                         displays: {
    172                             ...styleState.blackjack.displays,
    173                             initialBet: false,
    174                             hitStand: true,
    175                         }
    176                     }))
    177                 }
    178 
    179                 if (parseInt(res.data?.status.toString().substr(1, 1)) == 3) {
    180                     dispatch(setBlackjack({
    181                         ...styleState.blackjack,
    182                         inputControls: {
    183                             ...styleState.blackjack.inputControls,
    184                             initialBet: {
    185                                 ...styleState.blackjack.inputControls.initialBet,
    186                                 chosenCredits: parseInt(res.data?.initialBet),
    187                             },
    188                             sideBet: {
    189                                 ...styleState.blackjack.inputControls.sideBet,
    190                                 chosenCredits: parseInt(res.data?.sideBet),
    191                             }
    192                         },
    193                         displays: {
    194                             ...styleState.blackjack.displays,
    195                             initialBet: false,
    196                             hitStand: true,
    197                         }
    198                     }))
    199                 }
    200 
    201                 if (parseInt(res.data?.status.toString().substr(1, 1)) == 2) {
    202                     dispatch(setBlackjack({
    203                         ...styleState.blackjack,
    204                         inputControls: {
    205                             ...styleState.blackjack.inputControls,
    206                             initialBet: {
    207                                 ...styleState.blackjack.inputControls.initialBet,
    208                                 chosenCredits: parseInt(res.data?.initialBet),
    209                             }
    210                         },
    211                         displays: {
    212                             ...styleState.blackjack.displays,
    213                             initialBet: false,
    214                             sideBet: true,
    215                         }
    216                     }))
    217                 }
    218 
    219                 if (parseInt(res.data?.status.toString().substr(1, 1)) == 1) {
    220                     dispatch(setBlackjack({
    221                         ...styleState.blackjack,
    222                         inputControls: {
    223                             ...styleState.blackjack.inputControls,
    224                             initialBet: {
    225                                 ...styleState.blackjack.inputControls.initialBet,
    226                                 chosenCredits: parseInt(res.data?.credits/2),
    227                             }
    228                         },
    229                     }))
    230                 }
     115                    });
     116                }, 1000);
    231117            }
    232118            else {
     
    244130            }
    245131        });
     132
     133        return () => {
     134            if (interval !== null) clearInterval(interval);
     135        };
    246136    }, []);
    247137
  • components/blackjack/DisplayBet.jsx

    r433e0c5 r55701f0  
    66  const playerState = useSelector(state => state.player);
    77  const styleState = useSelector(state => state.style);
    8 
    98  let display = parseInt(playerState.blackjackGame.status.substr(1, 1)) >= 2 ? 'block' : 'none';
    10   let displaySideBet = ( parseInt(playerState.blackjackGame.status.substr(1, 1)) >= 3 && parseInt(styleState.blackjack.inputControls.sideBet.chosenCredits) > 0 ) ? 'block' : 'none';
     9  let displaySideBet = ( parseInt(playerState.blackjackGame.status.substr(1, 1)) >= 3 && parseInt(playerState.blackjackGame.sideBet) > 0 && !playerState.blackjackGame.sideBetOutcomeMessageShown ) ? 'block' : 'none';
    1110
    1211  return (
    1312    <div className="blackjackDisplayBet" style={{display: display}}>
    14       <span>${styleState.blackjack.inputControls.initialBet.chosenCredits}{displaySideBet === 'block' ? ` + $${styleState.blackjack.inputControls.sideBet.chosenCredits}` : ''}</span>
     13      <span>${playerState.blackjackGame.initialBet}{displaySideBet === 'block' ? ` + $${playerState.blackjackGame.sideBet}` : ''}</span>
    1514    </div>
    1615  )
  • components/blackjack/PlayButtons.jsx

    r433e0c5 r55701f0  
    4444
    4545    function placeInitialBetClicked() {
    46         axios.get(`/api/blackjack?action=make_initial_bet&session_id=${localStorage.CAESSINO_SESSION_ID}&bet=${styleState.blackjack.inputControls.initialBet.chosenCredits}`).then(res => {
    47             if (res.data?.success) {
    48                 dispatch(setBlackjackGame({
    49                     ...playerState.blackjackGame,
    50                     status: res.data?.status,
    51                 }))
    52 
    53                 dispatch(setPlayer({
    54                     ...playerState.player,
    55                     credits: res.data?.credits,
    56                 }))
    57 
    58                 dispatch(setBlackjack({
    59                     ...styleState.blackjack,
    60                     displays: {
    61                         ...styleState.blackjack.displays,
    62                         initialBet: false,
    63                         sideBet: true,
    64                     }
    65                 }))
    66             }
    67         });
     46        axios.get(`/api/blackjack?action=make_initial_bet&session_id=${localStorage.CAESSINO_SESSION_ID}&bet=${styleState.blackjack.inputControls.initialBet.chosenCredits}`);
    6847    }
    6948
     
    124103        const texts = splitTexts(e.target.innerHTML);
    125104
    126         dispatch(setBlackjackGame({
    127             ...playerState.blackjackGame,
    128             sideBetName: sideBetName
    129         }))
    130 
    131         dispatch(setBlackjack({
    132             ...styleState.blackjack,
     105        dispatch(setBlackjack({
     106            ...styleState.blackjack,
     107            sideBetName: sideBetName,
    133108            displays: {
    134109                ...styleState.blackjack.displays,
     
    145120
    146121    function placeSideBetClicked() {
    147         axios.get(`/api/blackjack?action=make_side_bet&session_id=${localStorage.CAESSINO_SESSION_ID}&bet=${styleState.blackjack.inputControls.sideBet.chosenCredits}&betName=${playerState.blackjackGame.sideBetName}`).then(res => {
     122        axios.get(`/api/blackjack?action=make_side_bet&session_id=${localStorage.CAESSINO_SESSION_ID}&bet=${styleState.blackjack.inputControls.sideBet.chosenCredits}&betName=${styleState.blackjack.sideBetName}`).then(res => {
    148123            if (res.data?.success) {
    149                 dispatch(setBlackjackGame({
    150                     ...playerState.blackjackGame,
    151                     status: res.data?.status,
    152                 }))
    153 
    154                 dispatch(setPlayer({
    155                     ...playerState.player,
    156                     credits: res.data?.credits,
    157                 }))
    158 
    159124                dispatch(setBlackjack({
    160125                    ...styleState.blackjack,
     
    162127                        ...styleState.blackjack.displays,
    163128                        sideBetsChooseCreditsModal: false,
    164                         sideBet: false,
    165                         hitStand: true,
    166129                    }
    167130                }))
     
    181144                        sideBetsModal: false,
    182145                        sideBetsChooseCreditsModal: false,
    183                         sideBet: false,
    184                         hitStand: true,
    185                     },
    186                     inputControls: {
    187                         ...styleState.blackjack.inputControls,
    188                         sideBet: {
    189                             ...styleState.blackjack.inputControls.sideBet,
    190                             chosenCredits: 0,
    191                         }
    192146                    },
    193147                }))
     
    199153
    200154    function getCards() {
    201         axios.get(`/api/blackjack?action=get_initial_cards&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
    202             if (res.data?.success) {
    203                 dispatch(setBlackjackGame({
    204                     ...playerState.blackjackGame,
    205                     status: res.data?.status,
    206                     playerCards: res.data?.playerCards,
    207                     dealerCards: res.data?.dealerCards,
    208                 }))
    209 
    210                 if (res.data?.sideBetOutcome !== '') {
    211                     if (res.data.sideBetOutcome === 'side_bet_won') {
    212                         dispatch(setBlackjackGame({
    213                             ...playerState.blackjackGame,
    214                             credits: res.data?.credits,
    215                         }))
    216                        
    217                         dispatch(setStyle({
    218                             ...styleState.style,
    219                             alert: {
    220                                 show: true,
    221                                 title: 'You won the side bet!',
    222                                 subtitle: `You won $${res.data?.sideBetEarnings}`,
    223                                 button: {
    224                                     text: 'Continue',
    225                                     action: 'continue_from_side_bet',
    226                                 }
    227                             }
    228                         }))
    229                     }
    230                     else if (res.data.sideBetOutcome === 'side_bet_lost') {
    231                         dispatch(setStyle({
    232                             ...styleState.style,
    233                             alert: {
    234                                 show: true,
    235                                 title: 'You lost the side bet!',
    236                                 subtitle: `You lost $${-1*res.data?.sideBetEarnings}`,
    237                                 button: {
    238                                     text: 'Continue',
    239                                     action: 'continue_from_side_bet',
    240                                 }
    241                             }
    242                         }))
    243                     }
    244                 }
    245             }
    246         });
     155        axios.get(`/api/blackjack?action=get_initial_cards&session_id=${localStorage.CAESSINO_SESSION_ID}`);
    247156    }
    248157
     
    282191
    283192    function standClicked() {
    284         axios.get(`/api/blackjack?action=stand&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
    285             if (res.data?.success) {
    286                 dispatch(setBlackjackGame({
    287                     ...playerState.blackjackGame,
    288                     status: res.data?.status,
    289                     playerCards: res.data?.playerCards,
    290                     dealerCards: res.data?.dealerCards,
    291                 }))
    292 
    293                 if (res.data?.status.toString().substr(1, 1) === '5') { // game_over
    294                     dispatch(setPlayer({
    295                         ...playerState.player,
    296                         credits: res.data?.credits,
    297                     }))
    298                    
    299                     if (res.data?.outcome === 'dealer_busted') {
    300                         dispatch(setStyle({
    301                             ...styleState.style,
    302                             alert: {
    303                                 show: true,
    304                                 title: 'Dealer busted!',
    305                                 subtitle: `You won $${res.data?.earnings}`,
    306                                 button: {
    307                                     text: 'Play again',
    308                                     action: 'play_again',
    309                                 }
    310                             }
    311                         }))
    312                     }
    313                     else if (res.data?.outcome === 'player_won') {
    314                         dispatch(setStyle({
    315                             ...styleState.style,
    316                             alert: {
    317                                 show: true,
    318                                 title: 'You won!',
    319                                 subtitle: `You won $${res.data?.earnings}`,
    320                                 button: {
    321                                     text: 'Play again',
    322                                     action: 'play_again',
    323                                 }
    324                             }
    325                         }))
    326                     }
    327                     else if (res.data?.outcome === 'player_lost') {
    328                         dispatch(setStyle({
    329                             ...styleState.style,
    330                             alert: {
    331                                 show: true,
    332                                 title: 'You lost!',
    333                                 subtitle: `You lost $${-1*res.data?.earnings}`,
    334                                 button: {
    335                                     text: 'Play again',
    336                                     action: 'play_again',
    337                                 }
    338                             }
    339                         }))
    340                     }
    341                     else if (res.data?.outcome === 'draw') {
    342                         dispatch(setStyle({
    343                             ...styleState.style,
    344                             alert: {
    345                                 show: true,
    346                                 title: 'Draw!',
    347                                 subtitle: `You got your $${res.data?.earnings} back`,
    348                                 button: {
    349                                     text: 'Play again',
    350                                     action: 'play_again',
    351                                 }
    352                             }
    353                         }))
    354                     }
    355                 }
    356             }
    357         });
     193        axios.get(`/api/blackjack?action=stand&session_id=${localStorage.CAESSINO_SESSION_ID}`);
    358194    }
    359195
    360196    return (
    361197        <div className="blackjackButtons">
    362             <div className="blackjackInitialBet" style={{display: styleState.blackjack.displays.initialBet ? 'flex' : 'none'}}>
     198            <div className="blackjackInitialBet" style={{display: playerState.blackjackGame.status.includes('_1_') ? 'flex' : 'none'}}>
    363199                <div>
    364200                    <input type="range" className="primarySlider" min={0} max={playerState.player.credits} step={1} value={styleState.blackjack.inputControls.initialBet.chosenCredits} onChange={(e) => chooseCoins(e)} />
     
    369205                <button className="primaryButton" onClick={() => placeInitialBetClicked()}>Place Initial Bet <GiTwoCoins style={{marginTop: '3px', marginBottom: '-3px'}} /></button>
    370206            </div>
    371             <div className="blackjackSideBet" style={{display: styleState.blackjack.displays.sideBet ? 'flex' : 'none'}}>
     207            <div className="blackjackSideBet" style={{display: playerState.blackjackGame.status.includes('_2_') ? 'flex' : 'none'}}>
    372208                <button className="primaryButton" onClick={() => placeSideBetsClicked()}>Place Side Bets <GiTwoCoins style={{marginTop: '3px', marginBottom: '-3px'}} /></button>
    373209                <button className="primaryButton" onClick={() => skipSideBetsClicked()}>Skip <GiReturnArrow style={{marginTop: '3px', marginBottom: '-3px', transform: 'rotateZ(-15deg)'}} /></button>
    374210            </div>
    375             <div className="blackjackHitStand" style={{display: styleState.blackjack.displays.hitStand ? 'flex' : 'none'}}>
     211            <div className="blackjackHitStand" style={{display: parseInt(playerState.blackjackGame.status.toString().substr(1, 1)) >= 3 ? 'flex' : 'none'}}>
    376212                <button className="primaryButton" onClick={() => hitClicked()}>Hit <GiCardDraw style={{marginTop: '3px', marginBottom: '-3px'}} /></button>
    377213                <button className="primaryButton" onClick={() => standClicked()}>Stand <AiFillCheckCircle style={{marginTop: '3px', marginBottom: '-3px'}} /></button>
  • components/poker/PokerHeader.jsx

    r433e0c5 r55701f0  
    8989            }
    9090        });
     91
     92        return () => {
     93            if (interval !== null) clearInterval(interval);
     94        };
    9195    }, [])
    9296
  • components/roulette/RouletteHeader.jsx

    r433e0c5 r55701f0  
    138138            }
    139139        });
     140
     141        return () => {
     142            if (interval !== null) clearInterval(interval);
     143        };
    140144    }, []);
    141145
  • 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
  • redux/reducers/playerSlice.js

    r433e0c5 r55701f0  
    1414        dealerCards: [],
    1515        sideBetName: '',
     16        betAmount: 0,
     17        sideBetAmount: 0,
    1618    },
    1719    rouletteGame: {
  • redux/reducers/styleSlice.js

    r433e0c5 r55701f0  
    104104            }
    105105        },
     106        sideBetName: '',
    106107        texts: {
    107108            sideBetsChooseCreditsText: '',
Note: See TracChangeset for help on using the changeset viewer.