diff --git a/packages/play-node-go/src/io.js b/packages/play-node-go/src/io.js index 7bc8d7c..0e4b9a7 100644 --- a/packages/play-node-go/src/io.js +++ b/packages/play-node-go/src/io.js @@ -1,43 +1,48 @@ -import socketIOClient from 'socket.io-client'; -import config from './config'; +import socketIOClient from "socket.io-client"; +import config from "./config"; const launch = (nsp, dispatch) => { const socket = socketIOClient(`${config.socketAddress}/${nsp}`); - - socket.on('connected', () => { - dispatch({ type:'SOCKET', message:'CONNECTED', body:{nsp: socket.nsp} }); - }); - socket.on('connect_error', err => { - dispatch({ type: 'ERR', message:'SOCKET_ERROR', body: { socketError: err }}); - }); - socket.on('error', err => { - dispatch({ type: 'ERR', message:'SOCKET_ERROR', body: { socketError: err } }); - }); - - socket.on('room_connected', (data) => { - dispatch({ type: 'ROOMS', message: 'CONNECT_ROOM', body: data }); - }); - - socket.on('new_user', (data) => { - console.log('new_user received') - dispatch({ type: 'ROOMS', message: 'NEW_USER', body: data }) - }) - socket.on('game_connected', (data) => { - console.log(data) - console.log('game_connected received') - dispatch({ type: 'GAMES', message: 'UPDATE_BOARD', body: data }) - }) - - socket.on('update_board', (data) => { - console.log(data) - console.log('update_board received') - dispatch({ type: 'GAMES', message: 'UPDATE_BOARD', body: data }) - }) + socket.on("connected", () => { + dispatch({ + type: "SOCKET", + message: "CONNECTED", + body: { nsp: socket.nsp }, + }); + }); + socket.on("connect_error", (err) => { + dispatch({ + type: "ERR", + message: "SOCKET_ERROR", + body: { socketError: err }, + }); + }); + socket.on("error", (err) => { + dispatch({ + type: "ERR", + message: "SOCKET_ERROR", + body: { socketError: err }, + }); + }); + + socket.on("room_connected", (data) => { + dispatch({ type: "ROOMS", message: "CONNECT_ROOM", body: data }); + }); + + socket.on("new_user", (data) => { + dispatch({ type: "ROOMS", message: "NEW_USER", body: data }); + }); + + socket.on("game_connected", (data) => { + dispatch({ type: "GAMES", message: "UPDATE_BOARD", body: data }); + }); + + socket.on("update_board", (data) => { + dispatch({ type: "GAMES", message: "UPDATE_BOARD", body: data }); + }); return socket; -} +}; -export { - launch -} \ No newline at end of file +export { launch }; diff --git a/packages/play-node-go/src/pages/Game/Game.js b/packages/play-node-go/src/pages/Game/Game.js index 7072105..e366152 100644 --- a/packages/play-node-go/src/pages/Game/Game.js +++ b/packages/play-node-go/src/pages/Game/Game.js @@ -11,6 +11,8 @@ const Game = (props) => { const gameId = parseInt(useParams().id) || 0; const [playerBlackMeta, setPlayerBlackMeta] = useState({}); const [playerWhiteMeta, setPlayerWhiteMeta] = useState({}); + const playerState = state?.meta?.playerState; + const game = state.active?.game; useEffect(() => { const fetchGameAPI = async () => { @@ -42,14 +44,9 @@ const Game = (props) => { }, [state.active.game, dispatch, state.user]); useEffect(() => { - if (!state.meta) return; - const { - playerBlack, - playerBlackRank, - playerWhite, - playerWhiteRank, - } = state.active.game; - const { bCaptures, wCaptures } = state.meta.playerState; + if (!game || !playerState) return; + const { playerBlack, playerBlackRank, playerWhite, playerWhiteRank } = game; + const { bCaptures, wCaptures } = playerState; setPlayerBlackMeta({ player: playerBlack, rank: playerBlackRank, @@ -62,7 +59,7 @@ const Game = (props) => { captures: wCaptures, stones: "white", }); - }, [state?.meta?.playerState, state.active?.game]); + }, [playerState, game]); return (