stub make move socket message to add move to game
This commit is contained in:
parent
23cd485cbb
commit
9b82236283
7 changed files with 33 additions and 26 deletions
|
@ -3,6 +3,7 @@ import './Point.scss';
|
||||||
|
|
||||||
const Point = (props) => {
|
const Point = (props) => {
|
||||||
const { posX, posY, user, game, record, dispatch } = props;
|
const { posX, posY, user, game, record, dispatch } = props;
|
||||||
|
const turn = game.turn > 0 ? 'black' : 'white';
|
||||||
const xFlag = () => {
|
const xFlag = () => {
|
||||||
if ( posX === 1 ) return `board__point--top`
|
if ( posX === 1 ) return `board__point--top`
|
||||||
if ( posX === game.boardSize ) return `board__point--bottom`
|
if ( posX === game.boardSize ) return `board__point--bottom`
|
||||||
|
@ -13,11 +14,26 @@ const Point = (props) => {
|
||||||
if ( posY === game.boardSize ) return `board__point--right`
|
if ( posY === game.boardSize ) return `board__point--right`
|
||||||
return '';
|
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 (
|
return (
|
||||||
<div
|
<div
|
||||||
className={`board__point ${xFlag()} ${yFlag()}`}
|
className={`board__point ${xFlag()} ${yFlag()}`}
|
||||||
onClick={() => dispatch({type: 'SOCKET', message: 'MAKE_MOVE', body: {user: {}, game: {}, room: {}, board: {}, move: {}}})}
|
onClick={e => clickHandle(e)}
|
||||||
>
|
>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -65,6 +65,7 @@ function connectGame (state, action) {
|
||||||
function makeMove (state, action) {
|
function makeMove (state, action) {
|
||||||
const { user, game, room, board, move } = action.body;
|
const { user, game, room, board, move } = action.body;
|
||||||
const socket = state.socket;
|
const socket = state.socket;
|
||||||
|
console.log(action)
|
||||||
socket.emit('make_move', {...action.body});
|
socket.emit('make_move', {...action.body});
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
|
@ -21,7 +21,6 @@ const show = async (req, res, next) => {
|
||||||
if (!roomId) throw('missing room parameter')
|
if (!roomId) throw('missing room parameter')
|
||||||
|
|
||||||
// TODO eventually add check for user's private rooms
|
// TODO eventually add check for user's private rooms
|
||||||
// socket.roomSocket(roomId);
|
|
||||||
|
|
||||||
const currentRoom = await roomQueries.findRoomById(roomId);
|
const currentRoom = await roomQueries.findRoomById(roomId);
|
||||||
const messages = await messageQueries.findMessageByRoom(roomId);
|
const messages = await messageQueries.findMessageByRoom(roomId);
|
||||||
|
|
|
@ -99,12 +99,14 @@ class Game {
|
||||||
}
|
}
|
||||||
|
|
||||||
findPointFromIdx = (arr) => {
|
findPointFromIdx = (arr) => {
|
||||||
|
console.log(this.boardState)
|
||||||
return this.boardState.find( point => point.pos[0] === arr[0] && point.pos[1] === arr[1] );
|
return this.boardState.find( point => point.pos[0] === arr[0] && point.pos[1] === arr[1] );
|
||||||
}
|
}
|
||||||
|
|
||||||
makeMove = (move) => {
|
makeMove = (move) => {
|
||||||
const player = move.player === 'white' ? -1 : 1;
|
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');
|
if ( !checkLegal(point, this) ) throw Error('illegal move');
|
||||||
clearKo(this);
|
clearKo(this);
|
||||||
clearPass(this);
|
clearPass(this);
|
||||||
|
|
|
@ -12,6 +12,7 @@ const initGame = (game) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const makeMove = (game, move) => {
|
const makeMove = (game, move) => {
|
||||||
|
if (!gamesInProgress[game.id]) initGame(game);
|
||||||
const newState = gamesInProgress[game.id].makeMove(move);
|
const newState = gamesInProgress[game.id].makeMove(move);
|
||||||
return {...newState}
|
return {...newState}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ const gameServices = require('./services/gameServices');
|
||||||
io.on('connection', socket=> {
|
io.on('connection', socket=> {
|
||||||
socket.emit('connected', {message: 'socket connected'});
|
socket.emit('connected', {message: 'socket connected'});
|
||||||
socket.on('connect_room', data => {
|
socket.on('connect_room', data => {
|
||||||
console.log(data)
|
|
||||||
if (data.user && data.user.email) {
|
if (data.user && data.user.email) {
|
||||||
delete data.user.email;
|
delete data.user.email;
|
||||||
}
|
}
|
||||||
|
@ -19,35 +18,24 @@ io.on('connection', socket=> {
|
||||||
socket.emit('new_user', data);
|
socket.emit('new_user', data);
|
||||||
socket.on('connect_game', data => {
|
socket.on('connect_game', data => {
|
||||||
const game = `game-${data.game.id}`;
|
const game = `game-${data.game.id}`;
|
||||||
socket.join(game, () => {
|
socket.join(game, async () => {
|
||||||
io.of(room).to(game).emit('game_connected', {})
|
// ! 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 => {
|
socket.on('make_move', data => {
|
||||||
const { user, move, board, game, room } = data;
|
const { user, move, board, game, room } = data;
|
||||||
gameServices.placeMove(1, {player: 'black', move: '7,4'})
|
console.log(move)
|
||||||
console.log(data)
|
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 = {
|
module.exports = {
|
||||||
io,
|
io
|
||||||
roomSocket
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ describe('game services', () => {
|
||||||
|
|
||||||
it('games services places move', done => {
|
it('games services places move', done => {
|
||||||
gameServices.initGame({id: 1, handicap: 4})
|
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 };
|
const afterMoveOneShould = { board:{ ...fourHandicapBoard, '6-3': -1}, meta: moveOneMeta };
|
||||||
afterMoveOne.should.eql(afterMoveOneShould);
|
afterMoveOne.should.eql(afterMoveOneShould);
|
||||||
done();
|
done();
|
||||||
|
@ -20,7 +20,7 @@ describe('game services', () => {
|
||||||
it('illegal move throws error', done => {
|
it('illegal move throws error', done => {
|
||||||
try {
|
try {
|
||||||
gameServices.initGame({id: 1, handicap: 4})
|
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) {
|
catch (err) {
|
||||||
err.message.should.equal('illegal move')
|
err.message.should.equal('illegal move')
|
||||||
|
|
Loading…
Reference in a new issue