From 2d04f40bd6bf84f8d1728d31ded47ea8326be937 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Fri, 31 Jan 2020 20:28:39 -0800 Subject: [PATCH] stub make move socket message to add move to game --- .../src/components/GameUI/Point/Point.js | 18 ++++++++++- .../src/reducers/socket/reducer.socket.js | 1 + .../server/server/controllers/api/apiRoom.js | 1 - packages/server/server/services/Game.js | 4 ++- .../server/server/services/gameServices.js | 1 + packages/server/server/socket.js | 30 ++++++------------- .../server/server/test/gameServices.spec.js | 4 +-- 7 files changed, 33 insertions(+), 26 deletions(-) diff --git a/packages/server/play-node-go/src/components/GameUI/Point/Point.js b/packages/server/play-node-go/src/components/GameUI/Point/Point.js index db8df00..d87a745 100644 --- a/packages/server/play-node-go/src/components/GameUI/Point/Point.js +++ b/packages/server/play-node-go/src/components/GameUI/Point/Point.js @@ -3,6 +3,7 @@ import './Point.scss'; const Point = (props) => { const { posX, posY, user, game, record, dispatch } = props; + const turn = game.turn > 0 ? 'black' : 'white'; const xFlag = () => { if ( posX === 1 ) return `board__point--top` if ( posX === game.boardSize ) return `board__point--bottom` @@ -13,11 +14,26 @@ const Point = (props) => { if ( posY === game.boardSize ) return `board__point--right` return ''; } + const clickHandle = (e) => { + const action = { + type: 'SOCKET', + message: 'MAKE_MOVE', + body: { + user, + game, + room: game.room, + board: {}, + move: { player: turn, pos: { x: posX, y: posY } } + } + } + console.log(action) + dispatch(action); + } return (
dispatch({type: 'SOCKET', message: 'MAKE_MOVE', body: {user: {}, game: {}, room: {}, board: {}, move: {}}})} + onClick={e => clickHandle(e)} >
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 da2568c..e7625e8 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 @@ -65,6 +65,7 @@ function connectGame (state, action) { function makeMove (state, action) { const { user, game, room, board, move } = action.body; const socket = state.socket; + console.log(action) socket.emit('make_move', {...action.body}); return state; } \ No newline at end of file diff --git a/packages/server/server/controllers/api/apiRoom.js b/packages/server/server/controllers/api/apiRoom.js index 7208adf..d0abb9c 100644 --- a/packages/server/server/controllers/api/apiRoom.js +++ b/packages/server/server/controllers/api/apiRoom.js @@ -21,7 +21,6 @@ const show = async (req, res, next) => { if (!roomId) throw('missing room parameter') // TODO eventually add check for user's private rooms - // socket.roomSocket(roomId); const currentRoom = await roomQueries.findRoomById(roomId); const messages = await messageQueries.findMessageByRoom(roomId); diff --git a/packages/server/server/services/Game.js b/packages/server/server/services/Game.js index bb4ebe6..c0f7650 100644 --- a/packages/server/server/services/Game.js +++ b/packages/server/server/services/Game.js @@ -99,12 +99,14 @@ class Game { } findPointFromIdx = (arr) => { + console.log(this.boardState) return this.boardState.find( point => point.pos[0] === arr[0] && point.pos[1] === arr[1] ); } makeMove = (move) => { const player = move.player === 'white' ? -1 : 1; - const point = this.findPointFromIdx([move.pos.X, move.pos.Y]) + const point = this.findPointFromIdx([move.pos.x, move.pos.y]) + console.log([move.pos.x, move.pos.y]) if ( !checkLegal(point, this) ) throw Error('illegal move'); clearKo(this); clearPass(this); diff --git a/packages/server/server/services/gameServices.js b/packages/server/server/services/gameServices.js index ccdf29c..7170e6a 100644 --- a/packages/server/server/services/gameServices.js +++ b/packages/server/server/services/gameServices.js @@ -12,6 +12,7 @@ const initGame = (game) => { } const makeMove = (game, move) => { + if (!gamesInProgress[game.id]) initGame(game); const newState = gamesInProgress[game.id].makeMove(move); return {...newState} } diff --git a/packages/server/server/socket.js b/packages/server/server/socket.js index 46ea33e..ee574ab 100644 --- a/packages/server/server/socket.js +++ b/packages/server/server/socket.js @@ -8,7 +8,6 @@ const gameServices = require('./services/gameServices'); io.on('connection', socket=> { socket.emit('connected', {message: 'socket connected'}); socket.on('connect_room', data => { - console.log(data) if (data.user && data.user.email) { delete data.user.email; } @@ -19,35 +18,24 @@ io.on('connection', socket=> { socket.emit('new_user', data); socket.on('connect_game', data => { const game = `game-${data.game.id}`; - socket.join(game, () => { - io.of(room).to(game).emit('game_connected', {}) + socket.join(game, async () => { + // ! temp + gameServices.initGame({id: data.game.id}) + // ! end-temp + const gameData = await gameServices.getBoard(data.game.id); + io.of(room).to(game).emit('game_connected', gameData) }); }); socket.on('make_move', data => { const { user, move, board, game, room } = data; - gameServices.placeMove(1, {player: 'black', move: '7,4'}) + console.log(move) console.log(data) + gameServices.makeMove(1, move) }) }); }) }) -const roomSocket = (roomId) => { - - const roomIo = io.of(roomId) - roomIo.on('connection', socket => { - console.log('connected room') - socket.on('connect_room', data => { - if (data.user && data.user.email) { - delete data.user.email; - } - socket.emit('new_user', data); - }) - }) - return roomIo; -} - module.exports = { - io, - roomSocket + io } diff --git a/packages/server/server/test/gameServices.spec.js b/packages/server/server/test/gameServices.spec.js index db513c5..8578ca3 100644 --- a/packages/server/server/test/gameServices.spec.js +++ b/packages/server/server/test/gameServices.spec.js @@ -11,7 +11,7 @@ describe('game services', () => { it('games services places move', done => { gameServices.initGame({id: 1, handicap: 4}) - const afterMoveOne = gameServices.makeMove({id: 1}, {player: 'white', pos: { X:6, Y:3 }}); + const afterMoveOne = gameServices.makeMove({id: 1}, {player: 'white', pos: { x:6, y:3 }}); const afterMoveOneShould = { board:{ ...fourHandicapBoard, '6-3': -1}, meta: moveOneMeta }; afterMoveOne.should.eql(afterMoveOneShould); done(); @@ -20,7 +20,7 @@ describe('game services', () => { it('illegal move throws error', done => { try { gameServices.initGame({id: 1, handicap: 4}) - const afterIllegalMove = gameServices.makeMove({id: 1}, {player: 'white', pos: { X:4, Y:4 }}); + const afterIllegalMove = gameServices.makeMove({id: 1}, {player: 'white', pos: { x:4, y:4 }}); } catch (err) { err.message.should.equal('illegal move')