source: components/blackjack/BlackjackHeader.jsx@ faff334

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

Added an admin panel, and the admin can now answer complaints

  • 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 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 }))
71 }
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 }))
105 }
106 }).catch(error => {
107 dispatch(setStyle({
108 ...styleState.style,
109 displayLoadingScreen: false,
110 lostConnectionInfo: {
111 show: true,
112 message: 'Game will resume upon reconnection to the server.'
113 }
114 }))
115 });
116 }, 1000);
117 }
118 else {
119 dispatch(setStyle({
120 ...styleState.style,
121 notification: {
122 show: true,
123 text: 'Please login in order to play blackjack.',
124 status: 'error',
125 },
126 displayLoadingScreen: false,
127 }))
128
129 router.push('/');
130 }
131 });
132
133 return () => {
134 if (interval !== null) clearInterval(interval);
135 };
136 }, []);
137
138 return (
139 <header className="header">
140 <Link href="/" passHref>
141 <h2>
142 <AiOutlineArrowLeft />
143 </h2>
144 </Link>
145 <nav>
146 <ul>
147 <li>Hi, {playerState?.player?.displayName}</li>
148 <li>Balance: ${playerState?.player?.credits}</li>
149 </ul>
150 </nav>
151 </header>
152 )
153}
154
155export default BlackjackHeader
Note: See TracBrowser for help on using the repository browser.