GameStore.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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} 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. });
  42. function setInitialState() {
  43. _gameOver = Map({
  44. status: false,
  45. type: null,
  46. winner: null
  47. });
  48. _capturedPieces = OrderedMap([
  49. ['w', List()],
  50. ['b', List()]
  51. ]);
  52. _moves = List();
  53. _promotion = 'q';
  54. _turn = 'w';
  55. _check = false;
  56. _lastMove = Map();
  57. _chess = new Chess();
  58. }
  59. function makeMove(from, to, capture, emitMove) {
  60. const move = _chess.move({
  61. from: from,
  62. to: to,
  63. promotion: _promotion
  64. });
  65. if (!move) {
  66. // move is not valid, return false and don't emit any event.
  67. return false;
  68. }
  69. _turn = _chess.turn();
  70. _check = _chess.in_check();
  71. _lastMove = _lastMove.set('from', from).set('to', to);
  72. _moves = _moves.isEmpty() || _moves.last().size === 2 ?
  73. _moves.push(List([move.san])) :
  74. _moves.update(_moves.size - 1, list => list.push(move.san));
  75. if (capture || move.flags === 'e') {
  76. const capturedPiece = capture ||
  77. (_turn === 'w' ? '\u2659' : '\u265f'); // en passant
  78. _capturedPieces = _capturedPieces
  79. .update(_turn, list => list.push(capturedPiece));
  80. }
  81. if (_chess.game_over()) {
  82. const type = _chess.in_checkmate() ? 'checkmate' :
  83. _chess.in_stalemate() ? 'stalemate' :
  84. _chess.in_threefold_repetition() ? 'threefoldRepetition' :
  85. _chess.insufficient_material() ? 'insufficientMaterial' :
  86. _chess.in_draw() ? 'draw' : null;
  87. gameOver({
  88. winner: _turn === 'b' ? 'White' : 'Black',
  89. type: type
  90. });
  91. }
  92. if (emitMove) {
  93. GameStore.emit(MOVE_EVENT, {
  94. from: from,
  95. to: to,
  96. capture: capture,
  97. gameOver: _chess.game_over()
  98. });
  99. }
  100. return true;
  101. }
  102. function gameOver(options) {
  103. _gameOver = _gameOver
  104. .set('status', true)
  105. .set('winner', options.winner)
  106. .set('type', options.type);
  107. }
  108. AppDispatcher.register(payload => {
  109. const action = payload.action;
  110. let emitEvent = true;
  111. switch (action.actionType) {
  112. case GameConstants.MAKE_MOVE:
  113. emitEvent = makeMove(
  114. action.from, action.to, action.capture, action.emitMove);
  115. break;
  116. case GameConstants.CHANGE_PROMOTION:
  117. _promotion = action.promotion;
  118. break;
  119. case GameConstants.GAME_OVER:
  120. gameOver(action.options);
  121. break;
  122. case GameConstants.REMATCH:
  123. setInitialState();
  124. break;
  125. default:
  126. return true;
  127. }
  128. if (emitEvent) {
  129. GameStore.emit(CHANGE_EVENT);
  130. }
  131. return true;
  132. });
  133. export default GameStore;