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
Line 
1/* eslint-disable @next/next/no-html-link-for-pages */
2import React from 'react'
3
4import Link from 'next/link'
5
6import { useRouter } from 'next/router'
7
8import { AiOutlineArrowLeft } from 'react-icons/ai'
9
10import { useEffect } from 'react'
11import { useDispatch, useSelector } from 'react-redux'
12
13import { setBlackjackGame, setPlayer } from '../../redux/reducers/playerSlice'
14import { setBlackjack, setStyle } from '../../redux/reducers/styleSlice'
15
16import axios from 'axios';
17
18const BlackjackHeader = () => {
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
33 let interval = null;
34 axios.get(`/api/blackjack?action=get_player_info_on_enter&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
35 if (res.data?.success) {
36 dispatch(setPlayer({
37 ...playerState.player,
38 displayName: res.data?.displayName,
39 session_id: res.data?.session_id,
40 credits: res.data?.credits,
41 }))
42
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),
50 },
51 sideBet: {
52 ...styleState.blackjack.inputControls.sideBet,
53 chosenCredits: parseInt(playerState.player.credits/2),
54 }
55 },
56 }))
57
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
65 if (newRes.data?.blackjackGame?.credits !== playerState.player.credits && parseInt(newRes.data?.blackjackGame?.credits) >= 0) {
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 }))
72 }
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 => {
108 dispatch(setStyle({
109 ...styleState.style,
110 displayLoadingScreen: false,
111 lostConnectionInfo: {
112 show: true,
113 message: 'Game will resume upon reconnection to the server.'
114 }
115 }))
116 });
117 }, 1000);
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 });
133
134 return () => {
135 if (interval !== null) clearInterval(interval);
136 };
137 }, []);
138
139 return (
140 <header className="header">
141 <a href="/">
142 <h2>
143 <AiOutlineArrowLeft />
144 </h2>
145 </a>
146 <nav>
147 <ul>
148 <li>Hi, {playerState?.player?.displayName}</li>
149 <li>Balance: ${playerState?.player?.credits}</li>
150 </ul>
151 </nav>
152 </header>
153 )
154}
155
156export default BlackjackHeader
Note: See TracBrowser for help on using the repository browser.