GameInterface.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict';
  2. const React = require('react/addons');
  3. const GameHeader = require('./GameHeader');
  4. const Chat = require('./Chat');
  5. const Immutable = require('immutable');
  6. const {Map} = Immutable;
  7. const GameInterface = React.createClass({
  8. propTypes: {
  9. io: React.PropTypes.object,
  10. params: React.PropTypes.array.isRequired
  11. },
  12. getInitialState() {
  13. return {
  14. color: 'white',
  15. modal: Map({open: false, message: ''}),
  16. soundsEnabled: false
  17. };
  18. },
  19. componentDidMount() {
  20. let {io, params} = this.props;
  21. io.emit('join', {
  22. token: params[0],
  23. time: params[1] * 60,
  24. inc: params[2]
  25. });
  26. io.on('token-invalid', () => this.setState({
  27. modal: this.state.modal
  28. .set('open', true)
  29. .set('message', 'Game link is invalid or has expired')
  30. }));
  31. io.on('joined', data => {
  32. if (data.color === 'white') {
  33. io.emit('timer-white', {
  34. token: params[0]
  35. });
  36. } else {
  37. this.setState({color: 'black'});
  38. }
  39. });
  40. },
  41. render() {
  42. let {io, params} = this.props;
  43. let {color, soundsEnabled} = this.state;
  44. return (
  45. <div>
  46. <GameHeader
  47. io={io}
  48. params={params}
  49. color={color}
  50. toggleModal={this._toggleModal} />
  51. <audio preload="auto" ref="moveSnd">
  52. <source src="/snd/move.mp3" />
  53. <source src="/snd/move.ogg" />
  54. </audio>
  55. <label id="sounds-label">
  56. <input type="checkbox"
  57. checked={soundsEnabled}
  58. onChange={this._toggleSounds} />
  59. <span> Enable sounds</span>
  60. </label>
  61. <Chat
  62. io={io}
  63. token={params[0]}
  64. color={color}
  65. soundsEnabled={soundsEnabled} />
  66. </div>
  67. );
  68. },
  69. _toggleModal(open, message) {
  70. this.setState({
  71. modal: Map({open: open, message: message})
  72. });
  73. },
  74. _toggleSounds() {
  75. this.setState({
  76. soundsEnabled: !this.state.soundsEnabled
  77. });
  78. }
  79. });
  80. module.exports = GameInterface;