GameStore.js 3.3 KB

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