/** * This method is called from within the constructor to initialize the form. WARNING: Do NOT * modify this code. The content of this method is always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { Conectar = new javax.swing.JButton(); jTextField1 = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); Conectar.setText("Conectar"); Conectar.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ConectarActionPerformed(evt); } }); jTextField1.setText("IP SERVIDOR"); org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add( layout .createSequentialGroup() .add(49, 49, 49) .add( layout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add(layout.createSequentialGroup().add(14, 14, 14).add(Conectar)) .add( jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 135, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) .addContainerGap(53, Short.MAX_VALUE))); layout.setVerticalGroup( layout .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) .add( org.jdesktop.layout.GroupLayout.TRAILING, layout .createSequentialGroup() .addContainerGap(39, Short.MAX_VALUE) .add( jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) .add(32, 32, 32) .add(Conectar) .add(28, 28, 28))); pack(); } // </editor-fold>//GEN-END:initComponents
public static void clean(String tabla) { Conectar con = new Conectar(); query = "TRUNCATE " + tabla; try { con.execute(query); Logger.add(query); } catch (SQLException e) { e.printStackTrace(); } con.cerrar(); }
public void accionSubirNivel(String usuario, String tipoUnidad) { String campo; if (tipoUnidad.equalsIgnoreCase("tecnologia")) campo = "nivel"; else if (tipoUnidad.equalsIgnoreCase("arma")) campo = "arma"; else campo = "armadura"; try { Connection conn = Conectar.conectar(); Statement st = conn.createStatement(); st.executeQuery( "UPDATE usuarios SET " + campo + "=(SELECT " + campo + "+1 FROM usuarios WHERE nick='" + usuario + "') WHERE nick='" + usuario + "'"); st.executeQuery("DELETE FROM investigaciones WHERE idInvestigacion = " + idConstruccion); st.executeQuery("commit"); st.close(); conn.close(); } catch (SQLException ex) { System.out.println( ex.getMessage() + ex.getErrorCode() + " :: Fallo subir nivel investigacion"); } }
public static int count(String query) { Conectar con = new Conectar(); int total = 0; ResultSet resultados = null; try { resultados = con.execute(query); if (resultados.next()) { total = resultados.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } con.cerrar(); return total; }
public static Integer[] getYears(String table) { Integer[] years = null; int i = 0; Conectar con = new Conectar(); String query = "SELECT count(distinct anio) as anio FROM " + table; try { i = Tabla.count(query); years = new Integer[i]; i = 0; query = "SELECT distinct anio as anio FROM " + table; ResultSet resultados = con.execute(query); while (resultados.next()) { years[i++] = resultados.getInt("anio"); } } catch (SQLException e) { e.printStackTrace(); } con.cerrar(); return years; }
public static String[] getHeaders(String tabla) { ResultSet rs = null; Conectar con = new Conectar(); ResultSetMetaData meta = null; String[] sensores = null; try { rs = con.execute("SELECT * FROM " + tabla + " limit 1"); meta = rs.getMetaData(); int colmax = meta.getColumnCount(); sensores = new String[colmax]; for (int j = 0; j < colmax; j++) { sensores[j] = (meta.getColumnName(j + 1)); } } catch (SQLException ex1) { ex1.printStackTrace(); } con.cerrar(); return sensores; }
/** * metodo el cual inicia el servidor y si hay algun ip en el campo de texto intanta conectarse con * el servidor con ese ip * * @param evt */ private void ConectarActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_ConectarActionPerformed // TODO add your handling code here: Ips ips = new Ips(); jTextField1.setEnabled(false); Conectar.setEnabled(false); servidor = new Servidor(ips); Thread nuevohiloservidor = new Thread(servidor); nuevohiloservidor.start(); try { hiloservidorconoce = new HiloServidor(this, ips); Thread nuevohiloservidorconoce = new Thread(hiloservidorconoce); nuevohiloservidorconoce.start(); } catch (UnknownHostException ex) { System.out.println("No consiguio localhost"); } if (jTextField1.getText().matches("IP SERVIDOR") == false) { // si hay un ip en el campo trata de conectarse con el hiloservidorconoce.conocer(jTextField1.getText()); } } // GEN-LAST:event_ConectarActionPerformed
public void accionInsertarEdificio(int idReg, int idUni) { // aqui se ejecuta el estamento try { // de insertar edificios Connection conn = Conectar.conectar(); Statement st = conn.createStatement(); // añade 1 System.out.println(idReg + " " + idUni); st.executeQuery( "INSERT INTO edificiosregion VALUES (" + idReg + ", " + idUni + ", " + 1 + ")"); if (cuentaatras == 0 && cantidad > 0) // y decrementa las tareas pendientes en la base de datos st.executeQuery( "UPDATE construcciones SET cantidad = (SELECT cantidad-1 FROM construcciones WHERE idConstruccion = " + idConstruccion + ") WHERE idConstruccion = " + idConstruccion); if (cuentaatras == 0 && cantidad == 0) { System.out.println(idConstruccion); st.executeQuery("DELETE FROM construcciones WHERE idConstruccion = " + idConstruccion); } st.executeQuery("commit"); st.close(); conn.close(); } catch (SQLException ex) { System.out.println( ex.getMessage() + ex.getErrorCode() + " :: Fallo de conexion del temporizador"); if (ex.getErrorCode() == 00001) { Connection conn; try { conn = Conectar.conectar(); // Si no se ha podido ejecutar la insercion porque la clave ya existe, se actualiza la // tabla Statement st = conn.createStatement(); st.executeQuery( "UPDATE edificiosregion SET cantidad = (SELECT cantidad+1 FROM edificiosregion WHERE idRegion = " + idReg + "AND idEdificio = " + idUni + ") WHERE idRegion = " + idReg + " AND idEdificio = " + idUni); if (cuentaatras == 0 && cantidad > 0) // y decrementa las tareas pendientes en la base de datos st.executeQuery( "UPDATE construcciones SET cantidad = (SELECT cantidad-1 FROM construcciones WHERE idConstruccion = " + idConstruccion + ") WHERE idConstruccion = " + idConstruccion); if (cuentaatras == 0 && cantidad == 0) { System.out.println(idConstruccion); st.executeQuery("DELETE FROM construcciones WHERE idConstruccion = " + idConstruccion); } st.executeQuery("commit"); st.close(); conn.close(); } catch (SQLException ex1) { System.out.println(ex1.getMessage() + " :: Fallo en catch de insertar"); } } } }
public void accionEjecutarMovimiento() { try { Connection conn = Conectar.conectar(); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT propietario FROM regiones WHERE idRegion = " + idRegionDestino); String propietario = "nulo"; if (rs.next()) { propietario = rs.getString(1); } // Si el territorio esta // vacio**************************************************************************** if (propietario.equalsIgnoreCase("Deshabitado")) { String nombre = generaNombre(); st.executeQuery( "UPDATE regiones SET nombreRegion = '" + nombre + "', propietario = '" + usuario + "' WHERE idRegion = " + idRegionDestino); st.executeQuery( "INSERT INTO edificiosregion VALUES (" + idRegionDestino + ", " + 1400 + ", " + 1 + ")"); for (int i = 0; i < pelotones.size(); i = i + 2) { st.executeQuery( "INSERT INTO despliegues VALUES (" + idRegionDestino + ", " + pelotones.get(i) + ", " + pelotones.get(i + 1) + ")"); } st.executeQuery("DELETE movimientos WHERE idTropa = " + idTropa); st.executeQuery("commit"); System.out.println("Query ejecutado."); } // Si el territorio te pertenece // ******************************************************************************* else if (propietario.equalsIgnoreCase(usuario)) { ResultSet rs2 = st.executeQuery("select * from despliegues where idRegion = " + idRegionDestino); ArrayList<Despliegue> despliegues = new ArrayList<Despliegue>(); while (rs2.next()) { despliegues.add(new Despliegue(rs2.getInt(1), rs2.getInt(2), rs2.getInt(3))); } Boolean encontrado = false; for (int i = 0; i < pelotones.size(); i = i + 2) { encontrado = false; for (Despliegue despliegue : despliegues) { if (pelotones.get(i).equals(despliegue.getIdUnidad())) { st.executeQuery( "update despliegues set cantidad = " + (despliegue.getCantidad() + pelotones.get(i + 1)) + " where idRegion = " + idRegionDestino + " and idUnidad = " + despliegue.getIdUnidad()); encontrado = true; } } if (!encontrado) { st.executeQuery( "INSERT INTO despliegues VALUES (" + idRegionDestino + ", " + pelotones.get(i) + ", " + pelotones.get(i + 1) + ")"); } } st.executeQuery("DELETE movimientos WHERE idTropa = " + idTropa); st.executeQuery("commit"); System.out.println("Query ejecutado."); rs2.close(); } // Si es un territorio enemigo ( COMBATE ) // ***************************************************************************** else { int ataque = 0; int defensa = 0; int defenemiga = 0; int ataenemigo = 0; SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy HH:mm"); java.util.Date fecha = new java.util.Date(); String fechahora = formato.format(fecha); // Recolectamos el ataque y la defensa del enemigo ResultSet rs3 = st.executeQuery("select * from despliegues where idRegion = " + idRegionDestino); ArrayList<Despliegue> despliegues = new ArrayList<Despliegue>(); while (rs3.next()) { despliegues.add(new Despliegue(rs3.getInt(1), rs3.getInt(2), rs3.getInt(3))); } for (Despliegue despliegue : despliegues) { if (despliegue.getIdUnidad() == 4100) { ataenemigo = ataenemigo + (15 * despliegue.getCantidad()); defenemiga = defenemiga + (30 * despliegue.getCantidad()); } else if (despliegue.getIdUnidad() == 4101) { ataenemigo = ataenemigo + (25 * despliegue.getCantidad()); defenemiga = defenemiga + (50 * despliegue.getCantidad()); } else if (despliegue.getIdUnidad() == 4102) { ataenemigo = ataenemigo + (55 * despliegue.getCantidad()); defenemiga = defenemiga + (20 * despliegue.getCantidad()); } else if (despliegue.getIdUnidad() == 4103) { ataenemigo = ataenemigo + 800; ataenemigo = ataenemigo * ((ataenemigo * 10) / 100); defenemiga = defenemiga + 600; } else if (despliegue.getIdUnidad() == 4104) { ataenemigo = ataenemigo + (400 * despliegue.getCantidad()); defenemiga = defenemiga + (100 * despliegue.getCantidad()); } else if (despliegue.getIdUnidad() == 4102) { ataenemigo = ataenemigo + (50 * despliegue.getCantidad()); defenemiga = defenemiga + (800 * despliegue.getCantidad()); } } // Recolectamos informacion de edificios defensivos ResultSet rs4 = st.executeQuery("select * from edificiosregion where idRegion = " + idRegionDestino); ArrayList<EdificiosRegion> despliegues2 = new ArrayList<EdificiosRegion>(); while (rs4.next()) { despliegues2.add(new EdificiosRegion(rs4.getInt(1), rs4.getInt(2), rs4.getInt(3))); } for (EdificiosRegion despliegue2 : despliegues2) { if (despliegue2.idEdificio == 1300) { ataenemigo = ataenemigo + (200 * despliegue2.cantidad); defenemiga = defenemiga + (1000 * despliegue2.cantidad); } else if (despliegue2.idEdificio == 1301) { ataenemigo = ataenemigo + (500 * despliegue2.cantidad); defenemiga = defenemiga + (500 * despliegue2.cantidad); } } // Recolectamos ataque y defensa nuestra*************************** for (int i = 0; i < pelotones.size(); i = i + 2) { if (pelotones.get(i) == 4100) { ataque = ataque + (15 * pelotones.get(i + 1)); defensa = defensa + (30 * pelotones.get(i + 1)); } else if (pelotones.get(i) == 4101) { ataque = ataque + (25 * pelotones.get(i + 1)); defensa = defensa + (50 * pelotones.get(i + 1)); } else if (pelotones.get(i) == 4102) { ataque = ataque + (55 * pelotones.get(i + 1)); defensa = defensa + (20 * pelotones.get(i + 1)); } else if (pelotones.get(i) == 4103) { ataque = ataque + 800; ataque = ataque * ((ataenemigo * 10) / 100); defensa = defensa + 600; } else if (pelotones.get(i) == 4104) { ataque = ataque + (400 * pelotones.get(i + 1)); defensa = defensa + (100 * pelotones.get(i + 1)); } else if (pelotones.get(i) == 4100) { ataque = ataque + (50 * pelotones.get(i + 1)); defensa = defensa + (800 * pelotones.get(i + 1)); } } int ataque1 = defenemiga - ataque; int ataque2 = defensa - ataenemigo; // Capturamos el nick del enemigo*************** String enemigo = "Deshabitado"; ResultSet rsenemigo = st.executeQuery("select propietario from regiones where idRegion = " + idRegionDestino); if (rsenemigo.next()) { enemigo = rsenemigo.getString(1); } // 1 --- Si el enemigo tiene vida inferior a 0 y tu no if (ataque1 <= 0 && ataque2 > 0) { // Se conquista el territorio, se compra aldea y se recolocan las unidades sobrantes // dependiendo del ataque 2 st.executeQuery( "UPDATE regiones SET propietario = '" + usuario + "' WHERE idRegion = " + idRegionDestino); st.executeQuery( "DELETE edificiosregion WHERE idRegion = " + idRegionDestino + " AND idEdificio != " + 1400); st.executeQuery("DELETE despliegues WHERE idRegion = " + idRegionDestino); st.executeQuery("DELETE construcciones WHERE idRegion = " + idRegionDestino); st.executeQuery("DELETE movimientos WHERE idTropa = " + idTropa); // Escribimos un mensaje al usuario con los resultados de la batalla st.executeQuery( "INSERT INTO mensajes VALUES (s_mensajes.NEXTVAL, 'System', '" + usuario + "', 'Informe de Batalla', '" + "¡Enhorabuena!#Has ganado la batalla en el territorio [" + idRegionDestino + "].#Daño Recibido-> " + ataenemigo + ".#Daño causado-> " + ataque + ".', '" + fechahora + "')"); // Escribimos un mensaje al enemigo con los resultados de la batalla st.executeQuery( "INSERT INTO mensajes VALUES (s_mensajes.NEXTVAL, 'System', '" + enemigo + "', 'Informe de Batalla', '" + "Lo siento.#Has perdido la batalla en el territorio [" + idRegionDestino + "].#Daño Recibido-> " + ataque + ".#Daño causado-> " + ataenemigo + ".', '" + fechahora + "')"); System.out.println(ataque2); // Algoritmo para ver el porcentaje de unidades que me quedan int porcperdido = (int) (ataque2 * 100) / defensa; for (int i = 0; i < pelotones.size(); i = i + 2) { pelotones.set(i + 1, (int) (pelotones.get(i + 1) * porcperdido) / 100); } for (int i = 0; i < pelotones.size(); i = i + 2) { st.executeQuery( "INSERT INTO despliegues VALUES (" + idRegionDestino + ", " + pelotones.get(i) + ", " + pelotones.get(i + 1) + ")"); } } // 2 --- Los 2 tienen la vida a 0 o menos else if (ataque1 <= 0 && ataque2 <= 0) { // El enemigo se queda con el territorio pero pierde todos los edificios y se recolocan // unidades dependiendo de ataque1. st.executeQuery( "DELETE edificiosregion WHERE idRegion = " + idRegionDestino + " AND idEdificio != " + 1400); st.executeQuery("DELETE movimientos WHERE idTropa = " + idTropa); st.executeQuery("DELETE despliegues WHERE idRegion = " + idRegionDestino); // Escribimos un mensaje al usuario con los resultados de la batalla st.executeQuery( "INSERT INTO mensajes VALUES (s_mensajes.NEXTVAL, 'System', '" + usuario + "', 'Informe de Batalla', '" + "Lo siento.#Has perdido la batalla en el territorio [" + idRegionDestino + "].#Daño Recibido-> " + ataenemigo + ".#Daño causado-> " + ataque + ".', '" + fechahora + "')"); // Escribimos un mensaje al enemigo con los resultados de la batalla st.executeQuery( "INSERT INTO mensajes VALUES (s_mensajes.NEXTVAL, 'System', '" + enemigo + "', 'Informe de Batalla', '" + "¡Enhorabuena!#Has conseguido defender tu territorio ubicado en [" + idRegionDestino + "].#" + "Aunque has perdido todas tus tropas y edificios.#Daño Recibido-> " + ataque + ".#Daño causado-> " + ataenemigo + ".', '" + fechahora + "')"); } // 3 -- Tu pierdes la batalla else if (ataque1 > 0 && ataque2 <= 0) { // El enemigo recoloca unidades y tu lo pierdes todo st.executeQuery("DELETE movimientos WHERE idTropa = " + idTropa); int porcperdido = (int) (ataque1 * 100) / defenemiga; for (Despliegue despliegue : despliegues) { despliegue.setCantidad((int) (despliegue.getCantidad() * porcperdido) / 100); } for (Despliegue despliegue : despliegues) { st.executeQuery( "UPDATE despliegues SET cantidad = " + despliegue.getCantidad() + " WHERE idRegion = " + idRegionDestino + " AND idUnidad = " + despliegue.getIdUnidad()); } // Escribimos un mensaje al usuario con los resultados de la batalla st.executeQuery( "INSERT INTO mensajes VALUES (s_mensajes.NEXTVAL, 'System', '" + usuario + "', 'Informe de Batalla', '" + "Lo siento.#Has perdido la batalla en el territorio [" + idRegionDestino + "].#Daño Recibido-> " + ataenemigo + ".#Daño causado-> " + ataque + ".', '" + fechahora + "')"); // Escribimos un mensaje al enemigo con los resultados de la batalla st.executeQuery( "INSERT INTO mensajes VALUES (s_mensajes.NEXTVAL, 'System', '" + enemigo + "', 'Informe de Batalla', '" + "¡Enhorabuena!#Has conseguido defender tu territorio ubicado en [" + idRegionDestino + "].#Daño Recibido-> " + ataque + ".#Daño causado-> " + ataenemigo + ".', '" + fechahora + "')"); } // 4 --- Los 2 quedan con vida************************************************************** else if (ataque1 > 0 && ataque2 > 0) { // Se recolocan unidades en los 2. st.executeQuery("DELETE movimientos WHERE idTropa = " + idTropa); int porcperdido1 = (int) (ataque1 * 100) / defenemiga; for (Despliegue despliegue : despliegues) { despliegue.setCantidad((int) (despliegue.getCantidad() * porcperdido1) / 100); } for (Despliegue despliegue : despliegues) { st.executeQuery( "UPDATE despliegues SET cantidad = " + despliegue.getCantidad() + " WHERE idRegion = " + idRegionDestino + " AND idUnidad = " + despliegue.getIdUnidad()); } int porcperdido2 = (int) (ataque2 * 100) / defensa; for (int i = 0; i < pelotones.size(); i = i + 2) { pelotones.set(i + 1, (int) (pelotones.get(i + 1) * porcperdido2) / 100); } for (int i = 0; i < pelotones.size(); i = i + 2) { st.executeQuery( "UPDATE despliegues SET cantidad = (SELECT cantidad+" + pelotones.get(i + 1) + " FROM despliegues WHERE idRegion = " + idRegion + " AND idUnidad = " + pelotones.get(i) + ") WHERE idRegion = " + idRegion + " AND idUnidad = " + pelotones.get(i)); } // Escribimos un mensaje al usuario con los resultados de la batalla st.executeQuery( "INSERT INTO mensajes VALUES (s_mensajes.NEXTVAL, 'System', '" + usuario + "', 'Informe de Batalla', '" + "Lo siento.#Has perdido la batalla en la region [" + idRegionDestino + "].#" + "Aunque has recuperado algunas unidades.#Daño Recibido-> " + ataenemigo + ".#Daño causado-> " + ataque + ".', '" + fechahora + "')"); // Escribimos un mensaje al enemigo con los resultados de la batalla st.executeQuery( "INSERT INTO mensajes VALUES (s_mensajes.NEXTVAL, 'System', '" + enemigo + "', 'Informe de Batalla', '" + "¡Enhorabuena!#Has conseguido defender tu territorio ubicado en [" + idRegionDestino + "].#Daño Recibido-> " + ataque + ".#Daño causado-> " + ataenemigo + ".', '" + fechahora + "')"); } st.executeQuery("commit"); rs3.close(); rs4.close(); rsenemigo.close(); } st.close(); rs.close(); conn.close(); } catch (SQLException e) { System.out.println(e.getMessage() + " Fallo ejecutar movimiento."); } }