diff --git a/Java-Missle/src/missile/Missile.java b/Java-Missle/src/missile/Missile.java index 4fae06b..b460935 100644 --- a/Java-Missle/src/missile/Missile.java +++ b/Java-Missle/src/missile/Missile.java @@ -101,13 +101,14 @@ public void setStatus(Status status) { public void run() { boolean reveal_status = false; try { + WarDBConnection.addNewMissile(missileId,destination,damage,flyTime,status.toString()); sleep(getLaunchTime() * War.TIME_INTERVAL); synchronized (launcher) { if (launcher.isRunning()) { this.setStatus(Status.Launched); - + WarDBConnection.updateMissileStatus(missileId, status.toString()); // make launcher not hidden for X amount of time if (launcher.isHidden()) { launcher.revealYourSelf(); @@ -117,7 +118,6 @@ public void run() { + " was launched from launcher: " + this.launcher.getLauncherId(); // java.sql.Date sqlDate = Utils.utilDateToSqlDate(birthdate); - int res = WarDBConnection.addNewMissile(missileId,destination,damage,flyTime,status.toString()); for (WarEventListener l : allListeners) { l.addedMissileToModelEvent(missileId,destination,damage,flyTime); } @@ -145,6 +145,8 @@ public void run() { } catch (InterruptedException e) { //missile is destroyed this.setStatus(Status.Destroyed); + WarDBConnection.updateMissileStatus(missileId, status.toString()); + if (reveal_status == true) { launcher.hideYourSelf(); // make launcher hide again } @@ -165,6 +167,7 @@ public void destroyTarget() { + this.damage + " damage"; logger.log(Level.INFO, print_log, this); this.setStatus(Status.Hit); + WarDBConnection.updateMissileStatus(missileId, status.toString()); } } diff --git a/Java-Missle/src/war/db/WarDBConnection.java b/Java-Missle/src/war/db/WarDBConnection.java index 4774d8d..943a7a3 100644 --- a/Java-Missle/src/war/db/WarDBConnection.java +++ b/Java-Missle/src/war/db/WarDBConnection.java @@ -40,11 +40,10 @@ public class WarDBConnection { } } - public static int addNewMissile(String id,String destination,int damage,int flyTime,String status) { - int res = 0; + public static void addNewMissile(String id,String destination,int damage,int flyTime,String status) { try { // connection.createStatement(); - statement = (PreparedStatement) connection.prepareStatement("INSERT INTO War.missile (id, date,destination,damage,flyTime,status) VALUES (?, now(), ?, ?, ?, ?)"); + statement = (PreparedStatement) connection.prepareStatement("INSERT INTO war.missile (id, date,destination,damage,flyTime,status) VALUES (?, now(), ?, ?, ?, ?)"); statement.setString(1, id); // statement.setString(1, "now()"); statement.setString(2, destination); @@ -52,14 +51,39 @@ public static int addNewMissile(String id,String destination,int damage,int flyT statement.setInt(4, flyTime); statement.setString(5, status); - res = statement.executeUpdate(); + // statement.executeUpdate(); - IMPORTANTE! This is a must to update the database + statement.executeUpdate(); } catch (SQLException e) { while (e != null) { System.out.println(e.getMessage()); e = e.getNextException(); } } - return res; + } + + public static void updateMissileStatus(String id, String status) { + try { + statement = (PreparedStatement) connection.prepareStatement("UPDATE war.missile SET `status` = ? WHERE missile.id = ?; "); + statement.setString(1, status); + statement.setString(2, id); + + statement.executeUpdate(); + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public static void addNewLauncher(){ + // TO DO + } + + public static void addNewDestructor(){ + // TO DO + } + + public static void updateLauncherStatus(){ + // TO DO } public static List getAllTablesNames() {