GameInterface.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict';
  2. import React from 'react/addons';
  3. import GameHeader from './GameHeader';
  4. import Chat from './Chat';
  5. import Modal from './Modal';
  6. import GameActions from '../actions/GameActions';
  7. import GameStore from '../stores/GameStore';
  8. import ChessboardInterface from './ChessboardInterface';
  9. import {Map} from 'immutable';
  10. const GameInterface = React.createClass({
  11. propTypes: {
  12. io: React.PropTypes.object.isRequired,
  13. params: React.PropTypes.array.isRequired
  14. },
  15. getInitialState() {
  16. return {
  17. isOpponentAvailable: false,
  18. color: 'white',
  19. modal: Map({
  20. open: false,
  21. message: '',
  22. type: 'info',
  23. callbacks: {
  24. hide: this._hideModal,
  25. accept: this._acceptRematch,
  26. decline: this._declineRematch
  27. }
  28. }),
  29. soundsEnabled: false,
  30. gameOver: GameStore.getState().gameOver
  31. };
  32. },
  33. componentDidMount() {
  34. const {io, params} = this.props;
  35. io.on('token-invalid', () => this.setState({
  36. modal: this.state.modal
  37. .set('open', true)
  38. .set('message', 'Game link is invalid or has expired.')
  39. .set('type', 'info')
  40. }));
  41. io.emit('join', {
  42. token: params[0],
  43. time: params[1] * 60,
  44. inc: params[2]
  45. });
  46. io.on('joined', data => {
  47. if (data.color === 'black') {
  48. this.setState({color: 'black'});
  49. }
  50. });
  51. io.on('both-joined', () =>
  52. this.setState({isOpponentAvailable: true}, () => {
  53. if (this.state.color === 'white') {
  54. io.emit('clock-run', {
  55. token: params[0],
  56. color: 'white'
  57. });
  58. }
  59. }));
  60. io.on('full', () => {
  61. window.alert(
  62. 'This game already has two players. You have to create a new one.');
  63. window.location = '/';
  64. });
  65. io.on('player-resigned', data => {
  66. GameActions.gameOver({
  67. type: 'resign',
  68. winner: data.color === 'black' ? 'White' : 'Black'
  69. });
  70. });
  71. io.on('rematch-offered', () =>
  72. this._openModal('offer', 'Your opponent has sent you a rematch offer.'));
  73. io.on('rematch-declined', () =>
  74. this._openModal('info', 'Rematch offer has been declined.'));
  75. io.on('rematch-accepted', () => {
  76. GameActions.rematch();
  77. this.setState({
  78. color: this.state.color === 'white' ? 'black' : 'white',
  79. modal: this.state.modal.set('open', false)
  80. }, () => {
  81. if (this.state.color === 'white') {
  82. io.emit('clock-run', {
  83. token: this.props.params[0],
  84. color: 'white'
  85. });
  86. }
  87. });
  88. });
  89. io.on('opponent-disconnected', () => {
  90. if (!this.state.gameOver.get('status')) {
  91. this._openModal('info', 'Your opponent has disconnected.');
  92. }
  93. this.setState({isOpponentAvailable: false});
  94. });
  95. GameStore.on('change', this._onGameChange);
  96. },
  97. componentWillUnmount() {
  98. GameStore.off('change', this._onGameChange);
  99. },
  100. render() {
  101. const {io, params} = this.props;
  102. const {color, soundsEnabled, gameOver, isOpponentAvailable} = this.state;
  103. const commonProps = {
  104. io: io,
  105. color: color,
  106. openModal: this._openModal,
  107. isOpponentAvailable: isOpponentAvailable
  108. };
  109. return (
  110. <div>
  111. <GameHeader
  112. {...commonProps}
  113. params={params}
  114. gameOver={gameOver.get('status')} />
  115. <label id="sounds-label">
  116. <input type="checkbox"
  117. checked={soundsEnabled}
  118. onChange={this._toggleSounds} />
  119. <span> Enable sounds</span>
  120. </label>
  121. <Chat
  122. {...commonProps}
  123. token={params[0]}
  124. soundsEnabled={soundsEnabled} />
  125. <ChessboardInterface
  126. {...commonProps}
  127. token={params[0]}
  128. soundsEnabled={soundsEnabled}
  129. gameOver={gameOver} />
  130. <Modal data={this.state.modal} />
  131. </div>
  132. );
  133. },
  134. _onGameChange() {
  135. this.setState({gameOver: GameStore.getState().gameOver});
  136. },
  137. _openModal(type, message) {
  138. this.setState({
  139. modal: this.state.modal
  140. .set('open', true)
  141. .set('message', message)
  142. .set('type', type)
  143. });
  144. },
  145. _hideModal() {
  146. this.setState({modal: this.state.modal.set('open', false)});
  147. },
  148. _acceptRematch() {
  149. const {io, params} = this.props;
  150. io.emit('rematch-accept', {
  151. token: params[0],
  152. time: params[1] * 60,
  153. inc: params[2]
  154. });
  155. this._hideModal();
  156. },
  157. _declineRematch() {
  158. const {io, params} = this.props;
  159. io.emit('rematch-decline', {
  160. token: params[0]
  161. });
  162. this._hideModal();
  163. },
  164. _toggleSounds(e) {
  165. this.setState({
  166. soundsEnabled: !this.state.soundsEnabled
  167. });
  168. },
  169. });
  170. export default GameInterface;