GameStore.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {EventEmitter2 as EventEmitter} from 'eventemitter2';
  2. import {Chess} from 'chess.js';
  3. import {List, Map, OrderedMap, Set} from 'immutable';
  4. import AppDispatcher from '../dispatcher/AppDispatcher';
  5. import GameConstants from '../constants/GameConstants';
  6. import ChessPieces from '../constants/ChessPieces';
  7. const CHANGE_EVENT = 'change';
  8. const MOVE_EVENT = 'new-move';
  9. var _gameOver;
  10. var _capturedPieces;
  11. var _moves;
  12. var _promotion;
  13. var _turn;
  14. var _check;
  15. var _lastMove;
  16. var _chess;
  17. setInitialState();
  18. const GameStore = Object.assign({}, EventEmitter.prototype, {
  19. getState() {
  20. return {
  21. gameOver: _gameOver,
  22. promotion: _promotion,
  23. turn: _turn,
  24. check: _check
  25. };
  26. },
  27. getCapturedPieces() {
  28. return _capturedPieces;
  29. },
  30. getMoves() {
  31. return _moves;
  32. },
  33. getChessboardState() {
  34. return {
  35. fen: _chess.fen(),
  36. lastMove: _lastMove,
  37. check: _check
  38. };
  39. },
  40. getValidMoves(square) {
  41. return square ? Set(
  42. _chess.moves({
  43. square: square,
  44. verbose: true
  45. }).map(move => move.to)) : Set();
  46. }
  47. });
  48. function setInitialState() {
  49. _gameOver = Map({
  50. status: false,
  51. type: null,
  52. winner: null
  53. });
  54. _capturedPieces = OrderedMap([
  55. ['w', List()],
  56. ['b', List()]
  57. ]);
  58. _moves = List();
  59. _promotion = 'q';
  60. _turn = 'w';
  61. _check = false;
  62. _lastMove = Map();
  63. _chess = new Chess();
  64. }
  65. function makeMove(from, to, capture, emitMove) {
  66. const move = _chess.move({
  67. from: from,
  68. to: to,
  69. promotion: _promotion
  70. });
  71. if (!move) {
  72. // move is not valid, return false and don't emit any event.
  73. return false;
  74. }
  75. _turn = _chess.turn();
  76. _check = _chess.in_check();
  77. _lastMove = _lastMove.set('from', from).set('to', to);
  78. _moves = _moves.isEmpty() || _moves.last().size === 2 ?
  79. _moves.push(List([move.san])) :
  80. _moves.update(_moves.size - 1, list => list.push(move.san));
  81. if (capture || move.flags === 'e') {
  82. const capturedPiece = capture ||
  83. ChessPieces[_turn === 'w' ? 'P' : 'p']; // en passant
  84. _capturedPieces = _capturedPieces
  85. .update(_turn, list => list.push(capturedPiece));
  86. }
  87. if (_chess.game_over()) {
  88. const type = _chess.in_checkmate() ? 'checkmate' :
  89. _chess.in_stalemate() ? 'stalemate' :
  90. _chess.in_threefold_repetition() ? 'threefoldRepetition' :
  91. _chess.insufficient_material() ? 'insufficientMaterial' :
  92. _chess.in_draw() ? 'draw' : null;
  93. gameOver({
  94. winner: _turn === 'b' ? 'White' : 'Black',
  95. type: type
  96. });
  97. }
  98. if (emitMove) {
  99. GameStore.emit(MOVE_EVENT, {
  100. from: from,
  101. to: to,
  102. capture: capture,
  103. gameOver: _chess.game_over()
  104. });
  105. }
  106. return true;
  107. }
  108. function gameOver(options) {
  109. _gameOver = _gameOver
  110. .set('status', true)
  111. .set('winner', options.winner)
  112. .set('type', options.type);
  113. }
  114. AppDispatcher.register(payload => {
  115. const action = payload.action;
  116. let emitEvent = true;
  117. switch (action.actionType) {
  118. case GameConstants.MAKE_MOVE:
  119. emitEvent = makeMove(
  120. action.from, action.to, action.capture, action.emitMove);
  121. break;
  122. case GameConstants.CHANGE_PROMOTION:
  123. _promotion = action.promotion;
  124. break;
  125. case GameConstants.GAME_OVER:
  126. gameOver(action.options);
  127. break;
  128. case GameConstants.REMATCH:
  129. setInitialState();
  130. break;
  131. default:
  132. return true;
  133. }
  134. if (emitEvent) {
  135. GameStore.emit(CHANGE_EVENT);
  136. }
  137. return true;
  138. });
  139. export default GameStore;