import React, { useContext, useEffect } from 'react'; import { GamesStateContext, GamesGuideContext } from '../context/games'; import { Routes, Route, Link } from 'react-router-dom'; import NewGame from './games/NewGame'; import { GameProposalSelector, ActiveGameSelector, GameArchiveSelector } from './games/GameSelector'; import { Create, Accept, Reject, Cancel, DrawRequest, DrawAccept, DrawReject, Surrender, Backward, Forward } from './games/ActionPanel'; import GameBoard from './games/GameBoard'; import { nextStone } from '../components/Checkers'; import Message2Opponent from './games/Message2Opponent'; import Counter from '../components/Counter'; import './Games.css'; export default function Games({ games, players }) { const gamesState = games.state; const gamesDispatchGuide = games.dispatchGuide; useEffect(() => { gamesDispatchGuide({ type: 'sync', gamesState }); }, [gamesState, gamesDispatchGuide]); return (
) }; function ViewSelector() { const guide = useContext(GamesGuideContext); return ( ) } function ViewProvider({ dispatchGuide, players }) { return (
dispatchGuide({ type: 'nextNewGame', opponentName, myColor })} /> } /> dispatchGuide({ type: 'selectedUUID', proposal: uuid })} /> } /> dispatchGuide({ type: 'selectedUUID', active: uuid })} /> } /> dispatchGuide({ type: 'selectedUUID', archive: uuid })} /> } />
) } function ActionPanel({ gamesApi }) { const games = useContext(GamesStateContext); const guide = useContext(GamesGuideContext); const fromUUID = (uuid) => (!uuid) ? [{}, null] : [ games.find((game) => game.uuid === uuid) || {}, // game guide.UUIDpushing[uuid] // pushing ]; return (
[guide.newGame, guide.newGame.isPushing]} onClick={(req) => gamesApi.pushNewGame(req)} /> } /> fromUUID(guide.selectedUUID.proposal)} onClick={(req) => gamesApi.pushGameProposalAccept(req)} />, fromUUID(guide.selectedUUID.proposal)} onClick={(req) => gamesApi.pushGameProposalReject(req)} />, fromUUID(guide.selectedUUID.proposal)} onClick={(req) => gamesApi.pushGameProposalCancel(req)} /> ]} /> fromUUID(guide.selectedUUID.active)} onClick={(req) => gamesApi.pushGameDrawRequest(req)} />, fromUUID(guide.selectedUUID.active)} onClick={(req) => gamesApi.pushGameDrawAccept(req)} />, fromUUID(guide.selectedUUID.active)} onClick={(req) => gamesApi.pushGameDrawReject(req)} />, fromUUID(guide.selectedUUID.active)} onClick={(req) => gamesApi.pushGameSurrender(req)} /> ]} /> , ]} />
) } function GameBoardRoutes({ dispatchGuide, gamesApi, username }) { const games = useContext(GamesStateContext); const guide = useContext(GamesGuideContext); const fromUUID = (uuid) => (!uuid) ? [{}, null, null] : [ games.find((game) => game.uuid === uuid) || {}, // game guide.UUIDpushing[uuid], // pushing guide.UUIDerror[uuid] // error (aka bad move) ]; const onStoneClick = (uuid, cellId) => { let board = { ...guide.newGame.board }; board[cellId] = nextStone(board[cellId]); dispatchGuide({ type: 'nextNewGame', board }); } return ( [guide.newGame, null]} onStoneClick={onStoneClick} /> } /> fromUUID(guide.selectedUUID.proposal)} /> } /> fromUUID(guide.selectedUUID.active)} onStoneMove={(uuid, move) => gamesApi.pushGameMove({ uuid, move, message: guide.UUIDmessage[uuid] })} dispatchGuide={dispatchGuide} /> } /> fromUUID(guide.selectedUUID.archive)} /> } /> ) } function Message2OpponentRoutes({ dispatchGuide }) { const guide = useContext(GamesGuideContext); const getMessage = (uuid) => !uuid ? undefined : // <<-- appears as inactive message field guide.UUIDmessage[uuid] || ''; return ( guide.newGame.message} setMessage={(message) => dispatchGuide({ type: 'nextNewGame', message })} /> } /> getMessage(guide.selectedUUID.active)} setMessage={(message) => dispatchGuide({ type: 'UUIDmessage', message, uuid: guide.selectedUUID.active })} /> } /> ) }