Skip to content

Commit

Permalink
statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
siqq committed Oct 15, 2014
1 parent 5a822be commit 1461e19
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 89 deletions.
93 changes: 6 additions & 87 deletions Java-Missle/src/main/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ public class Program {
public static void main(String[] args) {

try {
// War warModel = xml.readXML();
// new WarDBConnection();
// WarDBConnection.clearWarDataBase();
War warModel = new War();
AbstractWarView viewGui = new WarGui();
// new Server();
WarController controller = new WarController(warModel,viewGui);
XMLparser xml = new XMLparser(controller, warModel);

// new WarDBConnection();
// WarDBConnection.clearWarDataBase();
// new Server();


} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
Expand All @@ -49,89 +50,7 @@ public static void main(String[] args) {

}

/**
* Print menu
*/

private static void checkEndOfWar(War war) {
int counter = 0;
int size = war.getMissileLaunchers().size();
//test if all launchers has been destroyed
if (size > 0) {
for (Launcher l : war.getMissileLaunchers() ) {
if (l.isRunning() == false) {
counter++;
//loop to check if there is a missile in air
int size_missiles = l.getMissiles().size();
for (int j = 0; j < size_missiles; j++) {
Missile m = l.getMissiles().get(j);
//once we find a missile in air we dont stop the war
if (m.getStatus() == Missile.Status.Launched) {
return;
}
}
}
}
//ok thats better
//you should really consider modifying the source. its fucking you up bigtime.
if (counter == size) {
/*String stats = displayStatistics(war);
logger.log(Level.INFO, "All launchers destroyed - end of war");
logger.log(Level.INFO, "end war\n" + stats);
System.out.println(stats);
System.exit(0);*/
}
}
}

/**
* The method displays the status of war
* @param war
* @return string of statistic
*/
private static void displayStatistics(War war) {
int total_launched_missiles = 0;
int total_destroyed_missiles = 0;
int total_missiles_hit = 0;
int total_destroyed_launchers = 0;
int total_damage = 0;
int size_launcher = war.getMissileLaunchers().size();
int size_missiles;


Launcher l;
Missile m;
String statistic = "";

for (int i = 0; i < size_launcher; i++) {
l = war.getMissileLaunchers().get(i);
if (l.isRunning() == false) {
total_destroyed_launchers++;
}
size_missiles = l.getMissiles().size();
for (int j = 0; j < size_missiles; j++) {
m = l.getMissiles().get(j);
if (m.getStatus() != Missile.Status.Waiting) {
total_launched_missiles++;
if(m.getStatus() == Missile.Status.Hit) {
total_missiles_hit++;
total_damage += m.getDamage();
} else if (m.getStatus() == Missile.Status.Destroyed){
total_destroyed_missiles++;
}
}
}
}
/* statistic = "The statistics of war is: \n"
+ "The number of missiles launched:\t"
+ total_launched_missiles + "\n"
+ "The number of missiles destroyed:\t"
+ total_destroyed_missiles + "\n"
+ "The number of missiles that hit:\t"
+ total_missiles_hit
+ "\n" + "The number launchers were destroyed:\t"
+ total_destroyed_launchers + "\n"
+ "The total value of damage caused:\t"
+ total_damage + "\n";
return statistic;*/
}
}
23 changes: 23 additions & 0 deletions Java-Missle/src/view/ControlPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ public void actionPerformed(ActionEvent e) {
selectLauncherToDestroy();
}
});

// STATISTICS BUTTON ACTION LISTENER
statisticsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ShowStatistics();

}
});

openClient.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Expand Down Expand Up @@ -227,6 +237,19 @@ public void addMessageToGui(String string) {
JOptionPane.showMessageDialog(null, string);

}

public void ShowStatistics() {
int[] statistics = new int[5];


for (WarUIEventsListener l : allListeners) {
statistics = l.getStatistics();
}


new StatisticsPopUpFrame(statistics[0], statistics[1], statistics[2], statistics[3], statistics[4]);

}



