import React from 'react/addons'; import omit from 'lodash.omit'; import GameStore from '../stores/GameStore'; import GameActions from '../actions/GameActions'; import onGameChange from '../mixins/onGameChange'; import Chessboard from './Chessboard'; import CapturedPieces from './CapturedPieces'; import TableOfMoves from './TableOfMoves'; const ChessboardInterface = React.createClass({ propTypes: { io: React.PropTypes.object.isRequired, token: React.PropTypes.string.isRequired, soundsEnabled: React.PropTypes.bool.isRequired, color: React.PropTypes.oneOf(['white', 'black']).isRequired, gameOver: React.PropTypes.object.isRequired, isOpponentAvailable: React.PropTypes.bool.isRequired }, mixins: [React.addons.PureRenderMixin, onGameChange], getInitialState() { return GameStore.getState(); }, componentDidUpdate(prevProps) { if (this.props.gameOver.get('status') && !prevProps.gameOver.get('status')) { this.props.openModal('info', this._getGameOverMessage()); } }, render() { const {promotion, turn, gameOver, check} = this.state; return (
{!gameOver.get('status') ? {/* F -> white king, f -> black king*/ turn === 'w' ? 'F' : 'f'} {`${turn === 'w' ? 'White' : 'Black'} to move.`} {check ? Check. : null} : {gameOver.get('winner') === 'White' ? 'F' : 'f'} {this._getGameOverMessage()} }
); }, _onGameChange() { this.setState(GameStore.getState()); }, _onPromotionChange(e) { GameActions.changePromotion(e.target.value); }, _maybePlaySound() { if (this.props.soundsEnabled) { this.refs[this.state.check ? 'checkSnd' : 'moveSnd'].getDOMNode().play(); } }, _getGameOverMessage() { const type = this.props.gameOver.get('type'); const winner = this.props.gameOver.get('winner'); const loser = winner === 'White' ? 'Black' : 'White'; return type === 'checkmate' ? `Checkmate. ${winner} wins!` : type === 'timeout' ? `${loser}‘s time is out. ${winner} wins!` : type === 'resign' ? `${loser} has resigned. ${winner} wins!` : type === 'draw' ? 'Draw.' : type === 'stalemate' ? 'Draw (Stalemate).' : type === 'threefoldRepetition' ? 'Draw (Threefold Repetition).' : type === 'insufficientMaterial' ? 'Draw (Insufficient Material)' : ''; } }); export default ChessboardInterface;