From d6a436d2eb4e4fbbd70a114492813880ae2f6e1f Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Tue, 15 Aug 2017 14:18:48 +1200 Subject: [PATCH 1/3] WIP: Initial commit for seperating server and client yacht classes tags: #story[1124] --- .../java/seng302/gameServer/GameState.java | 33 +++++++++---------- .../gameServer/ServerToClientThread.java | 15 ++++----- src/main/java/seng302/model/Player.java | 6 ++-- .../model/stream/xml/generator/Race.java | 9 ++--- .../RegularPacketsTest.java | 3 +- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/main/java/seng302/gameServer/GameState.java b/src/main/java/seng302/gameServer/GameState.java index 3c3ec2c6..110892a8 100644 --- a/src/main/java/seng302/gameServer/GameState.java +++ b/src/main/java/seng302/gameServer/GameState.java @@ -4,9 +4,9 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import seng302.gameServer.server.messages.BoatAction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import seng302.gameServer.server.messages.BoatAction; import seng302.gameServer.server.messages.MarkRoundingMessage; import seng302.gameServer.server.messages.MarkType; import seng302.gameServer.server.messages.Message; @@ -14,7 +14,7 @@ import seng302.gameServer.server.messages.RoundingBoatStatus; import seng302.model.GeoPoint; import seng302.model.Player; import seng302.model.PolarTable; -import seng302.model.Yacht; +import seng302.model.ServerYacht; import seng302.model.mark.CompoundMark; import seng302.model.mark.Mark; import seng302.model.mark.MarkOrder; @@ -44,7 +44,7 @@ public class GameState implements Runnable { private static String hostIpAddress; private static List players; - private static Map yachts; + private static Map yachts; private static Boolean isRaceStarted; private static GameStages currentStage; private static MarkOrder markOrder; @@ -102,7 +102,7 @@ public class GameState implements Runnable { playerStringMap.remove(player); } - public static void addYacht(Integer sourceId, Yacht yacht) { + public static void addYacht(Integer sourceId, ServerYacht yacht) { yachts.put(sourceId, yacht); } @@ -146,7 +146,7 @@ public class GameState implements Runnable { return GeoUtility.mmsToKnots(windSpeed); // TODO: 26/07/17 cir27 - remove magic numbers } - public static Map getYachts() { + public static Map getYachts() { return yachts; } @@ -185,7 +185,7 @@ public class GameState implements Runnable { } public static void updateBoat(Integer sourceId, BoatAction actionType) { - Yacht playerYacht = yachts.get(sourceId); + ServerYacht playerYacht = yachts.get(sourceId); switch (actionType) { case VMG: playerYacht.turnToVMG(); @@ -215,7 +215,7 @@ public class GameState implements Runnable { public void update() { Double timeInterval = (System.currentTimeMillis() - previousUpdateTime) / 1000000.0; previousUpdateTime = System.currentTimeMillis(); - for (Yacht yacht : yachts.values()) { + for (ServerYacht yacht : yachts.values()) { updateVelocity(yacht); yacht.runAutoPilot(); yacht.updateLocation(timeInterval); @@ -226,12 +226,11 @@ public class GameState implements Runnable { } - private void updateVelocity(Yacht yacht) { + private void updateVelocity(ServerYacht yacht) { Double velocity = yacht.getCurrentVelocity(); Double trueWindAngle = Math.abs(windDirection - yacht.getHeading()); Double boatSpeedInKnots = PolarTable.getBoatSpeed(getWindSpeedKnots(), trueWindAngle); Double maxBoatSpeed = GeoUtility.knotsToMMS(boatSpeedInKnots); - yacht.setCurrentMaxVelocity(maxBoatSpeed); if (yacht.getSailIn() && yacht.getCurrentVelocity() <= maxBoatSpeed && maxBoatSpeed != 0d) { if (velocity < maxBoatSpeed) { @@ -263,7 +262,7 @@ public class GameState implements Runnable { * @throws IndexOutOfBoundsException If the next mark is null (ie the last mark in the race) * Check first using {@link seng302.model.mark.MarkOrder#isLastMark(Integer)} */ - private Double calcDistanceToCurrentMark(Yacht yacht) throws IndexOutOfBoundsException { + private Double calcDistanceToCurrentMark(ServerYacht yacht) throws IndexOutOfBoundsException { Integer currentMarkSeqID = yacht.getCurrentMarkSeqID(); CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID); GeoPoint location = yacht.getLocation(); @@ -292,7 +291,7 @@ public class GameState implements Runnable { * in-race Gate 3 - Passing any in-race Mark 4 - Passing the finish line * @param yacht the current yacht to check for progression */ - private void checkForLegProgression(Yacht yacht) { + private void checkForLegProgression(ServerYacht yacht) { Integer currentMarkSeqID = yacht.getCurrentMarkSeqID(); CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID); @@ -324,7 +323,7 @@ public class GameState implements Runnable { * * @param yacht The current yacht to check for */ - private Boolean checkStartLineCrossing(Yacht yacht) { + private Boolean checkStartLineCrossing(ServerYacht yacht) { Integer currentMarkSeqID = yacht.getCurrentMarkSeqID(); CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID); GeoPoint lastLocation = yacht.getLastLocation(); @@ -354,7 +353,7 @@ public class GameState implements Runnable { * * @param yacht The current yacht to check for */ - private Boolean checkMarkRounding(Yacht yacht) { + private Boolean checkMarkRounding(ServerYacht yacht) { Integer currentMarkSeqID = yacht.getCurrentMarkSeqID(); CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID); GeoPoint lastLocation = yacht.getLastLocation(); @@ -383,7 +382,7 @@ public class GameState implements Runnable { * * @param yacht The current yacht to check for */ - private Boolean checkGateRounding(Yacht yacht) { + private Boolean checkGateRounding(ServerYacht yacht) { Integer currentMarkSeqID = yacht.getCurrentMarkSeqID(); CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID); GeoPoint lastLocation = yacht.getLastLocation(); @@ -426,7 +425,7 @@ public class GameState implements Runnable { * * @param yacht The current yacht to check for */ - private Boolean checkFinishLineCrossing(Yacht yacht) { + private Boolean checkFinishLineCrossing(ServerYacht yacht) { Integer currentMarkSeqID = yacht.getCurrentMarkSeqID(); CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID); GeoPoint lastLocation = yacht.getLastLocation(); @@ -450,7 +449,7 @@ public class GameState implements Runnable { return false; } - private void sendMarkRoundingMessage(Yacht yacht) { + private void sendMarkRoundingMessage(ServerYacht yacht) { Integer sourceID = yacht.getSourceId(); Integer currentMarkSeqID = yacht.getCurrentMarkSeqID(); CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID); @@ -468,7 +467,7 @@ public class GameState implements Runnable { } - private void logMarkRounding(Yacht yacht) { + private void logMarkRounding(ServerYacht yacht) { Mark roundingMark = yacht.getClosestCurrentMark(); logger.debug( diff --git a/src/main/java/seng302/gameServer/ServerToClientThread.java b/src/main/java/seng302/gameServer/ServerToClientThread.java index a5ae3257..c5bad4bb 100644 --- a/src/main/java/seng302/gameServer/ServerToClientThread.java +++ b/src/main/java/seng302/gameServer/ServerToClientThread.java @@ -19,6 +19,7 @@ import java.util.zip.CRC32; import java.util.zip.Checksum; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import seng302.gameServer.server.messages.BoatAction; import seng302.gameServer.server.messages.BoatLocationMessage; import seng302.gameServer.server.messages.BoatStatus; import seng302.gameServer.server.messages.BoatSubMessage; @@ -32,13 +33,12 @@ import seng302.gameServer.server.messages.RegistrationResponseStatus; import seng302.gameServer.server.messages.XMLMessage; import seng302.gameServer.server.messages.XMLMessageSubType; import seng302.model.Player; -import seng302.model.Yacht; +import seng302.model.ServerYacht; import seng302.model.stream.packets.PacketType; import seng302.model.stream.packets.StreamPacket; import seng302.model.stream.xml.generator.Race; import seng302.model.stream.xml.generator.Regatta; import seng302.utilities.XMLGenerator; -import seng302.gameServer.server.messages.BoatAction; /** * A class describing a single connection to a Client for the purposes of sending and receiving on @@ -115,8 +115,7 @@ public class ServerToClientThread implements Runnable, Observer { all = ln.lines().collect(Collectors.toList()); lName = all.get(ThreadLocalRandom.current().nextInt(0, all.size())); - - Yacht yacht = new Yacht( + ServerYacht yacht = new ServerYacht( "Yacht", sourceId, sourceId.toString(), fName, fName + " " + lName, "NZ" ); @@ -221,7 +220,7 @@ public class ServerToClientThread implements Runnable, Observer { xml = new XMLGenerator(); Race race = new Race(); - for (Yacht yacht : GameState.getYachts().values()) { + for (ServerYacht yacht : GameState.getYachts().values()) { race.addBoat(yacht); } @@ -299,8 +298,8 @@ public class ServerToClientThread implements Runnable, Observer { public void sendBoatLocationPackets() { - ArrayList yachts = new ArrayList<>(GameState.getYachts().values()); - for (Yacht yacht : yachts) { + ArrayList yachts = new ArrayList<>(GameState.getYachts().values()); + for (ServerYacht yacht : yachts) { BoatLocationMessage boatLocationMessage = new BoatLocationMessage( yacht.getSourceId(), @@ -326,7 +325,7 @@ public class ServerToClientThread implements Runnable, Observer { RaceStatus raceStatus; for (Player player : GameState.getPlayers()) { - Yacht y = player.getYacht(); + ServerYacht y = player.getYacht(); if (GameState.getCurrentStage() == GameStages.PRE_RACE) { boatStatus = BoatStatus.PRESTART; diff --git a/src/main/java/seng302/model/Player.java b/src/main/java/seng302/model/Player.java index 175b7a45..1ed2b6dc 100644 --- a/src/main/java/seng302/model/Player.java +++ b/src/main/java/seng302/model/Player.java @@ -9,11 +9,11 @@ import java.net.Socket; public class Player { private Socket socket; - private Yacht yacht; + private ServerYacht yacht; private Integer lastMarkPassed; - public Player(Socket socket, Yacht yacht) { + public Player(Socket socket, ServerYacht yacht) { this.socket = socket; this.yacht = yacht; } @@ -30,7 +30,7 @@ public class Player { this.lastMarkPassed = lastMarkPassed; } - public Yacht getYacht() { + public ServerYacht getYacht() { return yacht; } diff --git a/src/main/java/seng302/model/stream/xml/generator/Race.java b/src/main/java/seng302/model/stream/xml/generator/Race.java index c9f8cded..cf42cb2c 100644 --- a/src/main/java/seng302/model/stream/xml/generator/Race.java +++ b/src/main/java/seng302/model/stream/xml/generator/Race.java @@ -4,13 +4,14 @@ import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import seng302.model.Yacht; +import seng302.model.ServerYacht; /** * A Race object that can be parsed into XML */ public class Race { - private List yachts; + + private List yachts; private LocalDateTime startTime; public Race(){ @@ -22,7 +23,7 @@ public class Race { * Add a boat to the race * @param yacht The boat to add */ - public void addBoat(Yacht yacht){ + public void addBoat(ServerYacht yacht) { yachts.add(yacht); } @@ -30,7 +31,7 @@ public class Race { * Get a list of boats in the race * @return A List of boats */ - public List getBoats(){ + public List getBoats() { return Collections.unmodifiableList(yachts); } diff --git a/src/test/java/seng302/visualiser/ClientToServerTests/RegularPacketsTest.java b/src/test/java/seng302/visualiser/ClientToServerTests/RegularPacketsTest.java index d8c09728..347dd14f 100644 --- a/src/test/java/seng302/visualiser/ClientToServerTests/RegularPacketsTest.java +++ b/src/test/java/seng302/visualiser/ClientToServerTests/RegularPacketsTest.java @@ -9,6 +9,7 @@ import seng302.gameServer.GameStages; import seng302.gameServer.GameState; import seng302.gameServer.MainServerThread; import seng302.gameServer.server.messages.BoatAction; +import seng302.model.ServerYacht; import seng302.model.Yacht; import seng302.visualiser.ClientToServerThread; @@ -33,7 +34,7 @@ public class RegularPacketsTest { final double TEST_DISTANCE = 10.0; serverThread.startGame(); SleepThreadMaxDelay(); - Yacht yacht = new ArrayList<>(GameState.getYachts().values()).get(0); + ServerYacht yacht = new ArrayList<>(GameState.getYachts().values()).get(0); double startAngle = yacht.getHeading(); long startTime = System.currentTimeMillis(); clientThread.sendBoatAction(BoatAction.UPWIND); From 2dc0ba07d98fc4a30d44dfc20afc6ba10124db47 Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Tue, 15 Aug 2017 14:30:39 +1200 Subject: [PATCH 2/3] WIP: Second commit for seperating server and client yacht classes tags: #story[1124] --- src/main/java/seng302/model/ServerYacht.java | 376 ++++++++++++++++++ src/main/java/seng302/model/Yacht.java | 309 +------------- src/test/java/seng302/models/YachtTest.java | 6 +- .../RegularPacketsTest.java | 4 +- 4 files changed, 390 insertions(+), 305 deletions(-) create mode 100644 src/main/java/seng302/model/ServerYacht.java diff --git a/src/main/java/seng302/model/ServerYacht.java b/src/main/java/seng302/model/ServerYacht.java new file mode 100644 index 00000000..b0b9a0a3 --- /dev/null +++ b/src/main/java/seng302/model/ServerYacht.java @@ -0,0 +1,376 @@ +package seng302.model; + +import java.util.HashMap; +import java.util.Observable; +import java.util.Observer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import seng302.gameServer.GameState; +import seng302.model.mark.Mark; +import seng302.utilities.GeoUtility; + +/** + * Yacht class for the racing boat.

