source: components/poker/PokerHeader.jsx@ 3a783f2

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

Finished poker and added ball to roulette

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[9bd09b0]1import React from 'react'
2
3import Link from 'next/link'
4
[b13f93b]5import { useRouter } from 'next/router'
6
[9bd09b0]7import { AiOutlineArrowLeft } from 'react-icons/ai'
8
[b13f93b]9import { useEffect } from 'react'
[9bd09b0]10import { useDispatch, useSelector } from 'react-redux'
11
[b13f93b]12import { setPlayer, setPokerGame, setSocket } from '../../redux/reducers/playerSlice'
13import { setStyle } from '../../redux/reducers/styleSlice'
14
15import axios from 'axios';
16
[9bd09b0]17const PokerHeader = () => {
[b13f93b]18 const dispatch = useDispatch();
19
20 const router = useRouter();
21
[9bd09b0]22 const playerState = useSelector(state => state.player);
23 const styleState = useSelector(state => state.style);
24
[3a783f2]25 useEffect(() => {
[b13f93b]26 // display loading screen
27 dispatch(setStyle({
28 ...styleState.style,
29 displayLoadingScreen: true,
30 }));
31
[3a783f2]32 let interval;
[b13f93b]33
34 axios.get(`/api/poker?action=get_player_info_on_enter&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
35 if (res.data?.success) {
36 dispatch(setPlayer({
37 ...playerState.player,
38 displayName: res.data?.displayName,
39 session_id: res.data?.session_id,
40 credits: res.data?.credits,
41 }));
42
43 dispatch(setStyle({
44 ...styleState.style,
45 displayLoadingScreen: false,
46 }))
[3a783f2]47
48 interval = setInterval(() => {
49 axios.get(`/api/poker?action=update_state&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(newRes => {
50 if (newRes.data?.success) {
51 dispatch(setPokerGame(newRes.data?.pokerGame))
52
53 if (newRes.data?.pokerGame?.player?.credits !== playerState.player.credits && newRes.data?.pokerGame?.player?.credits > 0) {
54 dispatch(setPlayer({
55 ...playerState.player,
56 credits: newRes.data?.pokerGame?.player?.credits,
57 }))
58 }
59 }
60 });
61 }, 2000);
[b13f93b]62 }
63 else {
64 dispatch(setStyle({
65 ...styleState.style,
66 notification: {
67 show: true,
68 text: 'Please login in order to play poker.',
69 status: 'error',
70 },
71 displayLoadingScreen: false,
72 }))
73
74 router.push('/');
75 }
76 });
[3a783f2]77 }, [])
78
79 function leaveTable() {
80 axios.get(`/api/poker?action=leave_table&session_id=${localStorage.CAESSINO_SESSION_ID}`);
81 }
[b13f93b]82
[9bd09b0]83 return (
84 <header className="header">
[3a783f2]85 <div style={{display: 'flex', alignItems: 'center'}}>
86 <Link href="/" passHref>
87 <h2>
88 <AiOutlineArrowLeft />
89 </h2>
90 </Link>
91 { playerState.pokerGame.player.table.length > 0 && <button style={{marginBottom: '4px', marginLeft: '32px', fontSize: '16px'}} className="tertiaryButton" onClick={() => leaveTable()}>Leave Table</button> }
92 </div>
[9bd09b0]93 <nav>
94 <ul>
95 <li>Hi, {playerState?.player?.displayName}</li>
96 <li>Balance: ${playerState?.player?.credits}</li>
97 </ul>
98 </nav>
99 </header>
100 )
101}
102
103export default PokerHeader
Note: See TracBrowser for help on using the repository browser.