[9bd09b0] | 1 | import React from 'react'
|
---|
| 2 |
|
---|
| 3 | import Link from 'next/link'
|
---|
| 4 |
|
---|
[b13f93b] | 5 | import { useRouter } from 'next/router'
|
---|
| 6 |
|
---|
[9bd09b0] | 7 | import { AiOutlineArrowLeft } from 'react-icons/ai'
|
---|
| 8 |
|
---|
[b13f93b] | 9 | import { useEffect } from 'react'
|
---|
[9bd09b0] | 10 | import { useDispatch, useSelector } from 'react-redux'
|
---|
| 11 |
|
---|
[433e0c5] | 12 | import { setPlayer, setPokerGame } from '../../redux/reducers/playerSlice'
|
---|
[b13f93b] | 13 | import { setStyle } from '../../redux/reducers/styleSlice'
|
---|
| 14 |
|
---|
| 15 | import axios from 'axios';
|
---|
| 16 |
|
---|
[9bd09b0] | 17 | const 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 |
|
---|
[189cd8f] | 32 | let interval = null;
|
---|
[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) {
|
---|
[189cd8f] | 36 | if (interval !== null) clearInterval(interval);
|
---|
| 37 |
|
---|
[3a783f2] | 38 | interval = setInterval(() => {
|
---|
| 39 | axios.get(`/api/poker?action=update_state&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(newRes => {
|
---|
| 40 | if (newRes.data?.success) {
|
---|
| 41 | dispatch(setPokerGame(newRes.data?.pokerGame))
|
---|
| 42 |
|
---|
| 43 | if (newRes.data?.pokerGame?.player?.credits !== playerState.player.credits && newRes.data?.pokerGame?.player?.credits > 0) {
|
---|
| 44 | dispatch(setPlayer({
|
---|
| 45 | ...playerState.player,
|
---|
[433e0c5] | 46 | displayName: res.data?.displayName,
|
---|
| 47 | session_id: res.data?.session_id,
|
---|
[3a783f2] | 48 | credits: newRes.data?.pokerGame?.player?.credits,
|
---|
| 49 | }))
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
[433e0c5] | 52 |
|
---|
| 53 | dispatch(setStyle({
|
---|
| 54 | ...styleState.style,
|
---|
| 55 | displayLoadingScreen: false,
|
---|
| 56 | notification: {
|
---|
| 57 | ...styleState.style.notification,
|
---|
| 58 | show: false,
|
---|
| 59 | },
|
---|
| 60 | lostConnectionInfo: {
|
---|
| 61 | show: false,
|
---|
| 62 | message: ''
|
---|
| 63 | }
|
---|
| 64 | }))
|
---|
| 65 | }).catch(error => {
|
---|
| 66 | dispatch(setStyle({
|
---|
| 67 | ...styleState.style,
|
---|
| 68 | displayLoadingScreen: false,
|
---|
| 69 | lostConnectionInfo: {
|
---|
| 70 | show: true,
|
---|
| 71 | message: 'Game will be played until the end upon server gets live. You cannot continue your game, but the money earned / lost will be updated.'
|
---|
| 72 | }
|
---|
| 73 | }))
|
---|
[3a783f2] | 74 | });
|
---|
[433e0c5] | 75 | }, 1000);
|
---|
[b13f93b] | 76 | }
|
---|
| 77 | else {
|
---|
| 78 | dispatch(setStyle({
|
---|
| 79 | ...styleState.style,
|
---|
| 80 | notification: {
|
---|
| 81 | show: true,
|
---|
| 82 | text: 'Please login in order to play poker.',
|
---|
| 83 | status: 'error',
|
---|
| 84 | },
|
---|
| 85 | displayLoadingScreen: false,
|
---|
| 86 | }))
|
---|
| 87 |
|
---|
| 88 | router.push('/');
|
---|
| 89 | }
|
---|
| 90 | });
|
---|
[3a783f2] | 91 | }, [])
|
---|
| 92 |
|
---|
| 93 | function leaveTable() {
|
---|
| 94 | axios.get(`/api/poker?action=leave_table&session_id=${localStorage.CAESSINO_SESSION_ID}`);
|
---|
| 95 | }
|
---|
[b13f93b] | 96 |
|
---|
[9bd09b0] | 97 | return (
|
---|
| 98 | <header className="header">
|
---|
[3a783f2] | 99 | <div style={{display: 'flex', alignItems: 'center'}}>
|
---|
| 100 | <Link href="/" passHref>
|
---|
| 101 | <h2>
|
---|
| 102 | <AiOutlineArrowLeft />
|
---|
| 103 | </h2>
|
---|
| 104 | </Link>
|
---|
[189cd8f] | 105 | { playerState.pokerGame?.player?.table?.length > 0 && <button style={{marginBottom: '4px', marginLeft: '32px', fontSize: '16px'}} className="tertiaryButton" onClick={() => leaveTable()}>Leave Table</button> }
|
---|
[3a783f2] | 106 | </div>
|
---|
[9bd09b0] | 107 | <nav>
|
---|
| 108 | <ul>
|
---|
| 109 | <li>Hi, {playerState?.player?.displayName}</li>
|
---|
| 110 | <li>Balance: ${playerState?.player?.credits}</li>
|
---|
| 111 | </ul>
|
---|
| 112 | </nav>
|
---|
| 113 | </header>
|
---|
| 114 | )
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | export default PokerHeader |
---|