Merge branch 'master' into MapWindRotation
# Conflicts: # racevisionGame/src/main/java/mock/app/Event.java # racevisionGame/src/main/java/mock/model/MockRace.java # racevisionGame/src/main/java/mock/model/commandFactory/CommandFactory.java # racevisionGame/src/main/java/shared/dataInput/RaceXMLReader.java # racevisionGame/src/main/java/shared/model/CompoundMark.java # racevisionGame/src/main/java/shared/model/Mark.java # racevisionGame/src/main/java/visualiser/Controllers/ConnectionController.java # racevisionGame/src/main/java/visualiser/Controllers/HostController.java # racevisionGame/src/main/java/visualiser/app/VisualiserInput.java # racevisionGame/src/main/java/visualiser/gameController/ControllerServer.java # racevisionGame/src/main/java/visualiser/model/VisualiserRace.java
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package shared.dataInput;
|
||||
|
||||
import shared.model.Boat;
|
||||
import shared.model.Mark;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* An empty {@link BoatDataSource}. Can be used to initialise a race with no data.
|
||||
*/
|
||||
public class EmptyBoatDataSource implements BoatDataSource {
|
||||
|
||||
/**
|
||||
* A map of source ID to boat for all boats in the race.
|
||||
*/
|
||||
private final Map<Integer, Boat> boatMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* A map of source ID to mark for all marks in the race.
|
||||
*/
|
||||
private final Map<Integer, Mark> markerMap = new HashMap<>();
|
||||
|
||||
|
||||
|
||||
public EmptyBoatDataSource() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the boats that are going to participate in this race
|
||||
* @return Dictionary of boats that are to participate in this race indexed by SourceID
|
||||
*/
|
||||
@Override
|
||||
public Map<Integer, Boat> getBoats() {
|
||||
return boatMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the marker Boats that are participating in this race
|
||||
* @return Dictionary of the Markers Boats that are in this race indexed by their Source ID.
|
||||
*/
|
||||
@Override
|
||||
public Map<Integer, Mark> getMarkerBoats() {
|
||||
return markerMap;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package shared.dataInput;
|
||||
|
||||
import network.Messages.Enums.RaceTypeEnum;
|
||||
import shared.model.CompoundMark;
|
||||
import shared.model.GPSCoordinate;
|
||||
import shared.model.Leg;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* An empty {@link RaceDataSource}. Can be used to initialise a race with no data.
|
||||
*/
|
||||
public class EmptyRaceDataSource implements RaceDataSource {
|
||||
|
||||
|
||||
/**
|
||||
* The GPS coordinate of the top left of the race boundary.
|
||||
*/
|
||||
private GPSCoordinate mapTopLeft = new GPSCoordinate(0, 0);
|
||||
|
||||
/**
|
||||
* The GPS coordinate of the bottom right of the race boundary.
|
||||
*/
|
||||
private GPSCoordinate mapBottomRight = new GPSCoordinate(0, 0);
|
||||
|
||||
|
||||
/**
|
||||
* A list of GPS coordinates that make up the boundary of the race.
|
||||
*/
|
||||
private final List<GPSCoordinate> boundary = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* A map between compoundMarkID and a CompoundMark for all CompoundMarks in a race.
|
||||
*/
|
||||
private final Map<Integer, CompoundMark> compoundMarkMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* A list of boat sourceIDs participating in the race.
|
||||
*/
|
||||
private final List<Integer> participants = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* A list of legs in the race.
|
||||
*/
|
||||
private final List<Leg> legs = new ArrayList<>();
|
||||
|
||||
|
||||
/**
|
||||
* The time that the race.xml file was created.
|
||||
*/
|
||||
private ZonedDateTime creationTimeDate = ZonedDateTime.now();
|
||||
|
||||
/**
|
||||
* The time that the race should start at, if it hasn't been postponed.
|
||||
*/
|
||||
private ZonedDateTime raceStartTime = ZonedDateTime.now().plusMinutes(5);
|
||||
|
||||
/**
|
||||
* Whether or not the race has been postponed.
|
||||
*/
|
||||
private boolean postpone = false;
|
||||
|
||||
|
||||
/**
|
||||
* The ID number of the race.
|
||||
*/
|
||||
private int raceID = 0;
|
||||
|
||||
/**
|
||||
* The type of the race.
|
||||
*/
|
||||
private RaceTypeEnum raceType = RaceTypeEnum.NOT_A_RACE_TYPE;
|
||||
|
||||
|
||||
|
||||
public EmptyRaceDataSource() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public List<GPSCoordinate> getBoundary() {
|
||||
return boundary;
|
||||
}
|
||||
|
||||
public GPSCoordinate getMapTopLeft() {
|
||||
return mapTopLeft;
|
||||
}
|
||||
|
||||
public GPSCoordinate getMapBottomRight() {
|
||||
return mapBottomRight;
|
||||
}
|
||||
|
||||
public List<Leg> getLegs() {
|
||||
return legs;
|
||||
}
|
||||
|
||||
public List<CompoundMark> getCompoundMarks() {
|
||||
return new ArrayList<>(compoundMarkMap.values());
|
||||
}
|
||||
|
||||
|
||||
public ZonedDateTime getCreationDateTime() {
|
||||
return creationTimeDate;
|
||||
}
|
||||
|
||||
public ZonedDateTime getStartDateTime() {
|
||||
return raceStartTime;
|
||||
}
|
||||
|
||||
public int getRaceId() {
|
||||
return raceID;
|
||||
}
|
||||
|
||||
public RaceTypeEnum getRaceType() {
|
||||
return raceType;
|
||||
}
|
||||
|
||||
public boolean getPostponed() {
|
||||
return postpone;
|
||||
}
|
||||
|
||||
public List<Integer> getParticipants() {
|
||||
return participants;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package shared.dataInput;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NodeList;
|
||||
import shared.enums.XMLFileType;
|
||||
import shared.exceptions.InvalidRegattaDataException;
|
||||
import shared.exceptions.XMLReaderException;
|
||||
import shared.model.GPSCoordinate;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* An empty {@link RegattaDataSource}. Can be used to initialise a race with no data.
|
||||
*/
|
||||
public class EmptyRegattaDataSource implements RegattaDataSource {
|
||||
/**
|
||||
* The regatta ID.
|
||||
*/
|
||||
private int regattaID = 0;
|
||||
|
||||
/**
|
||||
* The regatta name.
|
||||
*/
|
||||
private String regattaName = "";
|
||||
|
||||
/**
|
||||
* The race ID.
|
||||
*/
|
||||
private int raceID = 0;
|
||||
|
||||
/**
|
||||
* The course name.
|
||||
*/
|
||||
private String courseName = "";
|
||||
|
||||
/**
|
||||
* The central latitude of the course.
|
||||
*/
|
||||
private double centralLatitude = 0;
|
||||
|
||||
/**
|
||||
* The central longitude of the course.
|
||||
*/
|
||||
private double centralLongitude = 0;
|
||||
|
||||
/**
|
||||
* The central altitude of the course.
|
||||
*/
|
||||
private double centralAltitude = 0;
|
||||
|
||||
/**
|
||||
* The UTC offset of the course.
|
||||
*/
|
||||
private float utcOffset = 0;
|
||||
|
||||
/**
|
||||
* The magnetic variation of the course.
|
||||
*/
|
||||
private float magneticVariation = 0;
|
||||
|
||||
|
||||
|
||||
|
||||
public EmptyRegattaDataSource() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public int getRegattaID() {
|
||||
return regattaID;
|
||||
}
|
||||
|
||||
|
||||
public String getRegattaName() {
|
||||
return regattaName;
|
||||
}
|
||||
|
||||
|
||||
public int getRaceID() {
|
||||
return raceID;
|
||||
}
|
||||
|
||||
|
||||
public String getCourseName() {
|
||||
return courseName;
|
||||
}
|
||||
|
||||
|
||||
public double getCentralLatitude() {
|
||||
return centralLatitude;
|
||||
}
|
||||
|
||||
|
||||
public double getCentralLongitude() {
|
||||
return centralLongitude;
|
||||
}
|
||||
|
||||
|
||||
public double getCentralAltitude() {
|
||||
return centralAltitude;
|
||||
}
|
||||
|
||||
|
||||
public float getUtcOffset() {
|
||||
return utcOffset;
|
||||
}
|
||||
|
||||
|
||||
public float getMagneticVariation() {
|
||||
return magneticVariation;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the GPS coorindates of the centre of the regatta.
|
||||
* @return The gps coordinate for the centre of the regatta.
|
||||
*/
|
||||
public GPSCoordinate getGPSCoordinate() {
|
||||
return new GPSCoordinate(centralLatitude, centralLongitude);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import shared.enums.RoundingType;
|
||||
import shared.enums.XMLFileType;
|
||||
import shared.exceptions.InvalidRaceDataException;
|
||||
import shared.exceptions.XMLReaderException;
|
||||
@@ -317,6 +318,8 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
return element.getAttribute("Name");
|
||||
}
|
||||
|
||||
private String getCompoundMarkRounding(Element element){return element.getAttribute("Rounding");}
|
||||
|
||||
|
||||
/**
|
||||
* Populates list of legs given CompoundMarkSequence element and referenced CompoundMark elements.
|
||||
@@ -335,6 +338,9 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
//Gets the ID number of this corner element.
|
||||
int cornerID = getCompoundMarkID(cornerElement);
|
||||
|
||||
//gets the Rounding of this corner element
|
||||
String cornerRounding = getCompoundMarkRounding(cornerElement);
|
||||
|
||||
int cornerSeq = Integer.parseInt(getAttribute(cornerElement, "SeqID"));
|
||||
|
||||
cornersList.add(new Corner(cornerID, cornerSeq, "SP", 3));
|
||||
@@ -345,6 +351,10 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
//The name of the leg is the name of the first compoundMark in the leg.
|
||||
String legName = lastCompoundMark.getName();
|
||||
|
||||
//Sets the rounding type of this compound mark
|
||||
|
||||
lastCompoundMark.setRoundingType(RoundingType.getValueOf(cornerRounding));
|
||||
|
||||
//For each following corner, create a leg between cornerN and cornerN+1.
|
||||
for(int i = 1; i < corners.getLength(); i++) {
|
||||
|
||||
@@ -357,9 +367,15 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
|
||||
cornerSeq = Integer.parseInt(getAttribute(cornerElement, "SeqID"));
|
||||
|
||||
cornersList.add(new Corner(cornerID, cornerSeq, "Port", 3));
|
||||
//gets the Rounding of this corner element
|
||||
cornerRounding = getCompoundMarkRounding(cornerElement);
|
||||
|
||||
//Gets the CompoundMark associated with this corner.
|
||||
CompoundMark currentCompoundMark = this.compoundMarkMap.get(cornerID);
|
||||
|
||||
//Sets the rounding type of this compound mark
|
||||
currentCompoundMark.setRoundingType(RoundingType.getValueOf(cornerRounding));
|
||||
|
||||
//Create a leg from these two adjacent compound marks.
|
||||
Leg leg = new Leg(legName, lastCompoundMark, currentCompoundMark, i - 1);
|
||||
legs.add(leg);
|
||||
|
||||
@@ -166,10 +166,9 @@ public abstract class XMLReader {
|
||||
* @param path path of the XML
|
||||
* @param encoding encoding of the xml
|
||||
* @return A string containing the contents of the specified file.
|
||||
* @throws TransformerException Issue with the XML format
|
||||
* @throws XMLReaderException Thrown if file cannot be read for some reason.
|
||||
*/
|
||||
public static String readXMLFileToString(String path, Charset encoding) throws TransformerException, XMLReaderException {
|
||||
public static String readXMLFileToString(String path, Charset encoding) throws XMLReaderException {
|
||||
|
||||
InputStream fileStream = XMLReader.class.getClassLoader().getResourceAsStream(path);
|
||||
|
||||
@@ -182,7 +181,11 @@ public abstract class XMLReader {
|
||||
|
||||
doc.getDocumentElement().normalize();
|
||||
|
||||
return XMLReader.getContents(doc);
|
||||
try {
|
||||
return XMLReader.getContents(doc);
|
||||
} catch (TransformerException e) {
|
||||
throw new XMLReaderException("Could not get XML file contents.", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package shared.enums;
|
||||
|
||||
/**
|
||||
* Enum for the types of rounding that can be done
|
||||
*/
|
||||
public enum RoundingType {
|
||||
/**
|
||||
* This is means it must be rounded port side
|
||||
*/
|
||||
Port,
|
||||
|
||||
/**
|
||||
* This is means it must be rounded starboard side
|
||||
*/
|
||||
Starboard,
|
||||
|
||||
/**
|
||||
* The boat within the compound mark with the SeqID
|
||||
* of 1 should be rounded to starboard and the boat
|
||||
* within the compound mark with the SeqID of 2 should
|
||||
* be rounded to port.
|
||||
*/
|
||||
SP,
|
||||
|
||||
/**
|
||||
* The boat within the compound mark with the SeqID
|
||||
* of 1 should be rounded to port and the boat
|
||||
* within the compound mark with the SeqID of 2 should
|
||||
* be rounded to starboard.
|
||||
*
|
||||
* opposite of SP
|
||||
*/
|
||||
PS;
|
||||
|
||||
public static RoundingType getValueOf(String value) {
|
||||
switch (value) {
|
||||
case "Port":
|
||||
return RoundingType.Port;
|
||||
case "Starboard":
|
||||
return RoundingType.Starboard;
|
||||
case "SP":
|
||||
return RoundingType.Port;
|
||||
case "PS":
|
||||
return RoundingType.Starboard;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package shared.exceptions;
|
||||
|
||||
/**
|
||||
* An exception thrown when a specific boat cannot be found.
|
||||
*/
|
||||
public class BoatNotFoundException extends Exception {
|
||||
|
||||
public BoatNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BoatNotFoundException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package shared.exceptions;
|
||||
|
||||
/**
|
||||
* An exception thrown when we the client-server handshake fails.
|
||||
*/
|
||||
public class HandshakeException extends Exception {
|
||||
|
||||
/**
|
||||
* Constructs the exception with a given message.
|
||||
* @param message Message to store.
|
||||
*/
|
||||
public HandshakeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the exception with a given message and cause.
|
||||
* @param message Message to store.
|
||||
* @param cause Cause to store.
|
||||
*/
|
||||
public HandshakeException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package shared.exceptions;
|
||||
|
||||
/**
|
||||
* An exception thrown when a specific mark cannot be found.
|
||||
*/
|
||||
public class MarkNotFoundException extends Exception {
|
||||
|
||||
public MarkNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public MarkNotFoundException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -82,6 +82,7 @@ public class Boat {
|
||||
/**
|
||||
* The time at which the boat is estimated to reach the next mark, in milliseconds since unix epoch.
|
||||
*/
|
||||
@Nullable
|
||||
private ZonedDateTime estimatedTimeAtNextMark;
|
||||
|
||||
/**
|
||||
@@ -106,6 +107,8 @@ public class Boat {
|
||||
|
||||
this.bearing = Bearing.fromDegrees(0d);
|
||||
|
||||
setCurrentPosition(new GPSCoordinate(0, 0));
|
||||
|
||||
this.status = BoatStatusEnum.UNDEFINED;
|
||||
}
|
||||
|
||||
@@ -365,6 +368,7 @@ public class Boat {
|
||||
* Returns the time at which the boat should reach the next mark.
|
||||
* @return Time at which the boat should reach next mark.
|
||||
*/
|
||||
@Nullable
|
||||
public ZonedDateTime getEstimatedTimeAtNextMark() {
|
||||
return estimatedTimeAtNextMark;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package shared.model;
|
||||
|
||||
import shared.xml.Race.XMLCompoundMark;
|
||||
|
||||
import shared.enums.RoundingType;
|
||||
|
||||
/**
|
||||
* Represents a compound mark - that is, either one or two individual marks which form a single compound mark.
|
||||
*/
|
||||
@@ -33,6 +35,11 @@ public class CompoundMark extends XMLCompoundMark{
|
||||
*/
|
||||
private GPSCoordinate averageGPSCoordinate;
|
||||
|
||||
/**
|
||||
* The side that the mark must be rounded on
|
||||
*/
|
||||
private RoundingType roundingType;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a compound mark from a single mark.
|
||||
@@ -138,4 +145,120 @@ public class CompoundMark extends XMLCompoundMark{
|
||||
return averageCoordinate;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to find how far apart the marks that make up this gate are
|
||||
* If this compound mark is only one point return base length of 250m
|
||||
* @return the acceptable distance to round a mark
|
||||
*/
|
||||
public double getRoundingDistance(){
|
||||
if (mark2 != null){
|
||||
return GPSCoordinate.calculateDistanceMeters(mark1.getPosition(), mark2.getPosition());
|
||||
}else{
|
||||
return 400;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to get how this mark should be rounded
|
||||
* @return rounding type for mark
|
||||
*/
|
||||
public RoundingType getRoundingType() {
|
||||
return roundingType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to set the type of rounding for this mark
|
||||
* @param roundingType rounding type to set
|
||||
*/
|
||||
public void setRoundingType(RoundingType roundingType) {
|
||||
this.roundingType = roundingType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to find the mark that is to be rounded at a gate when approaching from the south
|
||||
* will also give the single mark if there is only one
|
||||
* @param bearing the bearing a boat will approach form
|
||||
* @return the mark to round
|
||||
*/
|
||||
public Mark getMarkForRounding(Bearing bearing){
|
||||
Mark westMostMark;
|
||||
Mark eastMostMark;
|
||||
Mark northMostMark;
|
||||
Mark southMostMark;
|
||||
|
||||
//check to see if there are two marks
|
||||
if (mark2 == null){
|
||||
return mark1;
|
||||
}
|
||||
|
||||
//finds the mark furthest west and east
|
||||
if(this.getMark1Position().getLatitude() > this.getMark2Position().getLatitude()){
|
||||
westMostMark = this.mark1;
|
||||
eastMostMark = this.mark2;
|
||||
}else{
|
||||
westMostMark = this.mark2;
|
||||
eastMostMark = this.mark1;
|
||||
}
|
||||
|
||||
//finds the mark furthest north and south
|
||||
if(this.getMark1Position().getLongitude() > this.getMark2Position().getLongitude()){
|
||||
northMostMark = this.mark1;
|
||||
southMostMark = this.mark2;
|
||||
}else{
|
||||
northMostMark = this.mark2;
|
||||
southMostMark = this.mark1;
|
||||
}
|
||||
|
||||
if (bearing.degrees() > 315 || bearing.degrees() <= 45){
|
||||
//north
|
||||
switch (this.getRoundingType()){
|
||||
case SP:
|
||||
case Port:
|
||||
return westMostMark;
|
||||
case PS:
|
||||
case Starboard:
|
||||
return eastMostMark;
|
||||
default:return null;
|
||||
}
|
||||
}else if(bearing.degrees() > 45 && bearing.degrees() <= 135){
|
||||
//east
|
||||
switch (this.getRoundingType()){
|
||||
case SP:
|
||||
case Port:
|
||||
return northMostMark;
|
||||
case PS:
|
||||
case Starboard:
|
||||
return southMostMark;
|
||||
default:return null;
|
||||
}
|
||||
}else if(bearing.degrees() > 135 && bearing.degrees() <= 225){
|
||||
//south
|
||||
switch (this.getRoundingType()){
|
||||
case SP:
|
||||
case Port:
|
||||
return eastMostMark;
|
||||
case PS:
|
||||
case Starboard:
|
||||
return westMostMark;
|
||||
default:return null;
|
||||
}
|
||||
}else if(bearing.degrees() > 225 && bearing.degrees() <= 315){
|
||||
//west
|
||||
switch (this.getRoundingType()){
|
||||
case SP:
|
||||
case Port:
|
||||
return southMostMark;
|
||||
case PS:
|
||||
case Starboard:
|
||||
return northMostMark;
|
||||
default:return null;
|
||||
}
|
||||
}else{
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package shared.model;
|
||||
|
||||
|
||||
import javafx.animation.AnimationTimer;
|
||||
import javafx.beans.property.IntegerProperty;
|
||||
import javafx.beans.property.SimpleIntegerProperty;
|
||||
|
||||
|
||||
/**
|
||||
* This class is used to track the framerate of something.
|
||||
* Use {@link #incrementFps(long)} to update it, and {@link #fpsProperty()} to observe it.
|
||||
*/
|
||||
public class FrameRateTracker {
|
||||
|
||||
|
||||
/**
|
||||
* The number of frames per second.
|
||||
* We essentially track the number of frames generated per second, over a one second period. When {@link #lastFpsResetTime} reaches 1 second, {@link #currentFps} is reset.
|
||||
*/
|
||||
private int currentFps = 0;
|
||||
|
||||
/**
|
||||
* The number of frames per second we generated over the last 1 second period.
|
||||
*/
|
||||
private IntegerProperty lastFps = new SimpleIntegerProperty(0);
|
||||
|
||||
/**
|
||||
* The time, in milliseconds, since we last reset our {@link #currentFps} counter.
|
||||
*/
|
||||
private long lastFpsResetTime;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a {@link FrameRateTracker}. Use {@link #incrementFps(long)} to update it, and {@link #fpsProperty()} to observe it.
|
||||
*/
|
||||
public FrameRateTracker() {
|
||||
timer.start();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of frames generated per second.
|
||||
* @return Frames per second.
|
||||
*/
|
||||
public int getFps() {
|
||||
return lastFps.getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fps property.
|
||||
* @return The fps property.
|
||||
*/
|
||||
public IntegerProperty fpsProperty() {
|
||||
return lastFps;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Increments the FPS counter, and adds timePeriod milliseconds to our FPS reset timer.
|
||||
* @param timePeriod Time, in milliseconds, to add to {@link #lastFpsResetTime}.
|
||||
*/
|
||||
private void incrementFps(long timePeriod) {
|
||||
//Increment.
|
||||
this.currentFps++;
|
||||
|
||||
//Add period to timer.
|
||||
this.lastFpsResetTime += timePeriod;
|
||||
|
||||
//If we have reached 1 second period, snapshot the framerate and reset.
|
||||
if (this.lastFpsResetTime > 1000) {
|
||||
this.lastFps.set(this.currentFps);
|
||||
|
||||
this.currentFps = 0;
|
||||
this.lastFpsResetTime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Timer used to update the framerate.
|
||||
* This is used because we care about frames in the javaFX thread.
|
||||
*/
|
||||
private AnimationTimer timer = new AnimationTimer() {
|
||||
|
||||
long previousFrameTime = System.currentTimeMillis();
|
||||
|
||||
@Override
|
||||
public void handle(long now) {
|
||||
|
||||
long currentFrameTime = System.currentTimeMillis();
|
||||
long framePeriod = currentFrameTime - previousFrameTime;
|
||||
|
||||
//Increment fps.
|
||||
incrementFps(framePeriod);
|
||||
|
||||
previousFrameTime = currentFrameTime;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Stops the {@link FrameRateTracker}'s timer.
|
||||
*/
|
||||
public void stop() {
|
||||
timer.stop();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -142,7 +142,7 @@ public class GPSCoordinate {
|
||||
* @param coordinate The coordinate to test.
|
||||
* @return true if a line from the point intersects the two boundary points
|
||||
*/
|
||||
private static boolean intersects(GPSCoordinate boundaryA, GPSCoordinate boundaryB, GPSCoordinate coordinate) {
|
||||
public static boolean intersects(GPSCoordinate boundaryA, GPSCoordinate boundaryB, GPSCoordinate coordinate) {
|
||||
double boundaryALat = boundaryA.getLatitude();
|
||||
double boundaryALon = boundaryA.getLongitude();
|
||||
double boundaryBLat = boundaryB.getLatitude();
|
||||
@@ -170,6 +170,46 @@ public class GPSCoordinate {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if a point passes or lands on a line
|
||||
* @param linePointA first point for a line
|
||||
* @param linePointB second point for a line
|
||||
* @param point point to check
|
||||
* @param directionBearing direction of the correct side of line
|
||||
* @return true if on the correct side
|
||||
*/
|
||||
public static boolean passesLine(GPSCoordinate linePointA, GPSCoordinate linePointB, GPSCoordinate point, Bearing directionBearing) {
|
||||
double d = lineCheck(linePointA, linePointB, point);//this gives a number < 0 for one side and > 0 for an other
|
||||
|
||||
//to find if the side is the correct one
|
||||
//compare with point that is known on the correct side
|
||||
GPSCoordinate pointForComparison = GPSCoordinate.calculateNewPosition(linePointA,
|
||||
250, Azimuth.fromDegrees(directionBearing.degrees()));
|
||||
double d2 = lineCheck(linePointA, linePointB, pointForComparison);
|
||||
|
||||
return (d > 0 && d2 > 0) || (d < 0 && d2 < 0) || d == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a double that is positive or negative based on which
|
||||
* side of the line it is on. returns 0 if it is on the line
|
||||
* @param linePointA first point to make up the line
|
||||
* @param linePointB second point to make up the line
|
||||
* @param point the point to check
|
||||
* @return greater than 0 for one side, less than 0 for another
|
||||
*/
|
||||
private static double lineCheck(GPSCoordinate linePointA, GPSCoordinate linePointB, GPSCoordinate point) {
|
||||
double linePointALat = linePointA.getLatitude();
|
||||
double linePointALon = linePointA.getLongitude();
|
||||
double linePointBLat = linePointB.getLatitude();
|
||||
double linePointBLon = linePointB.getLongitude();
|
||||
double pointLat = point.getLatitude();
|
||||
double pointLon = point.getLongitude();
|
||||
|
||||
double d1 = (pointLat - linePointALat) * (linePointBLon - linePointALon);
|
||||
double d2 = (pointLon - linePointALon) * (linePointBLat - linePointALat);
|
||||
return d1 - d2; //this gives a number < 0 for one side and > 0 for an other
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,6 +16,7 @@ public class Mark extends XMLMark{
|
||||
*/
|
||||
private GPSCoordinate position;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs a mark with a given source ID, name, and position.
|
||||
* @param sourceID The source ID of the mark.
|
||||
@@ -32,6 +33,15 @@ public class Mark extends XMLMark{
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to create marks that are not visible in the race
|
||||
* @param position position of the mark
|
||||
* @return the new mark
|
||||
*/
|
||||
public static Mark tempMark(GPSCoordinate position){
|
||||
return new Mark(-1, "Hidden Mark", position);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name of the mark.
|
||||
|
||||
@@ -6,17 +6,17 @@ import javafx.beans.property.SimpleIntegerProperty;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
import network.Messages.Enums.RaceStatusEnum;
|
||||
import network.Messages.Enums.RaceTypeEnum;
|
||||
import network.Messages.LatestMessages;
|
||||
import shared.dataInput.BoatDataSource;
|
||||
import shared.dataInput.RaceDataSource;
|
||||
import shared.dataInput.RegattaDataSource;
|
||||
import visualiser.model.VisualiserRaceEvent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Represents a yacht race.
|
||||
* This is a base class inherited by {@link mock.model.MockRace} and {@link visualiser.model.VisualiserRace}.
|
||||
* This is a base class inherited by {@link mock.model.MockRace} and {@link VisualiserRaceEvent}.
|
||||
* Has a course, state, wind, boundaries, etc.... Boats are added by inheriting classes (see {@link Boat}, {@link mock.model.MockBoat}, {@link visualiser.model.VisualiserBoat}.
|
||||
*/
|
||||
public abstract class Race {
|
||||
@@ -37,11 +37,6 @@ public abstract class Race {
|
||||
*/
|
||||
protected RegattaDataSource regattaDataSource;
|
||||
|
||||
/**
|
||||
* The collection of latest race messages.
|
||||
* Can be either read from or written to.
|
||||
*/
|
||||
protected LatestMessages latestMessages;
|
||||
|
||||
/**
|
||||
* A list of compound marks in the race.
|
||||
@@ -116,16 +111,14 @@ public abstract class Race {
|
||||
* @param boatDataSource Data source for boat related data (yachts and marker boats).
|
||||
* @param raceDataSource Data source for race related data (participating boats, legs, etc...).
|
||||
* @param regattaDataSource Data source for race related data (course name, location, timezone, etc...).
|
||||
* @param latestMessages The collection of latest messages, which can be written to, or read from.
|
||||
*/
|
||||
public Race(BoatDataSource boatDataSource, RaceDataSource raceDataSource, RegattaDataSource regattaDataSource, LatestMessages latestMessages) {
|
||||
public Race(BoatDataSource boatDataSource, RaceDataSource raceDataSource, RegattaDataSource regattaDataSource) {
|
||||
|
||||
//Keep a reference to data sources.
|
||||
this.raceDataSource = raceDataSource;
|
||||
this.boatDataSource = boatDataSource;
|
||||
this.regattaDataSource = regattaDataSource;
|
||||
|
||||
this.latestMessages = latestMessages;
|
||||
|
||||
|
||||
//Marks.
|
||||
@@ -241,7 +234,7 @@ public abstract class Race {
|
||||
* @param windBearing New wind bearing.
|
||||
* @param windSpeedKnots New wind speed, in knots.
|
||||
*/
|
||||
protected void setWind(Bearing windBearing, double windSpeedKnots) {
|
||||
public void setWind(Bearing windBearing, double windSpeedKnots) {
|
||||
Wind wind = new Wind(windBearing, windSpeedKnots);
|
||||
setWind(wind);
|
||||
}
|
||||
@@ -250,7 +243,7 @@ public abstract class Race {
|
||||
* Updates the race to have a specified wind (bearing and speed).
|
||||
* @param wind New wind.
|
||||
*/
|
||||
protected void setWind(Wind wind) {
|
||||
public void setWind(Wind wind) {
|
||||
this.raceWind.setValue(wind);
|
||||
}
|
||||
|
||||
@@ -316,6 +309,23 @@ public abstract class Race {
|
||||
return boundary;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the marks of the race.
|
||||
* @return Marks of the race.
|
||||
*/
|
||||
public List<CompoundMark> getCompoundMarks() {
|
||||
return compoundMarks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the legs of the race.
|
||||
* @return Legs of the race.
|
||||
*/
|
||||
public List<Leg> getLegs() {
|
||||
return legs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of frames generated per second.
|
||||
* @return Frames per second.
|
||||
@@ -333,6 +343,7 @@ public abstract class Race {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Increments the FPS counter, and adds timePeriod milliseconds to our FPS reset timer.
|
||||
* @param timePeriod Time, in milliseconds, to add to {@link #lastFpsResetTime}.
|
||||
|
||||
@@ -0,0 +1,359 @@
|
||||
package shared.model;
|
||||
|
||||
import javafx.beans.property.Property;
|
||||
import javafx.beans.property.SimpleObjectProperty;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import network.Messages.Enums.RaceStatusEnum;
|
||||
import network.Messages.Enums.RaceTypeEnum;
|
||||
import shared.dataInput.BoatDataSource;
|
||||
import shared.dataInput.RaceDataSource;
|
||||
import shared.dataInput.RegattaDataSource;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Represents a yacht race.
|
||||
* This is a base class inherited by {@link mock.model.MockRace} and {@link visualiser.model.VisualiserRaceState}.
|
||||
* Has a course, state, wind, boundaries, etc.... Boats are added by inheriting classes (see {@link Boat}, {@link mock.model.MockBoat}, {@link visualiser.model.VisualiserBoat}.
|
||||
*/
|
||||
public abstract class RaceState {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Data source for race information.
|
||||
*/
|
||||
private RaceDataSource raceDataSource;
|
||||
|
||||
/**
|
||||
* Data source for boat information.
|
||||
*/
|
||||
private BoatDataSource boatDataSource;
|
||||
|
||||
/**
|
||||
* Data source for regatta information.
|
||||
*/
|
||||
private RegattaDataSource regattaDataSource;
|
||||
|
||||
/**
|
||||
* Legs in the race.
|
||||
* We have this in a separate list so that it can be observed.
|
||||
*/
|
||||
private ObservableList<Leg> legs;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The clock which tracks the race's start time, current time, and elapsed duration.
|
||||
*/
|
||||
private RaceClock raceClock;
|
||||
|
||||
|
||||
/**
|
||||
* The current status of the race.
|
||||
*/
|
||||
private RaceStatusEnum raceStatusEnum;
|
||||
|
||||
|
||||
/**
|
||||
* The race's wind.
|
||||
*/
|
||||
private Property<Wind> raceWind = new SimpleObjectProperty<>();
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Constructs an empty race object.
|
||||
* This is initialised into a "default" state, with no data.
|
||||
*/
|
||||
public RaceState() {
|
||||
|
||||
//Legs.
|
||||
this.legs = FXCollections.observableArrayList();
|
||||
|
||||
//Race clock.
|
||||
this.raceClock = new RaceClock(ZonedDateTime.now());
|
||||
|
||||
//Race status.
|
||||
this.setRaceStatusEnum(RaceStatusEnum.NOT_ACTIVE);
|
||||
|
||||
//Wind.
|
||||
this.setWind(Bearing.fromDegrees(0), 0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Initialise the boats in the race.
|
||||
* This sets their starting positions and current legs.
|
||||
*/
|
||||
protected abstract void initialiseBoats();
|
||||
|
||||
|
||||
/**
|
||||
* Updates the race to use a new list of legs, and adds a dummy "Finish" leg at the end.
|
||||
* @param legs The new list of legs to use.
|
||||
*/
|
||||
protected void useLegsList(List<Leg> legs) {
|
||||
this.legs.setAll(legs);
|
||||
//We add a "dummy" leg at the end of the race.
|
||||
if (getLegs().size() > 0) {
|
||||
getLegs().add(new Leg("Finish", getLegs().size()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines whether or not a specific leg is the last leg in the race.
|
||||
* @param leg The leg to check.
|
||||
* @return Returns true if it is the last, false otherwise.
|
||||
*/
|
||||
protected boolean isLastLeg(Leg leg) {
|
||||
|
||||
//Get the last leg.
|
||||
Leg lastLeg = getLegs().get(getLegs().size() - 1);
|
||||
|
||||
//Check its ID.
|
||||
int lastLegID = lastLeg.getLegNumber();
|
||||
|
||||
//Get the specified leg's ID.
|
||||
int legID = leg.getLegNumber();
|
||||
|
||||
|
||||
//Check if they are the same.
|
||||
return legID == lastLegID;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the race data source for the race.
|
||||
* @param raceDataSource New race data source.
|
||||
*/
|
||||
public void setRaceDataSource(RaceDataSource raceDataSource) {
|
||||
this.raceDataSource = raceDataSource;
|
||||
this.getRaceClock().setStartingTime(raceDataSource.getStartDateTime());
|
||||
useLegsList(raceDataSource.getLegs());
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the boat data source for the race.
|
||||
* @param boatDataSource New boat data source.
|
||||
*/
|
||||
public void setBoatDataSource(BoatDataSource boatDataSource) {
|
||||
this.boatDataSource = boatDataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the regatta data source for the race.
|
||||
* @param regattaDataSource New regatta data source.
|
||||
*/
|
||||
public void setRegattaDataSource(RegattaDataSource regattaDataSource) {
|
||||
this.regattaDataSource = regattaDataSource;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the race data source for the race.
|
||||
* @return Race data source.
|
||||
*/
|
||||
public RaceDataSource getRaceDataSource() {
|
||||
return raceDataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the race data source for the race.
|
||||
* @return Race data source.
|
||||
*/
|
||||
public BoatDataSource getBoatDataSource() {
|
||||
return boatDataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the race data source for the race.
|
||||
* @return Race data source.
|
||||
*/
|
||||
public RegattaDataSource getRegattaDataSource() {
|
||||
return regattaDataSource;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a list of {@link Mark} boats.
|
||||
* @return List of mark boats.
|
||||
*/
|
||||
public List<Mark> getMarks() {
|
||||
return new ArrayList<>(boatDataSource.getMarkerBoats().values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of sourceIDs participating in the race.
|
||||
* @return List of sourceIDs participating in the race.
|
||||
*/
|
||||
public List<Integer> getParticipants() {
|
||||
return raceDataSource.getParticipants();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns the current race status.
|
||||
* @return The current race status.
|
||||
*/
|
||||
public RaceStatusEnum getRaceStatusEnum() {
|
||||
return raceStatusEnum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current race status.
|
||||
* @param raceStatusEnum The new status of the race.
|
||||
*/
|
||||
public void setRaceStatusEnum(RaceStatusEnum raceStatusEnum) {
|
||||
this.raceStatusEnum = raceStatusEnum;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the type of race this is.
|
||||
* @return The type of race this is.
|
||||
*/
|
||||
public RaceTypeEnum getRaceType() {
|
||||
return raceDataSource.getRaceType();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name of the regatta.
|
||||
* @return The name of the regatta.
|
||||
*/
|
||||
public String getRegattaName() {
|
||||
return regattaDataSource.getRegattaName();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Updates the race to have a specified wind bearing and speed.
|
||||
* @param windBearing New wind bearing.
|
||||
* @param windSpeedKnots New wind speed, in knots.
|
||||
*/
|
||||
public void setWind(Bearing windBearing, double windSpeedKnots) {
|
||||
Wind wind = new Wind(windBearing, windSpeedKnots);
|
||||
setWind(wind);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the race to have a specified wind (bearing and speed).
|
||||
* @param wind New wind.
|
||||
*/
|
||||
public void setWind(Wind wind) {
|
||||
this.raceWind.setValue(wind);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the wind bearing.
|
||||
* @return The wind bearing.
|
||||
*/
|
||||
public Bearing getWindDirection() {
|
||||
return raceWind.getValue().getWindDirection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the wind speed.
|
||||
* Measured in knots.
|
||||
* @return The wind speed.
|
||||
*/
|
||||
public double getWindSpeed() {
|
||||
return raceWind.getValue().getWindSpeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the race's wind.
|
||||
* @return The race's wind.
|
||||
*/
|
||||
public Property<Wind> windProperty() {
|
||||
return raceWind;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the RaceClock for this race.
|
||||
* This is used to track the start time, current time, and elapsed duration of the race.
|
||||
* @return The RaceClock for the race.
|
||||
*/
|
||||
public RaceClock getRaceClock() {
|
||||
return raceClock;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns the number of legs in the race.
|
||||
* @return The number of legs in the race.
|
||||
*/
|
||||
public int getLegCount() {
|
||||
//We minus one, as we have added an extra "dummy" leg.
|
||||
return getLegs().size() - 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the race boundary.
|
||||
* @return The race boundary.
|
||||
*/
|
||||
public List<GPSCoordinate> getBoundary() {
|
||||
return raceDataSource.getBoundary();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the marks of the race.
|
||||
* @return Marks of the race.
|
||||
*/
|
||||
public List<CompoundMark> getCompoundMarks() {
|
||||
return raceDataSource.getCompoundMarks();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the legs of the race.
|
||||
* @return Legs of the race.
|
||||
*/
|
||||
public ObservableList<Leg> getLegs() {
|
||||
return legs;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the ID of the race.
|
||||
* @return ID of the race.
|
||||
*/
|
||||
public int getRaceId() {
|
||||
return raceDataSource.getRaceId();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the ID of the regatta.
|
||||
* @return The ID of the regatta.
|
||||
*/
|
||||
public int getRegattaID() {
|
||||
return regattaDataSource.getRegattaID();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the name of the course.
|
||||
* @return Name of the course.
|
||||
*/
|
||||
public String getCourseName() {
|
||||
return regattaDataSource.getCourseName();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package shared.model;
|
||||
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* This interface is a {@link Runnable} interface, with the ability to sleep until a given time period has elapsed.
|
||||
*/
|
||||
public interface RunnableWithFramePeriod extends Runnable {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Waits for enough time for the period of this frame to be greater than minimumFramePeriod.
|
||||
* @param previousFrameTime The timestamp of the previous frame.
|
||||
* @param currentFrameTime The timestamp of the current frame.
|
||||
* @param minimumFramePeriod The minimum period the frame must be.
|
||||
*/
|
||||
default void waitForFramePeriod(long previousFrameTime, long currentFrameTime, long minimumFramePeriod) {
|
||||
|
||||
|
||||
//This is the time elapsed, in milliseconds, since the last server "frame".
|
||||
long framePeriod = currentFrameTime - previousFrameTime;
|
||||
|
||||
//We only attempt to send packets every X milliseconds.
|
||||
if (framePeriod >= minimumFramePeriod) {
|
||||
return;
|
||||
|
||||
} else {
|
||||
//Wait until the frame period will be large enough.
|
||||
long timeToWait = minimumFramePeriod - framePeriod;
|
||||
|
||||
try {
|
||||
Thread.sleep(timeToWait);
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
//If we get interrupted, exit the function.
|
||||
Logger.getGlobal().log(Level.SEVERE, "RunnableWithFramePeriod.waitForFramePeriod().sleep(framePeriod) was interrupted on thread: " + Thread.currentThread(), e);
|
||||
//Re-set the interrupt flag.
|
||||
Thread.currentThread().interrupt();
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user