Created FrameRateTracker which can be used to track framerate. Updated RequestToJoinEnum and JoinAcceptanceEnum to match the agreed connection API. Added AssignPlayerBoat message, which is used internally on the client to assign the player a source ID once they have connected. Fixed some race conditions in the MessageRouter. Updated ConnectionAcceptor.CheckClientConnection to wait slightly longer before removing connection (there was a slight race condition before). Race no longer has a reference to LatestMessages. LatestMessages no longer has specific messages types in it. Created RaceState class, which contains the state that is shared between VisualiserRaceState and MockRaceState (currently only used on visualiser). Split VisualiserRace into VisualiserRaceState and VisualiserRaceService. Added the VisualiserRace commands (BoatLocatonCommand, RaceStatusCommand, etc...). Slightly increased the preferred width of race.fxml table columns. issues #27 #37 #35 #story[1095]main
parent
89b0aa8b77
commit
7366aba5ec
@ -0,0 +1,39 @@
|
|||||||
|
package network.Messages;
|
||||||
|
|
||||||
|
import network.Messages.Enums.JoinAcceptanceEnum;
|
||||||
|
import network.Messages.Enums.MessageType;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the message the client generates and sends to itself once the server has assigned a boat source ID with {@link JoinAcceptance}.
|
||||||
|
*/
|
||||||
|
public class AssignPlayerBoat extends AC35Data {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The source ID of the boat assigned to the client.
|
||||||
|
* 0 indicates they haven't been assigned a boat.
|
||||||
|
*/
|
||||||
|
private int sourceID = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a AssignPlayerBoat message.
|
||||||
|
* @param sourceID The sourceID to assign to the client. 0 indicates no sourceID.
|
||||||
|
*/
|
||||||
|
public AssignPlayerBoat(int sourceID){
|
||||||
|
super(MessageType.ASSIGN_PLAYER_BOAT);
|
||||||
|
this.sourceID = sourceID;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the source ID of the boat assigned to the client.
|
||||||
|
* @return The source ID of the boat assigned to the client.
|
||||||
|
*/
|
||||||
|
public int getSourceID() {
|
||||||
|
return sourceID;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,346 @@
|
|||||||
|
package shared.model;
|
||||||
|
|
||||||
|
import javafx.beans.property.Property;
|
||||||
|
import javafx.beans.property.SimpleObjectProperty;
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
||||||
|
|
||||||
|
//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) {
|
||||||
|
//We add a "dummy" leg at the end of the race.
|
||||||
|
if (legs.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());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 List<Leg> getLegs() {
|
||||||
|
return raceDataSource.getLegs();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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,57 @@
|
|||||||
|
package visualiser.Commands.ConnectionToServerCommands;
|
||||||
|
|
||||||
|
import mock.model.commandFactory.Command;
|
||||||
|
import network.Messages.AssignPlayerBoat;
|
||||||
|
import network.Messages.JoinAcceptance;
|
||||||
|
import visualiser.enums.ConnectionToServerState;
|
||||||
|
import visualiser.network.ConnectionToServer;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command created when a {@link network.Messages.Enums.JoinAcceptanceEnum#JOIN_SUCCESSFUL_PARTICIPANT} {@link network.Messages.JoinAcceptance} message is received.
|
||||||
|
*/
|
||||||
|
public class JoinSuccessParticipantCommand implements Command {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The message to operate on.
|
||||||
|
*/
|
||||||
|
private JoinAcceptance joinAcceptance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The context to operate on.
|
||||||
|
*/
|
||||||
|
private ConnectionToServer connectionToServer;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link JoinSuccessParticipantCommand}, which operates on a given {@link ConnectionToServer}.
|
||||||
|
* @param joinAcceptance The message to operate on.
|
||||||
|
* @param connectionToServer The context to operate on.
|
||||||
|
*/
|
||||||
|
public JoinSuccessParticipantCommand(JoinAcceptance joinAcceptance, ConnectionToServer connectionToServer) {
|
||||||
|
this.joinAcceptance = joinAcceptance;
|
||||||
|
this.connectionToServer = connectionToServer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
|
||||||
|
connectionToServer.setJoinAcceptance(joinAcceptance);
|
||||||
|
|
||||||
|
connectionToServer.setConnectionState(ConnectionToServerState.CONNECTED);
|
||||||
|
|
||||||
|
|
||||||
|
AssignPlayerBoat assignPlayerBoat = new AssignPlayerBoat(joinAcceptance.getSourceID());
|
||||||
|
try {
|
||||||
|
connectionToServer.send(assignPlayerBoat);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Logger.getGlobal().log(Level.WARNING, "JoinSuccessParticipantCommand: " + this + " was interrupted on thread: " + Thread.currentThread() + " while sending AssignPlayerBoat message.", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,53 @@
|
|||||||
|
package visualiser.Commands.VisualiserRaceCommands;
|
||||||
|
|
||||||
|
import mock.model.commandFactory.Command;
|
||||||
|
import network.Messages.AssignPlayerBoat;
|
||||||
|
import network.Messages.BoatLocation;
|
||||||
|
import shared.exceptions.BoatNotFoundException;
|
||||||
|
import shared.exceptions.MarkNotFoundException;
|
||||||
|
import shared.model.GPSCoordinate;
|
||||||
|
import shared.model.Mark;
|
||||||
|
import visualiser.model.VisualiserBoat;
|
||||||
|
import visualiser.model.VisualiserRaceState;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command created when a {@link AssignPlayerBoat} message is received.
|
||||||
|
*/
|
||||||
|
public class AssignPlayerBoatCommand implements Command {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The message to operate on.
|
||||||
|
*/
|
||||||
|
private AssignPlayerBoat assignPlayerBoat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The context to operate on.
|
||||||
|
*/
|
||||||
|
private VisualiserRaceState visualiserRace;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link AssignPlayerBoatCommand}, which operates on a given {@link VisualiserRaceState}.
|
||||||
|
* @param assignPlayerBoat The message to operate on.
|
||||||
|
* @param visualiserRace The context to operate on.
|
||||||
|
*/
|
||||||
|
public AssignPlayerBoatCommand(AssignPlayerBoat assignPlayerBoat, VisualiserRaceState visualiserRace) {
|
||||||
|
this.assignPlayerBoat = assignPlayerBoat;
|
||||||
|
this.visualiserRace = visualiserRace;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
|
||||||
|
visualiserRace.setPlayerBoatID(assignPlayerBoat.getSourceID());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,127 @@
|
|||||||
|
package visualiser.Commands.VisualiserRaceCommands;
|
||||||
|
|
||||||
|
import mock.model.commandFactory.Command;
|
||||||
|
import network.Messages.BoatLocation;
|
||||||
|
import network.Messages.Enums.BoatStatusEnum;
|
||||||
|
import shared.exceptions.BoatNotFoundException;
|
||||||
|
import shared.exceptions.MarkNotFoundException;
|
||||||
|
import shared.model.GPSCoordinate;
|
||||||
|
import shared.model.Mark;
|
||||||
|
import visualiser.model.VisualiserBoat;
|
||||||
|
import visualiser.model.VisualiserRaceEvent;
|
||||||
|
import visualiser.model.VisualiserRaceState;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command created when a {@link BoatLocation} message is received.
|
||||||
|
*/
|
||||||
|
public class BoatLocationCommand implements Command {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The message to operate on.
|
||||||
|
*/
|
||||||
|
private BoatLocation boatLocation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The context to operate on.
|
||||||
|
*/
|
||||||
|
private VisualiserRaceState visualiserRace;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link BoatLocationCommand}, which operates on a given {@link VisualiserRaceState}.
|
||||||
|
* @param boatLocation The message to operate on.
|
||||||
|
* @param visualiserRace The context to operate on.
|
||||||
|
*/
|
||||||
|
public BoatLocationCommand(BoatLocation boatLocation, VisualiserRaceState visualiserRace) {
|
||||||
|
this.boatLocation = boatLocation;
|
||||||
|
this.visualiserRace = visualiserRace;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
|
||||||
|
if (visualiserRace.isVisualiserBoat(boatLocation.getSourceID())) {
|
||||||
|
updateBoatLocation();
|
||||||
|
} else if (visualiserRace.isMark(boatLocation.getSourceID())) {
|
||||||
|
updateMarkLocation();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the boat specified in the message.
|
||||||
|
*/
|
||||||
|
private void updateBoatLocation() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
VisualiserBoat boat = visualiserRace.getBoat(boatLocation.getSourceID());
|
||||||
|
|
||||||
|
//Get the new position.
|
||||||
|
GPSCoordinate gpsCoordinate = new GPSCoordinate(
|
||||||
|
boatLocation.getLatitude(),
|
||||||
|
boatLocation.getLongitude());
|
||||||
|
|
||||||
|
boat.setCurrentPosition(gpsCoordinate);
|
||||||
|
|
||||||
|
//Bearing.
|
||||||
|
boat.setBearing(boatLocation.getHeading());
|
||||||
|
|
||||||
|
|
||||||
|
//Speed.
|
||||||
|
boat.setCurrentSpeed(boatLocation.getBoatSpeedKnots());
|
||||||
|
|
||||||
|
|
||||||
|
//Attempt to add a track point.
|
||||||
|
attemptAddTrackPoint(boat);
|
||||||
|
|
||||||
|
|
||||||
|
} catch (BoatNotFoundException e) {
|
||||||
|
Logger.getGlobal().log(Level.WARNING, "BoatLocationCommand: " + this + " could not execute. Boat with sourceID: " + boatLocation.getSourceID() + " not found.", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to add a track point to the boat. Only works if the boat is currently racing.
|
||||||
|
* @param boat The boat to add a track point to.
|
||||||
|
*/
|
||||||
|
private void attemptAddTrackPoint(VisualiserBoat boat) {
|
||||||
|
if (boat.getStatus() == BoatStatusEnum.RACING) {
|
||||||
|
boat.addTrackPoint(boat.getCurrentPosition(), visualiserRace.getRaceClock().getCurrentTime());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the marker boat specified in message.
|
||||||
|
*/
|
||||||
|
private void updateMarkLocation() {
|
||||||
|
|
||||||
|
try {
|
||||||
|
Mark mark = visualiserRace.getMark(boatLocation.getSourceID());
|
||||||
|
|
||||||
|
GPSCoordinate gpsCoordinate = new GPSCoordinate(
|
||||||
|
boatLocation.getLatitude(),
|
||||||
|
boatLocation.getLongitude());
|
||||||
|
|
||||||
|
mark.setPosition(gpsCoordinate);
|
||||||
|
} catch (MarkNotFoundException e) {
|
||||||
|
Logger.getGlobal().log(Level.WARNING, "BoatLocationCommand: " + this + " could not execute. Mark with sourceID: " + boatLocation.getSourceID() + " not found.", e);
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
package visualiser.Commands.VisualiserRaceCommands;
|
||||||
|
|
||||||
|
import mock.model.commandFactory.Command;
|
||||||
|
import network.Messages.XMLMessage;
|
||||||
|
import shared.dataInput.BoatDataSource;
|
||||||
|
import shared.dataInput.BoatXMLReader;
|
||||||
|
import shared.enums.XMLFileType;
|
||||||
|
import shared.exceptions.InvalidBoatDataException;
|
||||||
|
import shared.exceptions.XMLReaderException;
|
||||||
|
import visualiser.model.VisualiserRaceEvent;
|
||||||
|
import visualiser.model.VisualiserRaceState;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command created when a {@link network.Messages.Enums.XMLMessageType#BOAT} {@link XMLMessage} message is received.
|
||||||
|
*/
|
||||||
|
public class BoatsXMLMessageCommand implements Command {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The data source to operate on.
|
||||||
|
*/
|
||||||
|
private BoatDataSource boatDataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The context to operate on.
|
||||||
|
*/
|
||||||
|
private VisualiserRaceState visualiserRace;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link BoatsXMLMessageCommand}, which operates on a given {@link VisualiserRaceEvent}.
|
||||||
|
* @param boatDataSource The data source to operate on.
|
||||||
|
* @param visualiserRace The context to operate on.
|
||||||
|
*/
|
||||||
|
public BoatsXMLMessageCommand(BoatDataSource boatDataSource, VisualiserRaceState visualiserRace) {
|
||||||
|
this.boatDataSource = boatDataSource;
|
||||||
|
this.visualiserRace = visualiserRace;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
|
||||||
|
visualiserRace.setBoatDataSource(boatDataSource);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,185 @@
|
|||||||
|
package visualiser.Commands.VisualiserRaceCommands;
|
||||||
|
|
||||||
|
import mock.model.commandFactory.Command;
|
||||||
|
import network.Messages.BoatStatus;
|
||||||
|
import network.Messages.Enums.BoatStatusEnum;
|
||||||
|
import network.Messages.RaceStatus;
|
||||||
|
import shared.exceptions.BoatNotFoundException;
|
||||||
|
import shared.model.Leg;
|
||||||
|
import visualiser.model.VisualiserBoat;
|
||||||
|
import visualiser.model.VisualiserRaceEvent;
|
||||||
|
import visualiser.model.VisualiserRaceState;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command created when a {@link RaceStatus} message is received.
|
||||||
|
*/
|
||||||
|
public class RaceStatusCommand implements Command {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The message to operate on.
|
||||||
|
*/
|
||||||
|
private RaceStatus raceStatus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The context to operate on.
|
||||||
|
*/
|
||||||
|
private VisualiserRaceState visualiserRace;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link RaceStatusCommand}, which operates on a given {@link VisualiserRaceState}.
|
||||||
|
* @param raceStatus The message to operate on.
|
||||||
|
* @param visualiserRace The context to operate on.
|
||||||
|
*/
|
||||||
|
public RaceStatusCommand(RaceStatus raceStatus, VisualiserRaceState visualiserRace) {
|
||||||
|
this.raceStatus = raceStatus;
|
||||||
|
this.visualiserRace = visualiserRace;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
|
||||||
|
//Race status enum.
|
||||||
|
visualiserRace.setRaceStatusEnum(raceStatus.getRaceStatus());
|
||||||
|
|
||||||
|
//Wind.
|
||||||
|
visualiserRace.setWind(
|
||||||
|
raceStatus.getWindDirection(),
|
||||||
|
raceStatus.getWindSpeed() );
|
||||||
|
|
||||||
|
//Current race time.
|
||||||
|
visualiserRace.getRaceClock().setUTCTime(raceStatus.getCurrentTime());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
for (BoatStatus boatStatus : raceStatus.getBoatStatuses()) {
|
||||||
|
updateBoatStatus(boatStatus);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
visualiserRace.updateBoatPositions(visualiserRace.getBoats());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates a single boat's status using the boatStatus message.
|
||||||
|
* @param boatStatus BoatStatus message to get data from.
|
||||||
|
*/
|
||||||
|
private void updateBoatStatus(BoatStatus boatStatus) {
|
||||||
|
try {
|
||||||
|
VisualiserBoat boat = visualiserRace.getBoat(boatStatus.getSourceID());
|
||||||
|
|
||||||
|
//Time at next mark.
|
||||||
|
updateEstimatedTimeAtNextMark(boatStatus, boat);
|
||||||
|
|
||||||
|
|
||||||
|
BoatStatusEnum newBoatStatusEnum = boatStatus.getBoatStatus();
|
||||||
|
|
||||||
|
//Time at last mark.
|
||||||
|
initialiseTimeAtLastMark(boat, boat.getStatus(), newBoatStatusEnum);
|
||||||
|
|
||||||
|
//Status.
|
||||||
|
boat.setStatus(newBoatStatusEnum);
|
||||||
|
|
||||||
|
|
||||||
|
List<Leg> legs = visualiserRace.getLegs();
|
||||||
|
|
||||||
|
//Leg.
|
||||||
|
updateLeg(boatStatus.getLegNumber(), boat, legs);
|
||||||
|
|
||||||
|
|
||||||
|
//Set finish time if boat finished.
|
||||||
|
attemptUpdateFinishTime(boatStatus, boat, legs);
|
||||||
|
|
||||||
|
|
||||||
|
} catch (BoatNotFoundException e) {
|
||||||
|
Logger.getGlobal().log(Level.WARNING, "RaceStatusCommand.updateBoatStatus: " + this + " could not execute. Boat with sourceID: " + boatStatus.getSourceID() + " not found.", e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attempts to update the finish time of the boat. Only works if the boat has actually finished the race.
|
||||||
|
* @param boatStatus BoatStatus to read data from.
|
||||||
|
* @param boat Boat to update.
|
||||||
|
* @param legs Legs of the race.
|
||||||
|
*/
|
||||||
|
private void attemptUpdateFinishTime(BoatStatus boatStatus, VisualiserBoat boat, List<Leg> legs) {
|
||||||
|
|
||||||
|
if (boat.getStatus() == BoatStatusEnum.FINISHED || boatStatus.getLegNumber() == legs.size()) {
|
||||||
|
boat.setTimeFinished(visualiserRace.getRaceClock().getCurrentTimeMilli());
|
||||||
|
boat.setStatus(BoatStatusEnum.FINISHED);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates a boat's leg.
|
||||||
|
* @param legNumber The new leg number.
|
||||||
|
* @param boat The boat to update.
|
||||||
|
* @param legs The legs in the race.
|
||||||
|
*/
|
||||||
|
private void updateLeg(int legNumber, VisualiserBoat boat, List<Leg> legs) {
|
||||||
|
|
||||||
|
if (legNumber >= 1 && legNumber < legs.size()) {
|
||||||
|
if (boat.getCurrentLeg() != legs.get(legNumber)) {
|
||||||
|
boatFinishedLeg(boat, legs.get(legNumber));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialises the time at last mark for a boat. Only changes if the boat's status is changing from non-racing to racing.
|
||||||
|
* @param boat The boat to update.
|
||||||
|
* @param currentBoatStatus The current status of the boat.
|
||||||
|
* @param newBoatStatusEnum The new status of the boat, from the BoatStatus message.
|
||||||
|
*/
|
||||||
|
private void initialiseTimeAtLastMark(VisualiserBoat boat, BoatStatusEnum currentBoatStatus, BoatStatusEnum newBoatStatusEnum) {
|
||||||
|
//If we are changing from non-racing to racing, we need to initialise boat with their time at last mark.
|
||||||
|
if ((currentBoatStatus != BoatStatusEnum.RACING) && (newBoatStatusEnum == BoatStatusEnum.RACING)) {
|
||||||
|
boat.setTimeAtLastMark(visualiserRace.getRaceClock().getCurrentTime());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the estimated time at next mark for a given boat.
|
||||||
|
* @param boatStatus BoatStatus to read data from.
|
||||||
|
* @param boat Boat to update.
|
||||||
|
*/
|
||||||
|
private void updateEstimatedTimeAtNextMark(BoatStatus boatStatus, VisualiserBoat boat) {
|
||||||
|
boat.setEstimatedTimeAtNextMark(visualiserRace.getRaceClock().getLocalTime(boatStatus.getEstTimeAtNextMark()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates a boat's leg to a specified leg. Also records the order in which the boat passed the leg.
|
||||||
|
* @param boat The boat to update.
|
||||||
|
* @param leg The leg to use.
|
||||||
|
*/
|
||||||
|
private void boatFinishedLeg(VisualiserBoat boat, Leg leg) {
|
||||||
|
|
||||||
|
//Record order in which boat finished leg.
|
||||||
|
visualiserRace.getLegCompletionOrder().get(boat.getCurrentLeg()).add(boat);
|
||||||
|
|
||||||
|
//Update boat.
|
||||||
|
boat.setCurrentLeg(leg);
|
||||||
|
boat.setTimeAtLastMark(visualiserRace.getRaceClock().getCurrentTime());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
package visualiser.Commands.VisualiserRaceCommands;
|
||||||
|
|
||||||
|
import mock.model.commandFactory.Command;
|
||||||
|
import network.Messages.XMLMessage;
|
||||||
|
import shared.dataInput.RaceDataSource;
|
||||||
|
import visualiser.model.VisualiserRaceEvent;
|
||||||
|
import visualiser.model.VisualiserRaceState;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command created when a {@link network.Messages.Enums.XMLMessageType#BOAT} {@link XMLMessage} message is received.
|
||||||
|
*/
|
||||||
|
public class RaceXMLMessageCommand implements Command {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The data source to operate on.
|
||||||
|
*/
|
||||||
|
private RaceDataSource raceDataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The context to operate on.
|
||||||
|
*/
|
||||||
|
private VisualiserRaceState visualiserRace;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link RaceXMLMessageCommand}, which operates on a given {@link VisualiserRaceEvent}.
|
||||||
|
* @param raceDataSource The data source to operate on.
|
||||||
|
* @param visualiserRace The context to operate on.
|
||||||
|
*/
|
||||||
|
public RaceXMLMessageCommand(RaceDataSource raceDataSource, VisualiserRaceState visualiserRace) {
|
||||||
|
this.raceDataSource = raceDataSource;
|
||||||
|
this.visualiserRace = visualiserRace;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
|
||||||
|
visualiserRace.setRaceDataSource(raceDataSource);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,44 @@
|
|||||||
|
package visualiser.Commands.VisualiserRaceCommands;
|
||||||
|
|
||||||
|
import mock.model.commandFactory.Command;
|
||||||
|
import network.Messages.XMLMessage;
|
||||||
|
import shared.dataInput.RegattaDataSource;
|
||||||
|
import visualiser.model.VisualiserRaceEvent;
|
||||||
|
import visualiser.model.VisualiserRaceState;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command created when a {@link network.Messages.Enums.XMLMessageType#BOAT} {@link XMLMessage} message is received.
|
||||||
|
*/
|
||||||
|
public class RegattaXMLMessageCommand implements Command {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The data source to operate on.
|
||||||
|
*/
|
||||||
|
private RegattaDataSource regattaDataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The context to operate on.
|
||||||
|
*/
|
||||||
|
private VisualiserRaceState visualiserRace;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link RegattaXMLMessageCommand}, which operates on a given {@link VisualiserRaceEvent}.
|
||||||
|
* @param regattaDataSource The data source to operate on.
|
||||||
|
* @param visualiserRace The context to operate on.
|
||||||
|
*/
|
||||||
|
public RegattaXMLMessageCommand(RegattaDataSource regattaDataSource, VisualiserRaceState visualiserRace) {
|
||||||
|
this.regattaDataSource = regattaDataSource;
|
||||||
|
this.visualiserRace = visualiserRace;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
|
||||||
|
visualiserRace.setRegattaDataSource(regattaDataSource);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,40 @@
|
|||||||
|
package visualiser.Commands.VisualiserRaceCommands;
|
||||||
|
|
||||||
|
|
||||||
|
import mock.exceptions.CommandConstructionException;
|
||||||
|
import mock.model.commandFactory.Command;
|
||||||
|
import network.Messages.*;
|
||||||
|
import visualiser.model.VisualiserRaceEvent;
|
||||||
|
import visualiser.model.VisualiserRaceState;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory to create VisualiserRace commands.
|
||||||
|
*/
|
||||||
|
public class VisualiserRaceCommandFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a command on an VisualiserRace.
|
||||||
|
* @param message The message to turn into a command.
|
||||||
|
* @param visualiserRace The context for the command to operate on.
|
||||||
|
* @return The command to execute the given action.
|
||||||
|
* @throws CommandConstructionException Thrown if the command cannot be constructed.
|
||||||
|
*/
|
||||||
|
public static Command create(AC35Data message, VisualiserRaceState visualiserRace) throws CommandConstructionException {
|
||||||
|
|
||||||
|
switch (message.getType()) {
|
||||||
|
|
||||||
|
case BOATLOCATION: return new BoatLocationCommand((BoatLocation) message, visualiserRace);
|
||||||
|
|
||||||
|
case RACESTATUS: return new RaceStatusCommand((RaceStatus) message, visualiserRace);
|
||||||
|
|
||||||
|
case XMLMESSAGE: return XMLMessageCommandFactory.create((XMLMessage) message, visualiserRace);
|
||||||
|
|
||||||
|
case ASSIGN_PLAYER_BOAT: return new AssignPlayerBoatCommand((AssignPlayerBoat) message, visualiserRace);
|
||||||
|
|
||||||
|
default: throw new CommandConstructionException("Could not create VisualiserRaceCommand. Unrecognised or unsupported MessageType: " + message.getType());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,63 @@
|
|||||||
|
package visualiser.Commands.VisualiserRaceCommands;
|
||||||
|
|
||||||
|
|
||||||
|
import mock.exceptions.CommandConstructionException;
|
||||||
|
import mock.model.commandFactory.Command;
|
||||||
|
import network.Messages.AC35Data;
|
||||||
|
import network.Messages.BoatLocation;
|
||||||
|
import network.Messages.RaceStatus;
|
||||||
|
import network.Messages.XMLMessage;
|
||||||
|
import shared.dataInput.*;
|
||||||
|
import shared.enums.XMLFileType;
|
||||||
|
import shared.exceptions.InvalidBoatDataException;
|
||||||
|
import shared.exceptions.InvalidRaceDataException;
|
||||||
|
import shared.exceptions.InvalidRegattaDataException;
|
||||||
|
import shared.exceptions.XMLReaderException;
|
||||||
|
import visualiser.model.VisualiserRaceState;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory to create VisualiserRace commands, from XMLMessages.
|
||||||
|
*/
|
||||||
|
public class XMLMessageCommandFactory {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a command on an VisualiserRace.
|
||||||
|
* @param message The message to turn into a command.
|
||||||
|
* @param visualiserRace The context for the command to operate on.
|
||||||
|
* @return The command to execute the given action.
|
||||||
|
* @throws CommandConstructionException Thrown if the command cannot be constructed.
|
||||||
|
*/
|
||||||
|
public static Command create(XMLMessage message, VisualiserRaceState visualiserRace) throws CommandConstructionException {
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
switch (message.getXmlMsgSubType()) {
|
||||||
|
|
||||||
|
case BOAT:
|
||||||
|
BoatDataSource boatDataSource = new BoatXMLReader(message.getXmlMessage(), XMLFileType.Contents);
|
||||||
|
return new BoatsXMLMessageCommand(boatDataSource, visualiserRace);
|
||||||
|
|
||||||
|
|
||||||
|
case RACE:
|
||||||
|
RaceDataSource raceDataSource = new RaceXMLReader(message.getXmlMessage(), XMLFileType.Contents);
|
||||||
|
return new RaceXMLMessageCommand(raceDataSource, visualiserRace);
|
||||||
|
|
||||||
|
|
||||||
|
case REGATTA:
|
||||||
|
RegattaDataSource regattaDataSource = new RegattaXMLReader(message.getXmlMessage(), XMLFileType.Contents);
|
||||||
|
return new RegattaXMLMessageCommand(regattaDataSource, visualiserRace);
|
||||||
|
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new CommandConstructionException("Could not create VisualiserRaceCommand/XMLCommand. Unrecognised or unsupported MessageType: " + message.getType());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (XMLReaderException | InvalidBoatDataException | InvalidRegattaDataException | InvalidRaceDataException e) {
|
||||||
|
throw new CommandConstructionException("Could not create VisualiserRaceCommand/XMLCommand. Could not parse XML message payload.", e);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,472 +0,0 @@
|
|||||||
package visualiser.model;
|
|
||||||
|
|
||||||
import javafx.animation.AnimationTimer;
|
|
||||||
import javafx.collections.FXCollections;
|
|
||||||
import javafx.collections.ObservableList;
|
|
||||||
import javafx.scene.paint.Color;
|
|
||||||
import network.Messages.BoatLocation;
|
|
||||||
import network.Messages.BoatStatus;
|
|
||||||
import network.Messages.Enums.BoatStatusEnum;
|
|
||||||
import network.Messages.Enums.RaceStatusEnum;
|
|
||||||
import network.Messages.LatestMessages;
|
|
||||||
import network.Messages.RaceStatus;
|
|
||||||
import shared.dataInput.BoatDataSource;
|
|
||||||
import shared.dataInput.RaceDataSource;
|
|
||||||
import shared.dataInput.RegattaDataSource;
|
|
||||||
import shared.model.*;
|
|
||||||
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The Class used to view the race streamed.
|
|
||||||
* Has a course, boats, boundaries, etc...
|
|
||||||
* Observes LatestMessages and updates its state based on new messages.
|
|
||||||
*/
|
|
||||||
public class VisualiserRace extends Race implements Runnable {
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An observable list of boats in the race.
|
|
||||||
*/
|
|
||||||
private final ObservableList<VisualiserBoat> boats;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* An observable list of marker boats in the race.
|
|
||||||
*/
|
|
||||||
private ObservableList<Mark> boatMarkers;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Maps between a Leg to a list of boats, in the order that they finished the leg.
|
|
||||||
* Used by the Sparkline to ensure it has correct information.
|
|
||||||
*/
|
|
||||||
private Map<Leg, List<VisualiserBoat>> legCompletionOrder = new HashMap<>();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs a race object with a given RaceDataSource, BoatDataSource, and RegattaDataSource and receives events from LatestMessages.
|
|
||||||
* @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 LatestMessages to send events to.
|
|
||||||
* @param colors A collection of colors used to assign a color to each boat.
|
|
||||||
*/
|
|
||||||
public VisualiserRace(BoatDataSource boatDataSource, RaceDataSource raceDataSource, RegattaDataSource regattaDataSource, LatestMessages latestMessages, List<Color> colors) {
|
|
||||||
|
|
||||||
super(boatDataSource, raceDataSource, regattaDataSource, latestMessages);
|
|
||||||
|
|
||||||
|
|
||||||
this.boats = FXCollections.observableArrayList(this.generateVisualiserBoats(boatDataSource.getBoats(), raceDataSource.getParticipants(), colors));
|
|
||||||
|
|
||||||
this.boatMarkers = FXCollections.observableArrayList(boatDataSource.getMarkerBoats().values());
|
|
||||||
|
|
||||||
|
|
||||||
//Initialise the leg completion order map.
|
|
||||||
for (Leg leg : this.legs) {
|
|
||||||
this.legCompletionOrder.put(leg, new ArrayList<>(this.boats.size()));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the race data source for this race to a new RaceDataSource.
|
|
||||||
* Uses the boundary and legs specified by the new RaceDataSource.
|
|
||||||
* @param raceDataSource The new RaceDataSource to use.
|
|
||||||
*/
|
|
||||||
public void setRaceDataSource(RaceDataSource raceDataSource) {
|
|
||||||
this.raceDataSource = raceDataSource;
|
|
||||||
|
|
||||||
this.boundary = raceDataSource.getBoundary();
|
|
||||||
|
|
||||||
this.useLegsList(raceDataSource.getLegs());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the boat data source for this race to a new BoatDataSource.
|
|
||||||
* Uses the marker boats specified by the new BoatDataSource.
|
|
||||||
* @param boatDataSource The new BoatDataSource to use.
|
|
||||||
*/
|
|
||||||
public void setBoatDataSource(BoatDataSource boatDataSource) {
|
|
||||||
this.boatDataSource = boatDataSource;
|
|
||||||
|
|
||||||
this.boatMarkers = FXCollections.observableArrayList(boatDataSource.getMarkerBoats().values());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the regatta data source for this race to a new RegattaDataSource.
|
|
||||||
* @param regattaDataSource The new RegattaDataSource to use.
|
|
||||||
*/
|
|
||||||
public void setRegattaDataSource(RegattaDataSource regattaDataSource) {
|
|
||||||
this.regattaDataSource = regattaDataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a list of {@link Mark} boats.
|
|
||||||
* @return List of mark boats.
|
|
||||||
*/
|
|
||||||
public ObservableList<Mark> getMarks() {
|
|
||||||
return boatMarkers;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generates a list of VisualiserBoats given a list of Boats, and a list of participating boats.
|
|
||||||
* @param boats The map of Boats describing boats that are potentially in the race. Maps boat sourceID to boat.
|
|
||||||
* @param sourceIDs The list of boat sourceIDs describing which specific boats are actually participating.
|
|
||||||
* @param colors The list of colors to be used for the boats.
|
|
||||||
* @return A list of MockBoats that are participating in the race.
|
|
||||||
*/
|
|
||||||
private List<VisualiserBoat> generateVisualiserBoats(Map<Integer, Boat> boats, List<Integer> sourceIDs, List<Color> colors) {
|
|
||||||
|
|
||||||
List<VisualiserBoat> visualiserBoats = new ArrayList<>(sourceIDs.size());
|
|
||||||
|
|
||||||
//For each sourceID participating...
|
|
||||||
int colorIndex = 0;
|
|
||||||
for (int sourceID : sourceIDs) {
|
|
||||||
|
|
||||||
//Get the boat associated with the sourceID.
|
|
||||||
Boat boat = boats.get(sourceID);
|
|
||||||
|
|
||||||
//Get a color for the boat.
|
|
||||||
Color color = colors.get(colorIndex);
|
|
||||||
|
|
||||||
//Construct a VisualiserBoat using the Boat and Polars.
|
|
||||||
VisualiserBoat visualiserBoat = new VisualiserBoat(boat, color);
|
|
||||||
|
|
||||||
visualiserBoats.add(visualiserBoat);
|
|
||||||
|
|
||||||
//Next color.
|
|
||||||
colorIndex++;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return visualiserBoats;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialise the boats in the race.
|
|
||||||
* This sets their current leg.
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
protected void initialiseBoats() {
|
|
||||||
|
|
||||||
Leg startingLeg = legs.get(0);
|
|
||||||
|
|
||||||
for (VisualiserBoat boat : boats) {
|
|
||||||
|
|
||||||
boat.setCurrentLeg(startingLeg);
|
|
||||||
boat.setTimeAtLastMark(this.raceClock.getCurrentTime());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates all of the racing boats based on messages received.
|
|
||||||
* @param boats The list of racing boats.
|
|
||||||
* @param boatLocationMap A map between boat sourceIDs and BoatLocation messages.
|
|
||||||
* @param boatStatusMap A map between boat sourceIDs and BoatStatus messages.
|
|
||||||
*/
|
|
||||||
private void updateBoats(ObservableList<VisualiserBoat> boats, Map<Integer, BoatLocation> boatLocationMap, Map<Integer, BoatStatus> boatStatusMap) {
|
|
||||||
|
|
||||||
for (VisualiserBoat boat : boats) {
|
|
||||||
BoatLocation boatLocation = boatLocationMap.get(boat.getSourceID());
|
|
||||||
BoatStatus boatStatus = boatStatusMap.get(boat.getSourceID());
|
|
||||||
updateBoat(boat, boatLocation, boatStatus);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates an individual racing boat based on messages received.
|
|
||||||
* @param boat The boat to update.
|
|
||||||
* @param boatLocation The BoatLocation message to use.
|
|
||||||
* @param boatStatus The BoatStatus message to use.
|
|
||||||
*/
|
|
||||||
private void updateBoat(VisualiserBoat boat, BoatLocation boatLocation, BoatStatus boatStatus) {
|
|
||||||
|
|
||||||
if (boatLocation != null && boatStatus != null) {
|
|
||||||
|
|
||||||
//Get the new position.
|
|
||||||
double latitude = boatLocation.getLatitude();
|
|
||||||
double longitude = boatLocation.getLongitude();
|
|
||||||
GPSCoordinate gpsCoordinate = new GPSCoordinate(latitude, longitude);
|
|
||||||
|
|
||||||
boat.setCurrentPosition(gpsCoordinate);
|
|
||||||
|
|
||||||
//Bearing.
|
|
||||||
boat.setBearing(boatLocation.getHeading());
|
|
||||||
|
|
||||||
//Time until next mark.
|
|
||||||
boat.setEstimatedTimeAtNextMark(raceClock.getLocalTime(boatStatus.getEstTimeAtNextMark()));
|
|
||||||
|
|
||||||
//Speed.
|
|
||||||
boat.setCurrentSpeed(boatLocation.getBoatSpeedKnots());
|
|
||||||
|
|
||||||
|
|
||||||
//Boat status.
|
|
||||||
BoatStatusEnum newBoatStatusEnum = boatStatus.getBoatStatus();
|
|
||||||
|
|
||||||
//If we are changing from non-racing to racing, we need to initialise boat with their time at last mark.
|
|
||||||
if ((boat.getStatus() != BoatStatusEnum.RACING) && (newBoatStatusEnum == BoatStatusEnum.RACING)) {
|
|
||||||
boat.setTimeAtLastMark(this.raceClock.getCurrentTime());
|
|
||||||
}
|
|
||||||
|
|
||||||
boat.setStatus(newBoatStatusEnum);
|
|
||||||
|
|
||||||
|
|
||||||
//Leg.
|
|
||||||
int legNumber = boatStatus.getLegNumber();
|
|
||||||
|
|
||||||
if (legNumber >= 1 && legNumber < legs.size()) {
|
|
||||||
if (boat.getCurrentLeg() != legs.get(legNumber)) {
|
|
||||||
boatFinishedLeg(boat, legs.get(legNumber));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//Attempt to add a track point.
|
|
||||||
if (newBoatStatusEnum == BoatStatusEnum.RACING) {
|
|
||||||
boat.addTrackPoint(boat.getCurrentPosition(), raceClock.getCurrentTime());
|
|
||||||
}
|
|
||||||
|
|
||||||
//Set finish time if boat finished.
|
|
||||||
if (newBoatStatusEnum == BoatStatusEnum.FINISHED || legNumber == this.legs.size()) {
|
|
||||||
boat.setTimeFinished(boatLocation.getTime());
|
|
||||||
boat.setStatus(BoatStatusEnum.FINISHED);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates a boat's leg to a specified leg. Also records the order in which the boat passed the leg.
|
|
||||||
* @param boat The boat to update.
|
|
||||||
* @param leg The leg to use.
|
|
||||||
*/
|
|
||||||
private void boatFinishedLeg(VisualiserBoat boat, Leg leg) {
|
|
||||||
|
|
||||||
//Record order in which boat finished leg.
|
|
||||||
this.legCompletionOrder.get(boat.getCurrentLeg()).add(boat);
|
|
||||||
|
|
||||||
//Update boat.
|
|
||||||
boat.setCurrentLeg(leg);
|
|
||||||
boat.setTimeAtLastMark(this.raceClock.getCurrentTime());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates all of the marker boats based on messages received.
|
|
||||||
* @param boatMarkers The list of marker boats.
|
|
||||||
* @param boatLocationMap A map between boat sourceIDs and BoatLocation messages.
|
|
||||||
* @param boatStatusMap A map between boat sourceIDs and BoatStatus messages.
|
|
||||||
*/
|
|
||||||
private void updateMarkers(ObservableList<Mark> boatMarkers, Map<Integer, BoatLocation> boatLocationMap, Map<Integer, BoatStatus> boatStatusMap) {
|
|
||||||
|
|
||||||
for (Mark mark : boatMarkers) {
|
|
||||||
BoatLocation boatLocation = boatLocationMap.get(mark.getSourceID());
|
|
||||||
updateMark(mark, boatLocation);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates an individual marker boat based on messages received.
|
|
||||||
* @param mark The marker boat to be updated.
|
|
||||||
* @param boatLocation The message describing the boat's new location.
|
|
||||||
*/
|
|
||||||
private void updateMark(Mark mark, BoatLocation boatLocation) {
|
|
||||||
|
|
||||||
if (boatLocation != null) {
|
|
||||||
|
|
||||||
//We only update the boat's position.
|
|
||||||
double latitude = boatLocation.getLatitude();
|
|
||||||
double longitude = boatLocation.getLongitude();
|
|
||||||
GPSCoordinate gpsCoordinate = new GPSCoordinate(latitude, longitude);
|
|
||||||
|
|
||||||
mark.setPosition(gpsCoordinate);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates the race status (RaceStatusEnum, wind bearing, wind speed) based on received messages.
|
|
||||||
* @param raceStatus The RaceStatus message received.
|
|
||||||
*/
|
|
||||||
private void updateRaceStatus(RaceStatus raceStatus) {
|
|
||||||
|
|
||||||
//Race status enum.
|
|
||||||
this.raceStatusEnum = raceStatus.getRaceStatus();
|
|
||||||
|
|
||||||
//Wind.
|
|
||||||
this.setWind(
|
|
||||||
raceStatus.getWindDirection(),
|
|
||||||
raceStatus.getWindSpeed() );
|
|
||||||
|
|
||||||
//Current race time.
|
|
||||||
this.raceClock.setUTCTime(raceStatus.getCurrentTime());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Runnable for the thread.
|
|
||||||
*/
|
|
||||||
public void run() {
|
|
||||||
initialiseBoats();
|
|
||||||
startRaceStream();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts the race.
|
|
||||||
* This updates the race based on {@link #latestMessages}.
|
|
||||||
*/
|
|
||||||
private void startRaceStream() {
|
|
||||||
|
|
||||||
new AnimationTimer() {
|
|
||||||
|
|
||||||
long lastFrameTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void handle(long arg0) {
|
|
||||||
|
|
||||||
|
|
||||||
//Calculate the frame period.
|
|
||||||
long currentFrameTime = System.currentTimeMillis();
|
|
||||||
long framePeriod = currentFrameTime - lastFrameTime;
|
|
||||||
|
|
||||||
|
|
||||||
//Update race status.
|
|
||||||
if (latestMessages.getRaceStatus() != null) {
|
|
||||||
updateRaceStatus(latestMessages.getRaceStatus());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//Update racing boats.
|
|
||||||
updateBoats(boats, latestMessages.getBoatLocationMap(), latestMessages.getBoatStatusMap());
|
|
||||||
//And their positions (e.g., 5th).
|
|
||||||
updateBoatPositions(boats);
|
|
||||||
|
|
||||||
|
|
||||||
//Update marker boats.
|
|
||||||
updateMarkers(boatMarkers, latestMessages.getBoatLocationMap(), latestMessages.getBoatStatusMap());
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (getRaceStatusEnum() == RaceStatusEnum.FINISHED) {
|
|
||||||
stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
lastFrameTime = currentFrameTime;
|
|
||||||
|
|
||||||
//Increment fps.
|
|
||||||
incrementFps(framePeriod);
|
|
||||||
|
|
||||||
}
|
|
||||||
}.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update position of boats in race (e.g, 5th), no position if on starting leg or DNF.
|
|
||||||
* @param boats The list of boats to update.
|
|
||||||
*/
|
|
||||||
private void updateBoatPositions(ObservableList<VisualiserBoat> boats) {
|
|
||||||
|
|
||||||
//Sort boats.
|
|
||||||
sortBoatsByPosition(boats);
|
|
||||||
|
|
||||||
//Assign new positions.
|
|
||||||
for (int i = 0; i < boats.size(); i++) {
|
|
||||||
VisualiserBoat boat = boats.get(i);
|
|
||||||
|
|
||||||
|
|
||||||
if ((boat.getStatus() == BoatStatusEnum.DNF) || (boat.getStatus() == BoatStatusEnum.PRESTART) || (boat.getCurrentLeg().getLegNumber() < 0)) {
|
|
||||||
|
|
||||||
boat.setPosition("-");
|
|
||||||
|
|
||||||
} else {
|
|
||||||
boat.setPosition(Integer.toString(i + 1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sorts the list of boats by their position within the race.
|
|
||||||
* @param boats The list of boats in the race.
|
|
||||||
*/
|
|
||||||
private void sortBoatsByPosition(ObservableList<VisualiserBoat> boats) {
|
|
||||||
|
|
||||||
FXCollections.sort(boats, (a, b) -> {
|
|
||||||
//Get the difference in leg numbers.
|
|
||||||
int legNumberDelta = b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber();
|
|
||||||
|
|
||||||
//If they're on the same leg, we need to compare time to finish leg.
|
|
||||||
if (legNumberDelta == 0) {
|
|
||||||
return (int) Duration.between(b.getEstimatedTimeAtNextMark(), a.getEstimatedTimeAtNextMark()).toMillis();
|
|
||||||
} else {
|
|
||||||
return legNumberDelta;
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the boats participating in the race.
|
|
||||||
* @return ObservableList of boats participating in the race.
|
|
||||||
*/
|
|
||||||
public ObservableList<VisualiserBoat> getBoats() {
|
|
||||||
return boats;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the order in which boats completed each leg. Maps the leg to a list of boats, ordered by the order in which they finished the leg.
|
|
||||||
* @return Leg completion order for each leg.
|
|
||||||
*/
|
|
||||||
public Map<Leg, List<VisualiserBoat>> getLegCompletionOrder() {
|
|
||||||
return legCompletionOrder;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Takes an estimated time an event will occur, and converts it to the
|
|
||||||
* number of seconds before the event will occur.
|
|
||||||
*
|
|
||||||
* @param estTimeMillis The estimated time, in milliseconds.
|
|
||||||
* @param currentTime The current time, in milliseconds.
|
|
||||||
* @return int difference between time the race started and the estimated time
|
|
||||||
*/
|
|
||||||
private int convertEstTime(long estTimeMillis, long currentTime) {
|
|
||||||
|
|
||||||
//Calculate millisecond delta.
|
|
||||||
long estElapsedMillis = estTimeMillis - currentTime;
|
|
||||||
|
|
||||||
//Convert milliseconds to seconds.
|
|
||||||
int estElapsedSecs = Math.round(estElapsedMillis / 1000);
|
|
||||||
|
|
||||||
return estElapsedSecs;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -0,0 +1,119 @@
|
|||||||
|
package visualiser.model;
|
||||||
|
|
||||||
|
|
||||||
|
import javafx.beans.property.IntegerProperty;
|
||||||
|
import mock.model.commandFactory.CompositeCommand;
|
||||||
|
import network.Messages.Enums.RequestToJoinEnum;
|
||||||
|
import shared.dataInput.EmptyBoatDataSource;
|
||||||
|
import shared.dataInput.EmptyRaceDataSource;
|
||||||
|
import shared.dataInput.EmptyRegattaDataSource;
|
||||||
|
import visualiser.gameController.ControllerClient;
|
||||||
|
import visualiser.network.ServerConnection;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class holds a race, and a client's connection to it
|
||||||
|
*/
|
||||||
|
public class VisualiserRaceEvent {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Our connection to the server.
|
||||||
|
*/
|
||||||
|
private ServerConnection serverConnection;
|
||||||
|
/**
|
||||||
|
* The thread serverConnection is running on.
|
||||||
|
*/
|
||||||
|
private Thread serverConnectionThread;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The race object which describes the currently occurring race.
|
||||||
|
*/
|
||||||
|
private VisualiserRaceState visualiserRaceState;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The service for updating the {@link #visualiserRaceState}.
|
||||||
|
*/
|
||||||
|
private VisualiserRaceService visualiserRaceService;
|
||||||
|
/**
|
||||||
|
* The thread {@link #visualiserRaceService} is running on.
|
||||||
|
*/
|
||||||
|
private Thread visualiserRaceServiceThread;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a visualiser race event, with a given socket and request type.
|
||||||
|
* @param socket The socket to connect to.
|
||||||
|
* @param requestType The type of {@link network.Messages.RequestToJoin} to make.
|
||||||
|
* @throws IOException Thrown if there is a problem with the socket.
|
||||||
|
*/
|
||||||
|
public VisualiserRaceEvent(Socket socket, RequestToJoinEnum requestType) throws IOException {
|
||||||
|
|
||||||
|
this.visualiserRaceState = new VisualiserRaceState(new EmptyRaceDataSource(), new EmptyRegattaDataSource(), new EmptyBoatDataSource());
|
||||||
|
|
||||||
|
|
||||||
|
CompositeCommand raceCommands = new CompositeCommand();
|
||||||
|
this.visualiserRaceService = new VisualiserRaceService(raceCommands, visualiserRaceState);
|
||||||
|
|
||||||
|
this.visualiserRaceServiceThread = new Thread(visualiserRaceService, "VisualiserRaceEvent()->VisualiserRaceService thread " + visualiserRaceService);
|
||||||
|
this.visualiserRaceServiceThread.start();
|
||||||
|
|
||||||
|
|
||||||
|
this.serverConnection = new ServerConnection(socket, visualiserRaceState, raceCommands, requestType);
|
||||||
|
this.serverConnectionThread = new Thread(serverConnection, "StartController.enterLobby()->serverConnection thread " + serverConnection);
|
||||||
|
this.serverConnectionThread.start();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the state of the race.
|
||||||
|
* @return The state of the race.
|
||||||
|
*/
|
||||||
|
public VisualiserRaceState getVisualiserRaceState() {
|
||||||
|
return visualiserRaceState;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the controller client, which writes BoatAction messages to the outgoing queue.
|
||||||
|
* @return The ControllerClient.
|
||||||
|
*/
|
||||||
|
public ControllerClient getControllerClient() {
|
||||||
|
return serverConnection.getControllerClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the connection to server.
|
||||||
|
* @return Connection to server.
|
||||||
|
*/
|
||||||
|
public ServerConnection getServerConnection() {
|
||||||
|
return serverConnection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the framerate property of the race.
|
||||||
|
* @return Framerate property of race.
|
||||||
|
*/
|
||||||
|
public IntegerProperty getFrameRateProperty() {
|
||||||
|
return visualiserRaceService.getFrameRateProperty();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Terminates the server connection and race service.
|
||||||
|
*/
|
||||||
|
public void terminate() {
|
||||||
|
this.serverConnectionThread.interrupt();
|
||||||
|
|
||||||
|
this.visualiserRaceServiceThread.interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,93 @@
|
|||||||
|
package visualiser.model;
|
||||||
|
|
||||||
|
import javafx.beans.property.IntegerProperty;
|
||||||
|
import mock.model.commandFactory.CompositeCommand;
|
||||||
|
import shared.model.FrameRateTracker;
|
||||||
|
import shared.model.RunnableWithFramePeriod;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles updating a {@link VisualiserRaceState} with incoming commands.
|
||||||
|
*/
|
||||||
|
public class VisualiserRaceService implements RunnableWithFramePeriod {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The race state to update.
|
||||||
|
*/
|
||||||
|
private VisualiserRaceState visualiserRaceState;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A composite commands to execute to update the race.
|
||||||
|
*/
|
||||||
|
private CompositeCommand raceCommands;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to track the framerate of the "simulation".
|
||||||
|
*/
|
||||||
|
private FrameRateTracker frameRateTracker;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a visualiser race which models a yacht race, and is modified by CompositeCommand.
|
||||||
|
* @param raceCommands A composite commands to execute to update the race.
|
||||||
|
* @param visualiserRaceState The race state to update.
|
||||||
|
*/
|
||||||
|
public VisualiserRaceService(CompositeCommand raceCommands, VisualiserRaceState visualiserRaceState) {
|
||||||
|
this.raceCommands = raceCommands;
|
||||||
|
this.visualiserRaceState = visualiserRaceState;
|
||||||
|
|
||||||
|
this.frameRateTracker = new FrameRateTracker();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the CompositeCommand executed by the race.
|
||||||
|
* @return CompositeCommand executed by race.
|
||||||
|
*/
|
||||||
|
public CompositeCommand getRaceCommands() {
|
||||||
|
return raceCommands;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
long previousFrameTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
while (!Thread.interrupted()) {
|
||||||
|
|
||||||
|
long currentFrameTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
waitForFramePeriod(previousFrameTime, currentFrameTime, 16);
|
||||||
|
|
||||||
|
previousFrameTime = currentFrameTime;
|
||||||
|
|
||||||
|
|
||||||
|
raceCommands.execute();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
frameRateTracker.stop();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the framerate property of the race.
|
||||||
|
* @return Framerate property of race.
|
||||||
|
*/
|
||||||
|
public IntegerProperty getFrameRateProperty() {
|
||||||
|
return frameRateTracker.fpsProperty();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,411 @@
|
|||||||
|
package visualiser.model;
|
||||||
|
|
||||||
|
|
||||||
|
import javafx.collections.FXCollections;
|
||||||
|
import javafx.collections.ObservableList;
|
||||||
|
import javafx.scene.paint.Color;
|
||||||
|
import network.Messages.Enums.BoatStatusEnum;
|
||||||
|
import shared.dataInput.BoatDataSource;
|
||||||
|
import shared.dataInput.RaceDataSource;
|
||||||
|
import shared.dataInput.RegattaDataSource;
|
||||||
|
import shared.exceptions.BoatNotFoundException;
|
||||||
|
import shared.exceptions.MarkNotFoundException;
|
||||||
|
import shared.model.*;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class contains all of the state of a race on the client (visualiser) side.
|
||||||
|
*/
|
||||||
|
public class VisualiserRaceState extends RaceState {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of boats in the race.
|
||||||
|
*/
|
||||||
|
private ObservableList<VisualiserBoat> boats;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The source ID of the boat assigned to the player.
|
||||||
|
* 0 if no boat has been assigned.
|
||||||
|
*/
|
||||||
|
private int playerBoatID;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps between a Leg to a list of boats, in the order that they finished the leg.
|
||||||
|
* Used by the Sparkline to ensure it has correct information.
|
||||||
|
* TODO BUG: if we receive a race.xml file during the race, then we need to add/remove legs to this, without losing information.
|
||||||
|
*/
|
||||||
|
private Map<Leg, List<VisualiserBoat>> legCompletionOrder;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An array of colors used to assign colors to each boat - passed in to the VisualiserRace constructor.
|
||||||
|
*/
|
||||||
|
private List<Color> unassignedColors = new ArrayList<>(Arrays.asList(
|
||||||
|
Color.BLUEVIOLET,
|
||||||
|
Color.BLACK,
|
||||||
|
Color.RED,
|
||||||
|
Color.ORANGE,
|
||||||
|
Color.DARKOLIVEGREEN,
|
||||||
|
Color.LIMEGREEN,
|
||||||
|
Color.PURPLE,
|
||||||
|
Color.DARKGRAY,
|
||||||
|
Color.YELLOW
|
||||||
|
//TODO may need to add more colors.
|
||||||
|
));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a visualiser race which models a yacht race.
|
||||||
|
*/
|
||||||
|
public VisualiserRaceState(RaceDataSource raceDataSource, RegattaDataSource regattaDataSource, BoatDataSource boatDataSource) {
|
||||||
|
|
||||||
|
this.boats = FXCollections.observableArrayList();
|
||||||
|
|
||||||
|
this.playerBoatID = 0;
|
||||||
|
|
||||||
|
this.legCompletionOrder = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
setRaceDataSource(raceDataSource);
|
||||||
|
setRegattaDataSource(regattaDataSource);
|
||||||
|
setBoatDataSource(boatDataSource);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the race data source for this race to a new RaceDataSource.
|
||||||
|
* Uses the boundary and legs specified by the new RaceDataSource.
|
||||||
|
* @param raceDataSource The new RaceDataSource to use.
|
||||||
|
*/
|
||||||
|
public void setRaceDataSource(RaceDataSource raceDataSource) {
|
||||||
|
super.setRaceDataSource(raceDataSource);
|
||||||
|
|
||||||
|
if (getBoatDataSource() != null) {
|
||||||
|
this.generateVisualiserBoats(this.boats, getBoatDataSource().getBoats(), raceDataSource.getParticipants(), unassignedColors);
|
||||||
|
}
|
||||||
|
|
||||||
|
useLegsList(raceDataSource.getLegs());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the boat data source for this race to a new BoatDataSource.
|
||||||
|
* Uses the marker boats specified by the new BoatDataSource.
|
||||||
|
* @param boatDataSource The new BoatDataSource to use.
|
||||||
|
*/
|
||||||
|
public void setBoatDataSource(BoatDataSource boatDataSource) {
|
||||||
|
super.setBoatDataSource(boatDataSource);
|
||||||
|
|
||||||
|
if (getRaceDataSource() != null) {
|
||||||
|
this.generateVisualiserBoats(this.boats, boatDataSource.getBoats(), getRaceDataSource().getParticipants(), unassignedColors);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the regatta data source for this race to a new RegattaDataSource.
|
||||||
|
* @param regattaDataSource The new RegattaDataSource to use.
|
||||||
|
*/
|
||||||
|
public void setRegattaDataSource(RegattaDataSource regattaDataSource) {
|
||||||
|
super.setRegattaDataSource(regattaDataSource);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* See {@link RaceState#useLegsList(List)}.
|
||||||
|
* Also initialises the {@link #legCompletionOrder} map.
|
||||||
|
* @param legs The new list of legs to use.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void useLegsList(List<Leg> legs) {
|
||||||
|
super.useLegsList(legs);
|
||||||
|
|
||||||
|
//Initialise the leg completion order map.
|
||||||
|
for (Leg leg : getLegs()) {
|
||||||
|
this.legCompletionOrder.put(leg, new ArrayList<>(this.boats.size()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a list of VisualiserBoats given a list of Boats, and a list of participating boats.
|
||||||
|
* This will add VisualiserBoats for newly participating sourceID, and remove VisualiserBoats for any participating sourceIDs that have been removed.
|
||||||
|
*
|
||||||
|
* @param existingBoats The visualiser boats that already exist in the race. This will be populated when we receive a new race.xml or boats.xml.
|
||||||
|
* @param boats The map of {@link Boat}s describing boats that are potentially in the race. Maps boat sourceID to boat.
|
||||||
|
* @param sourceIDs The list of boat sourceIDs describing which specific boats are actually participating.
|
||||||
|
* @param colors The list of unassignedColors to be used for the boats.
|
||||||
|
*/
|
||||||
|
private void generateVisualiserBoats(ObservableList<VisualiserBoat> existingBoats, Map<Integer, Boat> boats, List<Integer> sourceIDs, List<Color> colors) {
|
||||||
|
|
||||||
|
//Remove any VisualiserBoats that are no longer participating.
|
||||||
|
for (VisualiserBoat boat : new ArrayList<>(existingBoats)) {
|
||||||
|
|
||||||
|
//Boat no longer is participating.
|
||||||
|
if (!sourceIDs.contains(boat.getSourceID())) {
|
||||||
|
//Return their colors to the color list.
|
||||||
|
colors.add(boat.getColor());
|
||||||
|
|
||||||
|
//Remove boat.
|
||||||
|
existingBoats.remove(boat);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//Get source IDs of already existing boats.
|
||||||
|
List<Integer> existingBoatIDs = new ArrayList<>();
|
||||||
|
for (VisualiserBoat boat : existingBoats) {
|
||||||
|
existingBoatIDs.add(boat.getSourceID());
|
||||||
|
}
|
||||||
|
|
||||||
|
//Get source IDs of only newly participating boats.
|
||||||
|
List<Integer> newBoatIDs = new ArrayList<>(sourceIDs);
|
||||||
|
newBoatIDs.removeAll(existingBoatIDs);
|
||||||
|
|
||||||
|
//Create VisualiserBoat for newly participating boats.
|
||||||
|
for (Integer sourceID : newBoatIDs) {
|
||||||
|
|
||||||
|
if (boats.containsKey(sourceID)) {
|
||||||
|
|
||||||
|
VisualiserBoat boat = new VisualiserBoat(
|
||||||
|
boats.get(sourceID),
|
||||||
|
colors.remove(colors.size() - 1));//TODO potential bug: not enough colors for boats.
|
||||||
|
|
||||||
|
boat.setCurrentLeg(getLegs().get(0));
|
||||||
|
|
||||||
|
existingBoats.add(boat);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
setPlayerBoat();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the boat the player has been assigned to as belonging to them.
|
||||||
|
*/
|
||||||
|
private void setPlayerBoat() {
|
||||||
|
|
||||||
|
if (getPlayerBoatID() != 0) {
|
||||||
|
|
||||||
|
for (VisualiserBoat boat : getBoats()) {
|
||||||
|
|
||||||
|
if (boat.getSourceID() == getPlayerBoatID()) {
|
||||||
|
boat.setClientBoat(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise the boats in the race.
|
||||||
|
* This sets their current leg.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
protected void initialiseBoats() {
|
||||||
|
|
||||||
|
Leg startingLeg = getLegs().get(0);
|
||||||
|
|
||||||
|
for (VisualiserBoat boat : boats) {
|
||||||
|
|
||||||
|
boat.setCurrentLeg(startingLeg);
|
||||||
|
boat.setTimeAtLastMark(getRaceClock().getCurrentTime());
|
||||||
|
boat.setCurrentPosition(new GPSCoordinate(0, 0));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update position of boats in race (e.g, 5th), no position if on starting leg or DNF.
|
||||||
|
* @param boats The list of boats to update.
|
||||||
|
*/
|
||||||
|
public void updateBoatPositions(List<VisualiserBoat> boats) {
|
||||||
|
|
||||||
|
//Sort boats.
|
||||||
|
sortBoatsByPosition(boats);
|
||||||
|
|
||||||
|
//Assign new positions.
|
||||||
|
for (int i = 0; i < boats.size(); i++) {
|
||||||
|
VisualiserBoat boat = boats.get(i);
|
||||||
|
|
||||||
|
|
||||||
|
if ((boat.getStatus() == BoatStatusEnum.DNF) || (boat.getStatus() == BoatStatusEnum.PRESTART) || (boat.getCurrentLeg().getLegNumber() < 0)) {
|
||||||
|
|
||||||
|
boat.setPosition("-");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
boat.setPosition(Integer.toString(i + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts the list of boats by their position within the race.
|
||||||
|
* @param boats The list of boats in the race.
|
||||||
|
*/
|
||||||
|
private void sortBoatsByPosition(List<VisualiserBoat> boats) {
|
||||||
|
|
||||||
|
boats.sort((a, b) -> {
|
||||||
|
//Get the difference in leg numbers.
|
||||||
|
int legNumberDelta = b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber();
|
||||||
|
|
||||||
|
//If they're on the same leg, we need to compare time to finish leg.
|
||||||
|
if (legNumberDelta == 0) {
|
||||||
|
|
||||||
|
//These are potentially null until we receive our first RaceStatus containing BoatStatuses.
|
||||||
|
if ((a.getEstimatedTimeAtNextMark() != null) && (b.getEstimatedTimeAtNextMark() != null)) {
|
||||||
|
|
||||||
|
return (int) Duration.between(
|
||||||
|
b.getEstimatedTimeAtNextMark(),
|
||||||
|
a.getEstimatedTimeAtNextMark() ).toMillis();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return legNumberDelta;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the boats participating in the race.
|
||||||
|
* @return List of boats participating in the race.
|
||||||
|
*/
|
||||||
|
public ObservableList<VisualiserBoat> getBoats() {
|
||||||
|
return boats;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a boat by sourceID.
|
||||||
|
* @param sourceID The source ID the boat.
|
||||||
|
* @return The boat.
|
||||||
|
* @throws BoatNotFoundException Thrown if there is no boat with the specified sourceID.
|
||||||
|
*/
|
||||||
|
public VisualiserBoat getBoat(int sourceID) throws BoatNotFoundException {
|
||||||
|
|
||||||
|
for (VisualiserBoat boat : boats) {
|
||||||
|
|
||||||
|
if (boat.getSourceID() == sourceID) {
|
||||||
|
return boat;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new BoatNotFoundException("Boat with sourceID: " + sourceID + " was not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether or not there exists a {@link VisualiserBoat} with the given source ID.
|
||||||
|
* @param sourceID SourceID of VisualiserBoat.
|
||||||
|
* @return True if VisualiserBoat exists, false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean isVisualiserBoat(int sourceID) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
getBoat(sourceID);
|
||||||
|
return true;
|
||||||
|
} catch (BoatNotFoundException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a mark by sourceID.
|
||||||
|
* @param sourceID The source ID the mark.
|
||||||
|
* @return The mark.
|
||||||
|
* @throws MarkNotFoundException Thrown if there is no mark with the specified sourceID.
|
||||||
|
*/
|
||||||
|
public Mark getMark(int sourceID) throws MarkNotFoundException {
|
||||||
|
|
||||||
|
for (Mark mark : getMarks()) {
|
||||||
|
|
||||||
|
if (mark.getSourceID() == sourceID) {
|
||||||
|
return mark;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new MarkNotFoundException("Mark with sourceID: " + sourceID + " was not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether or not there exists a {@link Mark} with the given source ID.
|
||||||
|
* @param sourceID SourceID of mark.
|
||||||
|
* @return True if mark exists, false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean isMark(int sourceID) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
getMark(sourceID);
|
||||||
|
return true;
|
||||||
|
} catch (MarkNotFoundException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the order in which boats completed each leg. Maps the leg to a list of boats, ordered by the order in which they finished the leg.
|
||||||
|
* @return Leg completion order for each leg.
|
||||||
|
*/
|
||||||
|
public Map<Leg, List<VisualiserBoat>> getLegCompletionOrder() {
|
||||||
|
return legCompletionOrder;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the source ID of the player's boat. 0 if not assigned.
|
||||||
|
* @return Players boat source ID.
|
||||||
|
*/
|
||||||
|
public int getPlayerBoatID() {
|
||||||
|
return playerBoatID;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sets the source ID of the player's boat. 0 if not assigned.
|
||||||
|
* @param playerBoatID Players boat source ID.
|
||||||
|
*/
|
||||||
|
public void setPlayerBoatID(int playerBoatID) {
|
||||||
|
this.playerBoatID = playerBoatID;
|
||||||
|
setPlayerBoat();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in new issue