Guest User

Untitled

a guest
Feb 20th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class GameListContainer extends BaseComponent {
  2.     constructor(props, context) {
  3.         super(props, context);
  4.  
  5.         this.engine.action({'action': 'game_list'}).then((response) => {
  6.             console.log('Game list init: ', response.data);
  7.             this.store.dispatch(getGamesSuccess(response.data));
  8.         });
  9.  
  10.  
  11.         this.update = this.update.bind(this);
  12.         document.addEventListener('update-gamelist', this.update);
  13.  
  14.         this.join_game_click = this.join_game_click.bind(this);
  15.         this.join_game = this.join_game.bind(this);
  16.         this.join_game_confirm = this.join_game_confirm.bind(this);
  17.  
  18.         this.state = {
  19.             game_join_to: {},
  20.             game_leave_from: {}
  21.         };
  22.     }
  23.  
  24.     join_game(game) {
  25.         this.engine.action({'action': 'join_game', 'data': {'uid': game.uid}}).then((response) => {
  26.             switch (response.status) {
  27.                 case 'success':
  28.                     console.log('Joined to game: ', response.data.game.name);
  29.                     this.store.dispatch(setCurrentGamesSuccess(response.data.game));
  30.                     this.store.dispatch(setGameSuccess(response.data.game));
  31.                     this.router.push('/game/'+game.uid);
  32.                     break;
  33.                 case 'error':
  34.                     console.log('Join game fail: ', response.msg);
  35.                     break;
  36.             }
  37.  
  38.         });
  39.     }
  40.  
  41.     join_game_click(game) {
  42.         console.log('Joining to... :', game.name);
  43.  
  44.         this.setState({ game_join_to: game });
  45.         this.setState({ game_leave_from: {} });
  46.  
  47.         (async function(that){
  48.             let current_game = await that.engine.get_current_game();
  49.  
  50.             if (current_game) {
  51.  
  52.                 that.setState({ game_leave_from: current_game });
  53.  
  54.                 if (current_game.uid != game.uid) {
  55.                     that.store.dispatch(showJoinModal(true, game, current_game));
  56.                 }
  57.                 else {
  58.                     console.log('You already in this game.');
  59.                     that.store.dispatch(setGameSuccess(game));
  60.                     that.router.push('/game/'+game.uid);
  61.                 }
  62.             }
  63.  
  64.             else {
  65.                 that.join_game(game);
  66.             }
  67.         })(this);
  68.     }
  69.  
  70.     join_game_confirm() {
  71.         console.log('Leave game:', this.state.game_leave_from.name);
  72.         this.join_game(this.state.game_join_to);
  73.     }
  74.  
  75.     update(event) {
  76.         console.log('Game list update: ', event.detail.data);
  77.         this.store.dispatch(getGamesSuccess(event.detail.data));
  78.     }
  79.  
  80.     render() {
  81.         return (
  82.             <div>
  83.                 <GameList games={this.props.games} current_game={this.props.current_game} layout={this.props.layout} join_game_click={this.join_game_click} />
  84.                 <ModalWarningJoinContainer join_confirm={this.join_game_confirm}/>
  85.             </div>
  86.         );
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment