Merge branch 'Development' into story_64_sails

# Conflicts:
#	racevisionGame/src/main/java/mock/model/commandFactory/CommandFactory.java
#	racevisionGame/src/main/java/visualiser/model/ResizableRaceCanvas.java
#	racevisionGame/src/test/java/mock/model/MockBoatTest.java
main
Joseph Gardner 8 years ago
commit 1302769a50

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectCodeStyleSettingsManager">
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
</component>
</project>

@ -1,5 +1,6 @@
package mock.app; package mock.app;
import mock.model.RaceLogic;
import network.Messages.Enums.XMLMessageType; import network.Messages.Enums.XMLMessageType;
import network.Messages.LatestMessages; import network.Messages.LatestMessages;
import network.Messages.XMLMessage; import network.Messages.XMLMessage;
@ -39,6 +40,10 @@ public class ConnectionAcceptor implements Runnable {
private short boatXMLSequenceNumber; private short boatXMLSequenceNumber;
//regatta xml sequence number //regatta xml sequence number
private short regattaXMLSequenceNumber; private short regattaXMLSequenceNumber;
//controller server
private ControllerServer controllerServer;
//
private RaceLogic rl = null;
/** /**
* Connection Acceptor Constructor * Connection Acceptor Constructor
@ -61,6 +66,11 @@ public class ConnectionAcceptor implements Runnable {
return serverPort; return serverPort;
} }
public void setRace(RaceLogic rl){
this.rl = rl;
}
/** /**
* Run the Acceptor * Run the Acceptor
*/ */
@ -72,9 +82,10 @@ public class ConnectionAcceptor implements Runnable {
Socket mockSocket = serverSocket.accept(); Socket mockSocket = serverSocket.accept();
DataOutputStream outToVisualiser = new DataOutputStream(mockSocket.getOutputStream()); DataOutputStream outToVisualiser = new DataOutputStream(mockSocket.getOutputStream());
MockOutput mockOutput = new MockOutput(latestMessages, outToVisualiser); MockOutput mockOutput = new MockOutput(latestMessages, outToVisualiser);
ControllerServer controllerServer = new ControllerServer(mockSocket); this.controllerServer = new ControllerServer(mockSocket, rl);
new Thread(mockOutput).start(); new Thread(mockOutput).start();
new Thread(controllerServer).start(); new Thread(controllerServer).start();
System.out.println("I'm in connectionAcceptor");
mockOutputList.add(mockOutput); mockOutputList.add(mockOutput);
System.out.println(String.format("%d number of Visualisers Connected.", mockOutputList.size())); System.out.println(String.format("%d number of Visualisers Connected.", mockOutputList.size()));
} catch (IOException e) { } catch (IOException e) {

@ -82,6 +82,7 @@ public class Event {
* @throws InvalidRegattaDataException Thrown if the regatta xml file cannot be parsed. * @throws InvalidRegattaDataException Thrown if the regatta xml file cannot be parsed.
*/ */
public void start() throws InvalidRaceDataException, XMLReaderException, InvalidBoatDataException, InvalidRegattaDataException { public void start() throws InvalidRaceDataException, XMLReaderException, InvalidBoatDataException, InvalidRegattaDataException {
new Thread(mockOutput).start(); new Thread(mockOutput).start();
sendXMLs(); sendXMLs();
@ -94,7 +95,11 @@ public class Event {
//Create and start race. //Create and start race.
RaceLogic newRace = new RaceLogic(new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale), this.latestMessages); RaceLogic newRace = new RaceLogic(new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale), this.latestMessages);
mockOutput.setRace(newRace);
new Thread(newRace).start(); new Thread(newRace).start();
System.out.println("I'm in event");
} }
/** /**

@ -22,6 +22,14 @@ public class MockBoat extends Boat {
*/ */
private long timeSinceTackChange = 0; private long timeSinceTackChange = 0;
/**
* This stores the boats current status of rounding a mark
* 0: not started rounding
* 1: passed only first check
* 2: passed first and second check
*/
private Integer roundingStatus = 0;
/** /**
* Stores whether the boat is on autoVMG or not * Stores whether the boat is on autoVMG or not
*/ */
@ -71,6 +79,7 @@ public class MockBoat extends Boat {
//Calculate bearing. //Calculate bearing.
Bearing bearing = GPSCoordinate.calculateBearing(currentPosition, nextMarkerPosition); Bearing bearing = GPSCoordinate.calculateBearing(currentPosition, nextMarkerPosition);
return bearing; return bearing;
} }
@ -196,6 +205,59 @@ public class MockBoat extends Boat {
return distanceTravelledMeters; return distanceTravelledMeters;
} }
/**
* Check if a mark is on the port side of the boat
* @param mark mark to be passed
* @return true if mark is on port side
*/
public boolean isPortSide(Mark mark){
//if this boat is lower than the mark check which way it is facing
if(this.getCurrentPosition().getLongitude() < mark.getPosition().getLongitude()){
return this.getBearing().degrees() <= 180;
}else{
return this.getBearing().degrees() > 180;
}
}
/**
* Check if a mark is on the starboard side of the boat
* @param mark mark to be passed
* @return true if mark is on starboard side
*/
public boolean isStarboardSide(Mark mark){
//if this boat is lower than the mark check which way it is facing
if(this.getCurrentPosition().getLongitude() < mark.getPosition().getLongitude()){
return this.getBearing().degrees() >= 180;
}else{
return this.getBearing().degrees() < 180;
}
}
/**
* Used to check if this boat is between a gate
* @param gate the gate to be checked
* @return true if the boat is between two marks that make up a gate
*/
public boolean isBetweenGate(CompoundMark gate){
if ((this.isPortSide(gate.getMark1()) && this.isStarboardSide(gate.getMark2())) ||
(this.isStarboardSide(gate.getMark2()) && this.isPortSide(gate.getMark1()))){
return true;
}else{
return false;
}
}
public Integer getRoundingStatus() {
return Integer.valueOf(roundingStatus);
}
public void increaseRoundingStatus() {
this.roundingStatus++;
}
public void resetRoundingStatus() {
this.roundingStatus = 0;
}
public boolean isAutoVMG() { public boolean isAutoVMG() {
return autoVMG; return autoVMG;
} }
@ -203,4 +265,8 @@ public class MockBoat extends Boat {
public void setAutoVMG(boolean autoVMG) { public void setAutoVMG(boolean autoVMG) {
this.autoVMG = autoVMG; this.autoVMG = autoVMG;
} }
public boolean getAutoVMG(){
return autoVMG;
}
} }

