From 8ac44d13dfed58170452fb5c626456cbc30dd893 Mon Sep 17 00:00:00 2001 From: Calum Date: Wed, 26 Jul 2017 12:05:03 +1200 Subject: [PATCH] Began fixing conflicts with LobbyController #bug --- .../controllers/StartScreenController.java | 161 ++++++++++++++++++ .../java/seng302/gameServer/GameState.java | 2 +- src/main/java/seng302/model/Yacht.java | 71 ++++++-- .../java/seng302/visualiser/GameClient.java | 7 +- .../java/seng302/visualiser/GameView.java | 9 +- .../controllers/LobbyController.java | 9 +- 6 files changed, 233 insertions(+), 26 deletions(-) create mode 100644 src/main/java/seng302/controllers/StartScreenController.java diff --git a/src/main/java/seng302/controllers/StartScreenController.java b/src/main/java/seng302/controllers/StartScreenController.java new file mode 100644 index 00000000..54e47e8f --- /dev/null +++ b/src/main/java/seng302/controllers/StartScreenController.java @@ -0,0 +1,161 @@ +//package seng302.controllers; +// +//import java.net.Inet4Address; +//import java.net.NetworkInterface; +//import java.util.Enumeration; +//import javafx.fxml.FXML; +//import javafx.fxml.FXMLLoader; +//import javafx.scene.control.Alert; +//import javafx.scene.control.Alert.AlertType; +//import javafx.scene.control.TextField; +//import javafx.scene.layout.AnchorPane; +//import javafx.scene.layout.GridPane; +//import javafx.scene.layout.Pane; +//import seng302.client.ClientState; +//import seng302.client.ClientToServerThread; +//import seng302.gameServer.GameState; +//import seng302.gameServer.MainServerThread; +// +//import java.io.IOException; +//import java.net.InetAddress; +//import java.net.UnknownHostException; +// +///** +// * A Class describing the actions of the start screen controller +// * Created by wmu16 on 10/07/17. +// */ +//public class StartScreenController { +// +// @FXML +// private TextField ipTextField; +// @FXML +// private TextField portTextField; +// @FXML +// private GridPane startScreen2; +// +// private Controller controller; +// +// /** +// * Loads the fxml content into the parent pane +// * @param jfxUrl +// * @return the controller of the fxml +// */ +// private Object setContentPane(String jfxUrl) { +// try { +// AnchorPane contentPane = (AnchorPane) startScreen2.getParent(); +// contentPane.getChildren().removeAll(); +// contentPane.getChildren().clear(); +// contentPane.getStylesheets().add(getClass().getResource("/css/master.css").toString()); +// FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(jfxUrl)); +// contentPane.getChildren().addAll((Pane) fxmlLoader.load()); +// +// return fxmlLoader.getController(); +// } catch (javafx.fxml.LoadException e) { +// e.printStackTrace(); +// } catch (IOException e) { +// e.printStackTrace(); +// } +// return null; +// } +// +// +// /** +// * ATTEMPTS TO: +// * Sets up a new game state with your IP address as designated as the host. +// * Starts a thread to listen for incoming connections. +// * Starts a client to server thread and connects to own ip. +// * Switches to the lobby screen +// */ +// @FXML +// public void hostButtonPressed() { +// try { +// // get the lobby controller so that we can pass the game server thread to it +// new GameState(getLocalHostIp()); +// MainServerThread mainServerThread = new MainServerThread(); +// ClientState.setHost(true); +// // host will connect and handshake to itself after setting up the server +// // TODO: 24/07/17 wmu16 - Make port number some static global type constant? +// ClientToServerThread clientToServerThread = new ClientToServerThread(ClientState.getHostIp(), 4942); +// ClientState.setConnectedToHost(true); +// controller.setClientToServerThread(clientToServerThread); +// LobbyController lobbyController = (LobbyController) setContentPane("/views/LobbyView.fxml"); +// lobbyController.setMainServerThread(mainServerThread); +// } catch (Exception e) { +// Alert alert = new Alert(AlertType.ERROR); +// alert.setHeaderText("Cannot host"); +// alert.setContentText("Oops, failed to host, try to restart."); +// alert.showAndWait(); +// e.printStackTrace(); +// } +// +// +// } +// +// /** +// * ATTEMPTS TO: +// * Connect to an ip address and port using the ip and port specified on start screen. +// * Starts a Client To Server Thread to maintain connection to host. +// * Switch view to lobby view. +// */ +// @FXML +// public void connectButtonPressed() { +// // TODO: 10/07/17 wmu16 - Finish function +// try { +// String ipAddress = ipTextField.getText().trim().toLowerCase(); +// Integer port = Integer.valueOf(portTextField.getText().trim()); +// +// ClientToServerThread clientToServerThread = new ClientToServerThread(ipAddress, port); +// ClientState.setHost(false); +// ClientState.setConnectedToHost(true); +// +// controller.setClientToServerThread(clientToServerThread); +// setContentPane("/views/LobbyView.fxml"); +// } catch (Exception e) { +// Alert alert = new Alert(AlertType.ERROR); +// alert.setHeaderText("Cannot reach the host"); +// alert.setContentText("Please check your host IP address."); +// alert.showAndWait(); +// } +// } +// +// public void setController(Controller controller) { +// this.controller = controller; +// } +// +// /** +// * Gets the local host ip address and sets this ip to ClientState. +// * Only runs by the host. +// * +// * @return the localhost ip address +// */ +// private String getLocalHostIp() { +// String ipAddress = null; +// try { +// Enumeration e = NetworkInterface.getNetworkInterfaces(); +// while (e.hasMoreElements()) { +// NetworkInterface ni = e.nextElement(); +// if (ni.isLoopback()) +// continue; +// if(ni.isPointToPoint()) +// continue; +// if(ni.isVirtual()) +// continue; +// +// Enumeration addresses = ni.getInetAddresses(); +// while(addresses.hasMoreElements()) { +// InetAddress address = addresses.nextElement(); +// if(address instanceof Inet4Address) { // skip all ipv6 +// ipAddress = address.getHostAddress(); +// } +// } +// } +// } catch (Exception e) { +// e.printStackTrace(); +// } +// if (ipAddress == null) { +// System.out.println("[HOST] Cannot obtain local host ip address."); +// } +// ClientState.setHostIp(ipAddress); +// return ipAddress; +// } +//} diff --git a/src/main/java/seng302/gameServer/GameState.java b/src/main/java/seng302/gameServer/GameState.java index 39641a9e..bd85da0b 100644 --- a/src/main/java/seng302/gameServer/GameState.java +++ b/src/main/java/seng302/gameServer/GameState.java @@ -108,7 +108,7 @@ public class GameState { } public static Double getWindSpeedKnots() { - return windSpeed / 1000 * ClientPacketParser.MS_TO_KNOTS; + return windSpeed / 1000 * 1.943844492; // TODO: 26/07/17 cir27 - remove magic numbers } public static Map getYachts() { diff --git a/src/main/java/seng302/model/Yacht.java b/src/main/java/seng302/model/Yacht.java index a4857733..f1cce907 100644 --- a/src/main/java/seng302/model/Yacht.java +++ b/src/main/java/seng302/model/Yacht.java @@ -1,21 +1,17 @@ package seng302.model; +import java.util.ArrayList; +import java.util.List; import javafx.beans.property.ReadOnlyDoubleProperty; import javafx.beans.property.ReadOnlyDoubleWrapper; import javafx.beans.property.ReadOnlyLongProperty; import javafx.beans.property.ReadOnlyLongWrapper; +import javafx.scene.control.ListView; import javafx.scene.paint.Color; -import javafx.scene.paint.Paint; import seng302.model.mark.Mark; import static seng302.utilities.GeoUtility.getGeoCoordinate; - import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.util.ArrayList; - -import javafx.scene.paint.Color; -import seng302.client.ClientPacketParser; -import seng302.controllers.RaceViewController; import seng302.gameServer.GameState; import seng302.utilities.GeoPoint; @@ -26,6 +22,12 @@ import seng302.utilities.GeoPoint; * also done outside Boat class because some old variables are not used anymore. */ public class Yacht { + + @FunctionalInterface + public interface YachtLocationListener { + void notifyLocation(Yacht yacht, double lat, double lon, double heading, double velocity); + } + //BOTH AFAIK private String boatType; private Integer sourceId; @@ -52,10 +54,13 @@ public class Yacht { private Double velocity; //CLIENT SIDE + private List locationListeners = new ArrayList<>(); private ReadOnlyDoubleWrapper velocityProperty = new ReadOnlyDoubleWrapper(); private ReadOnlyLongWrapper timeTillNextProperty = new ReadOnlyLongWrapper(); private ReadOnlyLongWrapper timeSinceLastMarkProperty = new ReadOnlyLongWrapper(); - private ReadOnlyDoubleProperty headingProperty = new ReadOnlyDoubleWrapper(); +// private ReadOnlyDoubleWrapper headingProperty = new ReadOnlyDoubleWrapper(); +// private ReadOnlyDoubleWrapper latitudeProperty = new ReadOnlyDoubleWrapper(); +// private ReadOnlyDoubleWrapper longitudeProperty = new ReadOnlyDoubleWrapper(); private Mark lastMarkRounded; private Mark nextMark; private Integer positionInt = 0; @@ -128,7 +133,7 @@ public class Yacht { Double windSpeedKnots = GameState.getWindSpeedKnots(); Double trueWindAngle = Math.abs(GameState.getWindDirection() - heading); Double boatSpeedInKnots = PolarTable.getBoatSpeed(windSpeedKnots, trueWindAngle); - velocity = boatSpeedInKnots / ClientPacketParser.MS_TO_KNOTS * 1000; + velocity = boatSpeedInKnots / 1.943844492 * 1000; // TODO: 26/07/17 cir27 - Remove magic number Double metersCovered = velocity * secondsElapsed; location = getGeoCoordinate(location, heading, metersCovered); } else { @@ -269,7 +274,7 @@ public class Yacht { this.positionInt = position; } - public void setVelocityProperty(double velocity) { + public void updateVelocityProperty(double velocity) { this.velocityProperty.set(velocity); } @@ -287,9 +292,10 @@ public class Yacht { public ReadOnlyLongProperty timeTillNextProperty() { return timeTillNextProperty.getReadOnlyProperty(); + } public Double getVelocityKnots() { - return velocity / 1000 * ClientPacketParser.MS_TO_KNOTS; + return velocity / 1000 * 1.943844492; // TODO: 26/07/17 cir27 - remove magic number } public Long getTimeTillNext() { @@ -361,10 +367,6 @@ public class Yacht { return timeSinceLastMarkProperty.getReadOnlyProperty(); } - public Long getTimeTillNext() { - return timeTillNext; - } - public void setTimeTillNext(Long timeTillNext) { this.timeTillNext = timeTillNext; } @@ -386,4 +388,43 @@ public class Yacht { public void setVelocity(Double velocity) { this.velocity = velocity; } + +// public void updateLatitudeProperty (Double lat) { +// latitudeProperty.set(lat); +// } +// +// public void updateLongitudeProperty (double lon) { +// longitudeProperty.set(lon); +// } +// +// public void updateHeadingProperty (double heading) { +// headingProperty.set(heading); +// } +// +// public ReadOnlyDoubleProperty latitudeProperty () { +// return latitudeProperty.getReadOnlyProperty(); +// } +// +// public ReadOnlyDoubleProperty longitudeProperty () { +// return longitudeProperty.getReadOnlyProperty(); +// } +// +// public ReadOnlyDoubleProperty headingProperty () { +// return headingProperty; +// } + + public void updateLocation (double lat, double lon, double heading, double velocity) { + this.lat = lat; + this.lon = lon; + this.heading = heading; + this.velocity = velocity; + updateVelocityProperty(velocity); + for (YachtLocationListener yll : locationListeners) { + yll.notifyLocation(this, lat, lon, heading, velocity); + } + } + + public void addLocationListener (YachtLocationListener listener) { + locationListeners.add(listener); + } } diff --git a/src/main/java/seng302/visualiser/GameClient.java b/src/main/java/seng302/visualiser/GameClient.java index 9b2b809a..e7295082 100644 --- a/src/main/java/seng302/visualiser/GameClient.java +++ b/src/main/java/seng302/visualiser/GameClient.java @@ -172,6 +172,7 @@ public class GameClient { ); lobbyList.clear(); allBoatsMap.forEach((id, boat) -> lobbyList.add(id.toString() + boat.getBoatName())); + allBoatsMap.forEach((i, b) -> System.out.println(b.getBoatName())); startRaceIfAllDataReceived(); break; @@ -206,10 +207,8 @@ public class GameClient { if (positionData.getType() == DeviceType.YACHT_TYPE) { if (allXMLReceived() && allBoatsMap.containsKey(positionData.getDeviceId())) { Yacht yacht = allBoatsMap.get(positionData.getDeviceId()); - yacht.setVelocityProperty(positionData.getGroundSpeed()); - yacht.setLat(positionData.getLat()); - yacht.setLon(positionData.getLon()); - yacht.setHeading(positionData.getHeading()); + yacht.updateLocation(positionData.getLat(), + positionData.getLon(), positionData.getHeading(), positionData.getGroundSpeed()); } } else if (positionData.getType() == DeviceType.MARK_TYPE) { Mark mark = courseData.getCompoundMarks().get(positionData.getDeviceId()); diff --git a/src/main/java/seng302/visualiser/GameView.java b/src/main/java/seng302/visualiser/GameView.java index 153e3fe0..69d6769c 100644 --- a/src/main/java/seng302/visualiser/GameView.java +++ b/src/main/java/seng302/visualiser/GameView.java @@ -262,8 +262,15 @@ public class GameView extends Pane { BoatObject newObject; for (Yacht yacht : yachts) { newObject = new BoatObject(); -// newObject.bindBoat(boat); newObject.setFill(Colors.getColor()); + boatObjects.put(yacht, newObject); + yacht.addLocationListener((boat, lat, lon, heading, velocity) ->{ + BoatObject bo = boatObjects.get(boat); + Point2D p2d = findScaledXY(lat, lon); + bo.setLayoutX(p2d.getX()); + bo.setLayoutY(p2d.getY()); +// bo.setTrajectory(heading, velocity * (metersPerPixelX + metersPerPixelY) / 2); + }); createAnnotationBox(yacht); } diff --git a/src/main/java/seng302/visualiser/controllers/LobbyController.java b/src/main/java/seng302/visualiser/controllers/LobbyController.java index d282e417..5cbd407c 100644 --- a/src/main/java/seng302/visualiser/controllers/LobbyController.java +++ b/src/main/java/seng302/visualiser/controllers/LobbyController.java @@ -88,7 +88,6 @@ public class LobbyController implements Initializable { private static ObservableList seventhCompetitor = FXCollections.observableArrayList(); private static ObservableList eighthCompetitor = FXCollections.observableArrayList(); // private ClientStateQueryingRunnable clientStateQueryingRunnable; - private ClientStateQueryingRunnable clientStateQueryingRunnable; private static List imageViews; private static List listViews; @@ -233,12 +232,12 @@ public class LobbyController implements Initializable { // } // }); // thread.start(); -// } + } private void initialiseImageView() { - for (int i = 0; i < MAX_NUM_PLAYERS; i++) { - imageViews.get(i).setImage(new Image(getClass().getResourceAsStream("/pics/sail.png"))); - } +// for (int i = 0; i < MAX_NUM_PLAYERS; i++) { +// imageViews.get(i).setImage(new Image(getClass().getResourceAsStream("/pics/sail.png"))); +// } // Image image1 = new Image(getClass().getResourceAsStream("/pics/sail.png")); // firstImageView.setImage(image1); // Image image2 = new Image(getClass().getResourceAsStream("/pics/sail.png"));