ChessboardInterface.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. },
  14. mixins: [React.addons.PureRenderMixin, onGameChange],
  15. getInitialState() {
  16. return GameStore.getState();
  17. },
  18. render() {
  19. const {promotion, turn, gameOver} = this.state;
  20. const cxFeedback = cx({
  21. feedback: true,
  22. whitefeedback: turn === 'w',
  23. blackfeedback: turn === 'b'
  24. });
  25. console.log(turn);
  26. console.log(cxFeedback);
  27. const goType = gameOver.get('type');
  28. const loser = gameOver.get('winner') === 'White' ? 'Black' : 'White';
  29. return (
  30. <div id="board-moves-wrapper" className="clearfix">
  31. <div id="board-wrapper">
  32. <CapturedPieces />
  33. <Chessboard io={this.props.io} />
  34. </div>
  35. <TableOfMoves />
  36. <span className="promotion">
  37. <label>
  38. <span>Promotion: </span>
  39. <select value={promotion}
  40. onChange={this._onPromotionChange}>
  41. <option value="q">Queen</option>
  42. <option value="r">Rook</option>
  43. <option value="n">Knight</option>
  44. <option value="b">Bishop</option>
  45. </select>
  46. </label>
  47. </span>
  48. <span className={cxFeedback}>
  49. {!gameOver.get('status') ?
  50. <span className="feedback-move">
  51. {`${turn === 'w' ? 'White' : 'Black'} to move.`}
  52. </span> :
  53. <span className="feedback-status">
  54. {goType === 'checkmate' ?
  55. `Checkmate. ${gameOver.get('winner')} wins!`
  56. :goType === 'timeout' ?
  57. `${loser}‘s time is out. ${gameOver.get('winner')} wins!`
  58. :goType === 'resign' ?
  59. `${loser} has resigned. ${gameOver.get('winner')} wins!`
  60. :goType === 'draw' ?
  61. 'Draw.'
  62. :goType === 'stalemate' ?
  63. 'Draw (Stalemate).'
  64. :goType === 'threefoldRepetition' ?
  65. 'Draw (Threefold Repetition).'
  66. :goType === 'insufficientMaterial' ?
  67. 'Draw (Insufficient Material)'
  68. :null}
  69. </span>
  70. }
  71. </span>
  72. </div>
  73. );
  74. },
  75. _onGameChange() {
  76. this.setState(GameStore.getState());
  77. },
  78. _onPromotionChange(e) {
  79. GameActions.changePromotion(e.target.value);
  80. }
  81. });
  82. module.exports = ChessboardInterface;