Skip to content

Commit

Permalink
Database works with missiles (+ status change)
Browse files Browse the repository at this point in the history
  • Loading branch information
siqq committed Sep 10, 2014
1 parent 321ac3f commit 9fda9ff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
7 changes: 5 additions & 2 deletions Java-Missle/src/missile/Missile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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());
}

}
34 changes: 29 additions & 5 deletions Java-Missle/src/war/db/WarDBConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,50 @@ 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);
statement.setInt(3, damage);
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<String> getAllTablesNames() {
Expand Down

0 comments on commit 9fda9ff

Please sign in to comment.