1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00

Move all this shit around

This commit is contained in:
Daniel Barber 2018-03-12 11:12:31 -04:00
parent 4ffc61f075
commit ac90fe71eb
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
2 changed files with 35 additions and 33 deletions

View File

@ -4,7 +4,7 @@ import classNames from "classnames";
import API from "../services/api"; import API from "../services/api";
import { setGame, setMoves, selectPiece } from "../store/actions"; import { selectPiece } from "../store/actions";
class ChessBoardSquare extends React.Component { class ChessBoardSquare extends React.Component {
constructor(props) { constructor(props) {
@ -15,27 +15,48 @@ class ChessBoardSquare extends React.Component {
return [this.props.file, this.props.rank]; return [this.props.file, this.props.rank];
} }
get squareId() {
return `f${this.props.file}-r${this.props.rank}`;
}
get squareClass() {
if (this.props.piece == undefined) {
return classNames(
"board-square",
{ "available": this.isAvailableSquare() }
);
} else {
return classNames(
"board-square",
this.props.piece.type,
this.props.piece.colour,
{ "selected": this.isSelectedSquare() },
{ "available": this.isAvailableSquare() }
);
}
}
selectSquare() { selectSquare() {
const { piece, store, channel } = this.props; const { piece, store, channel } = this.props;
const { gameId, selectedSquare, player } = store.getState(); const { gameId, selectedSquare, player } = store.getState();
if (selectedSquare != null && this.moveIsValid()) { if (this.moveIsValid(selectedSquare)) {
store.dispatch(setMoves([]));
channel.sendMove({ channel.sendMove({
from: selectedSquare, from: selectedSquare,
to: this.squareCoords, to: this.squareCoords,
}); });
} else if (selectedSquare != null) { } else if (selectedSquare != null) {
store.dispatch(setMoves([]));
store.dispatch(selectPiece(null)); store.dispatch(selectPiece(null));
} else if (this.playerCanSelectPiece(player, piece)) { } else if (this.playerCanSelectPiece(player, piece)) {
channel.getAvailableMoves(this.squareCoords);
store.dispatch(selectPiece(this.squareCoords)); store.dispatch(selectPiece(this.squareCoords));
channel.getAvailableMoves(this.squareCoords);
} }
} }
moveIsValid() { moveIsValid(selectedSquare) {
return !this.isSelectedSquare; return selectedSquare != null &&
!this.isSelectedSquare() &&
this.isAvailableSquare();
} }
playerCanSelectPiece(player, piece) { playerCanSelectPiece(player, piece) {
@ -47,7 +68,7 @@ class ChessBoardSquare extends React.Component {
player == turn; player == turn;
} }
get isSelectedSquare() { isSelectedSquare() {
const { store } = this.props; const { store } = this.props;
if (store.getState().selectedSquare == null) { if (store.getState().selectedSquare == null) {
@ -57,39 +78,18 @@ class ChessBoardSquare extends React.Component {
} }
} }
get isAvailableSquare() { isAvailableSquare() {
const { store } = this.props; const { store } = this.props;
const moves = store.getState().moves; const moves = store.getState().moves;
return _.find(moves, function(square) { return _.find(moves, (square) => {
return square.join() == this.squareCoords.join(); return square.join() == this.squareCoords.join();
}.bind(this)); });
}
squareId() {
return `f${this.props.file}-r${this.props.rank}`;
}
get squareClass() {
if (this.props.piece == undefined) {
return classNames(
"board-square",
{ "available": this.isAvailableSquare }
);
} else {
return classNames(
"board-square",
this.props.piece.type,
this.props.piece.colour,
{ "selected": this.isSelectedSquare },
{ "available": this.isAvailableSquare }
);
}
} }
render() { render() {
return <div return <div
id={this.squareId()} id={this.squareId}
className={this.squareClass} className={this.squareClass}
onClick={this.selectSquare.bind(this)} onClick={this.selectSquare.bind(this)}
/>; />;

View File

@ -14,6 +14,7 @@ const chessBoardReducer = (state = defaultState, action) => {
.set("board", action.board) .set("board", action.board)
.set("turn", action.turn) .set("turn", action.turn)
.set("selectedSquare", null) .set("selectedSquare", null)
.set("moves", [])
.toJS(); .toJS();
case "SET_MOVES": case "SET_MOVES":
@ -29,6 +30,7 @@ const chessBoardReducer = (state = defaultState, action) => {
case "SELECT_PIECE": case "SELECT_PIECE":
return Immutable.fromJS(state) return Immutable.fromJS(state)
.set("selectedSquare", action.coords) .set("selectedSquare", action.coords)
.set("moves", [])
.toJS(); .toJS();
default: default: