From 5651e1fa4c0a30eb6ddaae554fae376d85f84ea5 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Wed, 29 Jan 2020 23:22:50 -0800 Subject: [PATCH] stub game connection via socket room --- packages/server/play-node-go/src/io.js | 5 +++ .../play-node-go/src/pages/Game/Game.js | 43 ++++++------------- .../play-node-go/src/pages/Game/Game.test.js | 2 +- .../src/reducers/init/reducer.init.js | 34 ++++++++++++--- .../src/reducers/socket/reducer.socket.js | 25 ++++++++++- .../src/services/api/gamesServices.js | 19 ++++++++ 6 files changed, 89 insertions(+), 39 deletions(-) diff --git a/packages/server/play-node-go/src/io.js b/packages/server/play-node-go/src/io.js index 9be38ab..223386b 100644 --- a/packages/server/play-node-go/src/io.js +++ b/packages/server/play-node-go/src/io.js @@ -23,6 +23,11 @@ const launch = (nsp, dispatch) => { dispatch({ type: 'ROOMS', message: 'NEW_USER', body: data }) }) + socket.on('game_connected', (data) => { + console.log(data) + console.log('game_connected received') + }) + return socket; } diff --git a/packages/server/play-node-go/src/pages/Game/Game.js b/packages/server/play-node-go/src/pages/Game/Game.js index b63eb52..87dc882 100644 --- a/packages/server/play-node-go/src/pages/Game/Game.js +++ b/packages/server/play-node-go/src/pages/Game/Game.js @@ -10,12 +10,9 @@ import Development from '../../components/Display/Development/Development'; const Game = (props) => { const { state, dispatch } = props; const gameId = parseInt(useParams().id) || 0; - const [ socket, setSocket ] = useState(false) const fetchGameAPI = async () => { - console.log(gameId) const response = await gamesServices.getGameService(gameId); - console.log(response) if (response) { const action = { type: 'GAMES', @@ -32,36 +29,20 @@ const Game = (props) => { // ! [start] gameSocket - const roomSocketConnect = (roomSocket) => { - roomSocket.on('connect', socket => { - roomSocket.emit('joined game', gameId) - roomSocket.on('success', () => setSocket(true)) - }); - roomSocket.on('connect_error', err => { - setSocket(false) - console.log(err); - }); - roomSocket.on('error', err => { - setSocket(false) - console.log(err); - }); + const roomSocketConnect = () => { + const game = state.active.game; + const user = state.user; + const action = { + type: 'SOCKET', + message: 'CONNECT_GAME', + body: { game, user, dispatch } + } + return dispatch(action); } useEffect(() => { - if (!state.active) return; - const roomSocket = socketIOClient(`${config.socketAddress}/${state.active.game.room}`) - roomSocketConnect(roomSocket); - }, [state.active]) - - useEffect(() => { - const data = { - user: state.user, - game: state.joinGame - }; - console.log('emitting request') - console.log(data) - // socket.emit('join_game_request', data) - }, [state.joinGame]) + roomSocketConnect(); + }, [state.active] ) // ! [end] @@ -74,7 +55,7 @@ const Game = (props) => { {socket ? '✓' : ' ⃠'} + >{state.socket ? '✓' : ' ⃠'} diff --git a/packages/server/play-node-go/src/pages/Game/Game.test.js b/packages/server/play-node-go/src/pages/Game/Game.test.js index ca0b821..a3ef0d7 100644 --- a/packages/server/play-node-go/src/pages/Game/Game.test.js +++ b/packages/server/play-node-go/src/pages/Game/Game.test.js @@ -8,7 +8,7 @@ const state = initState(); test('renders Game without crashing', () => { const { getByTestId } = render( - + {}}/> ); const GameDiv = getByTestId('Game'); diff --git a/packages/server/play-node-go/src/reducers/init/reducer.init.js b/packages/server/play-node-go/src/reducers/init/reducer.init.js index 6346cdc..51cd2ac 100644 --- a/packages/server/play-node-go/src/reducers/init/reducer.init.js +++ b/packages/server/play-node-go/src/reducers/init/reducer.init.js @@ -5,22 +5,44 @@ const socket = require('../../io'); export const initState = (): state => { return { - user: { username: '', email: '', id: 0 }, + active: { + game: { + id: 0, room: 0, + mainTime: '', timePeriod: 0, periodLength: 0, overtime: '', overtimePeriod: 0, overtimeLength: 0, + application: '', applicationVersion: '', open: false, description: null, + event: null, round: null, name: null, + winType: null, capturesBlack: null, capturesWhite: null, score: null, + boardSize: 0, komi: 0.0, handicap: 0, + playerBlack: '', playerBlackRank: '', playerWhite: '', playerWhiteRank: '', + }, + record: [] + }, + + connect: { location: '', type: '' }, + + currentRoom: { + description: '', id: 0, language: '', name: '' + }, + errors: {}, - currentRoom: { description: '', id: 0, language: '', name: '' }, - messages: [ { - admin: false, content: '', username: '' - } ], + games: [ { boardSize: 0, handicap: 0, id: 0, komi: 0.0, open: false, playerBlack: '', playerBlackRank: '', playerWhite: '', playerWhiteRank: '', winType: null } ], + joinGame: {}, + + messages: [ { + admin: false, content: '', username: '' + } ], + socket: { connected: false, nsp: '' }, - connect: { location: '', type: '' } + + user: { username: '', email: '', id: 0 } }; } \ No newline at end of file diff --git a/packages/server/play-node-go/src/reducers/socket/reducer.socket.js b/packages/server/play-node-go/src/reducers/socket/reducer.socket.js index b3c3b6d..74568b7 100644 --- a/packages/server/play-node-go/src/reducers/socket/reducer.socket.js +++ b/packages/server/play-node-go/src/reducers/socket/reducer.socket.js @@ -29,8 +29,31 @@ export const socketReducer = (state: state, action: action):state => { const socket = io.launch(room, dispatch); return {...state, socket} } - + + case 'CONNECT_GAME': { + return connectGame(state, action); + } + default: return state; } +} + +function connectGame (state, action) { + const { user, game, dispatch } = action.body; + const priorSocket = state.socket; + let updatedState; + if ( !priorSocket.nsp || priorSocket.nsp !== `/${game.room}` ) { + const connectRoomAction = { + type: 'SOCKET', + message: 'CONNECT_ROOM', + body: { user, room: game.room, dispatch} + } + updatedState = stateReducer(state, connectRoomAction); + + } + if (!updatedState) updatedState = {...state}; + const socket = updatedState.socket; + socket.emit('connect_game', {user, game}); + return {...updatedState}; } \ No newline at end of file diff --git a/packages/server/play-node-go/src/services/api/gamesServices.js b/packages/server/play-node-go/src/services/api/gamesServices.js index 537ee81..8c9fd37 100644 --- a/packages/server/play-node-go/src/services/api/gamesServices.js +++ b/packages/server/play-node-go/src/services/api/gamesServices.js @@ -14,6 +14,25 @@ const getGameService = async (gameIndex) => { ) .then(res => res.text()) .then(text => JSON.parse(text)) + .then(obj => { + // Reformat from SQL coluumn name convention to JS + delete Object.assign(obj.game, {applicationVersion: obj.game.application_version }).application_version; + delete Object.assign(obj.game, {boardSize: obj.game.board_size }).board_size; + delete Object.assign(obj.game, {playerBlack: obj.game.player_black }).player_black; + delete Object.assign(obj.game, {playerBlackRank: obj.game.player_black_rank }).player_black_rank; + delete Object.assign(obj.game, {playerWhite: obj.game.player_white }).player_white; + delete Object.assign(obj.game, {playerWhiteRank: obj.game.player_white_rank }).player_white_rank; + delete Object.assign(obj.game, {capturesWhite: obj.game.captures_white }).captures_white; + delete Object.assign(obj.game, {capturesBlack: obj.game.captures_black }).captures_black; + delete Object.assign(obj.game, {mainTime: obj.game.main_time }).main_time; + delete Object.assign(obj.game, {timePeriod: obj.game.time_period }).time_period; + delete Object.assign(obj.game, {periodLength: obj.game.period_length }).period_length; + delete Object.assign(obj.game, {overtimePeriod: obj.game.overtime_period }).overtime_period; + delete Object.assign(obj.game, {overtimeLength: obj.game.overtime_length }).overtime_length; + delete Object.assign(obj.game, {winType: obj.game.win_type }).win_type; + + return obj; + }) .catch(err => err); return response;