Skip to content

Commit

Permalink
Added click handling to redux game reducer.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnteronGitHub committed Aug 6, 2017
1 parent dbe1492 commit c94adc5
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/reducers/game.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@

import Chess from 'chess'

function handleClick(state, square) {
const selectedSquare = state.client.game.board.getSquare(square);
const currentSide = state.client.game.getCurrentSide();

if(state.selectedSquare===square)
return {
client: state.client,
selectedSquare: null
};
if(selectedSquare.piece
&& selectedSquare.piece.side === currentSide)
return {
client: state.client,
selectedSquare: square
};
try {
state.client.move(state.selectedSquare, selectedSquare.file + selectedSquare.rank)
return {
client: state.client,
selectedSquare: null
}
} catch (error) {
if(!error.includes("Move is invalid"))
throw error
}

return state;
}

export default (state = {client: Chess.createSimple(), selectedSquare: null}, action) => {
switch(action.type) {
case 'CLICK_SQUARE':
return handleClick(state, action.square);
default:
return state;
}
Expand Down

0 comments on commit c94adc5

Please sign in to comment.