Guest User

Untitled

a guest
Jan 19th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import { connect } from 'react-redux';
  3. import moment from 'moment';
  4.  
  5. import classnames from 'classnames';
  6. import styles from './index.module.sass';
  7.  
  8. import { FullWidth } from '../../components/Layout';
  9. import Loader from '../../components/Loader';
  10. import Button from '../../components/Button';
  11.  
  12. import GameScene from './scenes/GameScene';
  13. import Countdown from './components/Countdown';
  14. import WelcomeTutorial from './components/WelcomeTutorial';
  15.  
  16. import Flag from '../../assets/icons/flag.png';
  17. import FlagBg from '../../assets/icons/flag-data.png';
  18. import Mode from '../../assets/icons/mode.png';
  19.  
  20. import socket from '../../initialization/sockets';
  21.  
  22. class GameScreen extends Component {
  23.   constructor() {
  24.     super();
  25.  
  26.     this.state = {
  27.       gameRequested: false,
  28.       game: null,
  29.       autostart: false,
  30.     };
  31.  
  32.     this.startGame = this.startGame.bind(this);
  33.     this.handleScroll = this.handleScroll.bind(this);
  34.     this.requestGameData = this.requestGameData.bind(this);
  35.   }
  36.  
  37.   componentDidMount() {
  38.     window.addEventListener('scroll', this.handleScroll);
  39.     this.handleScroll();
  40.     this.requestGameData();
  41.   }
  42.  
  43.   requestGameData() {
  44.     const { id } = this.props;
  45.  
  46.     if (id === null ) {
  47.       this.setState({ gameRequested: true, game: 'empty' });
  48.     }
  49.  
  50.     socket.emit('requestGameData', id);
  51.     socket.on('sendGameData', (data) => {
  52.       if (data.id === this.props.id || data.id.toString() === this.props.id) {
  53.         let game = data.game;
  54.  
  55.         console.log(game.players[0].place)
  56.         this.setState({ gameRequested: true, game, autostart: data.autostart }, () => {
  57.           console.log("callback", this.state.game.players[0].place)
  58.         });
  59.       }
  60.     });
  61.   }
  62.  
  63.   componentWillUnmount() {
  64.     window.removeEventListener('scroll', this.handleScroll);
  65.     socket.off('sendGameData');
  66.   }
  67.  
  68.   componentWillReceiveProps(nextProps) {
  69.     if (nextProps.id && nextProps.id !== this.props.id) {
  70.       socket.emit('requestGameData', nextProps.id);
  71.     }
  72.   }
  73.  
  74.   startGame() {
  75.     const { id } = this.props;
  76.     socket.emit('startGameManually', id);
  77.   }
  78.  
  79.   handleScroll() {
  80.     if (!this.fixedButton || !this.gameContainer) { return; }
  81.     const windowHeight = window.innerHeight;
  82.     const bottomContainer = this.gameContainer.getBoundingClientRect().bottom;
  83.  
  84.  
  85.     if (bottomContainer < windowHeight) {
  86.       this.fixedButton.classList.add(styles.position);
  87.     } else {
  88.       this.fixedButton.classList.remove(styles.position);
  89.     }
  90.   }
  91.  
  92.   render() {
  93.     const { game, gameRequested, autostart } = this.state;
  94.     const { user, id, toggleSidebar, sidebarOpen, empty } = this.props;
  95.  
  96.     return (
  97.       gameRequested
  98.         ? game
  99.           ? (
  100.             <FullWidth setRef={(el) => { this.gameContainer = el; }} customClass={styles.container}>
  101.               { game.stage === 0 && <Countdown {...{ game}} /> }
  102.               { game === 'empty'
  103.                 ? <GameScene empty />
  104.                 : <GameScene {...{ game, user, autostart }} />
  105.               }
  106.               <div
  107.                 ref={(el) => { this.fixedButton = el; }}
  108.                 className={styles.gameData}
  109.               >
  110.                 { id !== null &&
  111.                   <React.Fragment>
  112.                     <div className={styles.gameDataItem}>
  113.                       <img className={styles.gameDataFlag} src={FlagBg} alt=""/>
  114.                       <img className={styles.gameDataIcon} src={Flag} alt="" />
  115.                       <div className={styles.gameDataText}>
  116.                         Game: #{id}
  117.                         <p className={styles.gameDataDetails}>
  118.                           Created:&nbsp;
  119.                           {moment(game.createdAt).format("MM/DD/YY")}
  120.                         </p>
  121.                       </div>
  122.                     </div>
  123.                     <div className={styles.gameDataItem}>
  124.                       <img className={styles.gameDataFlag} src={FlagBg} alt=""/>
  125.                       <img className={styles.gameDataIcon} src={Mode} alt="" />
  126.                       <div className={styles.gameDataText}>
  127.                         Mode
  128.                         <p className={styles.gameDataDetails}>off-chain</p>
  129.                       </div>
  130.                     </div>
  131.                   </React.Fragment>
  132.                 }
  133.                 {(game.stage === 0 || game === 'empty') &&
  134.                   <Button
  135.                     customClass={styles.joinButton}
  136.                     onClick={toggleSidebar}
  137.                   >
  138.                     Join Race
  139.                     <img className={styles.flag} src={Flag} alt="" />
  140.                   </Button>
  141.                 }
  142.               </div>
  143.               { (game.stage === 0 || game === 'empty') && (user.totalGames === 0 || user.account === 'Guest') &&
  144.                 <WelcomeTutorial sidebarOpen={sidebarOpen} />
  145.               }
  146.             </FullWidth>
  147.           )
  148.           : <p>We could not retrive data for game {id}</p>
  149.         : (
  150.           <div>
  151.             <p>Loading game #{id}</p>
  152.             <Loader />
  153.           </div>
  154.         )
  155.     );
  156.   }
  157. }
  158.  
  159. function mapStateToProps(state) {
  160.   return {
  161.     user: state.user,
  162.   };
  163. }
  164.  
  165. export default connect(mapStateToProps)(GameScreen);
Add Comment
Please, Sign In to add comment