source: components/blackjack/BlackjackHeader.jsx@ 22367db

main
Last change on this file since 22367db was 22367db, checked in by anastasovv <simon@…>, 2 years ago

Added authentication with google

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