Expand Down
3 changes: 3 additions & 0 deletions Java-Missle/src/view/IronDomesPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ public void actionPerformed(ActionEvent e) {
for (WarUIEventsListener l : allListeners) {
l.addInterceptionToUI(missileId, id);
}
fireMissileButtonPressed = false;

}
setBorder(new LineBorder(new Color(0, 0, 0)));
validate();
}

});
Expand Down
98 changes: 98 additions & 0 deletions Java-Missle/src/view/StatisticsPopUpFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package view;

import javax.swing.JFrame;
import javax.swing.SpringLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class StatisticsPopUpFrame extends JFrame {

private JTextField launchedMissiles;
private JTextField destroyedMissiles;
private JTextField hitMissiles;
private JTextField destroyedLaunchers;
private JTextField totalDamage;

public StatisticsPopUpFrame(int launched_m, int destroyed_m, int hit_m, int destroyed_l, int total_d) {


SpringLayout springLayout = new SpringLayout();
getContentPane().setLayout(springLayout);

JLabel launched_missiles = new JLabel("Total Launched Missiles");
springLayout.putConstraint(SpringLayout.NORTH, launched_missiles, 7, SpringLayout.NORTH, getContentPane());
getContentPane().add(launched_missiles);

launchedMissiles = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, launchedMissiles, -3, SpringLayout.NORTH, launched_missiles);
springLayout.putConstraint(SpringLayout.EAST, launchedMissiles, -74, SpringLayout.EAST, getContentPane());
getContentPane().add(launchedMissiles);
launchedMissiles.setColumns(10);

JLabel destroyed_missiles = new JLabel("Total Destroyed Missiles");
springLayout.putConstraint(SpringLayout.NORTH, destroyed_missiles, 31, SpringLayout.SOUTH, launched_missiles);
springLayout.putConstraint(SpringLayout.WEST, launched_missiles, 0, SpringLayout.WEST, destroyed_missiles);
getContentPane().add(destroyed_missiles);

destroyedMissiles = new JTextField();
springLayout.putConstraint(SpringLayout.EAST, destroyed_missiles, -78, SpringLayout.WEST, destroyedMissiles);
springLayout.putConstraint(SpringLayout.NORTH, destroyedMissiles, -3, SpringLayout.NORTH, destroyed_missiles);
springLayout.putConstraint(SpringLayout.EAST, destroyedMissiles, 0, SpringLayout.EAST, launchedMissiles);
getContentPane().add(destroyedMissiles);
destroyedMissiles.setColumns(10);

JLabel missiles_hit = new JLabel("Total Missiles Hit");
springLayout.putConstraint(SpringLayout.WEST, missiles_hit, 0, SpringLayout.WEST, launched_missiles);
getContentPane().add(missiles_hit);

hitMissiles = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, hitMissiles, -3, SpringLayout.NORTH, missiles_hit);
springLayout.putConstraint(SpringLayout.EAST, hitMissiles, 0, SpringLayout.EAST, launchedMissiles);
getContentPane().add(hitMissiles);
hitMissiles.setColumns(10);

JLabel destroyed_launchers = new JLabel("Total Destroyed Launchers");
springLayout.putConstraint(SpringLayout.NORTH, destroyed_launchers, 162, SpringLayout.NORTH, getContentPane());
springLayout.putConstraint(SpringLayout.SOUTH, missiles_hit, -40, SpringLayout.NORTH, destroyed_launchers);
springLayout.putConstraint(SpringLayout.WEST, destroyed_launchers, 0, SpringLayout.WEST, launched_missiles);
getContentPane().add(destroyed_launchers);

destroyedLaunchers = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, destroyedLaunchers, -3, SpringLayout.NORTH, destroyed_launchers);
springLayout.putConstraint(SpringLayout.EAST, destroyedLaunchers, 0, SpringLayout.EAST, launchedMissiles);
getContentPane().add(destroyedLaunchers);
destroyedLaunchers.setColumns(10);