Class created to store more variables (eg. boat statuses) + * compared to the XMLParser boat class, also done outside Boat class because some old variables are + * not used anymore. + */ +public class ServerYacht extends Observable { + + private Logger logger = LoggerFactory.getLogger(Yacht.class); + + public static final Double TURN_STEP = 5.0; + + private String boatType; + private Integer sourceId; + private String hullID; //matches HullNum in the XML spec. + private String shortName; + private String boatName; + private String country; + + private Double lastHeading; + private Boolean sailIn; + private Double heading; + private GeoPoint location; + private Double currentVelocity; + private Boolean isAuto; + private Double autoHeading; + + private Integer currentMarkSeqID = 0; + private GeoPoint lastLocation; + private Boolean hasEnteredRoundingZone; + private Mark closestCurrentMark; + private Boolean hasPassedLine; + private Boolean hasPassedThroughGate; + private Boolean finishedRace; + + + public ServerYacht(String boatType, Integer sourceId, String hullID, String shortName, + String boatName, String country) { + this.boatType = boatType; + this.sourceId = sourceId; + this.hullID = hullID; + this.shortName = shortName; + this.boatName = boatName; + this.country = country; + this.sailIn = false; + this.isAuto = false; + this.location = new GeoPoint(57.670341, 11.826856); + this.lastLocation = location; + this.heading = 120.0; //In degrees + this.currentVelocity = 0d; //in mms-1 + + this.hasEnteredRoundingZone = false; + this.hasPassedLine = false; + this.hasPassedThroughGate = false; + this.finishedRace = false; + } + + + /** + * Changes the boats current currentVelocity by a set amount, positive or negative + * + * @param velocityChange The ammount to change the currentVelocity by, in mms-1 + */ + public void changeVelocity(Double velocityChange) { + currentVelocity += velocityChange; + } + + /** + * Updates the boat to a new GeoPoint whilst preserving the last location + * + * @param secondsElapsed The seconds elapsed since the last update of this yacht + */ + public void updateLocation(Double secondsElapsed) { + lastLocation = location; + location = GeoUtility.getGeoCoordinate(location, heading, currentVelocity * secondsElapsed); + } + + /** + * Add ServerToClientThread as the observer, this observer pattern mainly server for the boat + * rounding package. + */ + @Override + public void addObserver(Observer o) { + super.addObserver(o); + } + + /** + * Adjusts the heading of the boat by a given amount, while recording the boats last heading. + * + * @param amount the amount by which to adjust the boat heading. + */ + public void adjustHeading(Double amount) { + Double newVal = heading + amount; + lastHeading = heading; + heading = (double) Math.floorMod(newVal.longValue(), 360L); + } + + /** + * Swaps the boats direction from one side of the wind to the other. + */ + public void tackGybe(Double windDirection) { + if (isAuto) { + disableAutoPilot(); + } else { + Double normalizedHeading = normalizeHeading(); + Double newVal = (-2 * normalizedHeading) + heading; + Double newHeading = (double) Math.floorMod(newVal.longValue(), 360L); + setAutoPilot(newHeading); + } + } + + /** + * Enables the boats auto pilot feature, which will move the boat towards a given heading. + * + * @param thisHeading The heading to move the boat towards. + */ + private void setAutoPilot(Double thisHeading) { + isAuto = true; + autoHeading = thisHeading; + } + + /** + * Disables the auto pilot function. + */ + public void disableAutoPilot() { + isAuto = false; + } + + /** + * Moves the boat towards the given heading when the auto pilot was set. Disables the auto pilot + * in the event that the boat is within the range of 1 turn step of its goal. + */ + public void runAutoPilot() { + if (isAuto) { + turnTowardsHeading(autoHeading); + if (Math.abs(heading - autoHeading) + <= TURN_STEP) { //Cancel when within 1 turn step of target. + isAuto = false; + } + } + } + + public void toggleSailIn() { + sailIn = !sailIn; + } + + public void turnUpwind() { + disableAutoPilot(); + Double normalizedHeading = normalizeHeading(); + if (normalizedHeading == 0) { + if (lastHeading < 180) { + adjustHeading(-TURN_STEP); + } else { + adjustHeading(TURN_STEP); + } + } else if (normalizedHeading == 180) { + if (lastHeading < 180) { + adjustHeading(TURN_STEP); + } else { + adjustHeading(-TURN_STEP); + } + } else if (normalizedHeading < 180) { + adjustHeading(-TURN_STEP); + } else { + adjustHeading(TURN_STEP); + } + } + + public void turnDownwind() { + disableAutoPilot(); + Double normalizedHeading = normalizeHeading(); + if (normalizedHeading == 0) { + if (lastHeading < 180) { + adjustHeading(TURN_STEP); + } else { + adjustHeading(-TURN_STEP); + } + } else if (normalizedHeading == 180) { + if (lastHeading < 180) { + adjustHeading(-TURN_STEP); + } else { + adjustHeading(TURN_STEP); + } + } else if (normalizedHeading < 180) { + adjustHeading(TURN_STEP); + } else { + adjustHeading(-TURN_STEP); + } + } + + /** + * Takes the VMG from the polartable for upwind or downwind depending on the boats direction, + * and uses this to calculate a heading to move the yacht towards. + */ + public void turnToVMG() { + if (isAuto) { + disableAutoPilot(); + } else { + Double normalizedHeading = normalizeHeading(); + Double optimalHeading; + HashMap optimalPolarMap; + + if (normalizedHeading >= 90 && normalizedHeading <= 270) { // Downwind + optimalPolarMap = PolarTable.getOptimalDownwindVMG(GameState.getWindSpeedKnots()); + } else { + optimalPolarMap = PolarTable.getOptimalUpwindVMG(GameState.getWindSpeedKnots()); + } + optimalHeading = optimalPolarMap.keySet().iterator().next(); + + if (normalizedHeading > 180) { + optimalHeading = 360 - optimalHeading; + } + + // Take optimal heading and turn into a boat heading rather than a wind heading. + optimalHeading = + optimalHeading + GameState.getWindDirection(); + + setAutoPilot(optimalHeading); + } + } + + /** + * Takes a given heading and rotates the boat towards that heading. This does not care about + * being upwind or downwind, just which direction will reach a given heading faster. + * + * @param newHeading The heading to turn the yacht towards. + */ + private void turnTowardsHeading(Double newHeading) { + Double newVal = heading - newHeading; + if (Math.floorMod(newVal.longValue(), 360L) > 180) { + adjustHeading(TURN_STEP / 5); + } else { + adjustHeading(-TURN_STEP / 5); + } + } + + /** + * Returns a heading normalized for the wind direction. Heading direction into the wind is 0, + * directly away is 180. + * + * @return The normalized heading accounting for wind direction. + */ + private Double normalizeHeading() { + Double normalizedHeading = heading - GameState.windDirection; + normalizedHeading = (double) Math.floorMod(normalizedHeading.longValue(), 360L); + return normalizedHeading; + } + + public Integer getSourceId() { + //@TODO Remove and merge with Creating Game Loop + if (sourceId == null) { + return 0; + } + return sourceId; + } + + // TODO: 15/08/17 EXTREME BUG DO NOT DELETE THIS FUNCTION BREAKS PROGRAM IDK WHY ¯\_(ツ)_/¯ + public String getHullID() { + System.out.println("HullId"); + if (hullID == null) { + return ""; + } + return hullID; + } + + // TODO: 15/08/17 EXTREME BUG DO NOT DELETE THIS FUNCTION BREAKS PROGRAM IDK WHY ¯\_(ツ)_/¯ + public String getShortName() { + System.out.println("shortName"); + return shortName; + } + + public String getBoatName() { + return boatName; + } + + public String getCountry() { + if (country == null) { + return ""; + } + return country; + } + + + public GeoPoint getLocation() { + return location; + } + + + public Double getHeading() { + return heading; + } + + public void setHeading(Double heading) { + this.heading = heading; + } + + public Boolean getSailIn() { + return sailIn; + } + + @Override + public String toString() { + return boatName; + } + + + public void setIsFinished(Boolean isFinished) { + finishedRace = isFinished; + } + + public Boolean getFinishedRace() { + return finishedRace; + } + + public Double getCurrentVelocity() { + return currentVelocity; + } + + public void setCurrentVelocity(Double currentVelocity) { + this.currentVelocity = currentVelocity; + } + + public Integer getCurrentMarkSeqID() { + return currentMarkSeqID; + } + + public GeoPoint getLastLocation() { + return lastLocation; + } + + public Mark getClosestCurrentMark() { + return closestCurrentMark; + } + + public void setClosestCurrentMark(Mark closestCurrentMark) { + this.closestCurrentMark = closestCurrentMark; + } + + public void setHasEnteredRoundingZone(Boolean hasEnteredRoundingZone) { + this.hasEnteredRoundingZone = hasEnteredRoundingZone; + } + + public void setHasPassedLine(Boolean hasPassedLine) { + this.hasPassedLine = hasPassedLine; + } + + public void setHasPassedThroughGate(Boolean hasPassedThroughGate) { + this.hasPassedThroughGate = hasPassedThroughGate; + } + + public void incrementMarkSeqID() { + currentMarkSeqID++; + } + + public Boolean hasEnteredRoundingZone() { + return hasEnteredRoundingZone; + } + + public Boolean hasPassedThroughGate() { + return hasPassedThroughGate; + } + + public Boolean hasPassedLine() { + return hasPassedLine; + } +} diff --git a/src/main/java/seng302/model/Yacht.java b/src/main/java/seng302/model/Yacht.java index ba967dfe..5a038f41 100644 --- a/src/main/java/seng302/model/Yacht.java +++ b/src/main/java/seng302/model/Yacht.java @@ -3,7 +3,6 @@ package seng302.model; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Observable; import java.util.Observer; @@ -14,10 +13,7 @@ import javafx.beans.property.ReadOnlyLongWrapper; import javafx.scene.paint.Color; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import seng302.gameServer.GameState; import seng302.model.mark.CompoundMark; -import seng302.model.mark.Mark; -import seng302.utilities.GeoUtility; /** * Yacht class for the racing boat.

Class created to store more variables (eg. boat statuses) @@ -46,29 +42,12 @@ public class Yacht extends Observable { private Long estimateTimeAtFinish; private Integer currentMarkSeqID = 0; private Long markRoundTime; - private Double distanceToCurrentMark; private Long timeTillNext; private Double heading; private Integer legNumber = 0; - - //SERVER SIDE - public static final Double TURN_STEP = 5.0; //This should be in some utils class somewhere 2bh. Public for tests sake. - private Double lastHeading; - private Boolean sailIn; private GeoPoint location; private Integer boatStatus; private Double currentVelocity; - private Double currentMaxVelocity; - private Boolean isAuto; - private Double autoHeading; - - //MARK ROUNDING INFO - private GeoPoint lastLocation; //For purposes of mark rounding calculations - private Boolean hasEnteredRoundingZone; //The distance that the boat must be from the mark to round - private Mark closestCurrentMark; - private Boolean hasPassedLine; - private Boolean hasPassedThroughGate; - private Boolean finishedRace; //CLIENT SIDE private List locationListeners = new ArrayList<>(); @@ -87,36 +66,9 @@ public class Yacht extends Observable { this.shortName = shortName; this.boatName = boatName; this.country = country; - this.sailIn = false; - this.isAuto = false; this.location = new GeoPoint(57.670341, 11.826856); - this.lastLocation = location; this.heading = 120.0; //In degrees - this.currentVelocity = 0d; //in mms-1 - - this.hasEnteredRoundingZone = false; - this.hasPassedLine = false; - this.hasPassedThroughGate = false; - this.finishedRace = false; - } - - - /** - * Changes the boats current currentVelocity by a set amount, positive or negative - * @param velocityChange The ammount to change the currentVelocity by, in mms-1 - */ - public void changeVelocity(Double velocityChange) { - currentVelocity += velocityChange; - } - - /** - * Updates the boat to a new GeoPoint whilst preserving the last location - * - * @param secondsElapsed The seconds elapsed since the last update of this yacht - */ - public void updateLocation(Double secondsElapsed) { - lastLocation = location; - location = GeoUtility.getGeoCoordinate(location, heading, currentVelocity * secondsElapsed); + this.currentVelocity = 0d; } /** @@ -128,169 +80,6 @@ public class Yacht extends Observable { super.addObserver(o); } - - /** - * Adjusts the heading of the boat by a given amount, while recording the boats last heading. - * - * @param amount the amount by which to adjust the boat heading. - */ - public void adjustHeading(Double amount) { - Double newVal = heading + amount; - lastHeading = heading; - heading = (double) Math.floorMod(newVal.longValue(), 360L); - } - - /** - * Swaps the boats direction from one side of the wind to the other. - */ - public void tackGybe(Double windDirection) { - if (isAuto) { - disableAutoPilot(); - } else { - Double normalizedHeading = normalizeHeading(); - Double newVal = (-2 * normalizedHeading) + heading; - Double newHeading = (double) Math.floorMod(newVal.longValue(), 360L); - setAutoPilot(newHeading); - } - } - - /** - * Enables the boats auto pilot feature, which will move the boat towards a given heading. - * - * @param thisHeading The heading to move the boat towards. - */ - private void setAutoPilot(Double thisHeading) { - isAuto = true; - autoHeading = thisHeading; - } - - /** - * Disables the auto pilot function. - */ - public void disableAutoPilot() { - isAuto = false; - } - - /** - * Moves the boat towards the given heading when the auto pilot was set. Disables the auto pilot - * in the event that the boat is within the range of 1 turn step of its goal. - */ - public void runAutoPilot() { - if (isAuto) { - turnTowardsHeading(autoHeading); - if (Math.abs(heading - autoHeading) - <= TURN_STEP) { //Cancel when within 1 turn step of target. - isAuto = false; - } - } - } - - public void toggleSailIn() { - sailIn = !sailIn; - } - - public void turnUpwind() { - disableAutoPilot(); - Double normalizedHeading = normalizeHeading(); - if (normalizedHeading == 0) { - if (lastHeading < 180) { - adjustHeading(-TURN_STEP); - } else { - adjustHeading(TURN_STEP); - } - } else if (normalizedHeading == 180) { - if (lastHeading < 180) { - adjustHeading(TURN_STEP); - } else { - adjustHeading(-TURN_STEP); - } - } else if (normalizedHeading < 180) { - adjustHeading(-TURN_STEP); - } else { - adjustHeading(TURN_STEP); - } - } - - public void turnDownwind() { - disableAutoPilot(); - Double normalizedHeading = normalizeHeading(); - if (normalizedHeading == 0) { - if (lastHeading < 180) { - adjustHeading(TURN_STEP); - } else { - adjustHeading(-TURN_STEP); - } - } else if (normalizedHeading == 180) { - if (lastHeading < 180) { - adjustHeading(-TURN_STEP); - } else { - adjustHeading(TURN_STEP); - } - } else if (normalizedHeading < 180) { - adjustHeading(TURN_STEP); - } else { - adjustHeading(-TURN_STEP); - } - } - - /** - * Takes the VMG from the polartable for upwind or downwind depending on the boats direction, - * and uses this to calculate a heading to move the yacht towards. - */ - public void turnToVMG() { - if (isAuto) { - disableAutoPilot(); - } else { - Double normalizedHeading = normalizeHeading(); - Double optimalHeading; - HashMap optimalPolarMap; - - if (normalizedHeading >= 90 && normalizedHeading <= 270) { // Downwind - optimalPolarMap = PolarTable.getOptimalDownwindVMG(GameState.getWindSpeedKnots()); - } else { - optimalPolarMap = PolarTable.getOptimalUpwindVMG(GameState.getWindSpeedKnots()); - } - optimalHeading = optimalPolarMap.keySet().iterator().next(); - - if (normalizedHeading > 180) { - optimalHeading = 360 - optimalHeading; - } - - // Take optimal heading and turn into a boat heading rather than a wind heading. - optimalHeading = - optimalHeading + GameState.getWindDirection(); - - setAutoPilot(optimalHeading); - } - } - - /** - * Takes a given heading and rotates the boat towards that heading. This does not care about - * being upwind or downwind, just which direction will reach a given heading faster. - * - * @param newHeading The heading to turn the yacht towards. - */ - private void turnTowardsHeading(Double newHeading) { - Double newVal = heading - newHeading; - if (Math.floorMod(newVal.longValue(), 360L) > 180) { - adjustHeading(TURN_STEP / 5); - } else { - adjustHeading(-TURN_STEP / 5); - } - } - - /** - * Returns a heading normalized for the wind direction. Heading direction into the wind is 0, - * directly away is 180. - * - * @return The normalized heading accounting for wind direction. - */ - private Double normalizeHeading() { - Double normalizedHeading = heading - GameState.windDirection; - normalizedHeading = (double) Math.floorMod(normalizedHeading.longValue(), 360L); - return normalizedHeading; - } - public String getBoatType() { return boatType; } @@ -338,9 +127,6 @@ public class Yacht extends Observable { } public void setLegNumber(Integer legNumber) { -// if (colour != null && position != "-" && legNumber != this.legNumber) { -// RaceViewController.updateYachtPositionSparkline(this, legNumber); -// } this.legNumber = legNumber; } @@ -377,18 +163,10 @@ public class Yacht extends Observable { return velocityProperty.getReadOnlyProperty(); } - public double getVelocityMMS() { - return currentVelocity; - } - public ReadOnlyLongProperty timeTillNextProperty() { return timeTillNextProperty.getReadOnlyProperty(); } - public Double getVelocityKnots() { - return currentVelocity / 1000 * 1.943844492; // TODO: 26/07/17 cir27 - remove magic number - } - public Long getTimeTillNext() { return timeTillNext; } @@ -416,8 +194,6 @@ public class Yacht extends Observable { * @param lng Longitude */ public void setLocation(Double lat, Double lng) { - lastLocation.setLat(location.getLat()); - lastLocation.setLng(location.getLng()); location.setLat(lat); location.setLng(lng); } @@ -430,10 +206,6 @@ public class Yacht extends Observable { this.heading = heading; } - public Boolean getSailIn() { - return sailIn; - } - @Override public String toString() { return boatName; @@ -460,82 +232,19 @@ public class Yacht extends Observable { this.colour = colour; } - public void setIsFinished(Boolean isFinished) { - finishedRace = isFinished; - } +// public Double getCurrentVelocity() { +// return currentVelocity; +// } +// +// public void setCurrentVelocity(Double currentVelocity) { +// this.currentVelocity = currentVelocity; +// } - public Boolean getFinishedRace() { - return finishedRace; - } - - public Double getCurrentVelocity() { - return currentVelocity; - } - - public void setCurrentVelocity(Double currentVelocity) { - this.currentVelocity = currentVelocity; - } - - public Integer getCurrentMarkSeqID() { - return currentMarkSeqID; - } - - public GeoPoint getLastLocation() { - return lastLocation; - } - - public Mark getClosestCurrentMark() { - return closestCurrentMark; - } - - public void setClosestCurrentMark(Mark closestCurrentMark) { - this.closestCurrentMark = closestCurrentMark; - } - - public void setHasEnteredRoundingZone(Boolean hasEnteredRoundingZone) { - this.hasEnteredRoundingZone = hasEnteredRoundingZone; - } - - public void setHasPassedLine(Boolean hasPassedLine) { - this.hasPassedLine = hasPassedLine; - } - - public void setHasPassedThroughGate(Boolean hasPassedThroughGate) { - this.hasPassedThroughGate = hasPassedThroughGate; - } - - public void incrementMarkSeqID() { - currentMarkSeqID++; - } - - public Boolean hasEnteredRoundingZone() { - return hasEnteredRoundingZone; - } - - public Boolean hasPassedThroughGate() { - return hasPassedThroughGate; - } - - public Boolean hasPassedLine() { - return hasPassedLine; - } - - public Double getDistanceToCurrentMark() { - return distanceToCurrentMark; - } - - public Double getCurrentMaxVelocity() { - return currentMaxVelocity; - } - - public void setCurrentMaxVelocity(Double currentMaxVelocity) { - this.currentMaxVelocity = currentMaxVelocity; - } public void updateLocation(double lat, double lng, double heading, double velocity) { setLocation(lat, lng); this.heading = heading; - this.currentVelocity = velocity; +// this.currentVelocity = velocity; updateVelocityProperty(velocity); for (YachtLocationListener yll : locationListeners) { yll.notifyLocation(this, lat, lng, heading, velocity); diff --git a/src/test/java/seng302/models/YachtTest.java b/src/test/java/seng302/models/YachtTest.java index 49b73daa..788ae1dc 100644 --- a/src/test/java/seng302/models/YachtTest.java +++ b/src/test/java/seng302/models/YachtTest.java @@ -8,12 +8,12 @@ import org.junit.BeforeClass; import org.junit.Test; import seng302.gameServer.GameState; import seng302.model.PolarTable; -import seng302.model.Yacht; +import seng302.model.ServerYacht; public class YachtTest { - private static Yacht y1; + private static ServerYacht y1; //Yacht y2; private static Double windDirection = 180d; private static Double windSpeed = 20d; @@ -21,7 +21,7 @@ public class YachtTest { @BeforeClass public static void setUp() { - y1 = new Yacht("Yacht", 101, "Y1", "Y1", "Yacht 1", "C1"); + y1 = new ServerYacht("Yacht", 101, "Y1", "Y1", "Yacht 1", "C1"); gs = new GameState("localhost"); } diff --git a/src/test/java/seng302/visualiser/ClientToServerTests/RegularPacketsTest.java b/src/test/java/seng302/visualiser/ClientToServerTests/RegularPacketsTest.java index 347dd14f..92bf165c 100644 --- a/src/test/java/seng302/visualiser/ClientToServerTests/RegularPacketsTest.java +++ b/src/test/java/seng302/visualiser/ClientToServerTests/RegularPacketsTest.java @@ -10,7 +10,6 @@ import seng302.gameServer.GameState; import seng302.gameServer.MainServerThread; import seng302.gameServer.server.messages.BoatAction; import seng302.model.ServerYacht; -import seng302.model.Yacht; import seng302.visualiser.ClientToServerThread; /** @@ -46,7 +45,8 @@ public class RegularPacketsTest { long endTime = System.currentTimeMillis(); SleepThreadMaxDelay(); //Allowed to be two loops of delay due to loop delay and processing delay at client + server ends. - Assert.assertEquals(TEST_DISTANCE / Yacht.TURN_STEP * ClientToServerThread.PACKET_SENDING_INTERVAL_MS, + Assert.assertEquals( + TEST_DISTANCE / ServerYacht.TURN_STEP * ClientToServerThread.PACKET_SENDING_INTERVAL_MS, (endTime - startTime), 2 * ClientToServerThread.PACKET_SENDING_INTERVAL_MS); } From c125708a4af78dc3666092937f20ac61f8b74aed Mon Sep 17 00:00:00 2001 From: Haoming Yin Date: Tue, 15 Aug 2017 14:49:16 +1200 Subject: [PATCH 3/3] Final commit for yacht refactor tags: #story[1124] #pair[hyi25, wmu16] --- .../model/{Yacht.java => ClientYacht.java} | 9 +++-- src/main/java/seng302/model/ServerYacht.java | 8 ++-- .../java/seng302/utilities/XMLParser.java | 9 +++-- .../java/seng302/visualiser/GameClient.java | 38 +++++++++---------- .../java/seng302/visualiser/GameView.java | 30 +++++++-------- .../FinishScreenViewController.java | 20 +++++----- .../controllers/RaceViewController.java | 37 +++++++++--------- 7 files changed, 76 insertions(+), 75 deletions(-) rename src/main/java/seng302/model/{Yacht.java => ClientYacht.java} (95%) diff --git a/src/main/java/seng302/model/Yacht.java b/src/main/java/seng302/model/ClientYacht.java similarity index 95% rename from src/main/java/seng302/model/Yacht.java rename to src/main/java/seng302/model/ClientYacht.java index 5a038f41..a0ee906e 100644 --- a/src/main/java/seng302/model/Yacht.java +++ b/src/main/java/seng302/model/ClientYacht.java @@ -20,15 +20,16 @@ import seng302.model.mark.CompoundMark; * compared to the XMLParser boat class, also done outside Boat class because some old variables are * not used anymore. */ -public class Yacht extends Observable { +public class ClientYacht extends Observable { @FunctionalInterface public interface YachtLocationListener { - void notifyLocation(Yacht yacht, double lat, double lon, double heading, double velocity); + void notifyLocation(ClientYacht clientYacht, double lat, double lon, double heading, + double velocity); } - private Logger logger = LoggerFactory.getLogger(Yacht.class); + private Logger logger = LoggerFactory.getLogger(ClientYacht.class); //BOTH AFAIK @@ -58,7 +59,7 @@ public class Yacht extends Observable { private Integer positionInt = 0; private Color colour; - public Yacht(String boatType, Integer sourceId, String hullID, String shortName, + public ClientYacht(String boatType, Integer sourceId, String hullID, String shortName, String boatName, String country) { this.boatType = boatType; this.sourceId = sourceId; diff --git a/src/main/java/seng302/model/ServerYacht.java b/src/main/java/seng302/model/ServerYacht.java index b0b9a0a3..87149c77 100644 --- a/src/main/java/seng302/model/ServerYacht.java +++ b/src/main/java/seng302/model/ServerYacht.java @@ -16,7 +16,7 @@ import seng302.utilities.GeoUtility; */ public class ServerYacht extends Observable { - private Logger logger = LoggerFactory.getLogger(Yacht.class); + private Logger logger = LoggerFactory.getLogger(ClientYacht.class); public static final Double TURN_STEP = 5.0; @@ -264,18 +264,16 @@ public class ServerYacht extends Observable { return sourceId; } - // TODO: 15/08/17 EXTREME BUG DO NOT DELETE THIS FUNCTION BREAKS PROGRAM IDK WHY ¯\_(ツ)_/¯ + // TODO: 15/08/17 This method is implicitly called from the XML generator for boats DO NOT DELETE public String getHullID() { - System.out.println("HullId"); if (hullID == null) { return ""; } return hullID; } - // TODO: 15/08/17 EXTREME BUG DO NOT DELETE THIS FUNCTION BREAKS PROGRAM IDK WHY ¯\_(ツ)_/¯ + // TODO: 15/08/17 This method is implicitly called from the XML generator for boats DO NOT DELETE public String getShortName() { - System.out.println("shortName"); return shortName; } diff --git a/src/main/java/seng302/utilities/XMLParser.java b/src/main/java/seng302/utilities/XMLParser.java index a88af9b4..a08a0769 100644 --- a/src/main/java/seng302/utilities/XMLParser.java +++ b/src/main/java/seng302/utilities/XMLParser.java @@ -8,8 +8,8 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import seng302.model.ClientYacht; import seng302.model.Limit; -import seng302.model.Yacht; import seng302.model.mark.CompoundMark; import seng302.model.mark.Corner; import seng302.model.mark.Mark; @@ -125,8 +125,8 @@ public class XMLParser { * @param doc XML Document Object * @return Mapping of sourceIds to Boats. */ - public static Map parseBoats(Document doc){ - Map competingBoats = new HashMap<>(); + public static Map parseBoats(Document doc) { + Map competingBoats = new HashMap<>(); Element docEle = doc.getDocumentElement(); @@ -135,7 +135,8 @@ public class XMLParser { Node currentBoat = boatsList.item(i); if (currentBoat.getNodeName().equals("Boat")) { // Boat boat = new Boat(currentBoat); - Yacht yacht = new Yacht(XMLParser.getNodeAttributeString(currentBoat, "Type"), + ClientYacht yacht = new ClientYacht( + XMLParser.getNodeAttributeString(currentBoat, "Type"), XMLParser.getNodeAttributeInt(currentBoat, "SourceID"), XMLParser.getNodeAttributeString(currentBoat, "HullNum"), XMLParser.getNodeAttributeString(currentBoat, "ShortName"), diff --git a/src/main/java/seng302/visualiser/GameClient.java b/src/main/java/seng302/visualiser/GameClient.java index e65cfbd9..62189263 100644 --- a/src/main/java/seng302/visualiser/GameClient.java +++ b/src/main/java/seng302/visualiser/GameClient.java @@ -14,8 +14,8 @@ import javafx.scene.input.KeyEvent; import javafx.scene.layout.Pane; import seng302.gameServer.MainServerThread; import seng302.gameServer.server.messages.BoatAction; +import seng302.model.ClientYacht; import seng302.model.RaceState; -import seng302.model.Yacht; import seng302.model.stream.packets.StreamPacket; import seng302.model.stream.parser.MarkRoundingData; import seng302.model.stream.parser.PositionUpdateData; @@ -41,7 +41,7 @@ public class GameClient { private RaceViewController raceView; - private Map allBoatsMap; + private Map allBoatsMap; private RegattaXMLData regattaData; private RaceXMLData courseData; private RaceState raceState = new RaceState(); @@ -151,7 +151,7 @@ public class GameClient { holderPane.getScene().setOnKeyPressed(this::keyPressed); holderPane.getScene().setOnKeyReleased(this::keyReleased); raceView = fxmlLoader.getController(); - Yacht player = allBoatsMap.get(socketThread.getClientId()); + ClientYacht player = allBoatsMap.get(socketThread.getClientId()); raceView.loadRace(allBoatsMap, courseData, raceState, player); } @@ -224,8 +224,8 @@ public class GameClient { private void updatePosition(PositionUpdateData positionData) { if (positionData.getType() == DeviceType.YACHT_TYPE) { if (allXMLReceived() && allBoatsMap.containsKey(positionData.getDeviceId())) { - Yacht yacht = allBoatsMap.get(positionData.getDeviceId()); - yacht.updateLocation(positionData.getLat(), + ClientYacht clientYacht = allBoatsMap.get(positionData.getDeviceId()); + clientYacht.updateLocation(positionData.getLat(), positionData.getLon(), positionData.getHeading(), positionData.getGroundSpeed()); } @@ -242,11 +242,11 @@ public class GameClient { */ private void updateMarkRounding(MarkRoundingData roundingData) { if (allXMLReceived()) { - Yacht yacht = allBoatsMap.get(roundingData.getBoatId()); - yacht.setMarkRoundingTime(roundingData.getTimeStamp()); - yacht.updateTimeSinceLastMarkProperty( + ClientYacht clientYacht = allBoatsMap.get(roundingData.getBoatId()); + clientYacht.setMarkRoundingTime(roundingData.getTimeStamp()); + clientYacht.updateTimeSinceLastMarkProperty( raceState.getRaceTime() - roundingData.getTimeStamp()); - yacht.setLastMarkRounded( + clientYacht.setLastMarkRounded( courseData.getCompoundMarks().get( roundingData.getMarkId() ) @@ -258,20 +258,20 @@ public class GameClient { if (allXMLReceived()) { raceState.updateState(data); for (long[] boatData : data.getBoatData()) { - Yacht yacht = allBoatsMap.get((int) boatData[0]); - yacht.setEstimateTimeTillNextMark(raceState.getRaceTime() - boatData[1]); - yacht.setEstimateTimeAtFinish(boatData[2]); + ClientYacht clientYacht = allBoatsMap.get((int) boatData[0]); + clientYacht.setEstimateTimeTillNextMark(raceState.getRaceTime() - boatData[1]); + clientYacht.setEstimateTimeAtFinish(boatData[2]); int legNumber = (int) boatData[3]; - yacht.setLegNumber(legNumber); - yacht.setBoatStatus((int) boatData[4]); - if (legNumber != yacht.getLegNumber()) { + clientYacht.setLegNumber(legNumber); + clientYacht.setBoatStatus((int) boatData[4]); + if (legNumber != clientYacht.getLegNumber()) { int placing = 1; - for (Yacht otherYacht : allBoatsMap.values()) { - if (otherYacht.getSourceId() != boatData[0] && - yacht.getLegNumber() <= otherYacht.getLegNumber()) + for (ClientYacht otherClientYacht : allBoatsMap.values()) { + if (otherClientYacht.getSourceId() != boatData[0] && + clientYacht.getLegNumber() <= otherClientYacht.getLegNumber()) placing++; } - yacht.setPositionInteger(placing); + clientYacht.setPositionInteger(placing); } } } diff --git a/src/main/java/seng302/visualiser/GameView.java b/src/main/java/seng302/visualiser/GameView.java index a7f353d9..24f42822 100644 --- a/src/main/java/seng302/visualiser/GameView.java +++ b/src/main/java/seng302/visualiser/GameView.java @@ -19,10 +19,10 @@ import javafx.scene.paint.Color; import javafx.scene.paint.Paint; import javafx.scene.shape.Polygon; import javafx.scene.text.Text; +import seng302.model.ClientYacht; import seng302.model.Colors; import seng302.model.GeoPoint; import seng302.model.Limit; -import seng302.model.Yacht; import seng302.model.mark.CompoundMark; import seng302.model.mark.Corner; import seng302.model.mark.Mark; @@ -61,8 +61,8 @@ public class GameView extends Pane { private List borderPoints; private Map markerObjects; - private Map boatObjects = new HashMap<>(); - private Map annotations = new HashMap<>(); + private Map boatObjects = new HashMap<>(); + private Map annotations = new HashMap<>(); private ObservableList gameObjects; private Group annotationsGroup = new Group(); private Group wakesGroup = new Group(); @@ -318,23 +318,23 @@ public class GameView extends Pane { /** * Draws all the boats. - * @param yachts The yachts to set in the race + * @param clientYachts The yachts to set in the race */ - public void setBoats(List yachts) { + public void setBoats(List clientYachts) { BoatObject newBoat; final List wakes = new ArrayList<>(); - for (Yacht yacht : yachts) { + for (ClientYacht clientYacht : clientYachts) { Paint colour = Colors.getColor(); newBoat = new BoatObject(); newBoat.setFill(colour); - boatObjects.put(yacht, newBoat); - createAndBindAnnotationBox(yacht, colour); + boatObjects.put(clientYacht, newBoat); + createAndBindAnnotationBox(clientYacht, colour); // wakesGroup.getChildren().add(newBoat.getWake()); wakes.add(newBoat.getWake()); boatObjectGroup.getChildren().add(newBoat); trails.getChildren().add(newBoat.getTrail()); // TODO: 1/08/17 Make this less vile to look at. - yacht.addLocationListener((boat, lat, lon, heading, velocity) ->{ + clientYacht.addLocationListener((boat, lat, lon, heading, velocity) -> { BoatObject bo = boatObjects.get(boat); Point2D p2d = findScaledXY(lat, lon); bo.moveTo(p2d.getX(), p2d.getY(), heading, velocity); @@ -358,11 +358,11 @@ public class GameView extends Pane { }); } - private void createAndBindAnnotationBox (Yacht yacht, Paint colour) { + private void createAndBindAnnotationBox(ClientYacht clientYacht, Paint colour) { AnnotationBox newAnnotation = new AnnotationBox(); newAnnotation.setFill(colour); newAnnotation.addAnnotation( - "name", "Player: " + yacht.getShortName() + "name", "Player: " + clientYacht.getShortName() ); // newAnnotation.addAnnotation( // "velocity", @@ -385,7 +385,7 @@ public class GameView extends Pane { // return format.format(time); // } // ); - annotations.put(yacht, newAnnotation); + annotations.put(clientYacht, newAnnotation); } private void drawFps(Double fps){ @@ -562,9 +562,9 @@ public class GameView extends Pane { fpsDisplay.setVisible(visibility); } - public void selectBoat (Yacht selectedYacht) { + public void selectBoat(ClientYacht selectedClientYacht) { boatObjects.forEach((boat, group) -> - group.setIsSelected(boat == selectedYacht) + group.setIsSelected(boat == selectedClientYacht) ); } @@ -576,7 +576,7 @@ public class GameView extends Pane { timer.start(); } - public void setBoatAsPlayer (Yacht playerYacht) { + public void setBoatAsPlayer(ClientYacht playerYacht) { boatObjects.get(playerYacht).setAsPlayer(); annotations.get(playerYacht).addAnnotation( "velocity", diff --git a/src/main/java/seng302/visualiser/controllers/FinishScreenViewController.java b/src/main/java/seng302/visualiser/controllers/FinishScreenViewController.java index db2a5d17..a4dc831b 100644 --- a/src/main/java/seng302/visualiser/controllers/FinishScreenViewController.java +++ b/src/main/java/seng302/visualiser/controllers/FinishScreenViewController.java @@ -17,24 +17,24 @@ import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; -import seng302.model.Yacht; +import seng302.model.ClientYacht; public class FinishScreenViewController implements Initializable { @FXML private GridPane finishScreenGridPane; @FXML - private TableView finishOrderTable; + private TableView finishOrderTable; @FXML - private TableColumn posCol; + private TableColumn posCol; @FXML - private TableColumn boatNameCol; + private TableColumn boatNameCol; @FXML - private TableColumn shortNameCol; + private TableColumn shortNameCol; @FXML - private TableColumn countryCol; + private TableColumn countryCol; - ObservableList data = FXCollections.observableArrayList(); + ObservableList data = FXCollections.observableArrayList(); @Override public void initialize(URL location, ResourceBundle resources) { @@ -61,9 +61,9 @@ public class FinishScreenViewController implements Initializable { finishOrderTable.refresh(); } - public void setFinishers (List participants) { - List sorted = new ArrayList<>(participants); - sorted.sort(Comparator.comparingInt(Yacht::getPositionInteger)); + public void setFinishers(List participants) { + List sorted = new ArrayList<>(participants); + sorted.sort(Comparator.comparingInt(ClientYacht::getPositionInteger)); finishOrderTable.getItems().setAll(sorted); } diff --git a/src/main/java/seng302/visualiser/controllers/RaceViewController.java b/src/main/java/seng302/visualiser/controllers/RaceViewController.java index 4af617fc..1721b027 100644 --- a/src/main/java/seng302/visualiser/controllers/RaceViewController.java +++ b/src/main/java/seng302/visualiser/controllers/RaceViewController.java @@ -34,8 +34,8 @@ import javafx.scene.text.Text; import javafx.stage.Stage; import javafx.stage.StageStyle; import javafx.util.StringConverter; +import seng302.model.ClientYacht; import seng302.model.RaceState; -import seng302.model.Yacht; import seng302.model.mark.CompoundMark; import seng302.model.mark.Mark; import seng302.model.stream.xml.parser.RaceXMLData; @@ -70,10 +70,10 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel @FXML private Button selectAnnotationBtn; @FXML - private ComboBox yachtSelectionComboBox; + private ComboBox yachtSelectionComboBox; //Race Data - private Map participants; + private Map participants; private Map markers; private RaceXMLData courseData; private GameView gameView; @@ -101,7 +101,8 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel } public void loadRace ( - Map participants, RaceXMLData raceData, RaceState raceState, Yacht player + Map participants, RaceXMLData raceData, RaceState raceState, + ClientYacht player ) { this.participants = participants; this.courseData = raceData; @@ -214,7 +215,7 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel // TODO: 2/08/17 there is about 0 chance of this working. Once we are keeping track of boat positions it can be fixed. // Collect the racing yachts that aren't already in the chart sparkLineData.clear(); - List sparkLineCandidates = new ArrayList<>(participants.values()); + List sparkLineCandidates = new ArrayList<>(participants.values()); // Create a new data series for new yachts sparkLineCandidates .stream() @@ -260,15 +261,15 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel /** * Updates the yachts sparkline of the desired yacht and using the new leg number - * @param yacht The yacht to be updated on the sparkline + * @param clientYacht The yacht to be updated on the sparkline * @param legNumber the leg number that the position will be assigned to */ - void updateYachtPositionSparkline(Yacht yacht, Integer legNumber){ + void updateYachtPositionSparkline(ClientYacht clientYacht, Integer legNumber) { for (XYChart.Series positionData : sparkLineData) { positionData.getData().add( new Data<>( Integer.toString(legNumber), - 1.0 + participants.size() - yacht.getPositionInteger() + 1.0 + participants.size() - clientYacht.getPositionInteger() ) ); } @@ -376,21 +377,21 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel // positionVbox.getStylesheets().add(getClass().getResource("/css/master.css").toString()); // list of racing yacht id - List sorted = new ArrayList<>(participants.values()); - sorted.sort(Comparator.comparingInt(Yacht::getPositionInteger)); + List sorted = new ArrayList<>(participants.values()); + sorted.sort(Comparator.comparingInt(ClientYacht::getPositionInteger)); List vboxEntries = new ArrayList<>(); - for (Yacht yacht : sorted) { + for (ClientYacht clientYacht : sorted) { // System.out.println("yacht == null " + String.valueOf(yacht == null)); - if (yacht.getBoatStatus() == 3) { // 3 is finish status - Text textToAdd = new Text(yacht.getPositionInteger() + ". " + - yacht.getShortName() + " (Finished)"); + if (clientYacht.getBoatStatus() == 3) { // 3 is finish status + Text textToAdd = new Text(clientYacht.getPositionInteger() + ". " + + clientYacht.getShortName() + " (Finished)"); textToAdd.setFill(Paint.valueOf("#d3d3d3")); vboxEntries.add(textToAdd); } else { - Text textToAdd = new Text(yacht.getPositionInteger() + ". " + - yacht.getShortName() + " "); + Text textToAdd = new Text(clientYacht.getPositionInteger() + ". " + + clientYacht.getShortName() + " "); textToAdd.setFill(Paint.valueOf("#d3d3d3")); textToAdd.setStyle(""); vboxEntries.add(textToAdd); @@ -575,9 +576,9 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel /** * Sets all the annotations of the selected yacht to be visible and all others to be hidden * - * @param yacht The yacht for which we want to view all annotations + * @param clientYacht The yacht for which we want to view all annotations */ - private void setSelectedBoat(Yacht yacht) { + private void setSelectedBoat(ClientYacht clientYacht) { // for (BoatObject bg : gameViewController.getBoatGroups()) { // //We need to iterate over all race groups to get the matching yacht group belonging to this yacht if we // //are to toggle its annotations, there is no other backwards knowledge of a yacht to its yachtgroup.