stub make move socket message to add move to game

This commit is contained in:
Sorrel Bri 2020-01-31 20:28:39 -08:00 committed by sorrelbri
parent 293a574d2b
commit 485771b8f2
7 changed files with 33 additions and 26 deletions

View file

@ -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 (
<div
className={`board__point ${xFlag()} ${yFlag()}`}
onClick={() => dispatch({type: 'SOCKET', message: 'MAKE_MOVE', body: {user: {}, game: {}, room: {}, board: {}, move: {}}})}
onClick={e => clickHandle(e)}
>
</div>

View file

@ -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;
}

View file

@ -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);

View file

@ -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);

View file

@ -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}
}

View file

@ -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
}

View file

@ -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')