import Icon from '@mdi/react'; import axios from 'axios'; import copy from 'copy-to-clipboard'; import { CSSProperties, useContext, useEffect, useState } from 'react'; import * as cookies from 'react-cookies'; import { Socket } from 'socket.io-client'; import { SocketContext } from '../components/socketContext'; import { DialogBox } from '../components/dialogBox'; import { GameBar } from '../components/gameBar'; import { CurrentGameSettings } from '../components/gameSettings'; import { NavBar } from '../components/navbar'; import { CenteredPage } from '../components/page'; import { ToastContext, toastType } from '../components/toast'; import { Button, IconLabelButton, SearchBar } from '../components/ui'; import { VoerBord } from '../components/voerBord'; import FlagOutlinedIcon from '@material-ui/icons/FlagOutlined'; import LinkRoundedIcon from '@material-ui/icons/LinkRounded'; import RefreshIcon from '@material-ui/icons/Refresh'; import WifiTetheringRoundedIcon from '@material-ui/icons/WifiTetheringRounded'; import { mdiContentCopy } from '@mdi/js'; function VoerGame(props: { gameID: string; active: boolean; player1: boolean; io: Socket; toast: toastType; }) { var width = 7; var height = 6; var [turn, setTurn] = useState(true); // var [winPositions, setWinPositions] = useState>>([]); var [outcome, setOutcome] = useState(-1); var board: Array = [...Array(width * height)].map(() => 0); useEffect(() => { props.io.on('connect', () => console.log('connect')); props.io.on('disconnect', () => console.log('disconnect')); props.io.on('fieldUpdate', (data: { field: string; }) => { board = data.field.split('').map(i => Number(i)); for (let i = 0; i < data.field.length; i++) { document.getElementById(`pos-${i}`).parentNode.children.item(1).classList.add(`state-${data.field[i]}`); } }); props.io.on('turnUpdate', (data: { player1: boolean; }) => setTurn(data.player1)); props.io.on('finish', (data: { winPositions: Array>; boardFull: boolean; winner: number; }) => { // setWinPositions(data.winPositions); if (data.boardFull) setOutcome(0); if (data.winPositions.length > 0) setOutcome(board[data.winPositions[0][0]]); }); props.io.on('resign', () => { props.toast({ message: 'Het potje is opgegeven', type: 'normal', icon: }); }); }, []); return
{ props.io.emit('newMove', { move: move % width + 1, token: cookies.load('token'), // TODO: get token from request game_id: props.gameID, }); }} active={props.active && outcome == -1} /> { props.io.emit('resign', { game_id: props.gameID }); }} />
; } function GameOutcomeDialog(props: { outcome: number; player: number; visible: boolean; }) { return { window.history.replaceState(null, null, '/'); window.location.reload(); }} >

{props.outcome == 0 ? 'Gelijkspel' : props.outcome == props.player ? 'Verloren' : props.outcome != props.player ? 'Gewonnen' : '???'}

{false &&

0 Gemiste winstzetten
6 Optimale zetten
0 Blunders

} {false && } style={{ float: 'none', marginTop: 24, padding: '12px 32px', }} />}
; } var InviteButtonStyle: CSSProperties = { backgroundColor: 'var(--page-background)', height: 160, padding: 12, }; var InviteButtonIconStyle: CSSProperties = { fontSize: 100, position: 'absolute', top: 12, left: '50%', transform: 'translateX(-50%)', }; var InviteButtonLabelStyle: CSSProperties = { position: 'absolute', bottom: 12, left: '50%', transform: 'translateX(-50%)', textAlign: 'center', color: 'var(--text-alt)', width: 136, fontSize: 20, userSelect: 'none', }; export default function GamePage() { var [gameID, setGameID] = useState(''); var [player1, setPlayer1] = useState(true); var [active, setActive] = useState(false); var [gameIDUrl, setGameIDUrl] = useState(''); var { io } = useContext(SocketContext); var { toast } = useContext(ToastContext); useEffect(() => { var gameIDUrl = new URLSearchParams(window.location.search).get('id') || ''; setGameIDUrl(gameIDUrl); if (!gameIDUrl || gameIDUrl == gameID) return; axios.request<{ id: string; player_1: boolean; game_started: boolean; }>({ method: 'post', url: '/api/game/accept', headers: { 'content-type': 'application/json' }, data: { id: gameIDUrl }, }) .then(response => { setGameID(response.data.id); setPlayer1(response.data.player_1); io.emit('registerGameListener', { game_id: response.data.id }); setActive(true); }) .catch(err => { toast({ message: 'error', type: 'confirmation', description: err.toString() }); }); setGameID(gameIDUrl); }, []); useEffect(() => { io.on('gameStart', () => setActive(true)); }, []); return
{ window.history.go(-1); }} >
; }