source: components/blackjack/BlackjackHeader.jsx@ b4369c8

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

Code cleanings

  • Property mode set to 100644
File size: 11.9 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 { setGame, 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 dispatch(setBlackjack({
33 ...styleState.blackjack,
34 inputControls: {
35 ...styleState.blackjack.inputControls,
36 initialBet: {
37 ...styleState.blackjack.inputControls.initialBet,
38 chosenCredits: parseInt(playerState.player.credits/2),
39 }
40 }
41 }));
42
43 axios.get(`/api/postgre?action=get_player_info_on_enter&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(postgreRes => {
44 if (postgreRes.data?.success) {
45 dispatch(setPlayer({
46 ...playerState.player,
47 displayName: postgreRes.data?.displayName,
48 session_id: postgreRes.data?.session_id,
49 credits: postgreRes.data?.credits,
50 }));
51
52 axios.get(`/api/blackjack?action=get_player_info_on_enter&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
53 if (res.data?.success) {
54 dispatch(setGame({
55 ...playerState.game,
56 status: res.data?.status,
57 playerCards: res.data?.playerCards,
58 dealerCards: res.data?.dealerCards,
59 }))
60
61 dispatch(setStyle({
62 ...styleState.style,
63 displayLoadingScreen: false,
64 }))
65
66 if (parseInt(res.data?.status.toString().substr(1, 1)) == 5) {
67 dispatch(setBlackjack({
68 ...styleState.blackjack,
69 inputControls: {
70 ...styleState.blackjack.inputControls,
71 initialBet: {
72 ...styleState.blackjack.inputControls.initialBet,
73 chosenCredits: parseInt(res.data?.initialBet),
74 },
75 sideBet: {
76 ...styleState.blackjack.inputControls.sideBet,
77 chosenCredits: 0,
78 }
79 },
80 displays: {
81 ...styleState.blackjack.displays,
82 initialBet: false,
83 hitStand: true,
84 }
85 }))
86
87 if (res.data?.outcome === 'player_busted') {
88 dispatch(setStyle({
89 ...styleState.style,
90 alert: {
91 show: true,
92 title: 'You busted!',
93 subtitle: `You lost $${-1*res.data?.earnings}`,
94 button: {
95 text: 'Play again',
96 action: 'play_again',
97 }
98 }
99 }))
100 }
101 else if (res.data?.outcome === 'dealer_busted') {
102 dispatch(setStyle({
103 ...styleState.style,
104 alert: {
105 show: true,
106 title: 'Dealer busted!',
107 subtitle: `You won $${res.data?.earnings}`,
108 button: {
109 text: 'Play again',
110 action: 'play_again',
111 }
112 }
113 }))
114 }
115 else if (res.data?.outcome === 'player_won') {
116 dispatch(setStyle({
117 ...styleState.style,
118 alert: {
119 show: true,
120 title: 'You won!',
121 subtitle: `You won $${res.data?.earnings}`,
122 button: {
123 text: 'Play again',
124 action: 'play_again',
125 }
126 }
127 }))
128 }
129 else if (res.data?.outcome === 'player_lost') {
130 dispatch(setStyle({
131 ...styleState.style,
132 alert: {
133 show: true,
134 title: 'You lost!',
135 subtitle: `You lost $${-1*res.data?.earnings}`,
136 button: {
137 text: 'Play again',
138 action: 'play_again',
139 }
140 }
141 }))
142 }
143 else if (res.data?.outcome === 'draw') {
144 dispatch(setStyle({
145 ...styleState.style,
146 alert: {
147 show: true,
148 title: 'Draw!',
149 subtitle: `You got your $${res.data?.earnings} back`,
150 button: {
151 text: 'Play again',
152 action: 'play_again',
153 }
154 }
155 }))
156 }
157 }
158
159 if (parseInt(res.data?.status.toString().substr(1, 1)) == 4) {
160 dispatch(setBlackjack({
161 ...styleState.blackjack,
162 inputControls: {
163 ...styleState.blackjack.inputControls,
164 initialBet: {
165 ...styleState.blackjack.inputControls.initialBet,
166 chosenCredits: parseInt(res.data?.initialBet),
167 },
168 sideBet: {
169 ...styleState.blackjack.inputControls.sideBet,
170 chosenCredits: 0,
171 }
172 },
173 displays: {
174 ...styleState.blackjack.displays,
175 initialBet: false,
176 hitStand: true,
177 }
178 }))
179 }
180
181 if (parseInt(res.data?.status.toString().substr(1, 1)) == 3) {
182 dispatch(setBlackjack({
183 ...styleState.blackjack,
184 inputControls: {
185 ...styleState.blackjack.inputControls,
186 initialBet: {
187 ...styleState.blackjack.inputControls.initialBet,
188 chosenCredits: parseInt(res.data?.initialBet),
189 },
190 sideBet: {
191 ...styleState.blackjack.inputControls.sideBet,
192 chosenCredits: parseInt(res.data?.sideBet),
193 }
194 },
195 displays: {
196 ...styleState.blackjack.displays,
197 initialBet: false,
198 hitStand: true,
199 }
200 }))
201 }
202
203 if (parseInt(res.data?.status.toString().substr(1, 1)) == 2) {
204 dispatch(setBlackjack({
205 ...styleState.blackjack,
206 inputControls: {
207 ...styleState.blackjack.inputControls,
208 initialBet: {
209 ...styleState.blackjack.inputControls.initialBet,
210 chosenCredits: parseInt(res.data?.initialBet),
211 }
212 },
213 displays: {
214 ...styleState.blackjack.displays,
215 initialBet: false,
216 sideBet: true,
217 }
218 }))
219 }
220
221 if (parseInt(res.data?.status.toString().substr(1, 1)) == 1) {
222 dispatch(setBlackjack({
223 ...styleState.blackjack,
224 inputControls: {
225 ...styleState.blackjack.inputControls,
226 initialBet: {
227 ...styleState.blackjack.inputControls.initialBet,
228 chosenCredits: parseInt(postgreRes.data?.credits/2),
229 }
230 },
231 }))
232 }
233 }
234 });
235 }
236 else {
237 dispatch(setStyle({
238 ...styleState.style,
239 notification: {
240 show: true,
241 text: 'Please login in order to play blackjack.',
242 status: 'error',
243 },
244 displayLoadingScreen: false,
245 }))
246
247 router.push('/');
248 }
249 });
250 }, []);
251
252 return (
253 <header className="header">
254 <Link href="/" passHref>
255 <h2>
256 <AiOutlineArrowLeft />
257 </h2>
258 </Link>
259 <nav>
260 <ul>
261 <li>Hi{playerState?.player?.displayName ? `, ${playerState.player.displayName}` : ``}</li>
262 <li>Balance: ${playerState?.player?.credits}</li>
263 </ul>
264 </nav>
265 </header>
266 )
267}
268
269export default BlackjackHeader
Note: See TracBrowser for help on using the repository browser.