private void cierraServer()
        {
            My_SQL mysql = new My_SQL();
            int    nq    = mysql.hazNoConsulta("update usuarios set connected='0', playing='0'");

            if (hiloEscucha != null)
            {
                hiloEscucha.Abort();
                hiloRegenera.Abort();
                servidor.Stop();
                for (int i = 0; i < ctes; i++)
                {
                    if (clientes[i] != null)
                    {
                        clientes[i].cierraCte("");
                        hilosCte[i].Abort();
                    }
                }
            }
            if (ctesUDP != null)
            {
                ctesUDP.cierraServidor();
                while (!ctesUDP.cerrado)
                {
                    Thread.Sleep(10);
                }
                ctesUDP.closeServer();
                ctesUDP = null;
            }
            if (hiloUDP != null)
            {
                hiloUDP.Abort();
            }
        }
 private void btnEliminar_Click(object sender, EventArgs e)
 {
     if (sel >= 0)
     {
         My_SQL mysql;
         int    nq;
         mysql = new My_SQL();
         nq    = mysql.hazNoConsulta("delete from " + roll + " where " + cam[0] + " = '" + val[0] + "'");
         dgv.Hide();
         SetupDataGridView();
         PopulateDataGridView("");
         actualiza();
         MessageBox.Show("Registro Borrado");
     }
 }
        private void btnIniciar_Click(object sender, EventArgs e)
        {
            My_SQL mysql = new My_SQL();
            int    nq    = mysql.hazNoConsulta("update usuarios set connected='0', playing='0'");

            btnIniciar.Enabled = false;
            btnDetener.Enabled = true;
            ctesUDP            = new HiloComsUDP();
            hiloUDP            = new Thread(new ThreadStart(ctesUDP.startserver));
            hiloUDP.Start();
            hiloEscucha = new Thread(new ThreadStart(this.hilo_Escucha));
            hiloEscucha.Start();
            hiloRegenera = new Thread(new ThreadStart(this.hilo_Regenera));
            hiloRegenera.Start();
        }
        internal void cierraCte(String cad)
        {
            My_SQL mysql;
            int    nq;

            if (cad == "CLOSED" && uso == "RETAR")
            {
                ctesUDP.send("GRANTEDCLOSE@", nCte);
            }
            else
            if (uso == "RETAR" || uso == "NOTHIGN")
            {
                ctesUDP.send("SERVERCLOSED@", nCte);
            }
            if (cte != null)
            {
                cte.Close();
                cte = null;
            }
            unload = true;
            if (user != "")
            {
                mysql = new My_SQL();
                nq    = mysql.hazNoConsulta("update usuarios set connected='0', playing='0' where user='******'");
                if (oponent != "")
                {
                    try
                    {
                        ctesUDP.send("LOSEOPONENT@", op);
                        if (lCte[op] != null)
                        {
                            cambiaTxt(lCte[op], "LISTO:" + oponent);
                        }
                    }
                    catch (Exception e1)
                    {
                        MessageBox.Show("Erro al Cerrar al Oponente " + e1.ToString());
                    }
                    mysql = new My_SQL();
                    nq    = mysql.hazNoConsulta("update usuarios set playing='0' where user='******'");
                }
            }
        }
        private void PopulateDataGridView(String cond)
        {
            My_SQL         mysql = new My_SQL();
            OdbcDataReader res;

            res = mysql.hazConsulta("select * from usuarios " + cond);
            int i;

            String[] row = new String[colums];
            if (res.HasRows)
            {
                while (res.Read())
                {
                    for (i = 0; i < colums; i++)
                    {
                        row[i] = res.GetString(i);
                    }
                    dgv.Rows.Add(row);
                }
            }
        }
