Skip to content

Commit

Permalink
1.Added client with JavaFX
Browse files Browse the repository at this point in the history
2.Added client ( there is still work to be done)
3.Added server (-||-)
  • Loading branch information
GalKarp committed Oct 4, 2014
1 parent 729e764 commit df038f0
Show file tree
Hide file tree
Showing 15 changed files with 604 additions and 215 deletions.
7 changes: 7 additions & 0 deletions Java-Missle/.checkstyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
<fileset name="all" enabled="true" check-config-name="Sun Checks (Eclipse)" local="false">
<file-match-pattern match-pattern="." include-pattern="true"/>
</fileset>
</fileset-config>
Binary file added Java-Missle/mysql-connector-java-5.1.33-bin.jar
Binary file not shown.
12 changes: 12 additions & 0 deletions Java-Missle/src/launcher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public Launcher(String id, boolean isHidden, Vector<Missile> missiles)
super();
this.id = id;
this.isHidden = true;
this.isHidden = true;
this.missiles = missiles;
this.isRunning = true;

Expand Down Expand Up @@ -133,6 +134,17 @@ public void addMissile(String id, String destination, int launchtime,
this.notify();
}
}
public Missile CreatWithoutAddMissile(String id, String destination, int launchtime,
String flytime, String damage) {
int flyTime = Integer.parseInt(flytime);
int daMage = Integer.parseInt(damage);
Missile missile = new Missile(id, destination, launchtime,
flyTime, daMage, this.fileHandler, this ,allListeners);
return missile;
// synchronized (this) {
// this.notify();
// }
}

/**
* Reveals the launcher and makes it not hidden
Expand Down
54 changes: 54 additions & 0 deletions Java-Missle/src/main/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

import launcher.Launcher;
import missile.Missile;


public class Server {
Socket socket = null;
ServerSocket server;
ObjectInputStream inputStream;
ObjectOutputStream outputStream;
Launcher launcher = null;
Missile missile = null;
public Server(){
try {
server = new ServerSocket(7000);
System.out.println(new Date() + " --> Server waits for client...");
socket = server.accept(); // blocking
System.out.println(new Date() + " --> Client connected from "
+ socket.getInetAddress() + ":" + socket.getPort());

outputStream = new ObjectOutputStream(socket.getOutputStream());
inputStream = new ObjectInputStream(socket.getInputStream());

do {
if( inputStream.readObject() instanceof Missile) {
missile = (Missile)inputStream.readObject();
outputStream.writeObject(missile);
}
else if( inputStream.readObject() instanceof Launcher) {
launcher = (Launcher)inputStream.readObject();
outputStream.writeObject(launcher);
}

} while (launcher != null || missile != null);
} catch (Exception e) {
System.out.println(e);
} finally {
try {
socket.close();
server.close();
System.out
.println("Sever is closing after client is disconnected");
} catch (IOException e) { }
}
}
}
6 changes: 4 additions & 2 deletions Java-Missle/src/missile/DestructedLanucher.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import launcher.Launcher;
import war.War;
import war.controller.WarEventListener;
import war.db.WarDBConnection;

public class DestructedLanucher extends AbstractMissile {

Expand Down Expand Up @@ -75,10 +76,10 @@ public void run() {
@Override
public void destroyTarget() throws Exception {
Object arr[] = {this, target};
double rate = 0 ;//Math.random(); // generate random success
double rate = 3 ;//Math.random(); // generate random success

if (target.isRunning()) {
if (!target.isHidden()) {
if (true){//!target.isHidden()) {
// if rate bigger than success rate it will destroy
if (rate > War.SUCCESS_RATE) {
for (WarEventListener l : allListeners) {
Expand All @@ -88,6 +89,7 @@ public void destroyTarget() throws Exception {
String print_log = "Launcher " + target.getLauncherId()
+ " was destroyed";
logger.log(Level.INFO, print_log, arr);
WarDBConnection.updateLauncherStatus(target.getLauncherId().toString(), "Destroy");
}
else {
for (WarEventListener l : allListeners) {
Expand Down
5 changes: 5 additions & 0 deletions Java-Missle/src/missile/DestructedMissile.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import java.util.logging.Logger;

import launcher.Destructor;
import missile.Missile.Status;
import war.War;
import war.controller.WarEventListener;
import war.db.WarDBConnection;

public class DestructedMissile extends AbstractMissile {
private static Logger logger;
Expand Down Expand Up @@ -86,6 +88,9 @@ public void destroyTarget() throws Exception {
}
String print_log = "Missle " + target.getMissileId()
+ " was destroyed";
target.setStatus(Status.Destroyed);
WarDBConnection.updateMissileStatusAndDestructor(target.getMissileId().toString(), destructor.getDestructorId().toString());
WarDBConnection.updateMissileStatus(target.getMissileId().toString(), target.getStatus().toString());
logger.log(Level.INFO, print_log, arr);
} else {
for (WarEventListener l : allListeners) {
Expand Down
2 changes: 2 additions & 0 deletions Java-Missle/src/missile/Missile.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import launcher.Launcher;
import war.War;
import war.controller.WarEventListener;
Expand Down Expand Up @@ -46,6 +47,7 @@ public Missile(String id, String destination, int launchTime, int flyTime,
logger = Logger.getLogger("warLogger");
}


/**
* @return id of missile
*/
Expand Down
44 changes: 44 additions & 0 deletions Java-Missle/src/view/ClientStyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@CHARSET "windows-1255";

