connect new Game module to service and update frontend connections
This commit is contained in:
parent
a02576532d
commit
7aed5b7bf9
7 changed files with 34 additions and 21 deletions
|
@ -32,7 +32,7 @@ const launch = (nsp, dispatch) => {
|
|||
socket.on('update_board', (data) => {
|
||||
console.log(data)
|
||||
console.log('update_board received')
|
||||
dispatch({ type: 'GAMES', message: 'UPDATE_BOARD', body: data.board })
|
||||
dispatch({ type: 'GAMES', message: 'UPDATE_BOARD', body: data })
|
||||
})
|
||||
|
||||
return socket;
|
||||
|
|
|
@ -39,7 +39,7 @@ const Game = (props) => {
|
|||
return dispatch(action);
|
||||
}
|
||||
roomSocketConnect();
|
||||
}, [ state.active , dispatch, state.user ] )
|
||||
}, [ state.active.game.open , dispatch, state.user ] )
|
||||
|
||||
return (
|
||||
<div
|
||||
|
|
|
@ -20,8 +20,8 @@ export const gamesReducer = (state, action) => {
|
|||
return {...state, joinGame: id};
|
||||
|
||||
case 'UPDATE_BOARD':
|
||||
console.log(action.body)
|
||||
return {...state, board: action.body};
|
||||
const { gameRecord, pass, turn, winner, playerState } = action.body.meta;
|
||||
return {...state, board: action.body.board, active: { game: {...state.active.game, gameRecord, pass, turn, winner, playerState } } };
|
||||
|
||||
case 'SET_ACTIVE':
|
||||
return {...state, active: action.body};
|
||||
|
|
|
@ -164,6 +164,15 @@ const initBoard = (game) => {
|
|||
|
||||
// returns Game object
|
||||
const Game = ({gameData = {}, gameRecord = []} = {}) => {
|
||||
const helper = {
|
||||
clearKo: function() {
|
||||
this.kos.forEach(ko => {
|
||||
this.boardState[ko] = { ...this.boardState[ko], legal: true, ko: false };
|
||||
})
|
||||
this.kos = [];
|
||||
},
|
||||
}
|
||||
|
||||
if (gameRecord.length) {
|
||||
// play through all the moves
|
||||
return gameRecord.reduce((game, move) => game.makeMove(move), Game({gameData}).initGame())
|
||||
|
@ -214,13 +223,6 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => {
|
|||
komi: this.komi
|
||||
}
|
||||
},
|
||||
|
||||
clearKo: function() {
|
||||
this.kos.forEach(ko => {
|
||||
this.boardState[ko] = { ...this.boardState[ko], legal: true, ko: false };
|
||||
})
|
||||
this.kos = [];
|
||||
},
|
||||
|
||||
makeMove: function({ player, pos: {x, y}}) {
|
||||
let game = this;
|
||||
|
@ -231,7 +233,7 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => {
|
|||
if (isTurn) {
|
||||
if (point.legal) {
|
||||
game.addToRecord({ player, pos: { x, y } });
|
||||
if (this.kos.length) this.clearKo();
|
||||
if (this.kos.length) helper.clearKo.call(this);
|
||||
point.makeMove(game);
|
||||
game.turn *= -1;
|
||||
success = true;
|
||||
|
|
|
@ -7,13 +7,16 @@ const storeGame = (game) => {
|
|||
}
|
||||
|
||||
const initGame = ({id, gameRecord = [], ...gameData}) => {
|
||||
if (gamesInProgress[id]) return getDataForUI(id);
|
||||
gamesInProgress[id] = Game({ gameData, gameRecord })
|
||||
gamesInProgress[id].initGame();
|
||||
return getDataForUI(id)
|
||||
}
|
||||
|
||||
const makeMove = ({id, move}) => {
|
||||
if (!gamesInProgress[id]) return { message: 'no game'};
|
||||
// console.log(id, move)
|
||||
// console.log(gamesInProgress[id])
|
||||
if (!gamesInProgress[id]) storeGame({id});
|
||||
gamesInProgress[id] = gamesInProgress[id].makeMove(move)
|
||||
if (gamesInProgress[id].success === false) return { message: 'illegal move' };
|
||||
return getDataForUI(id)
|
||||
|
@ -26,6 +29,12 @@ const getDataForUI = (id) => {
|
|||
};
|
||||
}
|
||||
|
||||
const dropGame = (id) => {
|
||||
return { message:
|
||||
`${delete gamesInProgress[id]}`
|
||||
}
|
||||
}
|
||||
|
||||
const getAllGames = () => {
|
||||
return gamesInProgress;
|
||||
}
|
||||
|
@ -34,5 +43,6 @@ module.exports = {
|
|||
makeMove,
|
||||
getAllGames,
|
||||
getDataForUI,
|
||||
initGame
|
||||
initGame,
|
||||
dropGame
|
||||
}
|
||||
|
|
|
@ -22,8 +22,8 @@ io.on('connection', async socket=> {
|
|||
// ! temp
|
||||
await gameServices.initGame({id: data.game.id})
|
||||
// ! end-temp
|
||||
const gameData = await gameServices.getDataForUI(data.game.id);
|
||||
io.of(room).to(game).emit('game_connected', gameData)
|
||||
const { board, ...meta } = await gameServices.getDataForUI(data.game.id);
|
||||
io.of(room).to(game).emit('game_connected', { board, meta });
|
||||
});
|
||||
});
|
||||
socket.on('make_move', async data => {
|
||||
|
@ -31,15 +31,14 @@ io.on('connection', async socket=> {
|
|||
const gameNsp = `game-${data.game.id}`;
|
||||
|
||||
try {
|
||||
const result = await gameServices.makeMove({ id: 1, move });
|
||||
console.log(result)
|
||||
let socketAction = 'update_board';
|
||||
if (result.message) socketAction = 'error';
|
||||
const { board, message, ...meta } = await gameServices.makeMove({ id: 1, move });
|
||||
const socketAction = message ? 'error' : 'update_board';
|
||||
socket.join(gameNsp, () => {
|
||||
io.of(room).to(gameNsp).emit(socketAction, result)
|
||||
io.of(room).to(gameNsp).emit(socketAction, { board, meta, message })
|
||||
});
|
||||
}
|
||||
catch (e) {
|
||||
console.log(e)
|
||||
socket.join(gameNsp, () => {
|
||||
io.of(room).to(gameNsp).emit('error', e)
|
||||
});
|
||||
|
|
|
@ -3,6 +3,8 @@ const should = chai.should();
|
|||
const gameServices = require('../services/gameServices');
|
||||
|
||||
describe('game services', () => {
|
||||
afterEach(() => gameServices.dropGame(1))
|
||||
|
||||
it('init game returns game board', done => {
|
||||
gameServices.initGame({ id: 1, handicap: 4 })
|
||||
.board.should.eql(fourHandicapBoard)
|
||||
|
|
Loading…
Reference in a new issue