ChessboardInterface.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. const React = require('react/addons');
  3. const GameStore = require('../stores/GameStore');
  4. const GameActions = require('../actions/GameActions');
  5. const onGameChange = require('../mixins/onGameChange');
  6. const Chessboard = require('./Chessboard');
  7. const CapturedPieces = require('./CapturedPieces');
  8. const TableOfMoves = require('./TableOfMoves');
  9. const cx = require('classnames');
  10. const ChessboardInterface = React.createClass({
  11. propTypes: {
  12. io: React.PropTypes.object.isRequired,
  13. token: React.PropTypes.string.isRequired,
  14. soundsEnabled: React.PropTypes.bool.isRequired,
  15. color: React.PropTypes.oneOf(['white', 'black']).isRequired
  16. },
  17. mixins: [React.addons.PureRenderMixin, onGameChange],
  18. getInitialState() {
  19. return GameStore.getState();
  20. },
  21. render() {
  22. const {promotion, turn, gameOver} = this.state;
  23. const cxFeedback = cx({
  24. feedback: true,
  25. whitefeedback: turn === 'w',
  26. blackfeedback: turn === 'b'
  27. });
  28. const goType = gameOver.get('type');
  29. const loser = gameOver.get('winner') === 'White' ? 'Black' : 'White';
  30. return (
  31. <div id="board-moves-wrapper" className="clearfix">
  32. <audio preload="auto" ref="moveSnd">
  33. <source src="/snd/move.mp3" />
  34. </audio>
  35. <div id="board-wrapper">
  36. <CapturedPieces />
  37. <Chessboard
  38. io={this.props.io}
  39. token={this.props.token}
  40. maybePlaySound={this._maybePlaySound}
  41. color={this.props.color} />
  42. </div>
  43. <TableOfMoves />
  44. <span className="promotion">
  45. <label>
  46. <span>Promotion: </span>
  47. <select value={promotion}
  48. onChange={this._onPromotionChange}>
  49. <option value="q">Queen</option>
  50. <option value="r">Rook</option>
  51. <option value="b">Bishop</option>
  52. <option value="n">Knight</option>
  53. </select>
  54. </label>
  55. </span>
  56. <span className={cxFeedback}>
  57. {!gameOver.get('status') ?
  58. <span className="feedback-move">
  59. {`${turn === 'w' ? 'White' : 'Black'} to move.`}
  60. </span> :
  61. <span className="feedback-status">
  62. {goType === 'checkmate' ?
  63. `Checkmate. ${gameOver.get('winner')} wins!`
  64. :goType === 'timeout' ?
  65. `${loser}‘s time is out. ${gameOver.get('winner')} wins!`
  66. :goType === 'resign' ?
  67. `${loser} has resigned. ${gameOver.get('winner')} wins!`
  68. :goType === 'draw' ?
  69. 'Draw.'
  70. :goType === 'stalemate' ?
  71. 'Draw (Stalemate).'
  72. :goType === 'threefoldRepetition' ?
  73. 'Draw (Threefold Repetition).'
  74. :goType === 'insufficientMaterial' ?
  75. 'Draw (Insufficient Material)'
  76. :null}
  77. </span>
  78. }
  79. </span>
  80. </div>
  81. );
  82. },
  83. _onGameChange() {
  84. this.setState(GameStore.getState());
  85. },
  86. _onPromotionChange(e) {
  87. GameActions.changePromotion(e.target.value);
  88. },
  89. _maybePlaySound() {
  90. if (this.props.soundsEnabled) {
  91. this.refs.moveSnd.getDOMNode().play();
  92. }
  93. }
  94. });
  95. module.exports = ChessboardInterface;