source: components/blackjack/BlackjackHeader.jsx@ 95ce58b

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

Roulette place a bet functionality

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