@ -349,7 +349,7 @@ public class MockRace extends Race {
} }
private void newOptimalVMG(MockBoat boat) { private void newOptimalVMG(MockBoat boat) {
long tackPeriod = 15000; long tackPeriod = 1000;
if (boat.getTimeSinceTackChange() > tackPeriod) { if (boat.getTimeSinceTackChange() > tackPeriod) {
//Calculate the new VMG. //Calculate the new VMG.
@ -373,13 +373,217 @@ public class MockRace extends Race {
this.getWindDirection(), this.getWindDirection(),
this.getWindSpeed(), this.getWindSpeed(),
boat.getBearing(), boat.getBearing(),
boat.getBearing(), Bearing.fromDegrees(boat.getBearing().degrees() - 1),
boat.getBearing()); Bearing.fromDegrees(boat.getBearing().degrees() + 1));
if (vmg.getSpeed() > 0) { if (vmg.getSpeed() > 0) {
boat.setCurrentSpeed(vmg.getSpeed()); boat.setCurrentSpeed(vmg.getSpeed());
} }
} }
/**
* Calculates the upper and lower bounds that the boat may have in order to not go outside of the course.
* @param boat The boat to check.
* @return An array of bearings. The first is the lower bound, the second is the upper bound.
*/
private Bearing[] calculateBearingBounds(MockBoat boat) {
Bearing[] bearings = new Bearing[2];
Bearing lowerBearing = Bearing.fromDegrees(0.001);
Bearing upperBearing = Bearing.fromDegrees(359.999);
double lastAngle = -1;
boolean lastAngleWasGood = false;
//Check all bearings between [0, 360).
for (double angle = 0; angle < 360; angle += 1) {
//Create bearing from angle.
Bearing bearing = Bearing.fromDegrees(angle);
//Check that if it is acceptable.
boolean bearingIsGood = this.checkBearingInsideCourse(bearing, boat.getCurrentPosition());
if (lastAngle != -1) {
if (lastAngleWasGood && !bearingIsGood) {
//We have flipped over from good bearings to bad bearings. So the last good bearing is the upper bearing.
upperBearing = Bearing.fromDegrees(lastAngle);
}
if (!lastAngleWasGood && bearingIsGood) {
//We have flipped over from bad bearings to good bearings. So the current bearing is the lower bearing.
lowerBearing = Bearing.fromDegrees(angle);
}
}
lastAngle = angle;
lastAngleWasGood = bearingIsGood;
}
//TODO BUG if it can't find either upper or lower, it returns (0, 359.999). Should return (boatbearing, boatbearing+0.0001)
bearings[0] = lowerBearing;
bearings[1] = upperBearing;
return bearings;
}
/**
* Checks if a given bearing, starting at a given position, would put a boat out of the course boundaries.
* @param bearing The bearing to check.
* @param position The position to start from.
* @return True if the bearing would keep the boat in the course, false if it would take it out of the course.
*/
private boolean checkBearingInsideCourse(Bearing bearing, GPSCoordinate position) {
//Get azimuth from bearing.
Azimuth azimuth = Azimuth.fromBearing(bearing);
//Tests to see if a point in front of the boat is out of bounds.
double epsilonMeters = 50d;
GPSCoordinate testCoord = GPSCoordinate.calculateNewPosition(position, epsilonMeters, azimuth);
//If it isn't inside the boundary, calculate new bearing.
if (GPSCoordinate.isInsideBoundary(testCoord, this.shrinkBoundary)) {
return true;
} else {
return false;
}
}
/**
* Checks to be run on boats rounding marks on the port side
* @param boat the boat that is rounding a mark
* @param roundingChecks the checks to run
*/
private void boatRoundingCheckPort(MockBoat boat, List<GPSCoordinate> roundingChecks){
//boats must pass all checks in order to round a mark
switch (boat.getRoundingStatus()) {
case 0://hasn't started rounding
// System.out.println("round 0");
if (boat.isPortSide(boat.getCurrentLeg().getEndCompoundMark().getMark1()) &&
GPSCoordinate.intersects(boat.getCurrentLeg().getEndCompoundMark().getMark1().getPosition(),
roundingChecks.get(0), boat.getCurrentPosition())) {
boat.increaseRoundingStatus();
}
break;
case 1://has been parallel to the mark
// System.out.println("round 1");
if (boat.isPortSide(boat.getCurrentLeg().getEndCompoundMark().getMark1()) &&
GPSCoordinate.intersects(boat.getCurrentLeg().getEndCompoundMark().getMark1().getPosition(),
roundingChecks.get(1), boat.getCurrentPosition())) {
boat.increaseRoundingStatus();
}
break;
case 2://has traveled 180 degrees around the mark
// System.out.println("round 2");
//Move boat on to next leg.
boat.resetRoundingStatus();
Leg nextLeg = this.legs.get(boat.getCurrentLeg().getLegNumber() + 1);
boat.setCurrentLeg(nextLeg);
break;
}
}
/**
* Checks to be run on boats rounding marks on the starboard side
* @param boat the boat that is rounding a mark
* @param roundingChecks the checks to run
*/
private void boatRoundingCheckStarboard(MockBoat boat, List<GPSCoordinate> roundingChecks){
//boats must pass all checks in order to round a mark
switch (boat.getRoundingStatus()) {
case 0://hasn't started rounding
if (boat.isStarboardSide(boat.getCurrentLeg().getEndCompoundMark().getMark1()) &&
GPSCoordinate.intersects(boat.getCurrentLeg().getEndCompoundMark().getMark1().getPosition(),
roundingChecks.get(0), boat.getCurrentPosition())) {
boat.increaseRoundingStatus();
}
break;
case 1://has been parallel to the mark
if (boat.isStarboardSide(boat.getCurrentLeg().getEndCompoundMark().getMark1()) &&
GPSCoordinate.intersects(boat.getCurrentLeg().getEndCompoundMark().getMark1().getPosition(),
roundingChecks.get(1), boat.getCurrentPosition())) {
boat.increaseRoundingStatus();
}
break;
case 2://has traveled 180 degrees around the mark
//Move boat on to next leg.
boat.resetRoundingStatus();
Leg nextLeg = this.legs.get(boat.getCurrentLeg().getLegNumber() + 1);
boat.setCurrentLeg(nextLeg);
break;
}
}
/**
* Checks if a boat has finished any legs, or has pulled out of race (DNF).
* @param boat The boat to check.
* @param timeElapsed The total time, in milliseconds, that has elapsed since the race started.
*/
protected void checkPosition(MockBoat boat, long timeElapsed) {
//The distance, in nautical miles, within which the boat needs to get in order to consider that it has reached the marker.
double epsilonNauticalMiles = 250.0 / Constants.NMToMetersConversion; //250 meters.
if (boat.calculateDistanceToNextMarker() < epsilonNauticalMiles) {
//Boat is within an acceptable distance from the mark.
GPSCoordinate startDirectionLinePoint = boat.getCurrentLeg().getStartCompoundMark().getMark1Position();
//todo will need to change this for gates, so that the end point is the side of the gate needed to be rounded
GPSCoordinate endDirectionLinePoint = boat.getCurrentLeg().getEndCompoundMark().getMark1Position();
Bearing bearingOfDirectionLine = GPSCoordinate.calculateBearing(startDirectionLinePoint, endDirectionLinePoint);
//use the direction line to create three invisible points that act as crossover lines a boat must cross
//to round a mark
GPSCoordinate roundCheck1 = GPSCoordinate.calculateNewPosition(startDirectionLinePoint,
epsilonNauticalMiles, Azimuth.fromDegrees(bearingOfDirectionLine.degrees() + 90));//adding 90 so the check line is parallel
GPSCoordinate roundCheck2 = GPSCoordinate.calculateNewPosition(startDirectionLinePoint,
epsilonNauticalMiles, Azimuth.fromDegrees(bearingOfDirectionLine.degrees()));
List<GPSCoordinate> roundingChecks = new ArrayList<GPSCoordinate>(Arrays.asList(roundCheck1, roundCheck2));
switch (boat.getCurrentLeg().getEndCompoundMark().getRoundingType()) {
case SP://Not yet implemented so these gates will be rounded port side
case Port:
boatRoundingCheckPort(boat, roundingChecks);
break;
case PS://not yet implemented so these gates will be rounded starboard side
case Starboard:
boatRoundingCheckStarboard(boat, roundingChecks);
break;
}
//Check if the boat has finished or stopped racing.
if (this.isLastLeg(boat.getCurrentLeg())) {
//Boat has finished.
boat.setTimeFinished(timeElapsed);
boat.setCurrentSpeed(0);
boat.setStatus(BoatStatusEnum.FINISHED);
}
}
}
/** /**
* Returns the number of boats that are still active in the race. * Returns the number of boats that are still active in the race.
* They become inactive by either finishing or withdrawing. * They become inactive by either finishing or withdrawing.

@ -1,12 +1,20 @@
package mock.model; package mock.model;
import javafx.animation.AnimationTimer; import javafx.animation.AnimationTimer;
import mock.model.commandFactory.Command;
import mock.model.commandFactory.CommandFactory;
import mock.model.commandFactory.CompositeCommand;
import network.Messages.Enums.BoatActionEnum;
import network.Messages.Enums.BoatStatusEnum; import network.Messages.Enums.BoatStatusEnum;
import network.Messages.Enums.RaceStatusEnum; import network.Messages.Enums.RaceStatusEnum;
import network.Messages.LatestMessages; import network.Messages.LatestMessages;
import shared.model.Race; import visualiser.gameController.ControllerServer;
public class RaceLogic implements Runnable { import java.util.Observable;
import java.util.Observer;
import java.util.Stack;
public class RaceLogic implements Observer, Runnable {
/** /**
* State of current race modified by this object * State of current race modified by this object
*/ */
@ -16,6 +24,8 @@ public class RaceLogic implements Runnable {
*/ */
private RaceServer server; private RaceServer server;
private CompositeCommand commands;
/** /**
* Initialises race loop with state and server message queue * Initialises race loop with state and server message queue
* @param race state of race to modify * @param race state of race to modify
@ -24,6 +34,7 @@ public class RaceLogic implements Runnable {
public RaceLogic(MockRace race, LatestMessages messages) { public RaceLogic(MockRace race, LatestMessages messages) {
this.race = race; this.race = race;
this.server = new RaceServer(race, messages); this.server = new RaceServer(race, messages);
this.commands = new CompositeCommand();
} }
/** /**
@ -123,7 +134,7 @@ public class RaceLogic implements Runnable {
//If it is still racing, update its position. //If it is still racing, update its position.
if (boat.getStatus() == BoatStatusEnum.RACING) { if (boat.getStatus() == BoatStatusEnum.RACING) {
commands.execute();
race.updatePosition(boat, framePeriod, race.getRaceClock().getDurationMilli()); race.updatePosition(boat, framePeriod, race.getRaceClock().getDurationMilli());
} }
@ -173,4 +184,13 @@ public class RaceLogic implements Runnable {
iters++; iters++;
} }
}; };
@Override
public void update(Observable o, Object arg) {
ControllerServer server = (ControllerServer)o;
BoatActionEnum action = server.getAction();
MockBoat boat = race.getBoats().get(0);
commands.addCommand(CommandFactory.createCommand(race, boat, action));
}
} }

@ -0,0 +1,555 @@
//package mock.model;
//
//import javafx.animation.AnimationTimer;
//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 network.Utils.AC35UnitConverter;
//import shared.dataInput.BoatDataSource;
//import shared.dataInput.RaceDataSource;
//import shared.dataInput.RegattaDataSource;
//import shared.model.*;
//
//import java.time.ZonedDateTime;
//import java.time.temporal.ChronoUnit;
//import java.util.ArrayList;
//import java.util.Iterator;
//import java.util.List;
//import java.util.Map;
//
//import static java.lang.Math.cos;
//
///**
// * Unused class, copy of MockRace so methods can be deleted once they are moved (more of a checklist)
// */
//public class SplitTODO {
//
//
// /**
// * Represents a yacht race.
// * Has a course, boats, boundaries, etc...
// * Is responsible for simulating the race, and sending messages to a MockOutput instance.
// */
// public class MockRace extends Race {
//
// /**
// * Constructs a race object with a given RaceDataSource, BoatDataSource, and RegattaDataSource and sends events to the given mockOutput.
// * @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 polars The polars table to be used for boat simulation.
// * @param timeScale The timeScale for the race. See {@link Constants#RaceTimeScale}.
// */
// public MockRace(BoatDataSource boatDataSource, RaceDataSource raceDataSource, RegattaDataSource regattaDataSource, LatestMessages latestMessages, Polars polars, int timeScale) {
//
// super(boatDataSource, raceDataSource, regattaDataSource, latestMessages);
//
// this.scaleFactor = timeScale;
// }
//
//
//
// /**
// * Parse the compound marker boats through mock output.
// */
// private void parseMarks() {
// for (CompoundMark compoundMark : this.compoundMarks) {
//
// //Get the individual marks from the compound mark.
// Mark mark1 = compoundMark.getMark1();
// Mark mark2 = compoundMark.getMark2();
//
// //If they aren't null, parse them (some compound marks only have one mark).
// if (mark1 != null) {
// this.parseIndividualMark(mark1);
// }
//
// if (mark2 != null) {
// this.parseIndividualMark(mark2);
// }
//
// }
// }
//
// /**
// * Parses an individual marker boat, and sends it to mockOutput.
// * @param mark The marker boat to parse.
// */
// private void parseIndividualMark(Mark mark) {
//
// //Create message.
// BoatLocation boatLocation = new BoatLocation(
// mark.getSourceID(),
// mark.getPosition().getLatitude(),
// mark.getPosition().getLongitude(),
// this.boatLocationSequenceNumber,
// 0, 0,
// this.raceClock.getCurrentTimeMilli());
//
// //Iterates the sequence number.
// this.boatLocationSequenceNumber++;
//
// this.latestMessages.setBoatLocation(boatLocation);
//
//
// }
//
// /**
// * Parse the boats in the race, and send it to mockOutput.
// */
// private void parseBoatLocations() {
//
// //Parse each boat.
// for (MockBoat boat : this.boats) {
//
// this.parseIndividualBoatLocation(boat);
//
// }
//
// }
//
// /**
// * Parses an individual boat, and sends it to mockOutput.
// * @param boat The boat to parse.
// */
// private void parseIndividualBoatLocation(MockBoat boat) {
//
// BoatLocation boatLocation = new BoatLocation(
// boat.getSourceID(),
// boat.getCurrentPosition().getLatitude(),
// boat.getCurrentPosition().getLongitude(),
// this.boatLocationSequenceNumber,
// boat.getBearing().degrees(),
// boat.getCurrentSpeed(),
// this.raceClock.getCurrentTimeMilli());
//
// //Iterates the sequence number.
// this.boatLocationSequenceNumber++;
//
// this.latestMessages.setBoatLocation(boatLocation);
//
// }
//
//
// /**
// * Updates the race time to a specified value, in milliseconds since the unix epoch.
// * @param currentTime Milliseconds since unix epoch.
// */
// private void updateRaceTime(long currentTime) {
// this.raceClock.setUTCTime(currentTime);
// }
//
//
// /**
// * Updates the race status enumeration based on the current time.
// */
// private void updateRaceStatusEnum() {
//
// //The millisecond duration of the race. Negative means it hasn't started, so we flip sign.
// long timeToStart = - this.raceClock.getDurationMilli();
//
//
// if (timeToStart > Constants.RacePreStartTime) {
// //Time > 3 minutes is the prestart period.
// this.setRaceStatusEnum(RaceStatusEnum.PRESTART);
//
// } else if ((timeToStart <= Constants.RacePreStartTime) && (timeToStart >= Constants.RacePreparatoryTime)) {
// //Time between [1, 3] minutes is the warning period.
// this.setRaceStatusEnum(RaceStatusEnum.WARNING);
//
// } else if ((timeToStart <= Constants.RacePreparatoryTime) && (timeToStart > 0)) {
// //Time between (0, 1] minutes is the preparatory period.
// this.setRaceStatusEnum(RaceStatusEnum.PREPARATORY);
//
// } else {
// //Otherwise, the race has started!
// this.setRaceStatusEnum(RaceStatusEnum.STARTED);
//
// }
//
//
// }
//
// /**
// * Parses the race status, and sends it to mockOutput.
// */
// private void parseRaceStatus() {
//
// //A race status message contains a list of boat statuses.
// List<BoatStatus> boatStatuses = new ArrayList<>();
//
// //Add each boat status to the status list.
// for (MockBoat boat : this.boats) {
//
// BoatStatus boatStatus = new BoatStatus(
// boat.getSourceID(),
// boat.getStatus(),
// boat.getCurrentLeg().getLegNumber(),
// boat.getEstimatedTimeAtNextMark().toInstant().toEpochMilli() );
//
// boatStatuses.add(boatStatus);
// }
//
//
// //Convert wind direction and speed to ints. //TODO this conversion should be done inside the racestatus class.
// int windDirectionInt = AC35UnitConverter.encodeHeading(this.getWindDirection().degrees());
// int windSpeedInt = (int) (this.getWindSpeed() * Constants.KnotsToMMPerSecond);
//
// //Create race status object, and send it.
// RaceStatus raceStatus = new RaceStatus(
// System.currentTimeMillis(),
// this.raceId,
// this.getRaceStatusEnum().getValue(),
// this.raceClock.getStartingTimeMilli(),
// windDirectionInt,
// windSpeedInt,
// this.getRaceType().getValue(),
// boatStatuses);
//
//
// this.latestMessages.setRaceStatus(raceStatus);
//
//
// }
//
//
// /**
// * Sets the status of all boats in the race to RACING.
// */
// private void setBoatsStatusToRacing() {
//
// for (MockBoat boat : this.boats) {
// boat.setStatus(BoatStatusEnum.RACING);
// }
// }
//
//
// /**
// * Sets the estimated time at next mark for each boat to a specified time. This is used during the countdown timer to provide this value to boat before the race starts.
// * @param time The time to provide to each boat.
// */
// private void setBoatsTimeNextMark(ZonedDateTime time) {
//
// for (MockBoat boat : this.boats) {
// boat.setEstimatedTimeAtNextMark(time);
// }
// }
//
//
// /**
// * Countdown timer until race starts.
// */
// protected AnimationTimer countdownTimer = new AnimationTimer() {
//
//
// long currentTime = System.currentTimeMillis();
//
// @Override
// public void handle(long arg0) {
//
// //Update race time.
// updateRaceTime(currentTime);
//
// //Update the race status based on the current time.
// updateRaceStatusEnum();
//
// //Provide boat's with an estimated time at next mark until the race starts.
// setBoatsTimeNextMark(raceClock.getCurrentTime());
//
// //Parse the boat locations.
// parseBoatLocations();
//
// //Parse the marks.
// parseMarks();
//
// // Change wind direction
// changeWindDirection();
//
// //Parse the race status.
// parseRaceStatus();
//
//
// if (getRaceStatusEnum() == RaceStatusEnum.STARTED) {
// setBoatsStatusToRacing();
// raceTimer.start();
// this.stop();
// }
//
// //Update the animations timer's time.
// currentTime = System.currentTimeMillis();
// }
// };
//
//
// /**
// * Timer that runs for the duration of the race, until all boats finish.
// */
// private AnimationTimer raceTimer = new AnimationTimer() {
//
// /**
// * Start time of loop, in milliseconds.
// */
// long timeRaceStarted = System.currentTimeMillis();
//
// /**
// * Current time during a loop iteration.
// */
// long currentTime = System.currentTimeMillis();
//
// /**
// * The time of the previous frame, in milliseconds.
// */
// long lastFrameTime = timeRaceStarted;
//
// @Override
// public void handle(long arg0) {
//
// //Get the current time.
// currentTime = System.currentTimeMillis();
//
// //Update race time.
// updateRaceTime(currentTime);
//
//
// //As long as there is at least one boat racing, we still simulate the race.
// if (getNumberOfActiveBoats() != 0) {
//
// //Get the time period of this frame.
// long framePeriod = currentTime - lastFrameTime;
//
// //For each boat, we update its position, and generate a BoatLocationMessage.
// for (MockBoat boat : boats) {
//
// //If it is still racing, update its position.
// if (boat.getStatus() == BoatStatusEnum.RACING) {
//
// updatePosition(boat, framePeriod, raceClock.getDurationMilli());
//
// }
//
// }
//
// } else {
// //Otherwise, the race is over!
// raceFinished.start();
// setRaceStatusEnum(RaceStatusEnum.FINISHED);
// this.stop();
// }
//
// if (getNumberOfActiveBoats() != 0) {
// // Change wind direction
// changeWindDirection();
//
// //Parse the boat locations.
// parseBoatLocations();
//
// //Parse the marks.
// parseMarks();
//
// //Parse the race status.
// parseRaceStatus();
//
//
// //Update the last frame time.
// this.lastFrameTime = currentTime;
// }
// }
// };
//
// /**
// * Broadcast that the race has finished.
// */
// protected AnimationTimer raceFinished = new AnimationTimer(){
// int iters = 0;
// @Override
// public void handle(long now) {
//
// parseRaceStatus();
//
// if (iters > 500) {
// stop();
// }
// iters++;
// }
// };
//
//
// /**
// * Calculates a boat's VMG.
// * @param boat The boat to calculate VMG for.
// * @return VMG for the specified boat.
// */
// private VMG calculateVMG(MockBoat boat) {
//
//
// //Find the VMG inside these bounds.
// VMG bestVMG = boat.getPolars().calculateVMG(this.getWindDirection(), this.getWindSpeed(), boat.calculateBearingToNextMarker(), Bearing.fromDegrees(0d), Bearing.fromDegrees(359.99999d));
//
//
// return bestVMG;
//
// }
//
//
// /**
// * Determines whether or not a given VMG improves the velocity of a boat, if it were currently using currentVMG.
// * @param currentVMG The current VMG of the boat.
// * @param potentialVMG The new VMG to test.
// * @param bearingToDestination The bearing between the boat and its destination.
// * @return True if the new VMG is improves velocity, false otherwise.
// */
// private boolean improvesVelocity(VMG currentVMG, VMG potentialVMG, Bearing bearingToDestination) {
//
// //Calculates the angle between the boat and its destination.
// Angle angleBetweenDestAndHeading = Angle.fromDegrees(currentVMG.getBearing().degrees() - bearingToDestination.degrees());
//
// //Calculates the angle between the new VMG and the boat's destination.
// Angle angleBetweenDestAndNewVMG = Angle.fromDegrees(potentialVMG.getBearing().degrees() - bearingToDestination.degrees());
//
//
// //Calculate the boat's current velocity.
// double currentVelocity = Math.cos(angleBetweenDestAndHeading.radians()) * currentVMG.getSpeed();
//
// //Calculate the potential velocity with the new VMG.
// double vmgVelocity = Math.cos(angleBetweenDestAndNewVMG.radians()) * potentialVMG.getSpeed();
//
// //Return whether or not the new VMG gives better velocity.
// return vmgVelocity > currentVelocity;
//
// }
//
// /**
// * Determines whether or not a given VMG improves the velocity of a boat.
// * @param boat The boat to test.
// * @param vmg The new VMG to test.
// * @return True if the new VMG is improves velocity, false otherwise.
// */
// private boolean improvesVelocity(MockBoat boat, VMG vmg) {
//
// //Get the boats "current" VMG.
// VMG boatVMG = new VMG(boat.getCurrentSpeed(), boat.getBearing());
//
// //Check if the new VMG is better than the boat's current VMG.
// return this.improvesVelocity(boatVMG, vmg, boat.calculateBearingToNextMarker());
//
// }
//
//
// /**
// * Calculates the distance a boat has travelled and updates its current position according to this value.
// *
// * @param boat The boat to be updated.
// * @param updatePeriodMilliseconds The time, in milliseconds, since the last update.
// * @param totalElapsedMilliseconds The total number of milliseconds that have elapsed since the start of the race.
// */
// protected void updatePosition(MockBoat boat, long updatePeriodMilliseconds, long totalElapsedMilliseconds) {
//
// //Checks if the current boat has finished the race or not.
// boolean finish = this.isLastLeg(boat.getCurrentLeg());
//
// if (!finish) {
//
//
// //Calculates the distance travelled, in meters, in the current timeslice.
// double distanceTravelledMeters = boat.calculateMetersTravelled(updatePeriodMilliseconds);
//
// //Scale it.
// distanceTravelledMeters = distanceTravelledMeters * this.scaleFactor;
//
//
// //Move the boat forwards that many meters, and advances its time counters by enough milliseconds.
// boat.moveForwards(distanceTravelledMeters, updatePeriodMilliseconds * this.scaleFactor);
//
// long tackPeriod = 15000;
// if (boat.getTimeSinceTackChange() > tackPeriod) {
// //Calculate the new VMG.
// VMG newVMG = this.calculateVMG(boat);
//
//
// //If the new vmg improves velocity, use it.
// if (improvesVelocity(boat, newVMG)) {
// boat.setVMG(newVMG);
//
// }
// }
//
// this.updateEstimatedTime(boat);
//
//
// //Check the boats position (update leg and stuff).
// this.checkPosition(boat, totalElapsedMilliseconds);
//
// }
//
// }
//
//
// /**
// * Checks if a boat has finished any legs, or has pulled out of race (DNF).
// * @param boat The boat to check.
// * @param timeElapsed The total time, in milliseconds, that has elapsed since the race started.
// */
// protected void checkPosition(MockBoat boat, long timeElapsed) {
//
// //The distance, in nautical miles, within which the boat needs to get in order to consider that it has reached the marker.
// double epsilonNauticalMiles = 100.0 / Constants.NMToMetersConversion; //100 meters. TODO should be more like 5-10.
//
// if (boat.calculateDistanceToNextMarker() < epsilonNauticalMiles) {
// //Boat has reached its target marker, and has moved on to a new leg.
//
//
//
// //Calculate how much the boat overshot the marker by.
// double overshootMeters = boat.calculateDistanceToNextMarker();
//
//
// //Move boat on to next leg.
// Leg nextLeg = this.legs.get(boat.getCurrentLeg().getLegNumber() + 1);
// boat.setCurrentLeg(nextLeg);
//
// //Add overshoot distance into the distance travelled for the next leg.
// boat.setDistanceTravelledInLeg(overshootMeters);
//
// //Setting a high value for this allows the boat to immediately do a large turn, as it needs to in order to get to the next mark.
// boat.setTimeSinceTackChange(999999);
//
//
// //Check if the boat has finished or stopped racing.
//
// if (this.isLastLeg(boat.getCurrentLeg())) {
// //Boat has finished.
// boat.setTimeFinished(timeElapsed);
// boat.setCurrentSpeed(0);
// boat.setStatus(BoatStatusEnum.FINISHED);
//
// }
//
// }
//
// }
//
// /**
// * Updates the boat's estimated time to next mark if positive
// * @param boat to estimate time given its velocity
// */
// private void updateEstimatedTime(MockBoat boat) {
//
// double velocityToMark = boat.getCurrentSpeed() * cos(boat.getBearing().radians() - boat.calculateBearingToNextMarker().radians()) / Constants.KnotsToMMPerSecond;
//
// if (velocityToMark > 0) {
//
// //Calculate milliseconds until boat reaches mark.
// long timeFromNow = (long) (1000 * boat.calculateDistanceToNextMarker() / velocityToMark);
//
// //Calculate time at which it will reach mark.
// ZonedDateTime timeAtMark = this.raceClock.getCurrentTime().plus(timeFromNow, ChronoUnit.MILLIS);
// boat.setEstimatedTimeAtNextMark(timeAtMark);
// }
//
// }
// }
//}

@ -0,0 +1,24 @@
package mock.model.commandFactory;
import java.util.ArrayDeque;
import java.util.Queue;
/**
* Wraps multiple commands into a composite to execute queued commands during a frame.
*/
public class CompositeCommand implements Command {
private Queue<Command> commands;
public CompositeCommand() {
this.commands = new ArrayDeque<>();
}
public void addCommand(Command command) {
commands.add(command);
}
@Override
public void execute() {
while(!commands.isEmpty()) commands.remove().execute();
}
}

@ -2,6 +2,8 @@ package mock.model.commandFactory;
import mock.model.MockBoat; import mock.model.MockBoat;
import mock.model.MockRace; import mock.model.MockRace;
import mock.model.VMG;
import shared.model.Bearing;
/** /**
* Created by David on 2/08/2017. * Created by David on 2/08/2017.
@ -18,15 +20,30 @@ public class TackGybeCommand implements Command {
//The refactoring of MockRace will require changes to be made //The refactoring of MockRace will require changes to be made
@Override @Override
public void execute() { public void execute() {
/*VMG newVMG = boat.getPolars().calculateVMG( /*if(boat.getBearing().degrees()>180){
race.getWindDirection(), boat.setBearing(Bearing.fromDegrees(360 - race.getWindDirection().degrees()));
race.getWindSpeed(), } else {
boat.calculateBearingToNextMarker(), boat.setBearing(Bearing.fromDegrees(race.getWindDirection().degrees()));
Bearing.fromDegrees(0d),
Bearing.fromDegrees(359.99999d));
VMG boatVMG = new VMG(boat.getCurrentSpeed(), boat.getBearing());
if(race.improvesVelocity(boatVMG, newVMG, boat.calculateBearingToNextMarker())){
boat.setVMG(newVMG);
}*/ }*/
/*double angle = Math.max(race.getWindDirection().degrees(), boat.getBearing().degrees()) - Math.min(race.getWindDirection().degrees(), boat.getBearing().degrees());
boat.setBearing(Bearing.fromDegrees(angle));*/
double boatAngle = boat.getBearing().degrees();
double windAngle =race.getWindDirection().degrees();
double differenceAngle = calcDistance(boatAngle, windAngle);
double angleA = windAngle + differenceAngle;
double angleB = windAngle - differenceAngle;
if(angleA % 360 == boatAngle){
boat.setBearing(Bearing.fromDegrees(angleB));
} else {
boat.setBearing(Bearing.fromDegrees(angleA));
}
} }
public double calcDistance(double degreeA, double degreeB){
double phi = Math.abs(degreeB - degreeA) % 360;
double distance = phi > 180 ? 360 - phi : phi;
return distance;
}
} }

