Ejemplo n.º 1
0
 public void insertar_jugador(Jugador jugador)
 {
     jugadores.Add(jugador);
 }
Ejemplo n.º 2
0
        public static void clienteHilo(Socket handler, int cont)
        {
            byte[]  bytes   = new Byte[200480];
            Jugador jugador = new Jugador(0, 0, "");

            try
            {
                while (true)
                {
                    int bytesRec = handler.Receive(bytes);
                    mut.WaitOne();
                    //Console.WriteLine("Waiting for a connection...");
                    cont = verificarContador(cont, jugador);
                    Dictionary <string, object> hash = DeserializaObject(bytes);

                    if (hash["Accion"].ToString() == "Registro")
                    {
                        Dictionary <string, object> jug = (Dictionary <string, object>)hash["Objeto"];
                        jugador = new Jugador(0, 0, "");
                        jugador.recombertirDat(jug);

                        registro(jugador);
                        iniciarJuego();
                    }
                    else if (hash["Accion"].ToString() == "Apuesta")
                    {
                        mesa.setApuesta(jugador.Nickname, (int)hash["Cantidad"]);
                    }
                    else if (hash["Accion"].ToString() == "Iniciar Juego")
                    {
                        iniciarJuego();
                        Dictionary <string, object> hash2 = new Dictionary <string, object>();

                        Dictionary <string, object> mes = mesa.combertirDat();
                        hash2.Add("Mesa", mes);
                        Dictionary <string, object> hash1 = new Dictionary <string, object>();

                        hash1.Add("Accion", "datJugador");
                        hash1.Add("Objeto", hash2);
                        byte[] msg = SerializeObject(hash1);

                        int bytesSent = handler.Send(msg);
                    }
                    else if (hash["Accion"].ToString() == "Log In")
                    {
                        Authentication auth = new Authentication();
                        bool           res  = false;
                        Dictionary <string, object> response = new Dictionary <string, object>();

                        response.Add("Accion", "Log In");
                        Console.WriteLine(hash["user"].ToString() + ", " + hash["password"].ToString());
                        if (auth.validate(hash["user"].ToString(), hash["password"].ToString()))
                        {
                            response.Add("response", "true");
                        }
                        else
                        {
                            response.Add("response", "false");
                        }
                        byte[] msg = SerializeObject(response);

                        int bytesSent = handler.Send(msg);
                    }
                    else if (hash["Accion"].ToString() == "Sign Up")
                    {
                        Authentication auth = new Authentication();
                        Dictionary <string, object> response = new Dictionary <string, object>();

                        response.Add("Accion", "Sign Up");
                        Console.WriteLine(hash["user"].ToString() + ", " + hash["password"].ToString());
                        //if (auth.create("jvega", "JersonVega", "*****@*****.**"))
                        if (auth.create(hash["user"].ToString(), hash["password"].ToString(), ""))
                        {
                            response.Add("response", "true");
                        }
                        else
                        {
                            response.Add("response", "false");
                        }
                        byte[] msg = SerializeObject(response);

                        int bytesSent = handler.Send(msg);
                    }
                    else if (hash["Accion"].ToString() == "getDatos")
                    {
                        if (inicio != false)
                        {
                            //mesa.repartir_cartas_inicio();
                            // Dictionary<string, object> hash2 = new Dictionary<string, object>();

                            //hash2.Add("Mesa", mes);
                            Dictionary <string, object> hash1 = new Dictionary <string, object>();

                            hash1.Add("Accion", "getDatos");
                            List <Jugador> jugadores = new List <Jugador>();
                            if (turno.Count == 0)
                            {
                                //cuenta crupier
                                //aqui
                                Console.WriteLine("Termino ronda");
                                //variables de los puntos
                                int puntosJugador = 0;
                                //se ponen los puntos del crupier
                                int puntosCrupier = 0;
                                foreach (Carta c in mesa.Crupier.mano)
                                {
                                    puntosCrupier += c.valor;
                                }
                                Console.WriteLine(puntosCrupier);
                                while (true)
                                {
                                    if (puntosCrupier > 16)
                                    {
                                        break;
                                    }
                                    mesa.obtener_Carta_Crupier();
                                    puntosCrupier = 0;
                                    foreach (Carta c in mesa.Crupier.mano)
                                    {
                                        puntosCrupier += c.valor;
                                    }
                                }
                                Console.WriteLine(puntosCrupier);
                                hash1.Add("Estado", false);
                                int contadorJugadores = 0;

                                foreach (Jugador j in mesa.getJugadores())
                                {
                                    jugadores.Add(j);
                                    partida[contadorJugadores] = false;
                                    foreach (Carta c in j.mano)
                                    {
                                        puntosJugador += c.valor;
                                    }
                                    if (puntosJugador < 22)
                                    {
                                        if (puntosCrupier > 21)
                                        {
                                            j.total_dinero += (j.Apuesta * 2);
                                        }
                                        else if (puntosJugador > puntosCrupier)
                                        {
                                            j.total_dinero += (j.Apuesta * 2);
                                        }
                                        else if (puntosJugador < puntosCrupier)
                                        {
                                            j.total_dinero -= j.Apuesta;
                                        }
                                    }
                                    else
                                    {
                                        j.total_dinero -= j.Apuesta;
                                    }
                                    puntosJugador = 0;
                                    contadorJugadores++;
                                }
                                //ronda = false;

                                reiniciar(jugadores);
                            }
                            else
                            {
                                hash1.Add("Estado", true);
                            }
                            Dictionary <string, object> mes = mesa.combertirDat();
                            hash1["Mesa"] = mes;
                            byte[] msg       = SerializeObject(hash1);
                            int    bytesSent = handler.Send(msg);
                        }
                        else
                        {
                            byte[] msg       = new byte[0];
                            int    bytesSent = handler.Send(msg);
                        }
                    }
                    else if (hash["Accion"].ToString() == "TerminarRonda")
                    {
                        if (ronda == false)
                        {
                            List <Jugador> jugadores = new List <Jugador>();
                            foreach (Jugador j in mesa.getJugadores())
                            {
                                jugadores.Add(j);
                            }
                            reiniciar(jugadores);
                            ronda = true;
                        }
                        partida[cont] = true;
                    }
                    else if (hash["Accion"].ToString() == "TerminarTurno")
                    {
                        if (turno.Peek().Nickname.Equals(hash["Jugador"].ToString()))
                        {
                            turno.Dequeue();
                        }
                        Console.WriteLine("\nEl jugador {0} termino su turno...", hash["Jugador"].ToString());
                    }
                    else if (hash["Accion"].ToString() == "AbandonarPartida")
                    {
                        List <Jugador> jugadors = new List <Jugador>();
                        string         name     = hash["Jugador"].ToString();
                        while (turno.Count != 0)
                        {
                            if (turno.Peek().Nickname.Equals(name))
                            {
                                turno.Dequeue();
                            }
                            else
                            {
                                jugadors.Add(turno.Dequeue());
                            }
                        }
                        foreach (Jugador jugado in jugadors)
                        {
                            turno.Enqueue(jugado);
                        }

                        Console.WriteLine("\nEl jugador {0} abandonó la partida...", name);
                        //listbool[cont] = false;
                        mesa.getJugadores()[cont].Estado = false;
                        //mesa.getJugadores().RemoveAt(cont);
                        //partida.RemoveAt(cont);
                        mut.ReleaseMutex();
                        break;
                    }
                    else if (hash["Accion"].ToString() == "setDatos")
                    {
                        if (turno.Peek().Nickname.Equals(hash["Jugador"].ToString()))
                        {
                            if (mesa.getJugadores().Count >= 2)
                            {
                                mesa.Repartidor.recombertirStack((Stack <Dictionary <string, object> >)hash["Objeto"]);
                            }
                        }
                    }
                    else if (hash["Accion"].ToString() == "ObtenerCarta")
                    {
                        if (turno.Count != 0)
                        {
                            if (turno.Peek().Nickname.Equals(hash["Jugador"].ToString()))
                            {
                                int sumaCartas = mesa.getJugador(hash["Jugador"].ToString()).mano.Count;
                                int sumaPuntos = 0;

                                foreach (Carta m in mesa.getJugador(hash["Jugador"].ToString()).mano)
                                {
                                    sumaPuntos += m.valor;
                                    Console.WriteLine(m.valor);
                                }

                                Dictionary <string, object> hash1 = new Dictionary <string, object>();

                                if (sumaPuntos < 21)
                                {
                                    Carta car = mesa.obtener_carta(turno.Peek());
                                    hash1.Add("Accion", "ObtenerCarta");
                                    hash1.Add("Objeto", true);
                                    hash1.Add("Mesa", mesa.combertirDat());
                                    hash1.Add("Carta", car.combertirDat());
                                }
                                else
                                {
                                    hash1.Add("Accion", "ObtenerCarta");
                                    hash1.Add("Objeto", false);
                                    hash1.Add("Mensaje", "mas21");
                                }
                                byte[] msg       = SerializeObject(hash1);
                                int    bytesSent = handler.Send(msg);
                            }
                            else
                            {
                                Dictionary <string, object> hash1 = new Dictionary <string, object>();

                                hash1.Add("Accion", "ObtenerCarta");
                                hash1.Add("Objeto", false);
                                hash1.Add("Mensaje", "noTurno");
                                byte[] msg       = SerializeObject(hash1);
                                int    bytesSent = handler.Send(msg);
                            }
                        }
                        else
                        {
                            Dictionary <string, object> hash1 = new Dictionary <string, object>();

                            hash1.Add("Accion", "ObtenerCarta");
                            hash1.Add("Objeto", false);
                            byte[] msg       = SerializeObject(hash1);
                            int    bytesSent = handler.Send(msg);
                        }
                    }
                    else if (hash["Accion"].ToString() == "estPartida")
                    {
                        Dictionary <string, object> hash1 = new Dictionary <string, object>();
                        hash1.Add("Accion", "estPartida");
                        if (partida[cont] == false)
                        {
                            Dictionary <string, object> mes = mesa.combertirDat();
                            hash1.Add("Mesa", mes);
                            hash1.Add("Reiniciar", true);
                        }
                        else
                        {
                            hash1.Add("Reiniciar", false);
                        }

                        byte[] msg       = SerializeObject(hash1);
                        int    bytesSent = handler.Send(msg);
                    }

                    //Console.WriteLine("Text received : {0}", hash["pearl"]);
                    mut.ReleaseMutex();
                }
            }
            catch (Exception)
            {
                List <Jugador> jugadors = new List <Jugador>();
                string         name     = jugador.Nickname;
                while (turno.Count != 0)
                {
                    if (turno.Peek().Nickname.Equals(name))
                    {
                        turno.Dequeue();
                    }
                    else
                    {
                        jugadors.Add(turno.Dequeue());
                    }
                }
                foreach (Jugador jugado in jugadors)
                {
                    turno.Enqueue(jugado);
                }

                Console.WriteLine("\nEl jugador {0} abandonó la partida...", name);
                mesa.getJugadores()[cont].Estado = false;
            }
            try
            {
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
            catch (Exception) {
                Console.WriteLine("Server Update: Code 341");
            }
            listHilos[cont].Abort();
            //listHilos.RemoveAt(cont);
        }