Advertisement
pmg2020

Untitled

Oct 25th, 2021
1,167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useEffect } from 'react';
  2. import LobbyPage from '../components/Lobbylayout.js';
  3. import { setHost, setSearch } from '../redux/user/userActions.js';
  4. import { connect } from 'react-redux';
  5.  
  6.  
  7. const Lobby = (props) => {
  8.     const [matchStarted, setMatchStarted] = useState(false);
  9.     const [listaJugadores, setListaJugadores] = useState();
  10.     const [hostActual, setHostActual] = useState();
  11.  
  12.    
  13.     const urlJugadores = "http://127.0.0.1:8000/matches/"+props.lobbyname+'/users/'
  14.     const fetchApiJugadores = async () => {
  15.        
  16.         const responseJSON = await fetch(urlJugadores)
  17.         const response = await responseJSON.json()
  18.        
  19.         setListaJugadores(response)
  20.     }
  21.    
  22.     const urlHost = "http://127.0.0.1:8000/matches/"+props.lobbyname+'/host/'
  23.     const fetchApiHost = async () => {
  24.        
  25.         const responseJSON = await fetch(urlHost)
  26.         const response = await responseJSON.json()
  27.         setHostActual(response[0][0])
  28.     }
  29.    
  30.     useEffect (() =>{
  31.         fetchApiJugadores()
  32.         fetchApiHost()  
  33.         console.log('lobby')
  34.         const urlWs = "ws://127.0.0.1:8000/matches/"+props.lobbyname+'/users/'+props.nick;
  35.         console.log('conexion ws')
  36.         const ws = new WebSocket(urlWs);
  37.                 ws.onopen = function(eventInfo) {
  38.             console.log("Socket connection is open!");      
  39.                        
  40.  
  41.         }
  42.         ws.onmessage = (ev) => {        
  43.             let response = JSON.parse(ev.data);
  44.              
  45.            
  46.             switch(response.notification) {
  47.                 case "someone joined":
  48.                     fetchApiJugadores();
  49.                     break
  50.                
  51.                 case "match is about to start":
  52.                     console.log('comenzar partida')
  53.                     break
  54.  
  55.                 default: console.log('error notif')
  56.             }
  57.            
  58.         }
  59.        
  60.     },[])
  61.  
  62.    
  63.     // TODO display players
  64.     const startMatch = () => {
  65.         let newMatchStarted = matchStarted;
  66.         newMatchStarted = true;
  67.         setMatchStarted(newMatchStarted);
  68.        
  69.     }    
  70.  
  71.     return (
  72.         <LobbyPage
  73.             nick={props.nick}
  74.             lobbyname={props.lobbyname}
  75.             host={hostActual}
  76.             list={listaJugadores}
  77.             startMatch={startMatch}
  78.         />
  79.     )
  80. }
  81.  
  82. const mapStateToProps = state => {
  83.     return {
  84.         nick: state.nick,
  85.         host: state.esHost,
  86.         search: state.usarSearch,
  87.         lobbyname: state.lobbyname        
  88.     }
  89. }
  90.  
  91. const mapDispatchToProps = dispatch => {
  92.     return {
  93.         setHost: () => dispatch(setHost()),
  94.         setSearch: () => dispatch(setSearch())
  95.     }
  96. }
  97.  
  98. export default connect(
  99.     mapStateToProps,
  100.     mapDispatchToProps
  101.     )(Lobby)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement