1 | import React from 'react'
|
---|
2 |
|
---|
3 | import { useEffect } from 'react';
|
---|
4 | import { useDispatch, useSelector } from 'react-redux'
|
---|
5 | import { setPlayer, setRouletteGame } from '../../redux/reducers/playerSlice';
|
---|
6 |
|
---|
7 | import axios from 'axios';
|
---|
8 | import { setRoulette, setStyle } from '../../redux/reducers/styleSlice';
|
---|
9 |
|
---|
10 | const Timer = () => {
|
---|
11 | const dispatch = useDispatch();
|
---|
12 |
|
---|
13 | const playerState = useSelector(state => state.player);
|
---|
14 | const styleState = useSelector(state => state.style);
|
---|
15 |
|
---|
16 | useEffect(() => {
|
---|
17 | let interval = setInterval(() => {
|
---|
18 | dispatch(setRouletteGame({
|
---|
19 | ...playerState.rouletteGame,
|
---|
20 | timeToStart: playerState.rouletteGame.timeToStart - 1,
|
---|
21 | }))
|
---|
22 |
|
---|
23 | if (playerState.rouletteGame.timeToStart === 0) {
|
---|
24 | axios.get(`/api/roulette?action=timer_done&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
|
---|
25 | if (res.data?.success) {
|
---|
26 | document.getElementById('rouletteWheelImg').classList.add('spin');
|
---|
27 |
|
---|
28 | setTimeout(() => {
|
---|
29 | document.getElementById('rouletteWheelImg').classList.remove('spin');
|
---|
30 |
|
---|
31 | const extraSpin = ( 5 + numbersOfWheel.indexOf(res.data?.magicNumber) * (360 / 37.0) ) + 'deg';
|
---|
32 | document.getElementById('rouletteWheelImg').style.transform = `translate(-50%, -50%) rotateZ(${extraSpin})`;
|
---|
33 |
|
---|
34 | setTimeout(() => {
|
---|
35 | dispatch(setStyle({
|
---|
36 | ...styleState.style,
|
---|
37 | alert: {
|
---|
38 | show: true,
|
---|
39 | title: `Winning number: ${res.data?.magicNumber}`,
|
---|
40 | subtitle: `Winning bets: [${res.data?.winningBets.join(', ')}]`,
|
---|
41 | button: {
|
---|
42 | text: 'Continue',
|
---|
43 | action: '',
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }))
|
---|
47 |
|
---|
48 | dispatch(setPlayer({
|
---|
49 | ...playerState.player,
|
---|
50 | credits: res.data?.credits,
|
---|
51 | }))
|
---|
52 |
|
---|
53 | dispatch(setRouletteGame(res.data?.game));
|
---|
54 | }, 600);
|
---|
55 | }, 4000);
|
---|
56 | }
|
---|
57 | });
|
---|
58 | }
|
---|
59 | else if (playerState.rouletteGame.timeToStart === 10) {
|
---|
60 | dispatch(setRoulette({
|
---|
61 | ...styleState.roulette,
|
---|
62 | displays: {
|
---|
63 | ...styleState.roulette.displays,
|
---|
64 | betModal: false,
|
---|
65 | }
|
---|
66 | }))
|
---|
67 | }
|
---|
68 | else if (playerState.rouletteGame.timeToStart === styleState.roulette.COUNTDOWN_FROM) {
|
---|
69 | axios.get(`/api/roulette?action=reset_game&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
|
---|
70 | if (res.data?.success) {
|
---|
71 | dispatch(setRouletteGame(res.data?.game));
|
---|
72 |
|
---|
73 | dispatch(setRoulette({
|
---|
74 | ...styleState.roulette,
|
---|
75 | showCoin: false,
|
---|
76 | }))
|
---|
77 | }
|
---|
78 | });
|
---|
79 | }
|
---|
80 | }, 1000);
|
---|
81 |
|
---|
82 | return () => clearInterval(interval);
|
---|
83 | }, [dispatch, playerState.rouletteGame.timeToStart, playerState.rouletteGame]);
|
---|
84 |
|
---|
85 | function getTimer() {
|
---|
86 | const timer = playerState.rouletteGame.timeToStart;
|
---|
87 |
|
---|
88 | const timeString = (timer < 0 || timer > styleState.roulette.COUNTDOWN_FROM - 2) ? '00:00' : '0' + parseInt(timer/60) + ':' + (timer%60 < 10 ? '0' : '') + (timer%60);
|
---|
89 |
|
---|
90 | return timeString;
|
---|
91 | }
|
---|
92 |
|
---|
93 | return (
|
---|
94 | <div className="rouletteTimer">
|
---|
95 | {playerState.rouletteGame.timeToStart > 0 && playerState.rouletteGame.timeToStart <= styleState.roulette.COUNTDOWN_FROM - 2 && <span style={{fontSize: '1rem'}}>Wheel will spin in:</span>}
|
---|
96 |
|
---|
97 | <span>{getTimer()}</span>
|
---|
98 |
|
---|
99 | {playerState.rouletteGame.timeToStart > 10 && playerState.rouletteGame.timeToStart <= styleState.roulette.COUNTDOWN_FROM - 2 && <span style={{fontSize: '1rem'}}>Make your bets.</span>}
|
---|
100 |
|
---|
101 | {playerState.rouletteGame.timeToStart <= 10 && <span style={{fontSize: '1rem'}}>Betting time is over.</span>}
|
---|
102 |
|
---|
103 | {playerState.rouletteGame.timeToStart > styleState.roulette.COUNTDOWN_FROM && <span style={{fontSize: '1rem'}}>Time to see who won :)</span>}
|
---|
104 | {playerState.rouletteGame.timeToStart > styleState.roulette.COUNTDOWN_FROM && <span style={{fontSize: '.7rem'}}>(And prepare, you can bet again soon.)</span>}
|
---|
105 | </div>
|
---|
106 | )
|
---|
107 | }
|
---|
108 |
|
---|
109 | // 10 starts from 5deg
|
---|
110 | const numbersOfWheel = [10, 23, 8, 30, 11, 36, 13, 27, 6, 34, 17, 25, 2, 21, 4, 19, 15, 32, 0, 26, 3, 35, 12, 28, 7, 29, 18, 22, 9, 31, 14, 20, 1, 33, 16, 24, 5];
|
---|
111 |
|
---|
112 | export default Timer |
---|