[87614a5] | 1 | import React from 'react'
|
---|
| 2 |
|
---|
| 3 | import Link from 'next/link'
|
---|
| 4 |
|
---|
| 5 | import { useRouter } from 'next/router'
|
---|
| 6 |
|
---|
| 7 | import { AiOutlineArrowLeft } from 'react-icons/ai'
|
---|
| 8 |
|
---|
[64dc53b] | 9 | import { useEffect } from 'react'
|
---|
[87614a5] | 10 | import { useDispatch, useSelector } from 'react-redux'
|
---|
| 11 |
|
---|
[9bd09b0] | 12 | import { setBlackjackGame, setPlayer } from '../../redux/reducers/playerSlice'
|
---|
[64dc53b] | 13 | import { setBlackjack, setStyle } from '../../redux/reducers/styleSlice'
|
---|
[87614a5] | 14 |
|
---|
| 15 | import axios from 'axios';
|
---|
| 16 |
|
---|
[64dc53b] | 17 | const BlackjackHeader = () => {
|
---|
[87614a5] | 18 | const dispatch = useDispatch();
|
---|
| 19 |
|
---|
| 20 | const router = useRouter();
|
---|
| 21 |
|
---|
| 22 | const playerState = useSelector(state => state.player);
|
---|
| 23 | const styleState = useSelector(state => state.style);
|
---|
| 24 |
|
---|
| 25 | useEffect(() => {
|
---|
| 26 | // display loading screen
|
---|
| 27 | dispatch(setStyle({
|
---|
| 28 | ...styleState.style,
|
---|
| 29 | displayLoadingScreen: true,
|
---|
| 30 | }));
|
---|
| 31 |
|
---|
[55701f0] | 32 | let interval = null;
|
---|
[285c3cc] | 33 | axios.get(`/api/blackjack?action=get_player_info_on_enter&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
|
---|
| 34 | if (res.data?.success) {
|
---|
[87614a5] | 35 | dispatch(setPlayer({
|
---|
| 36 | ...playerState.player,
|
---|
[285c3cc] | 37 | displayName: res.data?.displayName,
|
---|
| 38 | session_id: res.data?.session_id,
|
---|
| 39 | credits: res.data?.credits,
|
---|
| 40 | }))
|
---|
| 41 |
|
---|
[55701f0] | 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),
|
---|
[285c3cc] | 49 | },
|
---|
[55701f0] | 50 | sideBet: {
|
---|
| 51 | ...styleState.blackjack.inputControls.sideBet,
|
---|
| 52 | chosenCredits: parseInt(playerState.player.credits/2),
|
---|
[285c3cc] | 53 | }
|
---|
[55701f0] | 54 | },
|
---|
| 55 | }))
|
---|
[285c3cc] | 56 |
|
---|
[55701f0] | 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) {
|
---|
[e007fcd] | 62 | console.log(newRes.data);
|
---|
| 63 |
|
---|
[55701f0] | 64 | dispatch(setBlackjackGame(newRes.data?.blackjackGame))
|
---|
| 65 |
|
---|
| 66 | if (newRes.data?.blackjackGame?.credits !== playerState.player.credits && parseInt(newRes.data?.blackjackGame?.credits) > 0) {
|
---|
| 67 | dispatch(setPlayer({
|
---|
| 68 | ...playerState.player,
|
---|
| 69 | displayName: res.data?.displayName,
|
---|
| 70 | session_id: res.data?.session_id,
|
---|
| 71 | credits: newRes.data?.blackjackGame?.credits,
|
---|
| 72 | }))
|
---|
[87614a5] | 73 | }
|
---|
[55701f0] | 74 |
|
---|
| 75 | dispatch(setStyle({
|
---|
| 76 | ...styleState.style,
|
---|
| 77 | displayLoadingScreen: false,
|
---|
| 78 | notification: {
|
---|
| 79 | ...styleState.style.notification,
|
---|
| 80 | show: false,
|
---|
| 81 | },
|
---|
| 82 | lostConnectionInfo: {
|
---|
| 83 | show: false,
|
---|
| 84 | message: ''
|
---|
| 85 | },
|
---|
| 86 | alert: (newRes.data?.blackjackGame?.status?.includes('_5_') && !newRes.data?.blackjackGame?.betOutcomeMessageShown) ? {
|
---|
| 87 | show: true,
|
---|
| 88 | title: newRes.data?.blackjackGame?.messageTitle,
|
---|
| 89 | subtitle: newRes.data?.blackjackGame?.messageDescription,
|
---|
| 90 | button: {
|
---|
| 91 | text: 'Play Again',
|
---|
| 92 | action: 'play_again',
|
---|
| 93 | }
|
---|
| 94 | } : (newRes.data?.blackjackGame?.status?.includes('_4_') && !newRes.data?.blackjackGame?.sideBetOutcomeMessageShown && newRes.data?.blackjackGame?.sideBet > 0) ? {
|
---|
| 95 | show: true,
|
---|
| 96 | title: newRes.data?.blackjackGame?.messageTitle,
|
---|
| 97 | subtitle: newRes.data?.blackjackGame?.messageDescription,
|
---|
| 98 | button: {
|
---|
| 99 | text: 'Continue',
|
---|
| 100 | action: 'continue_from_side_bet',
|
---|
| 101 | }
|
---|
| 102 | } : {
|
---|
| 103 | ...styleState.style.alert,
|
---|
| 104 | show: false,
|
---|
| 105 | },
|
---|
| 106 | }))
|
---|
| 107 | }
|
---|
| 108 | }).catch(error => {
|
---|
[285c3cc] | 109 | dispatch(setStyle({
|
---|
| 110 | ...styleState.style,
|
---|
[55701f0] | 111 | displayLoadingScreen: false,
|
---|
| 112 | lostConnectionInfo: {
|
---|
[285c3cc] | 113 | show: true,
|
---|
[55701f0] | 114 | message: 'Game will resume upon reconnection to the server.'
|
---|
[87614a5] | 115 | }
|
---|
[285c3cc] | 116 | }))
|
---|
[55701f0] | 117 | });
|
---|
| 118 | }, 1000);
|
---|
[87614a5] | 119 | }
|
---|
| 120 | else {
|
---|
| 121 | dispatch(setStyle({
|
---|
| 122 | ...styleState.style,
|
---|
| 123 | notification: {
|
---|
| 124 | show: true,
|
---|
| 125 | text: 'Please login in order to play blackjack.',
|
---|
| 126 | status: 'error',
|
---|
| 127 | },
|
---|
| 128 | displayLoadingScreen: false,
|
---|
| 129 | }))
|
---|
| 130 |
|
---|
| 131 | router.push('/');
|
---|
| 132 | }
|
---|
| 133 | });
|
---|
[55701f0] | 134 |
|
---|
| 135 | return () => {
|
---|
| 136 | if (interval !== null) clearInterval(interval);
|
---|
| 137 | };
|
---|
[87614a5] | 138 | }, []);
|
---|
| 139 |
|
---|
| 140 | return (
|
---|
| 141 | <header className="header">
|
---|
| 142 | <Link href="/" passHref>
|
---|
| 143 | <h2>
|
---|
| 144 | <AiOutlineArrowLeft />
|
---|
| 145 | </h2>
|
---|
| 146 | </Link>
|
---|
| 147 | <nav>
|
---|
| 148 | <ul>
|
---|
[ebf5e04] | 149 | <li>Hi, {playerState?.player?.displayName}</li>
|
---|
[87614a5] | 150 | <li>Balance: ${playerState?.player?.credits}</li>
|
---|
| 151 | </ul>
|
---|
| 152 | </nav>
|
---|
| 153 | </header>
|
---|
| 154 | )
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[64dc53b] | 157 | export default BlackjackHeader |
---|