GameStore.js 3.4 KB

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