source: components/blackjack/BlackjackHeader.jsx@ e007fcd

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

Now you need to activate your account via email & also added mail sending after server crash

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