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