示例#1
0
        private void AtenderServidor()//Funcion que atiende al servidor. Dependiendo del codigo que reciba hara unas acciones.
        {
            while (true)
            {
                byte[] msg2 = new byte[80];
                try
                {
                    server.Receive(msg2);
                }
                catch (SocketException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }

                string[] mensaje = Encoding.ASCII.GetString(msg2).Split(':');

                int    codigo    = Convert.ToInt32(mensaje[0]);
                string contenido = mensaje[1].Split('\0')[0];


                switch (codigo)
                {
                case 1:
                    if (contenido == "0")
                    {
                        DelegadoInicioSesion delegadoStart = new DelegadoInicioSesion(DelegarInicioSesion);
                        Enviar.Invoke(delegadoStart);
                        MessageBox.Show("Sesion Iniciada");
                    }
                    else
                    {
                        string mensaje2 = "0/" + User.Text;
                        byte[] msg3     = System.Text.Encoding.ASCII.GetBytes(mensaje2);
                        server.Send(msg3);
                        server.Shutdown(SocketShutdown.Both);
                        server.Close();
                        MessageBox.Show("El nombre ya está registrado");
                        Atender.Abort();
                    }
                    break;

                case 2:
                    if (contenido == "0")
                    {
                        DelegadoInicioSesion delegadoStart = new DelegadoInicioSesion(DelegarInicioSesion);
                        Enviar.Invoke(delegadoStart);
                        MessageBox.Show("Sesion Iniciada");
                    }
                    else
                    {
                        string mensaje2 = "0/" + User.Text;
                        byte[] msg3     = System.Text.Encoding.ASCII.GetBytes(mensaje2);
                        server.Send(msg3);
                        server.Shutdown(SocketShutdown.Both);
                        server.Close();
                        MessageBox.Show("El nombre y/o la contraseña son incorrectos.");
                        Atender.Abort();
                    }
                    break;

                case 3:
                    if (contenido == "NoEncontrado")
                    {
                        MessageBox.Show("No se ha encontrado el jugador");
                    }
                    else if (contenido != "")
                    {
                        MessageBox.Show("Número de turnos:" + contenido);
                    }
                    else
                    {
                        MessageBox.Show("Error");
                    }
                    break;

                case 4:
                    if (contenido == "NoEncontrado")
                    {
                        MessageBox.Show("No se ha encontrado la partida");
                    }
                    else if (contenido != "")
                    {
                        MessageBox.Show("Jugadores:" + contenido);
                    }
                    else
                    {
                        MessageBox.Show("Error");
                    }
                    break;

                case 5:
                    if (contenido == "NoEncontrado")
                    {
                        MessageBox.Show("No se ha encontrado el jugador");
                    }
                    else if (contenido != "")
                    {
                        MessageBox.Show("Número de turnos:" + contenido);
                    }
                    else
                    {
                        MessageBox.Show("Error");
                    }
                    break;

                case 6:
                    DelegadoListaConectados delegadoLista = new DelegadoListaConectados(RellenarListaConectados);
                    dataGridView1.Invoke(delegadoLista, new object[] { contenido });
                    break;

                case 7:
                    DelegadoInvitacionRecibida delegadoInv = new DelegadoInvitacionRecibida(DelegarInvitacionRecibida);
                    Invitacion.Invoke(delegadoInv, new object[] { contenido });
                    UsuarioInvita = contenido;
                    break;

                case 8:
                    Aceptados.Add(contenido);
                    Respuestas.Add(contenido);
                    MessageBox.Show(contenido + " ha aceptado la partida");
                    if (Invitaciones == Aceptados.Count)
                    {
                        MessageBox.Show("Todos los jugadores han aceptado la partida");
                        Empezar_Partida();
                        Respuestas.Clear();
                        Aceptados.Clear();
                    }
                    else if ((Invitaciones == Respuestas.Count()) && (Respuestas.Count != Aceptados.Count()))
                    {
                        MessageBox.Show("Algun jugador ha rechazado la partida");
                        DelegadoInvitacionRechazada delegadorech1 = new DelegadoInvitacionRechazada(DelegarInvitacionRechazada);
                        Invite.Invoke(delegadorech1);
                        Invitaciones = 0;
                        Respuestas.Clear();
                        Aceptados.Clear();
                    }
                    break;

                case 9:
                    MessageBox.Show(contenido + " ha rechazado la partida");
                    Respuestas.Add(contenido);
                    if ((Invitaciones == Respuestas.Count()) && (Respuestas.Count != Aceptados.Count()))
                    {
                        DelegadoInvitacionRechazada delegadorech2 = new DelegadoInvitacionRechazada(DelegarInvitacionRechazada);
                        Invite.Invoke(delegadorech2);
                        MessageBox.Show("Algun jugador ha rechazado la partida");
                        Invitaciones = 0;
                        Respuestas.Clear();
                        Aceptados.Clear();
                    }
                    break;

                case 10:
                    IDChat = Convert.ToInt32(contenido);
                    MessageBox.Show("Iniciando partida " + IDChat);
                    DelegadoInvitacionRechazada delegadorech3 = new DelegadoInvitacionRechazada(DelegarInvitacionRechazada);
                    Invite.Invoke(delegadorech3);
                    string mensaje3 = "13/" + IDChat + "," + User.Text + "," + EquipoBatallaPropio.GetPokemon(0).Nombre + "," + EquipoBatallaPropio.GetPokemon(1).Nombre + "," + EquipoBatallaPropio.GetPokemon(2).Nombre;
                    byte[] msg      = System.Text.Encoding.ASCII.GetBytes(mensaje3);
                    server.Send(msg);
                    break;

                case 11:
                    int ID = Convert.ToInt32(contenido.Split('-')[0]);
                    contenido = contenido.Split('-')[1];
                    int IDindex = BuscarID(ID);
                    Chats[IDindex].EscribirMensaje(contenido);
                    break;

                case 12:
                    ID = Convert.ToInt32(contenido.Split('-')[0]);
                    string usuario = contenido.Split('-')[1];
                    int    salida  = Convert.ToInt32(contenido.Split('-')[2]);
                    IDindex = BuscarID(ID);
                    if (salida == 1)
                    {
                        MessageBox.Show("Partida finalizada");
                    }
                    else
                    {
                        MessageBox.Show("El usuario " + usuario + " ha abandonado la partida");
                    }
                    Chats[IDindex].AbandonarPartida();
                    break;

                case 13:
                    string[] TuEquipo = contenido.Split(',');
                    SetEquipoPropio(TuEquipo[0], TuEquipo[1], TuEquipo[2]);
                    DelegadoActivarInvitacion delegadoInvite = new DelegadoActivarInvitacion(DelegarActivarInvitacion);
                    Invite.Invoke(delegadoInvite);
                    break;

                case 14:
                    string[] content = contenido.Split('-');
                    int      IDa     = Convert.ToInt32(content[0]);
                    contenido = content[1];
                    content   = contenido.Split(',');
                    bool OponenteRecibido = false;
                    if (content[0] != User.Text)
                    {
                        SetEquipoOponente(content[1], content[2], content[3]);
                        Oponente         = content[0];
                        OponenteRecibido = true;
                    }
                    if (OponenteRecibido == true)
                    {
                        ThreadStart ts2   = delegate { AbrirChat(); };
                        Thread      forms = new Thread(ts2);
                        forms.Start();
                    }
                    break;

                case 15:
                    ID        = Convert.ToInt32(contenido.Split('-')[0]);
                    contenido = contenido.Split('-')[1];
                    IDindex   = BuscarID(ID);
                    Chats[IDindex].bt.RecibirOrden(contenido);
                    break;

                case 16:
                    if (contenido == "0")
                    {
                        MessageBox.Show("Usuario Eliminado");
                        DelegadoDesconexion delegadoStart = new DelegadoDesconexion(DelegarDesconexion);
                        Desconexion.Invoke(delegadoStart);
                    }
                    else
                    {
                        MessageBox.Show("No se ha podido eliminar el usuario");
                    }
                    break;

                case 17:
                    ID        = Convert.ToInt32(contenido.Split('-')[0]);
                    contenido = contenido.Split('-')[1];
                    IDindex   = BuscarID(ID);
                    Chats[IDindex].PokemonDebilitadoDelegado(contenido);
                    break;
                }
            }
        }