JLabel total_damage = new JLabel("Total Damage");
springLayout.putConstraint(SpringLayout.NORTH, total_damage, 42, SpringLayout.SOUTH, destroyed_launchers);
springLayout.putConstraint(SpringLayout.WEST, total_damage, 0, SpringLayout.WEST, launched_missiles);
getContentPane().add(total_damage);

totalDamage = new JTextField();
springLayout.putConstraint(SpringLayout.NORTH, totalDamage, -3, SpringLayout.NORTH, total_damage);
springLayout.putConstraint(SpringLayout.EAST, totalDamage, 0, SpringLayout.EAST, launchedMissiles);
getContentPane().add(totalDamage);
totalDamage.setColumns(10);

launchedMissiles.setHorizontalAlignment(JTextField.CENTER);
destroyedMissiles.setHorizontalAlignment(JTextField.CENTER);
hitMissiles.setHorizontalAlignment(JTextField.CENTER);
destroyedLaunchers.setHorizontalAlignment(JTextField.CENTER);
totalDamage.setHorizontalAlignment(JTextField.CENTER);

launchedMissiles.setText(Integer.toString(launched_m));
destroyedMissiles.setText(Integer.toString(destroyed_m));
hitMissiles.setText(Integer.toString(hit_m));
destroyedLaunchers.setText(Integer.toString(destroyed_l));
totalDamage.setText(Integer.toString(total_d));

setResizable(false);
setSize(450, 300);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);



}
}
44 changes: 42 additions & 2 deletions Java-Missle/src/war/War.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,7 @@ public void destroyLauncher(String destructor_id, String target_id) {
}

public void startMissileInterception(String missileId , String ironDome) {
Destructor selected_destructor = WarUtility.getDestructorById(
ironDome, this, MISSILE);
Destructor selected_destructor = WarUtility.getDestructorById(ironDome, this, MISSILE);
int destruct_time = (int) (TAKES_TIME_MIN + (Math.random() *
(TAKES_TIME_MAX - TAKES_TIME_MIN + 1)));
Missile target = WarUtility.getMissileById(missileId, this);
Expand All @@ -257,6 +256,47 @@ public void startMissileInterception(String missileId , String ironDome) {
}

}

public int[] displayStatistics() {
int statistics[] = new int[5];
int total_launched_missiles = 0;
int total_destroyed_missiles = 0;
int total_missiles_hit = 0;
int total_destroyed_launchers = 0;
int total_damage = 0;
int size_launcher = getMissileLaunchers().size();
int size_missiles;

Launcher l;
Missile m;

for (int i = 0; i < size_launcher; i++) {
l = getMissileLaunchers().get(i);
if (l.isRunning() == false) {
total_destroyed_launchers++;
}
size_missiles = l.getMissiles().size();
for (int j = 0; j < size_missiles; j++) {
m = l.getMissiles().get(j);
if (m.getStatus() != Missile.Status.Waiting) {
total_launched_missiles++;
if(m.getStatus() == Missile.Status.Hit) {
total_missiles_hit++;
total_damage += m.getDamage();
} else if (m.getStatus() == Missile.Status.Destroyed){
total_destroyed_missiles++;
}
}
}
}
statistics[0] = total_launched_missiles;
statistics[1] = total_destroyed_missiles;
statistics[2] = total_missiles_hit;
statistics[3] = total_destroyed_launchers;
statistics[4] = total_damage;

return statistics;
}


public void addLauncherToClient(String id) {
Expand Down
5 changes: 5 additions & 0 deletions Java-Missle/src/war/controller/WarController.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,9 @@ public void connectToServer() {

}

@Override
public int[] getStatistics() {
return warModel.displayStatistics();
}

}
2 changes: 2 additions & 0 deletions Java-Missle/src/war/controller/WarUIEventsListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ void addMissileToProgress(String name, String dest, String damage,
void addMissileThroughClient(String id, String dest, String damageT,
String flyTime, String launcherId);

int[] getStatistics();

}

0 comments on commit 1461e19

Please sign in to comment.