Ejemplo n.º 1
0
        public string entrarCola(Jugador jugador)
        {
            int    min    = jugador.puntaje - 200;
            int    max    = jugador.puntaje + 200;
            string nombre = jugador.usuario;
            string rival  = "";
            string juga   = "";

            Byte[] sendBytes = null;
            cola.Add(jugador);
            //Se agrega el usuario a la cola y con el foreach busca un posible rival
            foreach (var player in cola)
            {
                if (player.puntaje >= min && player.puntaje <= max && player.usuario != nombre)
                {
                    //Se obtiene la conexion del rival y se setea la conexion de ambos en null para que se pueda serializar y enviar
                    TcpClient conexion = player.conexion;

                    Partida.setJugador1(player);
                    Partida.verificarConexiones1(player);
                    player.conexion = null;
                    rival           = JsonConvert.SerializeObject(player);

                    Partida.setJugador2(jugador);
                    Partida.verificarConexiones2(jugador);
                    jugador.conexion = null;
                    juga             = JsonConvert.SerializeObject(jugador);

                    //Se obtiene el jugador reciente de la cola y se envia un mensaje al jugador que se quedo esperando a su rival
                    string serverResponse = juga;
                    sendBytes = System.Text.Encoding.ASCII.GetBytes(serverResponse);
                    byte[]        intBytes      = BitConverter.GetBytes(sendBytes.Length);
                    NetworkStream networkStream = conexion.GetStream();
                    networkStream.Write(intBytes, 0, intBytes.Length);
                    networkStream.Write(sendBytes, 0, sendBytes.Length);
                    networkStream.Flush();

                    break;
                }
            }
            return(rival);
        }
Ejemplo n.º 2
0
        private void doChat()
        {
            try
            {
                string dataFromClient = null;

                Byte[] sendBytes      = null;
                string serverResponse = null;

                while (true)
                {
                    // Recibi mensaje
                    byte[]        bytesFrom     = new byte[4];
                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Read(bytesFrom, 0, bytesFrom.Length);
                    int buffersize = BitConverter.ToInt32(bytesFrom, 0);
                    bytesFrom = new byte[buffersize];
                    networkStream.Read(bytesFrom, 0, bytesFrom.Length);
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);

                    //Verifico si es un usuario
                    if (dataFromClient.Contains("usuario"))
                    {
                        Jugador jugador = JsonConvert.DeserializeObject <Jugador>(dataFromClient);
                        jugador.setConexion(clientSocket);
                        Partida.setTcpClients(clientSocket);

                        //Depende del mensaje que mande un usuario, el servidor realiza una acción distinta
                        switch (jugador.mensaje)
                        {
                        case 0:
                            System.Console.WriteLine("Jugador " + jugador.usuario + " ha ingresado al servidor");
                            System.Console.WriteLine("Id: " + jugador.id + "/ Nombre: " + jugador.usuario + "/ Puntaje: " + jugador.puntaje + "/ Victorias: " + jugador.calcularPorcentaje() + "%");
                            enviarMensaje("ok");
                            break;

                        case 1:
                            string rival = ListadoCola.getInstancia().entrarCola(jugador);
                            System.Console.WriteLine("Jugador " + jugador.usuario + " ha ingresado a la cola");
                            if (rival != "")
                            {
                                //Se envia al rival y se vuelve a setear la conexion para que no quede en null
                                enviarMensaje(rival);
                                jugador.setConexion(clientSocket);
                            }
                            break;

                        case 2:
                            ListadoCola.getInstancia().salirCola(jugador);
                            System.Console.WriteLine("Jugador " + jugador.usuario + " ha salido de la cola");
                            enviarMensaje("ok");
                            Partida.reiniciarChecks();
                            break;
                        }
                    }
                    else
                    {
                        Partida.interpretarMensaje(dataFromClient, clientSocket);
                        //Mensajes provenientes de la partida
                    }



                    void enviarMensaje(string mensaje)
                    {
                        serverResponse = mensaje;
                        sendBytes      = System.Text.Encoding.ASCII.GetBytes(serverResponse);
                        byte[] intBytes = BitConverter.GetBytes(sendBytes.Length);
                        networkStream.Write(intBytes, 0, intBytes.Length);
                        networkStream.Write(sendBytes, 0, sendBytes.Length);
                        networkStream.Flush();
                    }
                }
            }
            catch (System.IO.IOException)
            {
                Console.WriteLine("Un usuario se ha desconectado");
            }
        }