GameInterface.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. const React = require('react/addons');
  3. const GameHeader = require('./GameHeader');
  4. const Chat = require('./Chat');
  5. const Modal = require('./Modal');
  6. const Immutable = require('immutable');
  7. const {Map} = Immutable;
  8. const GameInterface = React.createClass({
  9. propTypes: {
  10. io: React.PropTypes.object,
  11. params: React.PropTypes.array.isRequired
  12. },
  13. getInitialState() {
  14. return {
  15. color: 'white',
  16. modal: Map({
  17. open: false,
  18. message: '',
  19. type: 'info',
  20. callbacks: {
  21. hide: this._hideModal,
  22. accept: this._acceptRematch,
  23. decline: this._declineRematch
  24. }
  25. }),
  26. soundsEnabled: false
  27. };
  28. },
  29. componentDidMount() {
  30. let {io, params} = this.props;
  31. io.emit('join', {
  32. token: params[0],
  33. time: params[1] * 60,
  34. inc: params[2]
  35. });
  36. io.on('token-invalid', () => this.setState({
  37. modal: this.state.modal
  38. .set('open', true)
  39. .set('message', 'Game link is invalid or has expired')
  40. .set('type', 'info')
  41. }));
  42. io.on('joined', data => {
  43. if (data.color === 'white') {
  44. io.emit('timer-white', {
  45. token: params[0]
  46. });
  47. } else {
  48. this.setState({color: 'black'});
  49. }
  50. });
  51. },
  52. render() {
  53. let {io, params} = this.props;
  54. let {color, soundsEnabled} = this.state;
  55. return (
  56. <div>
  57. <GameHeader
  58. io={io}
  59. params={params}
  60. color={color}
  61. openModal={this._openModal} />
  62. <audio preload="auto" ref="moveSnd">
  63. <source src="/snd/move.mp3" />
  64. <source src="/snd/move.ogg" />
  65. </audio>
  66. <label id="sounds-label">
  67. <input type="checkbox"
  68. checked={soundsEnabled}
  69. onChange={this._toggleSounds} />
  70. <span> Enable sounds</span>
  71. </label>
  72. <Chat
  73. io={io}
  74. token={params[0]}
  75. color={color}
  76. soundsEnabled={soundsEnabled} />
  77. <Modal data={this.state.modal} />
  78. </div>
  79. );
  80. },
  81. _openModal(type, message) {
  82. this.setState({
  83. modal: this.state.modal
  84. .set('open', true)
  85. .set('message', message)
  86. .set('type', type)
  87. });
  88. },
  89. _hideModal() {
  90. this.setState({modal: this.state.modal.set('open', false)});
  91. },
  92. _acceptRematch() {
  93. let {io, params} = this.props;
  94. io.emit('rematch-confirm', {
  95. token: params[0],
  96. time: params[1] * 60,
  97. inc: params[2]
  98. });
  99. },
  100. _declineRematch() {
  101. let {io, params} = this.props;
  102. io.emit('rematch-decline', {
  103. token: params[0]
  104. });
  105. },
  106. _toggleSounds() {
  107. this.setState({
  108. soundsEnabled: !this.state.soundsEnabled
  109. });
  110. },
  111. });
  112. module.exports = GameInterface;