@ -18,12 +18,12 @@ public class VMGCommand implements Command {
//The refactoring of MockRace will require changes to be made //The refactoring of MockRace will require changes to be made
@Override @Override
public void execute() { public void execute() {
/*VMG newVMG = boat.getPolars().calculateVMG( if (boat.getAutoVMG()){
race.getWindDirection(), boat.setAutoVMG(false);
race.getWindSpeed(), System.out.println("Auto VMG off!");
boat.calculateBearingToNextMarker(), } else {
Bearing.fromDegrees(0d), boat.setAutoVMG(true);
Bearing.fromDegrees(359.99999d)); System.out.println("Auto VMG on!");
boat.setVMG(newVMG);*/ }
} }
} }

@ -0,0 +1,33 @@
package mock.model.commandFactory;
import mock.model.MockBoat;
import mock.model.MockRace;
import shared.model.Bearing;
/**
* Created by connortaylorbrown on 4/08/17.
*/
public class WindCommand implements Command {
private MockRace race;
private MockBoat boat;
private int direction;
public WindCommand(MockRace race, MockBoat boat, boolean upwind) {
this.race = race;
this.boat = boat;
this.direction = upwind? -1 : 1;
}
@Override
public void execute() {
double wind = race.getWindDirection().degrees();
double heading = boat.getBearing().degrees();
double offset = 3.0;
offset *= direction;
if(wind - heading < 0) offset *= -1;
boat.setBearing(Bearing.fromDegrees(heading + offset));
}
}

