-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
251 lines (210 loc) · 6.36 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* eslint-disable jsx-a11y/no-static-element-interactions */
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */
/* eslint-disable jsx-a11y/click-events-have-key-events */
import React, { useEffect, useState } from 'react';
import { RouletteTable, RouletteWheel } from 'react-casino-roulette';
import 'react-casino-roulette/dist/index.css';
import { getRandomInt } from './utills';
import { getRandomRouletteWinBet } from './helpers';
import whiteChip from '../public/images/chips/white-chip.png';
import blueChip from '../public/images/chips/blue-chip.png';
import blackChip from '../public/images/chips/black-chip.png';
import cyanChip from '../public/images/chips/cyan-chip.png';
import './App.css';
const API = {
getRandomBet: async () => {
return getRandomRouletteWinBet();
},
};
const chipsMap = {
whiteChip: {
icon: whiteChip,
value: 1,
},
blueChip: {
icon: blueChip,
value: 10,
},
blackChip: {
icon: blackChip,
value: 100,
},
cyanChip: {
icon: cyanChip,
value: 500,
},
};
const calcTotalBet = (bets) =>
Object.entries(bets).reduce((acc, [, value]) => acc + value.number, 0);
export const App = () => {
const [bets, setBets] = useState({});
const [betHistory, setBetHistory] = useState([]);
const [isDebug, setIsDebug] = useState(false);
const [activeChip, setActiveChip] = useState(Object.keys(chipsMap)[0]);
const [shouldShowData, setShouldShowData] = useState(false);
const [isRouletteWheelSpinning, setIsRouletteWheelSpinning] = useState(false);
const [rouletteWheelStart, setRouletteWheelStart] = useState(false);
const [rouletteWheelBet, setRouletteWheelBet] = useState('-1');
useEffect(() => {
const backgroundIndex = getRandomInt(0, 5);
const backgroundClass = `bg-${backgroundIndex}`;
document.body.classList.add(backgroundClass);
return () => {
document.body.classList.remove(backgroundClass);
};
}, []);
// you are here for
useEffect(() => {
if (rouletteWheelBet === '-1' || rouletteWheelStart === true) {
return;
}
setRouletteWheelStart(true);
}, [rouletteWheelBet, rouletteWheelStart]);
useEffect(() => {
if (isRouletteWheelSpinning === false) {
return;
}
const prepare = async () => {
const bet = await API.getRandomBet();
console.info('gotta win bet', bet);
setRouletteWheelStart(false);
setRouletteWheelBet(bet);
};
prepare();
}, [isRouletteWheelSpinning]);
const handleDoSpin = () => {
setIsRouletteWheelSpinning(true);
};
const handleEndSpin = () => {
setIsRouletteWheelSpinning(false);
};
// end you are here for
const undoLastBet = () => {
if (betHistory.length === 0) {
console.error('Nothing to undo');
return;
}
setBets((prevState) => {
const state = JSON.parse(JSON.stringify(prevState));
const lastBet = betHistory[betHistory.length - 1];
const prevIcon = betHistory[betHistory.length - 2]?.icon;
const { id: lastBetId, value } = lastBet;
if (state[lastBetId].number === 1) {
delete state[lastBetId];
return state;
}
state[lastBetId].icon = prevIcon;
state[lastBetId].number -= value;
return state;
});
setBetHistory((prevState) => prevState.slice(0, -1));
};
const cleanAllBets = () => {
setBetHistory([]);
setBets({});
};
const addBet = (id) => {
const { icon, value } = chipsMap[activeChip];
setBetHistory((prevState) => [...prevState, { id, icon, value }]);
setBets((prevState) => {
const state = JSON.parse(JSON.stringify(prevState));
if (state[id] !== undefined) {
state[id] = {
...state[id],
icon,
number: state[id].number + value,
};
return state;
}
state[id] = {
icon,
number: value,
};
return state;
});
};
const changeIsDebug = () => setIsDebug((prevState) => !prevState);
const handleOnBet = ({ bet, payload, id }) => {
console.info(
'handleOnBet',
`bet ===> ${bet}`,
'payload ===>',
payload,
'id ===> ',
id,
);
addBet(id);
};
const handleChipChange = (event) => {
const chipName = event.target.closest('[data-name]').dataset.name;
setActiveChip(chipName);
};
const handleShowData = () => {
setShouldShowData((prevState) => !prevState);
};
const totalBet = calcTotalBet(bets);
return (
<div>
<h1 className="heading">React Casino Roulette</h1>
<div className="roulette-wheel-wrapper">
<RouletteWheel
start={rouletteWheelStart}
winningBet={rouletteWheelBet}
onSpinningEnd={handleEndSpin}
/>
<div className="buttons">
<button
type="button"
disabled={isRouletteWheelSpinning}
onClick={handleDoSpin}
>
Let's go
</button>
{/* <button type="button" onClick={handleDoSpin}>
Spin
</button> */}
</div>
</div>
<div className="roulette-wrapper">
<RouletteTable onBet={handleOnBet} bets={bets} isDebug={isDebug} />
<div className="menu">
<ul className="chips">
{Object.entries(chipsMap).map(([name, { icon }]) => (
<li
key={name}
data-name={name}
className={activeChip === name ? 'active' : ''}
onClick={handleChipChange}
>
<img width={64} height={64} src={icon} alt="chip" />
</li>
))}
</ul>
<div className="score">
<p>Total bet: {totalBet}</p>
</div>
<div className="buttons">
<button type="button" onClick={undoLastBet}>
Undo
</button>
<button type="button" onClick={cleanAllBets}>
Clean
</button>
<button type="button" onClick={changeIsDebug}>
Debug
</button>
<button type="button" onClick={handleShowData}>
{shouldShowData === false ? 'Show' : 'Hide'} data
</button>
</div>
</div>
<div>
{shouldShowData === true && (
<p className="data">{JSON.stringify(bets, null, 2)}</p>
)}
</div>
<div style={{ height: 50 }} />
</div>
</div>
);
};