.label {
-fx-font-size: 12px;
-fx-font-weight: bold;
-fx-text-fill: gray;
}

.button {
-fx-text-fill: white;
-fx-font-family: "Comic Sans MS";
-fx-font-weight: bold;
-fx-background-color: linear-gradient(#61a2b1, #2A5058);
-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );
}
.tab {
-fx-text-fill: white;
-fx-font-family: "Comic Sans MS";
-fx-font-weight: bold;
-fx-background-color: linear-gradient(#61a2b1, #2A5058);
-fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );
}
.button:hover {
-fx-background-color: linear-gradient(#2A5058, #61a2b1);
}

#survivor-name {
-fx-font-size: 14px;
-fx-font-family: "Comic Sans MS";
-fx-font-weight: bold;
-fx-fill: #151515;
}

#survivor-background
{
-fx-fill: #D8D8D8;
}

#tribe {
-fx-background-color:beige;
}
#mainPane {
-fx-background-color: #B2D1FF
}
146 changes: 146 additions & 0 deletions Java-Missle/src/view/ConsoleApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package view;

import java.util.List;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

import javax.swing.JOptionPane;

import war.controller.WarUIEventsListener;

public class ConsoleApp extends Application {
// private List<WarUIEventsListener> allListeners;
// private String launcherName;
// public ConsoleApp(List<WarUIEventsListener> allListeners, WarGui warGui) {
// this.allListeners = allListeners;
// this.launcherName = launcherName;
// main(null);
// }





public void start(Stage primaryStage) throws Exception {
// Group root = new Group();
// primaryStage.setScene(new Scene(root));
final Tab tabMissile = new Tab();
// tabMissile.setId("tabMissile");
tabMissile.setText("Missile");
final Tab tabLauncher = new Tab();
// tabLauncher.setId("tabLauncher");
tabLauncher.setText("Launcher");
TabPane tabPane = new TabPane();
tabPane.setId("mainPane");

AnchorPane Missiletab = new AnchorPane();
AnchorPane LauncherTab = new AnchorPane();
final TextField missileID = new TextField();
final TextField destination = new TextField();
final TextField damage = new TextField();
final TextField flytime = new TextField();
Button add = new Button("Resquest & Add missile from server");
final Button connect = new Button("Connect to server");
Button connectLauncher = new Button("Connect to server");
TextField launcherId = new TextField();
Button addLauncher = new Button("Resquest & Add Launcher from server");
Missiletab.setMaxSize(285.0, 371.0);
LauncherTab.setMaxSize(285.0, 371.0);

missileID.setPromptText("missileID");
missileID.setMaxSize(221, 25);
missileID.setLayoutX(14.0);
missileID.setLayoutY(37.0);

launcherId.setPromptText("LauncherID");
launcherId.setMaxSize(221, 25);
launcherId.setLayoutX(14.0);
launcherId.setLayoutY(37.0);

destination.setPromptText("destination");
destination.setMaxSize(221, 25);
destination.setLayoutX(14.0);
destination.setLayoutY(69.0);

damage.setPromptText("damage");
damage.setMaxSize(221, 25);
damage.setLayoutX(14.0);
damage.setLayoutY(101.0);

flytime.setPromptText("flytime");
flytime.setMaxSize(221, 25);
flytime.setLayoutX(14.0);
flytime.setLayoutY(133.0);

add.setLayoutX(14.0);
add.setLayoutY(167.0);

addLauncher.setLayoutX(14.0);
addLauncher.setLayoutY(78.0);

connect.setLayoutX(14.0);
connect.setLayoutY(5.0);

add.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
new Thread(new Runnable() {
@Override
public void run() {
// while (true) {

String id = missileID.getText();
String dest = destination.getText();
String damageT = damage.getText();
String flyTime = flytime.getText();
if (id.isEmpty() || dest.isEmpty() || damageT.isEmpty()) {
JOptionPane.showMessageDialog(null,
"You must fill all details!");
return;
}
// for (WarUIEventsListener l : allListeners) {
// l.addMessageThroughClient(id, dest, damageT, flyTime , launcherName);
// }

}
}).start();
}
});


connectLauncher.setLayoutX(14.0);
connectLauncher.setLayoutY(5.0);

Missiletab.getChildren().addAll(connect, missileID, destination, damage, flytime,add);
LauncherTab.getChildren().addAll(connectLauncher, launcherId, addLauncher);
tabMissile.setContent(Missiletab);
tabLauncher.setContent(LauncherTab);
tabPane.getTabs().addAll(tabMissile , tabLauncher);

Scene scene = new Scene(tabPane, 255, 240);
// primaryStage.setScene(scene);
// root.getChildren().add(tabPane);
scene.getStylesheets().add(this.getClass().getResource("ClientStyle").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}





public static void main(String[] args) {
launch(args);
}

}
Loading

0 comments on commit df038f0

Please sign in to comment.