@ -5,6 +5,7 @@ import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap; import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import shared.enums.RoundingType;
import shared.enums.XMLFileType; import shared.enums.XMLFileType;
import shared.exceptions.InvalidRaceDataException; import shared.exceptions.InvalidRaceDataException;
import shared.exceptions.XMLReaderException; import shared.exceptions.XMLReaderException;
@ -316,6 +317,8 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
return element.getAttribute("Name"); return element.getAttribute("Name");
} }
private String getCompoundMarkRounding(Element element){return element.getAttribute("Rounding");}
/** /**
* Populates list of legs given CompoundMarkSequence element and referenced CompoundMark elements. * Populates list of legs given CompoundMarkSequence element and referenced CompoundMark elements.
@ -334,12 +337,19 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
//Gets the ID number of this corner element. //Gets the ID number of this corner element.
int cornerID = getCompoundMarkID(cornerElement); int cornerID = getCompoundMarkID(cornerElement);
//gets the Rounding of this corner element
String cornerRounding = getCompoundMarkRounding(cornerElement);
//Gets the CompoundMark associated with this corner. //Gets the CompoundMark associated with this corner.
CompoundMark lastCompoundMark = this.compoundMarkMap.get(cornerID); CompoundMark lastCompoundMark = this.compoundMarkMap.get(cornerID);
//The name of the leg is the name of the first compoundMark in the leg. //The name of the leg is the name of the first compoundMark in the leg.
String legName = lastCompoundMark.getName(); 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 each following corner, create a leg between cornerN and cornerN+1.
for(int i = 1; i < corners.getLength(); i++) { for(int i = 1; i < corners.getLength(); i++) {
@ -349,9 +359,15 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
//Gets the ID number of this corner element. //Gets the ID number of this corner element.
cornerID = getCompoundMarkID(cornerElement); cornerID = getCompoundMarkID(cornerElement);
//gets the Rounding of this corner element
cornerRounding = getCompoundMarkRounding(cornerElement);
//Gets the CompoundMark associated with this corner. //Gets the CompoundMark associated with this corner.
CompoundMark currentCompoundMark = this.compoundMarkMap.get(cornerID); CompoundMark currentCompoundMark = this.compoundMarkMap.get(cornerID);
//Sets the rounding type of this compound mark
currentCompoundMark.setRoundingType(RoundingType.valueOf(cornerRounding));
//Create a leg from these two adjacent compound marks. //Create a leg from these two adjacent compound marks.
Leg leg = new Leg(legName, lastCompoundMark, currentCompoundMark, i - 1); Leg leg = new Leg(legName, lastCompoundMark, currentCompoundMark, i - 1);
legs.add(leg); legs.add(leg);

@ -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;
}
}
}

@ -1,6 +1,8 @@
package shared.model; package shared.model;
import shared.enums.RoundingType;
/** /**
* Represents a compound mark - that is, either one or two individual marks which form a single compound mark. * Represents a compound mark - that is, either one or two individual marks which form a single compound mark.
*/ */
@ -31,6 +33,11 @@ public class CompoundMark {
*/ */
private GPSCoordinate averageGPSCoordinate; private GPSCoordinate averageGPSCoordinate;
/**
* The side that the mark must be rounded on
*/
private RoundingType roundingType;
/** /**
* Constructs a compound mark from a single mark. * Constructs a compound mark from a single mark.
@ -141,4 +148,107 @@ public class CompoundMark {
return averageCoordinate; return averageCoordinate;
} }
/**
* 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;
}
}
} }

@ -142,7 +142,7 @@ public class GPSCoordinate {
* @param coordinate The coordinate to test. * @param coordinate The coordinate to test.
* @return true if a line from the point intersects the two boundary points * @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 boundaryALat = boundaryA.getLatitude();
double boundaryALon = boundaryA.getLongitude(); double boundaryALon = boundaryA.getLongitude();
double boundaryBLat = boundaryB.getLatitude(); double boundaryBLat = boundaryB.getLatitude();

@ -22,7 +22,6 @@ public class Mark {
private GPSCoordinate position; private GPSCoordinate position;
/** /**
* Constructs a mark with a given source ID, name, and position. * Constructs a mark with a given source ID, name, and position.
* @param sourceID The source ID of the mark. * @param sourceID The source ID of the mark.

@ -241,7 +241,7 @@ public abstract class Race {
* @param windBearing New wind bearing. * @param windBearing New wind bearing.
* @param windSpeedKnots New wind speed, in knots. * @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); Wind wind = new Wind(windBearing, windSpeedKnots);
setWind(wind); setWind(wind);
} }
@ -250,7 +250,7 @@ public abstract class Race {
* Updates the race to have a specified wind (bearing and speed). * Updates the race to have a specified wind (bearing and speed).
* @param wind New wind. * @param wind New wind.
*/ */
protected void setWind(Wind wind) { public void setWind(Wind wind) {
this.raceWind.setValue(wind); this.raceWind.setValue(wind);
} }
@ -332,6 +332,13 @@ public abstract class Race {
return lastFps; return lastFps;
} }
/**
* Returns the legs of this race
* @return list of legs
*/
public List<Leg> getLegs() {
return legs;
}
/** /**
* Increments the FPS counter, and adds timePeriod milliseconds to our FPS reset timer. * Increments the FPS counter, and adds timePeriod milliseconds to our FPS reset timer.

@ -1,5 +1,8 @@
package visualiser.gameController; package visualiser.gameController;
import mock.model.RaceLogic;
import mock.model.commandFactory.Command;
import mock.model.commandFactory.CommandFactory;
import network.BinaryMessageDecoder; import network.BinaryMessageDecoder;
import network.MessageDecoders.BoatActionDecoder; import network.MessageDecoders.BoatActionDecoder;
import network.Messages.Enums.BoatActionEnum; import network.Messages.Enums.BoatActionEnum;
@ -7,11 +10,12 @@ import network.Messages.Enums.BoatActionEnum;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.util.Observable;
/** /**
* Service for dispatching key press data to race from client * Service for dispatching key press data to race from client
*/ */
public class ControllerServer implements Runnable { public class ControllerServer extends Observable implements Runnable {
/** /**
* Socket to client * Socket to client
*/ */
@ -20,13 +24,23 @@ public class ControllerServer implements Runnable {
* Wrapper for input from client * Wrapper for input from client
*/ */
private DataInputStream inputStream; private DataInputStream inputStream;
/**
* Last received boat action
*/
private BoatActionEnum action;
/**
*
*/
private RaceLogic rc;
/** /**
* Initialise server-side controller with live client socket * Initialise server-side controller with live client socket
* @param socket to client * @param socket to client
*/ */
public ControllerServer(Socket socket) { public ControllerServer(Socket socket, RaceLogic rc) {
this.socket = socket; this.socket = socket;
this.rc = rc;
this.addObserver(rc);
try { try {
this.inputStream = new DataInputStream(this.socket.getInputStream()); this.inputStream = new DataInputStream(this.socket.getInputStream());
} catch (IOException e) { } catch (IOException e) {
@ -34,6 +48,10 @@ public class ControllerServer implements Runnable {
} }
} }
public BoatActionEnum getAction() {
return action;
}
/** /**
* Wait for controller key input from client and loop. * Wait for controller key input from client and loop.
*/ */
@ -46,8 +64,11 @@ public class ControllerServer implements Runnable {
inputStream.read(message); inputStream.read(message);
BinaryMessageDecoder encodedMessage = new BinaryMessageDecoder(message); BinaryMessageDecoder encodedMessage = new BinaryMessageDecoder(message);
BoatActionDecoder boatActionDecoder = new BoatActionDecoder(encodedMessage.getMessageBody()); BoatActionDecoder boatActionDecoder = new BoatActionDecoder(encodedMessage.getMessageBody());
BoatActionEnum decodedMessage = boatActionDecoder.getBoatAction(); action = boatActionDecoder.getBoatAction();
System.out.println("Received key: " + decodedMessage);
// Notify observers of most recent action
this.notifyObservers();
this.setChanged();
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();

@ -27,8 +27,8 @@ public class KeyFactory {
keyState.put("SPACE", new VMGKey("VMG")); keyState.put("SPACE", new VMGKey("VMG"));
keyState.put("SHIFT", new SailsToggleKey("Toggle Sails")); keyState.put("SHIFT", new SailsToggleKey("Toggle Sails"));
keyState.put("ENTER", new TackGybeKey("Tack/Gybe")); keyState.put("ENTER", new TackGybeKey("Tack/Gybe"));
keyState.put("PAGE_UP", new UpWindKey("Upwind")); keyState.put("UP", new UpWindKey("Upwind"));
keyState.put("PAGE_DOWN", new DownWindKey("Downwind")); keyState.put("DOWN", new DownWindKey("Downwind"));
} }
/** /**

@ -1,14 +1,14 @@
package visualiser.model; package visualiser.model;
import javafx.scene.image.Image; import javafx.scene.image.Image;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.scene.paint.Paint; import javafx.scene.paint.Paint;
import javafx.scene.transform.Rotate; import javafx.scene.transform.Rotate;
import network.Messages.Enums.BoatStatusEnum; import network.Messages.Enums.BoatStatusEnum;
import shared.dataInput.RaceDataSource; import shared.dataInput.RaceDataSource;
import shared.model.GPSCoordinate; import shared.enums.RoundingType;
import shared.model.Mark; import shared.model.*;
import shared.model.RaceClock;
import java.util.List; import java.util.List;
@ -466,10 +466,148 @@ public class ResizableRaceCanvas extends ResizableCanvas {
clear(); // clear the previous canvas clear(); // clear the previous canvas
drawBoundary(); drawBoundary();
//Guiding Line
drawRaceLine();
//Boats.
drawBoats(); drawBoats();
drawMarks(); drawMarks();
} }
/**
* draws a transparent line around the course that shows the paths boats must travel
*/
public void drawRaceLine(){
List<Leg> legs = this.visualiserRace.getLegs();
GPSCoordinate legStartPoint = legs.get(0).getStartCompoundMark().getAverageGPSCoordinate();
GPSCoordinate nextStartPoint;
for (int i = 0; i < legs.size() -1; i++) {
nextStartPoint = drawLineRounding(legs, i, legStartPoint);
legStartPoint = nextStartPoint;
}
}
/**
* Draws a line around a course that shows where boats need to go. This method
* draws the line leg by leg
* @param legs the legs of a race
* @param index the index of the current leg to use
* @return the end point of the current leg that has been drawn
*/
private GPSCoordinate drawLineRounding(List<Leg> legs, int index, GPSCoordinate legStartPoint){
GPSCoordinate startDirectionLinePoint;
GPSCoordinate endDirectionLinePoint;
Bearing bearingOfDirectionLine;
GPSCoordinate startNextDirectionLinePoint;
GPSCoordinate endNextDirectionLinePoint;
Bearing bearingOfNextDirectionLine;
//finds the direction of the current leg as a bearing
startDirectionLinePoint = legStartPoint;
GPSCoordinate tempEndDirectionLinePoint = legs.get(index).getEndCompoundMark().getAverageGPSCoordinate();
bearingOfDirectionLine = GPSCoordinate.calculateBearing(startDirectionLinePoint, tempEndDirectionLinePoint);
//after finding the initial bearing pick the mark used for rounding
endDirectionLinePoint = legs.get(index).getEndCompoundMark().getMarkForRounding(bearingOfDirectionLine).getPosition();
bearingOfDirectionLine = GPSCoordinate.calculateBearing(startDirectionLinePoint, endDirectionLinePoint);
//finds the direction of the next leg as a bearing
if (index < legs.size() -2){ // not last leg
startNextDirectionLinePoint = legs.get(index + 1).getStartCompoundMark().getMark1Position();
endNextDirectionLinePoint = legs.get(index + 1).getEndCompoundMark().getMark1Position();
bearingOfNextDirectionLine = GPSCoordinate.calculateBearing(startNextDirectionLinePoint, endNextDirectionLinePoint);
double degreesToAdd;
//find which side is need to be used
if (legs.get(index).getEndCompoundMark().getRoundingType() == RoundingType.Port ||
legs.get(index).getEndCompoundMark().getRoundingType() == RoundingType.SP){
degreesToAdd = 90;
}else{
degreesToAdd = -90;
}
//use the direction line to find a point parallel to it by the mark
GPSCoordinate pointToStartCurve = GPSCoordinate.calculateNewPosition(endDirectionLinePoint,
100, Azimuth.fromDegrees(bearingOfDirectionLine.degrees()+degreesToAdd));
//use the direction line to find a point to curve too
GPSCoordinate pointToEndCurve = GPSCoordinate.calculateNewPosition(endDirectionLinePoint,
100, Azimuth.fromDegrees(bearingOfNextDirectionLine.degrees()+degreesToAdd));
//use the curve points to find the two control points for the bezier curve
GPSCoordinate controlPoint;
GPSCoordinate controlPoint2;
Bearing bearingOfCurveLine = GPSCoordinate.calculateBearing(pointToStartCurve, pointToEndCurve);
if ((bearingOfDirectionLine.degrees() - bearingOfNextDirectionLine.degrees() +360)%360< 145){
//small turn
controlPoint = GPSCoordinate.calculateNewPosition(pointToStartCurve,
50, Azimuth.fromDegrees(bearingOfCurveLine.degrees()+(degreesToAdd/2)));
controlPoint2 = controlPoint;
}else{
//large turn
controlPoint = GPSCoordinate.calculateNewPosition(pointToStartCurve,
150, Azimuth.fromDegrees(bearingOfCurveLine.degrees()+degreesToAdd));
controlPoint2 = GPSCoordinate.calculateNewPosition(pointToEndCurve,
150, Azimuth.fromDegrees(bearingOfCurveLine.degrees()+degreesToAdd));
}
//change all gps into graph coordinate
GraphCoordinate startPath = this.map.convertGPS(startDirectionLinePoint);
GraphCoordinate curvePoint = this.map.convertGPS(pointToStartCurve);
GraphCoordinate curvePointEnd = this.map.convertGPS(pointToEndCurve);
GraphCoordinate c1 = this.map.convertGPS(controlPoint);
GraphCoordinate c2 = this.map.convertGPS(controlPoint2);
gc.setLineWidth(2);
gc.setStroke(Color.MEDIUMAQUAMARINE);
gc.beginPath();
gc.moveTo(startPath.getX(), startPath.getY());
gc.lineTo(curvePoint.getX(), curvePoint.getY());
drawArrowHead(startDirectionLinePoint, pointToStartCurve);
gc.bezierCurveTo(c1.getX(), c1.getY(), c2.getX(), c2.getY(), curvePointEnd.getX(), curvePointEnd.getY());
gc.stroke();
gc.closePath();
gc.save();
return pointToEndCurve;
}else{//last leg so no curve
GraphCoordinate startPath = this.map.convertGPS(legStartPoint);
GraphCoordinate endPath = this.map.convertGPS(legs.get(index).getEndCompoundMark().getAverageGPSCoordinate());
gc.beginPath();
gc.moveTo(startPath.getX(), startPath.getY());
gc.lineTo(endPath.getX(), endPath.getY());
gc.stroke();
gc.closePath();
gc.save();
drawArrowHead(legStartPoint, legs.get(index).getEndCompoundMark().getAverageGPSCoordinate());
return null;
}
}
private void drawArrowHead(GPSCoordinate start, GPSCoordinate end){
GraphCoordinate lineStart = this.map.convertGPS(start);
GraphCoordinate lineEnd = this.map.convertGPS(end);
double arrowAngle = Math.toRadians(45.0);
double arrowLength = 10.0;
double dx = lineStart.getX() - lineEnd.getX();
double dy = lineStart.getY() - lineEnd.getY();
double angle = Math.atan2(dy, dx);
double x1 = Math.cos(angle + arrowAngle) * arrowLength + lineEnd.getX();
double y1 = Math.sin(angle + arrowAngle) * arrowLength + lineEnd.getY();
double x2 = Math.cos(angle - arrowAngle) * arrowLength + lineEnd.getX();
double y2 = Math.sin(angle - arrowAngle) * arrowLength + lineEnd.getY();
gc.strokeLine(lineEnd.getX(), lineEnd.getY(), x1, y1);
gc.strokeLine(lineEnd.getX(), lineEnd.getY(), x2, y2);
}
@ -480,7 +618,6 @@ public class ResizableRaceCanvas extends ResizableCanvas {
* @see TrackPoint * @see TrackPoint
*/ */
private void drawTrack(VisualiserBoat boat) { private void drawTrack(VisualiserBoat boat) {
//Check that track points are enabled. //Check that track points are enabled.
if (this.annoPath) { if (this.annoPath) {

@ -8,12 +8,12 @@
<Yacht SourceID="125"/> <Yacht SourceID="125"/>
</Participants> </Participants>
<CompoundMarkSequence> <CompoundMarkSequence>
<Corner CompoundMarkID="1" SeqID="1"/> <Corner SeqID="1" CompoundMarkID="1" Rounding="SP" ZoneSize="3" />
<Corner CompoundMarkID="2" SeqID="2"/> <Corner SeqID="2" CompoundMarkID="2" Rounding="Port" ZoneSize="3" />
<Corner CompoundMarkID="4" SeqID="3"/> <Corner SeqID="3" CompoundMarkID="4" Rounding="Port" ZoneSize="3" />
<Corner CompoundMarkID="3" SeqID="4"/> <Corner SeqID="4" CompoundMarkID="3" Rounding="Port" ZoneSize="3" />
<Corner CompoundMarkID="4" SeqID="5"/> <Corner SeqID="5" CompoundMarkID="4" Rounding="Port" ZoneSize="3" />
<Corner CompoundMarkID="5" SeqID="6"/> <Corner SeqID="6" CompoundMarkID="5" Rounding="SP" ZoneSize="3"/>
</CompoundMarkSequence> </CompoundMarkSequence>
<Course> <Course>
<CompoundMark CompoundMarkID="1" Name="Start Line"> <CompoundMark CompoundMarkID="1" Name="Start Line">

@ -1,5 +1,174 @@
package mock.model; package mock.model;
import mock.dataInput.PolarParser;
import mock.exceptions.InvalidPolarFileException;
import org.junit.Before;
import org.junit.Test;
import shared.model.Bearing;
import shared.model.CompoundMark;
import shared.model.GPSCoordinate;
import shared.model.Mark;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class MockBoatTest { public class MockBoatTest {
//TODO
/**
* boat made for testing
*/
private MockBoat firstTestBoat;
private Mark markToTest;
private Mark markToTest2;
private GPSCoordinate highGPS;
private GPSCoordinate middleGPS;
private GPSCoordinate lowGPS;
/**
* Creates the Polars object for the tests.
*/
@Before
public void setUp() {
//Read in polars.
try {
//Parse data file.
Polars polars = PolarParser.parse("mock/polars/acc_polars.csv");
firstTestBoat = new MockBoat(1, "test", "NZ", polars);
highGPS = new GPSCoordinate(-64.854000, 32.296577);
middleGPS = new GPSCoordinate(-64.854000, 32.292500);
lowGPS = new GPSCoordinate(-64.854000, 32.290000);
markToTest = new Mark(1, "test MARK", middleGPS);
markToTest2 = new Mark(2, "test MARK2", middleGPS);
}
catch (InvalidPolarFileException e) {
fail("Couldn't parse polar file.");
}
}
//////////////////////////////Mark Higher////////////////////////////////
/**
* Tests if the boat is lower than the mark that the port side method works if
* boat is facing east
*/
@Test
public void testIsPortSide() {
firstTestBoat.setBearing(Bearing.fromDegrees(90));
firstTestBoat.setCurrentPosition(lowGPS);
markToTest.setPosition(highGPS);
assertEquals(firstTestBoat.isPortSide(markToTest), true);
}
/**
* Tests if the boat is lower than the mark that the port side method works if
* boat is facing west
*/
@Test
public void testIsPortSideWrong() {
firstTestBoat.setBearing(Bearing.fromDegrees(270));
firstTestBoat.setCurrentPosition(lowGPS);
markToTest.setPosition(highGPS);
assertEquals(firstTestBoat.isPortSide(markToTest), false);
}
/**
* Tests if the boat is lower than the mark that the starboard side method works if
* boat is facing east
*/
@Test
public void testIsStarboardSideWrong() {
firstTestBoat.setBearing(Bearing.fromDegrees(90));
firstTestBoat.setCurrentPosition(lowGPS);
markToTest.setPosition(highGPS);
assertEquals(firstTestBoat.isStarboardSide(markToTest), false);
}
/**
* Tests if the boat is lower than the mark that the starboard side method works if
* boat is facing west
*/
@Test
public void testIsStarboardSide() {
firstTestBoat.setBearing(Bearing.fromDegrees(270));
firstTestBoat.setCurrentPosition(lowGPS);
markToTest.setPosition(highGPS);
assertEquals(firstTestBoat.isStarboardSide(markToTest), true);
}
//////////////////////////////Mark Lower////////////////////////////////
/**
* Tests if the boat is higher than the mark that the port side method works if
* boat is facing east
*/
@Test
public void testIsPortSideHigherWrong() {
firstTestBoat.setBearing(Bearing.fromDegrees(90));
firstTestBoat.setCurrentPosition(highGPS);
markToTest.setPosition(lowGPS);
assertEquals(firstTestBoat.isPortSide(markToTest), false);
}
/**
* Tests if the boat is higher than the mark that the port side method works if
* boat is facing west
*/
@Test
public void testIsPortSideHigher() {
firstTestBoat.setBearing(Bearing.fromDegrees(270));
firstTestBoat.setCurrentPosition(highGPS);
markToTest.setPosition(lowGPS);
assertEquals(firstTestBoat.isPortSide(markToTest), true);
}
/**
* Tests if the boat is higher than the mark that the starboard side method works if
* boat is facing east
*/
@Test
public void testIsStarboardSideHigher() {
firstTestBoat.setBearing(Bearing.fromDegrees(90));
firstTestBoat.setCurrentPosition(highGPS);
markToTest.setPosition(lowGPS);
assertEquals(firstTestBoat.isStarboardSide(markToTest), true);
}
/**
* Tests if the boat is higher than the mark that the starboard side method works if
* boat is facing west
*/
@Test
public void testIsStarboardSideHigherWrong() {
firstTestBoat.setBearing(Bearing.fromDegrees(270));
firstTestBoat.setCurrentPosition(highGPS);
markToTest.setPosition(lowGPS);
assertEquals(firstTestBoat.isStarboardSide(markToTest), false);
}
/**
* Tests if a boat is between a gate
*/
@Test
public void testIsBetweenGate(){
markToTest.setPosition(highGPS);
markToTest2.setPosition(lowGPS);
CompoundMark testGate = new CompoundMark(1, "test GATE", markToTest, markToTest2);
firstTestBoat.setCurrentPosition(middleGPS);
assertEquals(firstTestBoat.isBetweenGate(testGate), true);
}
} }

@ -0,0 +1,42 @@
package mock.model.commandFactory;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Created by David on 7/08/2017.
*/
public class TackGybeCommandTest {
private double degreeA;
private double degreeB;
private double degreeC;
private double degreeD;
TackGybeCommand tgc;
//Run before tests
@Before
public void setUp(){
degreeA = 150.0;
degreeB = 300.0;
degreeC = 10.0;
degreeD = 350.0;
tgc = new TackGybeCommand(null, null);
}
//Test when degree difference is <180
@Test
public void angleDistanceCalculationLow(){
double result = tgc.calcDistance(degreeA, degreeB);
assertEquals(150.0, result, 0);
}
//Test when degree difference is >180
@Test
public void angleDistanceCalculationHigh(){
double result = tgc.calcDistance(degreeC, degreeD);
assertEquals(20.0, result, 0);
}
}

@ -0,0 +1,58 @@
package mock.model.commandFactory;
import mock.model.MockBoat;
import mock.model.MockRace;
import network.Messages.Enums.BoatActionEnum;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import shared.model.Bearing;
import shared.model.Boat;
import shared.model.Race;
import visualiser.model.VisualiserRace;
import static org.mockito.Mockito.when;
import static org.testng.Assert.*;
import static org.mockito.Mockito.mock;
/**
* Created by connortaylorbrown on 4/08/17.
*/
public class WindCommandTest {
private MockRace race;
private MockBoat boat;
private Command upwind;
private Command downwind;
private double initial;
private double offset = 3.0;
@Before
public void setUp() {
race = mock(MockRace.class);
boat = new MockBoat(0, "Bob", "NZ", null);
when(race.getWindDirection()).thenReturn(Bearing.fromDegrees(0.0));
boat.setBearing(Bearing.fromDegrees(45.0));
upwind = CommandFactory.createCommand(race, boat, BoatActionEnum.UPWIND);
downwind = CommandFactory.createCommand(race, boat, BoatActionEnum.DOWNWIND);
initial = boat.getBearing().degrees();
}
/**
* Ensure the difference between initial and final angle is 3 degrees
*/
@Test
public void upwindCommandDecreasesAngle() {
upwind.execute();
assertEquals(initial - boat.getBearing().degrees(), -offset, 1e-5);
}
@Test
public void downwindCommandIncreasesAngle() {
downwind.execute();
assertEquals(initial - boat.getBearing().degrees(), offset, 1e-5);
}
}
Loading…
Cancel
Save