Exemple #6
0
 private void btnAceptar_Click(object sender, EventArgs e)
 {
     if (uso == "Agregar" || uso == "Modificar")
     {
         if (txbCampo[0].Text != "" && txbCampo[1].Text != "" && txbCampo[2].Text != "")
         {
             if (roll == "usuarios")
             {
                 try
                 {
                     My_SQL c = new My_SQL();
                     int    nq;
                     if (uso == "Agregar")
                     {
                         nq = c.hazNoConsulta("insert into usuarios values('" + txbCampo[0].Text + "','" + txbCampo[1].Text + "','" + txbCampo[2].Text + "','0','0')");
                     }
                     if (uso == "Modificar")
                     {
                         nq = c.hazNoConsulta("update usuarios set user='******',nombre='" + txbCampo[1].Text + "',pssw='" + txbCampo[2].Text + "' where user='******'");
                     }
                     this.Close();
                 }
                 catch (Exception exc1)
                 {
                     MessageBox.Show(exc1.ToString());
                 }
             }
         }
         else
         {
             MessageBox.Show("No se admiten campos vacios");
         }
     }
     if (uso == "Buscar")
     {
         if (txbCampo[0].Text != "")
         {
             Sval[0] = txbCampo[0].Text.ToUpper();
         }
         if (roll == "usuarios")
         {
             if (txbCampo[1].Text != "")
             {
                 Sval[1] = txbCampo[1].Text;
             }
             if (txbCampo[2].Text != "")
             {
                 Sval[2] = txbCampo[2].Text;
             }
             if (cbConn.Text != "")
             {
                 try
                 {
                     bool s1 = bool.Parse(cbConn.Text);
                     if (s1)
                     {
                         Sval[3] = "1";
                     }
                     else
                     {
                         Sval[3] = "0";
                     }
                 }
                 catch (FormatException formato31)
                 {
                     MessageBox.Show("Connected es de Tipo Bool " + formato31.ToString());
                 }
                 catch (Exception exc31)
                 {
                     MessageBox.Show(exc31.ToString());
                 }
             }
             if (cbPlay.Text != "")
             {
                 try
                 {
                     bool s2 = bool.Parse(cbPlay.Text);
                     if (s2)
                     {
                         Sval[4] = "1";
                     }
                     else
                     {
                         Sval[4] = "0";
                     }
                 }
                 catch (FormatException formato310)
                 {
                     MessageBox.Show("Connected es de Tipo Bool " + formato310.ToString());
                 }
                 catch (Exception exc310)
                 {
                     MessageBox.Show(exc310.ToString());
                 }
             }
         }
         this.Close();
     }
 }
        public void startserver()
        {
            // 1-Create an IPEndPoint to receive messages
            IPEndPoint recvpt = new IPEndPoint(IPAddress.Any, 0);

            byte[]   data;
            String[] cds;
            String   str, aux;
            int      i, nq;
            My_SQL   mysql = null;

            while (activo)
            {
                try
                {
                    // 2-Receive data
                    data = server.Receive(ref recvpt);
                    str  = Encoding.ASCII.GetString(data);
                    cds  = str.Split('@');
                    // 3-Check the answer
                    if (cds[0] == "identifica")
                    {
                        i              = int.Parse(cds[2]);
                        ctes[i]        = new ClientesUDP();
                        ctes[i].usr    = cds[1];
                        ctes[i].nCte   = i;
                        ctes[i].pcName = cds[3];
                    }
                    if (cds[0] == "partida")
                    {
                        if (cds[1] == "CERRAR")
                        {
                            i = int.Parse(cds[2]);
                            send("partida@GRANTEDCLOSEGAME@NOCLOSEBOARD", i);
                            i = int.Parse(cds[3]);
                            send("partida@GRANTEDCLOSEGAME@CLOSEBOARD", i);
                            mysql = new My_SQL();
                            nq    = mysql.hazNoConsulta("update usuarios set playing='0' where user='******' or user='******'");
                        }
                        if (cds[1] == "MOVIMIENTO")
                        {
                            i = int.Parse(cds[2]);
                            send("partida@MOVIMIENTO@" + cds[4] + "@" + cds[5] + "@" + cds[6] + "@" + cds[7] + "@" + cds[8], i);
                            i = int.Parse(cds[3]);
                            send("partida@MOVIMIENTO@" + cds[4] + "@" + cds[5] + "@" + cds[6] + "@" + cds[7] + "@" + cds[8], i);
                        }
                    }
                    if (cds[0] == "cierraserver")
                    {
                        activo = false;
                        for (i = 0; i < 10; i++)
                        {
                            if (ctes[i] != null)
                            {
                                if (ctes[i].usr != "")
                                {
                                    send("cierraserver@CERRAR", ctes[i].nCte);
                                }
                            }
                        }
                    }
                }
                catch (Exception e1)
                {
                    MessageBox.Show("Problemas con el Servidor " + e1.ToString());
                }
            }
            cerrado = true;
        }
        internal void atiende()
        {
            byte[] bytes = new byte[256];
            String datos = null, edo;

            String[]       cds, row;
            int            i = 0, nq, x;
            My_SQL         mysql = null;
            OdbcDataReader res   = null;

            while (cte != null)
            {
                try
                {
                    if ((me && !disponible) || (!me && disponible))
                    {
                        if ((i = flujo.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            datos = Encoding.ASCII.GetString(bytes, 0, i);
                            if (datos.Equals("@X"))
                            {
                                cierraCte("CLOSED");
                            }
                            else
                            {
                                cds = datos.Split('@');
                                if (cds[0] == "login")
                                {
                                    uso   = "LOGIN";
                                    mysql = new My_SQL();
                                    res   = mysql.hazConsulta("select * from usuarios where user='******' and pssw='" + cds[2] + "'");
                                    if (res.HasRows)
                                    {
                                        if (res.Read())
                                        {
                                            edo = res.GetString(3);
                                            if (edo == "1")
                                            {
                                                notificaEdo("login@ALREADYCONNECTED");
                                            }
                                            else
                                            {
                                                nq = mysql.hazNoConsulta("update usuarios set connected='1' where user='******'");
                                                cambiaTxt(lCte[nCte], cds[1] + ":" + cds[2]);
                                                notificaEdo("login@GRANTED");
                                                user = cds[1];
                                            }
                                        }
                                    }
                                    else
                                    {
                                        notificaEdo("login@DENIED");
                                    }
                                }
                                if (cds[0] == "registro")
                                {
                                    uso   = "REGISTRO";
                                    mysql = new My_SQL();
                                    res   = mysql.hazConsulta("select * from usuarios where user='******'");
                                    row   = new String[4];
                                    if (res.HasRows)
                                    {
                                        notificaEdo("registro@EXIST");
                                    }
                                    else
                                    {
                                        mysql = new My_SQL();
                                        nq    = mysql.hazNoConsulta("insert into usuarios values('" + cds[1] + "','" + cds[2] + "','" + cds[3] + "','1','0')");
                                        cambiaTxt(lCte[nCte], cds[1] + ":" + cds[2]);
                                        notificaEdo("registro@GRANTED");
                                        user = cds[1];
                                    }
                                }
                                if (cds[0] == "retador")
                                {
                                    uso = "RETAR";
                                    if (cds[1] == "LISTO")
                                    {
                                        mysql = new My_SQL();
                                        user  = cds[2];
                                        cambiaTxt(lCte[nCte], cds[1] + ":" + cds[2]);
                                        nq = mysql.hazNoConsulta("update usuarios set connected='1' where user='******'");
                                        notificaEdo("retador@OK@" + nCte);
                                    }
                                    if (cds[1] == "RETAR")
                                    {
                                        oponent = cds[2];
                                        for (x = 0; x < 10; x++)
                                        {
                                            if (lCte[x] != null)
                                            {
                                                row = lCte[x].Text.Split(':');
                                                if (row[0] == "LISTO")
                                                {
                                                    if (row[1] == oponent)
                                                    {
                                                        op = x;
                                                        //cambiaTxt(lCte[x], oponent + ":" + user);
                                                    }
                                                }
                                            }
                                        }
                                        //cambiaTxt(lCte[nCte], user + ":" + oponent);
                                        mysql = new My_SQL();
                                        nq    = mysql.hazNoConsulta("update usuarios set playing='1' where user='******'");
                                        mysql = new My_SQL();
                                        nq    = mysql.hazNoConsulta("update usuarios set playing='1' where user='******'");
                                        notificaEdo("retador@ESPERANOTIFICAR");
                                        ctesUDP.send("JUEGOINI@" + user + "@" + oponent + "@" + nCte.ToString() + "@" + op.ToString() + "@blancas", nCte);
                                        ctesUDP.send("JUEGOINI@" + oponent + "@" + user + "@" + op.ToString() + "@" + nCte.ToString() + "@doradas", op);
                                    }
                                    if (cds[1] == "RENEW")
                                    {
                                        cambiaTxt(lCte[nCte], "LISTO:" + user);
                                        oponent = "";
                                        notificaEdo("retador@RENEW_OK");
                                    }
                                    if (cds[1] == "AQUIRE_OP")
                                    {
                                        cambiaTxt(lCte[nCte], user + ":" + cds[2]);
                                        oponent = cds[2];
                                        op      = int.Parse(cds[3]);
                                        notificaEdo("retador@RENEW_OK");
                                    }
                                }
                            }
                        }
                    }
                }
                catch (SocketException se)
                {
                    cierraCte("");
                    MessageBox.Show("Problemas de conexion " + se.ToString());
                }
                catch (Exception e)
                {
                    cierraCte("");
                    MessageBox.Show(e.ToString());
                }
                Thread.Sleep(10);
            }
        }