[87614a5] | 1 | import React from 'react'
|
---|
| 2 |
|
---|
| 3 | import Link from 'next/link'
|
---|
| 4 |
|
---|
[64dc53b] | 5 | import { useEffect } from 'react'
|
---|
[87614a5] | 6 | import { useDispatch, useSelector } from 'react-redux'
|
---|
| 7 |
|
---|
| 8 | import { setPlayer } from '../redux/reducers/playerSlice'
|
---|
| 9 | import { setStyle } from '../redux/reducers/styleSlice'
|
---|
| 10 |
|
---|
| 11 | import axios from 'axios';
|
---|
| 12 |
|
---|
| 13 | const Header = () => {
|
---|
| 14 | const dispatch = useDispatch();
|
---|
| 15 |
|
---|
| 16 | const playerState = useSelector(state => state.player);
|
---|
| 17 | const styleState = useSelector(state => state.style);
|
---|
| 18 |
|
---|
| 19 | function register() {
|
---|
| 20 | dispatch(setStyle({
|
---|
| 21 | ...styleState.style,
|
---|
| 22 | displayRegisterScreen: true,
|
---|
[64dc53b] | 23 | registerScreenInfo: {
|
---|
| 24 | ...styleState.style.registerScreenInfo,
|
---|
| 25 | setFocus: true
|
---|
| 26 | }
|
---|
[87614a5] | 27 | }))
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | function login() {
|
---|
| 31 | dispatch(setStyle({
|
---|
| 32 | ...styleState.style,
|
---|
| 33 | displayLoginScreen: true,
|
---|
[64dc53b] | 34 | loginScreenInfo: {
|
---|
| 35 | ...styleState.style.loginScreenInfo,
|
---|
| 36 | setFocus: true
|
---|
| 37 | }
|
---|
[87614a5] | 38 | }))
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | function logout() {
|
---|
| 42 | axios.get(`/api/postgre?action=logout&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
|
---|
| 43 | if (res.data?.success) {
|
---|
| 44 | localStorage.removeItem('CAESSINO_SESSION_ID');
|
---|
| 45 | dispatch(setPlayer({
|
---|
| 46 | displayName: '',
|
---|
| 47 | username: '',
|
---|
| 48 | session_id: '',
|
---|
| 49 | room_id: '',
|
---|
| 50 | credits: 0,
|
---|
| 51 | }))
|
---|
| 52 | }
|
---|
| 53 | })
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | function showStats() {
|
---|
| 57 | axios.get(`/api/postgre?action=get_stats&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
|
---|
| 58 | if (res.data?.success) {
|
---|
| 59 | dispatch(setStyle({
|
---|
| 60 | ...styleState.style,
|
---|
| 61 | displayStatsScreen: true,
|
---|
| 62 | statsScreenInfo: {
|
---|
| 63 | money: {
|
---|
| 64 | ...styleState.style.statsScreenInfo.money,
|
---|
| 65 | earned: res.data.stats.money_earned,
|
---|
| 66 | },
|
---|
| 67 | blackjack: {
|
---|
| 68 | ...styleState.style.statsScreenInfo.blackjack,
|
---|
| 69 | wins: res.data.stats.blackjack_won_games,
|
---|
| 70 | },
|
---|
| 71 | roulette: {
|
---|
| 72 | ...styleState.style.statsScreenInfo.roulette,
|
---|
| 73 | wins: res.data.stats.roulette_won_games,
|
---|
| 74 | },
|
---|
| 75 | poker: {
|
---|
| 76 | ...styleState.style.statsScreenInfo.poker,
|
---|
| 77 | wins: res.data.stats.poker_won_games,
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }))
|
---|
| 81 | }
|
---|
| 82 | })
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | function manageCredits() {
|
---|
| 86 | dispatch(setStyle({
|
---|
| 87 | ...styleState.style,
|
---|
| 88 | displayManageCreditsScreen: true,
|
---|
| 89 | }))
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | useEffect(() => {
|
---|
| 93 | if (playerState.player.displayName === '') {
|
---|
| 94 | dispatch(setStyle({
|
---|
| 95 | ...styleState.style,
|
---|
| 96 | displayLoadingScreen: true
|
---|
| 97 | }));
|
---|
| 98 |
|
---|
| 99 | let success = false;
|
---|
| 100 |
|
---|
| 101 | if (localStorage?.CAESSINO_SESSION_ID && localStorage.CAESSINO_SESSION_ID.length > 0) {
|
---|
| 102 | axios.get(`/api/postgre?action=check_if_logged_in&session_id=${localStorage.CAESSINO_SESSION_ID}`).then(res => {
|
---|
| 103 | if (res.data?.success) {
|
---|
| 104 | success = true;
|
---|
| 105 |
|
---|
| 106 | dispatch(setPlayer({
|
---|
| 107 | ...playerState.player,
|
---|
| 108 | displayName: res.data?.displayName,
|
---|
| 109 | sesssion_id: res.data?.session_id,
|
---|
| 110 | credits: res.data?.credits,
|
---|
| 111 | }));
|
---|
| 112 | }
|
---|
| 113 | });
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | if (!success) {
|
---|
| 117 | dispatch(setPlayer({
|
---|
| 118 | ...playerState.player,
|
---|
| 119 | displayName: 'Guest',
|
---|
| 120 | }))
|
---|
| 121 | dispatch(setStyle({
|
---|
| 122 | ...styleState.style,
|
---|
| 123 | displayLoadingScreen: false,
|
---|
| 124 | }))
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }, [dispatch, playerState.player, styleState.style]);
|
---|
| 128 |
|
---|
| 129 | return (
|
---|
| 130 | <header className="header">
|
---|
| 131 | <Link href="/" passHref>
|
---|
| 132 | <div className="logo">
|
---|
| 133 |
|
---|
| 134 | </div>
|
---|
| 135 | </Link>
|
---|
[64dc53b] | 136 | <nav className='mainHeaderNavigation'>
|
---|
[87614a5] | 137 | <ul>
|
---|
| 138 | {playerState.player.displayName === '' || playerState.player.displayName === 'Guest' ? (
|
---|
| 139 | <>
|
---|
| 140 | <li onClick={() => {register()}}>Register</li>
|
---|
| 141 | <li onClick={() => {login()}}>Login</li>
|
---|
| 142 | </>
|
---|
| 143 | ) : (
|
---|
| 144 | <>
|
---|
| 145 | <li onClick={() => {manageCredits()}}>Manage Credits</li>
|
---|
| 146 | <li onClick={() => {showStats()}}>Stats</li>
|
---|
| 147 | <li onClick={() => {logout()}}>Logout</li>
|
---|
| 148 | </>
|
---|
| 149 | )}
|
---|
| 150 | </ul>
|
---|
| 151 | </nav>
|
---|
| 152 | </header>
|
---|
| 153 | )
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | export default Header |
---|