Merge remote-tracking branch 'remotes/origin/story63' into Development

# Conflicts:
#	racevisionGame/src/main/java/mock/model/MockBoat.java
#	racevisionGame/src/main/java/mock/model/MockRace.java
main
Connor Taylor-Brown 8 years ago
commit 8ee4e84236

@ -1,20 +1,17 @@
package mock.app;
import mock.model.RaceLogic;
import network.Messages.Enums.XMLMessageType;
import network.Messages.LatestMessages;
import network.Messages.XMLMessage;
import org.mockito.Mock;
import visualiser.gameController.ControllerServer;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
/**
@ -43,6 +40,10 @@ public class ConnectionAcceptor implements Runnable {
private short boatXMLSequenceNumber;
//regatta xml sequence number
private short regattaXMLSequenceNumber;
//controller server
private ControllerServer controllerServer;
//
private RaceLogic rl = null;
/**
* Connection Acceptor Constructor
@ -65,6 +66,11 @@ public class ConnectionAcceptor implements Runnable {
return serverPort;
}
public void setRace(RaceLogic rl){
this.rl = rl;
}
/**
* Run the Acceptor
*/
@ -76,7 +82,7 @@ public class ConnectionAcceptor implements Runnable {
Socket mockSocket = serverSocket.accept();
DataOutputStream outToVisualiser = new DataOutputStream(mockSocket.getOutputStream());
MockOutput mockOutput = new MockOutput(latestMessages, outToVisualiser);
ControllerServer controllerServer = new ControllerServer(mockSocket);
this.controllerServer = new ControllerServer(mockSocket, rl);
new Thread(mockOutput).start();
new Thread(controllerServer).start();
mockOutputList.add(mockOutput);

@ -3,6 +3,7 @@ package mock.app;
import mock.dataInput.PolarParser;
import mock.model.MockRace;
import mock.model.Polars;
import mock.model.RaceLogic;
import network.Messages.LatestMessages;
import shared.dataInput.*;
import shared.enums.XMLFileType;
@ -91,7 +92,9 @@ public class Event {
RegattaDataSource regattaDataSource = new RegattaXMLReader(this.regattaXML, this.xmlFileType);
//Create and start race.
MockRace newRace = new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale);
RaceLogic newRace = new RaceLogic(new MockRace(boatDataSource, raceDataSource, regattaDataSource, this.latestMessages, this.boatPolars, Constants.RaceTimeScale), this.latestMessages);
mockOutput.setRace(newRace);
new Thread(newRace).start();
}

@ -30,6 +30,11 @@ public class MockBoat extends Boat {
*/
private Integer roundingStatus = 0;
/**
* Stores whether the boat is on autoVMG or not
*/
private boolean autoVMG = true;
/**
@ -71,8 +76,6 @@ public class MockBoat extends Boat {
//Get the start and end points.
GPSCoordinate currentPosition = this.getCurrentPosition();
GPSCoordinate nextMarkerPosition = this.getCurrentLeg().getEndCompoundMark().getAverageGPSCoordinate();
// GPSCoordinate nextMarkerPosition = this.getCurrentLeg().getEndCompoundMark().getMark1Position();
// todo:may need to change this so that boats are send to corners of the gates rather than the middle
//Calculate bearing.
Bearing bearing = GPSCoordinate.calculateBearing(currentPosition, nextMarkerPosition);
@ -145,20 +148,9 @@ public class MockBoat extends Boat {
/**
* Moves the boat meters forward in the direction that it is facing
* @param meters The number of meters to move forward.
* @param milliseconds The number of milliseconds to advance the boat's timers by.
*
*/
public void moveForwards(double meters, long milliseconds) {
//Update the boat's time since last tack.
this.setTimeSinceTackChange(this.getTimeSinceTackChange() + milliseconds);
//Update the time into the current leg.
this.setTimeElapsedInCurrentLeg(this.getTimeElapsedInCurrentLeg() + milliseconds);
//Update the distance into the current leg.
this.setDistanceTravelledInLeg(this.getDistanceTravelledInLeg() + meters);
public void moveForwards(double meters) {
//Updates the current position of the boat.
GPSCoordinate newPosition = GPSCoordinate.calculateNewPosition(this.getCurrentPosition(), meters, Azimuth.fromBearing(this.getBearing()));
this.setCurrentPosition(newPosition);
@ -266,4 +258,11 @@ public class MockBoat extends Boat {
public void resetRoundingStatus() {
this.roundingStatus = 0;
}
public boolean isAutoVMG() {
return autoVMG;
}
public void setAutoVMG(boolean autoVMG) {
this.autoVMG = autoVMG;
}
}

@ -1,17 +1,14 @@
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 org.opengis.geometry.primitive.*;
import shared.dataInput.BoatDataSource;
import shared.dataInput.RaceDataSource;
import shared.dataInput.RegattaDataSource;
import shared.model.*;
import shared.model.Bearing;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
@ -46,22 +43,11 @@ public class MockRace extends Race {
*/
private int scaleFactor;
/**
* The percent chance that a boat fails the race, and enters a DNF state, at each checkpoint.
* 0 = 0%, 100 = 100%.
*/
private int dnfChance = 0;
/**
* Object used to generate changes in wind speed/direction.
*/
private WindGenerator windGenerator;
/**
* 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).
@ -81,7 +67,6 @@ public class MockRace extends Race {
this.shrinkBoundary = GPSCoordinate.getShrinkBoundary(this.boundary);
//Set up wind generator. It may be tidier to create this outside the race (with the values sourced from a data file maybe?) and pass it in.
this.windGenerator = new WindGenerator(
Bearing.fromDegrees(225),
@ -93,7 +78,6 @@ public class MockRace extends Race {
//Wind.
this.setWind(windGenerator.generateBaselineWind());
}
/**
@ -125,103 +109,11 @@ public class MockRace extends Race {
}
/**
* Runnable for the thread.
*/
public void run() {
initialiseBoats();
initialiseWindDirection();
this.countdownTimer.start();
}
/**
* 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) {
public void updateRaceTime(long currentTime) {
this.raceClock.setUTCTime(currentTime);
}
@ -229,7 +121,7 @@ public class MockRace extends Race {
/**
* Updates the race status enumeration based on the current time.
*/
private void updateRaceStatusEnum() {
public void updateRaceStatusEnum() {
//The millisecond duration of the race. Negative means it hasn't started, so we flip sign.
long timeToStart = - this.raceClock.getDurationMilli();
@ -254,55 +146,13 @@ public class MockRace extends Race {
}
}
/**
* 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() {
public void setBoatsStatusToRacing() {
for (MockBoat boat : this.boats) {
boat.setStatus(BoatStatusEnum.RACING);
@ -314,7 +164,7 @@ public class MockRace extends Race {
* 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) {
public void setBoatsTimeNextMark(ZonedDateTime time) {
for (MockBoat boat : this.boats) {
boat.setEstimatedTimeAtNextMark(time);
@ -322,150 +172,12 @@ public class MockRace extends Race {
}
/**
* 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++;
}
};
/**
* Initialise the boats in the race.
* This sets their starting positions and current legs.
*/
@Override
protected void initialiseBoats() {
public void initialiseBoats() {
//Gets the starting positions of the boats.
List<GPSCoordinate> startingPositions = getSpreadStartingPositions();
@ -498,8 +210,7 @@ public class MockRace extends Race {
boat.setStatus(BoatStatusEnum.PRESTART);
//We set a large time since tack change so that it calculates a new VMG when the simulation starts.
boat.setTimeSinceTackChange(999999);
boat.setTimeSinceTackChange(Long.MAX_VALUE);
}
}
@ -510,7 +221,7 @@ public class MockRace extends Race {
*
* @return A list of starting positions.
*/
public List<GPSCoordinate> getSpreadStartingPositions() {
private List<GPSCoordinate> getSpreadStartingPositions() {
//The first compound marker of the race - the starting gate.
CompoundMark compoundMark = this.legs.get(0).getStartCompoundMark();
@ -552,33 +263,6 @@ public class MockRace extends Race {
}
/**
* Calculates a boat's VMG.
* @param boat The boat to calculate VMG for.
* @param bearingBounds An array containing the lower and upper acceptable bearing bounds to keep the boat in the course.
* @return VMG for the specified boat.
*/
private VMG calculateVMG(MockBoat boat, Bearing[] bearingBounds) {
//Get the lower and upper acceptable bounds.
Bearing lowerAcceptableBound = bearingBounds[0];
Bearing upperAcceptableBound = bearingBounds[1];
//Find the VMG inside these bounds.
VMG bestVMG = boat.getPolars().calculateVMG(
this.getWindDirection(),
this.getWindSpeed(),
boat.calculateBearingToNextMarker(),
lowerAcceptableBound,
upperAcceptableBound);
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.
@ -586,7 +270,7 @@ public class MockRace extends Race {
* @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) {
public 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());
@ -630,13 +314,19 @@ public class MockRace extends Race {
* @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) {
public 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) {
if (!finish && totalElapsedMilliseconds >= updatePeriodMilliseconds) {
if (boat.getCurrentSpeed() == 0) {
newOptimalVMG(boat);
boat.setBearing(boat.calculateBearingToNextMarker());
}
setBoatSpeed(boat);
//Calculates the distance travelled, in meters, in the current timeslice.
double distanceTravelledMeters = boat.calculateMetersTravelled(updatePeriodMilliseconds);
@ -646,42 +336,48 @@ public class MockRace extends Race {
//Move the boat forwards that many meters, and advances its time counters by enough milliseconds.
boat.moveForwards(distanceTravelledMeters, updatePeriodMilliseconds * this.scaleFactor);
boat.moveForwards(distanceTravelledMeters);
boat.setTimeSinceTackChange(boat.getTimeSinceTackChange() + updatePeriodMilliseconds);
//Only get a new VMG if the boat will go outside the course, or X seconds have elapsed.
boolean willStayInsideCourse = this.checkBearingInsideCourse(boat.getBearing(), boat.getCurrentPosition());
long tackPeriod = 15000;
if (!willStayInsideCourse || (boat.getTimeSinceTackChange() > tackPeriod)) {
if (boat.isAutoVMG()) {
newOptimalVMG(boat);
}
//Calculate the boat's bearing bounds, to ensure that it doesn't go out of the course.
Bearing[] bearingBounds = this.calculateBearingBounds(boat);
this.updateEstimatedTime(boat);
}
}
//Calculate the new VMG.
VMG newVMG = this.calculateVMG(boat, bearingBounds);
private void newOptimalVMG(MockBoat boat) {
long tackPeriod = 15000;
if (boat.getTimeSinceTackChange() > tackPeriod) {
//Calculate the new VMG.
VMG newVMG = boat.getPolars().calculateVMG(
this.getWindDirection(),
this.getWindSpeed(),
boat.calculateBearingToNextMarker(),
Bearing.fromDegrees(0d),
Bearing.fromDegrees(359.99999d));
//If the new vmg improves velocity, use it.
if (improvesVelocity(boat, newVMG)) {
boat.setVMG(newVMG);
} else {
//We also need to use the new VMG if our current bearing will take us out of the course.
if (!willStayInsideCourse) {
boat.setVMG(newVMG);
}
}
//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);
}
}
private void setBoatSpeed(MockBoat boat) {
VMG vmg = boat.getPolars().calculateVMG(
this.getWindDirection(),
this.getWindSpeed(),
boat.getBearing(),
boat.getBearing(),
boat.getBearing());
if (vmg.getSpeed() > 0) {
boat.setCurrentSpeed(vmg.getSpeed());
}
}
/**
@ -1001,20 +697,10 @@ public class MockRace extends Race {
return boats;
}
/**
* Initialises the wind bearing with the value of the windBaselineBearing.
*/
protected void initialiseWindDirection() {
//Set the starting bearing.
this.setWind(windGenerator.generateBaselineWind());
}
/**
* Changes the wind direction randomly, while keeping it within [windLowerBound, windUpperBound].
*/
protected void changeWindDirection() {
public void changeWindDirection() {
Wind nextWind = windGenerator.generateNextWind(raceWind.getValue());
@ -1042,4 +728,8 @@ public class MockRace extends Race {
}
}
}
public List<CompoundMark> getCompoundMarks() {
return compoundMarks;
}
}

@ -0,0 +1,196 @@
package mock.model;
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.RaceStatusEnum;
import network.Messages.LatestMessages;
import visualiser.gameController.ControllerServer;
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
*/
private MockRace race;
/**
* High-level interface to AC35 protocol server
*/
private RaceServer server;
private CompositeCommand commands;
/**
* Initialises race loop with state and server message queue
* @param race state of race to modify
* @param messages to send to server
*/
public RaceLogic(MockRace race, LatestMessages messages) {
this.race = race;
this.server = new RaceServer(race, messages);
this.commands = new CompositeCommand();
}
/**
* Initialise boats and start countdown timer
*/
@Override
public void run() {
race.initialiseBoats();
this.countdownTimer.start();
}
/**
* Countdown timer until race starts.
*/
protected AnimationTimer countdownTimer = new AnimationTimer() {
long currentTime = System.currentTimeMillis();
@Override
public void handle(long arg0) {
//Update race time.
race.updateRaceTime(currentTime);
//Update the race status based on the current time.
race.updateRaceStatusEnum();
//Provide boat's with an estimated time at next mark until the race starts.
race.setBoatsTimeNextMark(race.getRaceClock().getCurrentTime());
//Parse the boat locations.
server.parseBoatLocations();
//Parse the marks.
server.parseMarks();
// Change wind direction
race.changeWindDirection();
//Parse the race status.
server.parseRaceStatus();
if (race.getRaceStatusEnum() == RaceStatusEnum.STARTED) {
race.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;
long framePeriod = currentTime - lastFrameTime;
@Override
public void handle(long arg0) {
//Get the current time.
currentTime = System.currentTimeMillis();
//Update race time.
race.updateRaceTime(currentTime);
//As long as there is at least one boat racing, we still simulate the race.
if (race.getNumberOfActiveBoats() != 0) {
//Get the time period of this frame.
framePeriod = currentTime - lastFrameTime;
//For each boat, we update its position, and generate a BoatLocationMessage.
for (MockBoat boat : race.getBoats()) {
//If it is still racing, update its position.
if (boat.getStatus() == BoatStatusEnum.RACING) {
commands.execute();
race.updatePosition(boat, framePeriod, race.getRaceClock().getDurationMilli());
}
}
} else {
//Otherwise, the race is over!
raceFinished.start();
race.setRaceStatusEnum(RaceStatusEnum.FINISHED);
this.stop();
}
if (race.getNumberOfActiveBoats() != 0) {
// Change wind direction
race.changeWindDirection();
//Parse the boat locations.
server.parseBoatLocations();
//Parse the marks.
server.parseMarks();
//Parse the race status.
server.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) {
server.parseRaceStatus();
if (iters > 500) {
stop();
}
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,153 @@
package mock.model;
import network.Messages.BoatLocation;
import network.Messages.BoatStatus;
import network.Messages.LatestMessages;
import network.Messages.RaceStatus;
import network.Utils.AC35UnitConverter;
import shared.model.CompoundMark;
import shared.model.Constants;
import shared.model.Mark;
import java.util.ArrayList;
import java.util.List;
/**
* Created by connortaylorbrown on 2/08/17.
*/
public class RaceServer {
private MockRace race;
private LatestMessages latestMessages;
/**
* The sequence number of the latest RaceStatus message sent or received.
*/
private int raceStatusSequenceNumber = 1;
/**
* The sequence number of the latest BoatLocation message sent or received.
*/
private int boatLocationSequenceNumber = 1;
public RaceServer(MockRace race, LatestMessages latestMessages) {
this.race = race;
this.latestMessages = latestMessages;
}
/**
* 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,
race.getRaceClock().getCurrentTimeMilli());
//Iterates the sequence number.
this.boatLocationSequenceNumber++;
this.latestMessages.setBoatLocation(boatLocation);
}
/**
* Parse the compound marker boats through mock output.
*/
public void parseMarks() {
for (CompoundMark compoundMark : race.getCompoundMarks()) {
//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);
}
}
}
/**
* Parse the boats in the race, and send it to mockOutput.
*/
public void parseBoatLocations() {
//Parse each boat.
for (MockBoat boat : race.getBoats()) {
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(),
race.getRaceClock().getCurrentTimeMilli());
//Iterates the sequence number.
this.boatLocationSequenceNumber++;
this.latestMessages.setBoatLocation(boatLocation);
}
/**
* Parses the race status, and sends it to mockOutput.
*/
public 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 : race.getBoats()) {
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(race.getWindDirection().degrees());
int windSpeedInt = (int) (race.getWindSpeed() * Constants.KnotsToMMPerSecond);
//Create race status object, and send it.
RaceStatus raceStatus = new RaceStatus(
System.currentTimeMillis(),
race.getRaceId(),
race.getRaceStatusEnum().getValue(),
race.getRaceClock().getStartingTimeMilli(),
windDirectionInt,
windSpeedInt,
race.getRaceType().getValue(),
boatStatuses);
this.latestMessages.setRaceStatus(raceStatus);
}
}

@ -0,0 +1,14 @@
package mock.model.commandFactory;
import mock.model.MockBoat;
import mock.model.MockRace;
/**
* Allows RaceLogic to control MockRace state according to the Command pattern
*/
public interface Command {
/**
* Execute command - standard method name in pattern
*/
void execute();
}

@ -0,0 +1,27 @@
package mock.model.commandFactory;
import mock.model.MockBoat;
import mock.model.MockRace;
import network.Messages.Enums.BoatActionEnum;
/**
* Factory class for Command objects
*/
public class CommandFactory {
/**
* Generates a command on a race and boat corresponding to the protocol action number.
* @param race to receive command
* @param boat to receive command in race
* @param action number to select command
* @return
*/
public static Command createCommand(MockRace race, MockBoat boat, BoatActionEnum action) {
switch(action) {
case AUTO_PILOT: return new VMGCommand(race, boat);
case TACK_GYBE: return new TackGybeCommand(race, boat);
case UPWIND: return new WindCommand(race, boat, true);
case DOWNWIND: return new WindCommand(race, boat, false);
default: return null; // TODO - please please have discussion over what to default to
}
}
}

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

@ -0,0 +1,32 @@
package mock.model.commandFactory;
import mock.model.MockBoat;
import mock.model.MockRace;
/**
* Created by David on 2/08/2017.
*/
public class TackGybeCommand implements Command {
private MockRace race;
private MockBoat boat;
public TackGybeCommand(MockRace race, MockBoat boat) {
this.race = race;
this.boat = boat;
}
//The refactoring of MockRace will require changes to be made
@Override
public void execute() {
/*VMG newVMG = boat.getPolars().calculateVMG(
race.getWindDirection(),
race.getWindSpeed(),
boat.calculateBearingToNextMarker(),
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);
}*/
}
}

@ -0,0 +1,29 @@
package mock.model.commandFactory;
import mock.model.MockBoat;
import mock.model.MockRace;
/**
* Created by David on 2/08/2017.
*/
public class VMGCommand implements Command {
private MockRace race;
private MockBoat boat;
public VMGCommand(MockRace race, MockBoat boat) {
this.race = race;
this.boat = boat;
}
//The refactoring of MockRace will require changes to be made
@Override
public void execute() {
/*VMG newVMG = boat.getPolars().calculateVMG(
race.getWindDirection(),
race.getWindSpeed(),
boat.calculateBearingToNextMarker(),
Bearing.fromDegrees(0d),
Bearing.fromDegrees(359.99999d));
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));
}
}

@ -38,7 +38,7 @@ public class Constants {
/**
* The race pre-start time, in milliseconds. 3 minutes.
*/
public static final long RacePreStartTime = 3 * 60 * 1000;
public static final long RacePreStartTime = 1 * 10 * 1000;
/**

@ -19,7 +19,7 @@ import java.util.List;
* This is a base class inherited by {@link mock.model.MockRace} and {@link visualiser.model.VisualiserRace}.
* Has a course, state, wind, boundaries, etc.... Boats are added by inheriting classes (see {@link Boat}, {@link mock.model.MockBoat}, {@link visualiser.model.VisualiserBoat}.
*/
public abstract class Race implements Runnable {
public abstract class Race {
/**
@ -43,18 +43,6 @@ public abstract class Race implements Runnable {
*/
protected LatestMessages latestMessages;
/**
* The sequence number of the latest BoatLocation message sent or received.
*/
protected int boatLocationSequenceNumber = 1;
/**
* The sequence number of the latest RaceStatus message sent or received.
*/
protected int raceStatusSequenceNumber = 1;
/**
* A list of compound marks in the race.
*/
@ -226,7 +214,7 @@ public abstract class Race implements Runnable {
* Sets the current race status.
* @param raceStatusEnum The new status of the race.
*/
protected void setRaceStatusEnum(RaceStatusEnum raceStatusEnum) {
public void setRaceStatusEnum(RaceStatusEnum raceStatusEnum) {
this.raceStatusEnum = raceStatusEnum;
}
@ -364,4 +352,8 @@ public abstract class Race implements Runnable {
this.lastFpsResetTime = 0;
}
}
public int getRaceId() {
return raceId;
}
}

@ -1,19 +1,19 @@
package visualiser.gameController;
import mock.model.RaceLogic;
import network.BinaryMessageDecoder;
import network.MessageDecoders.BoatActionDecoder;
import network.Messages.Enums.BoatActionEnum;
import visualiser.gameController.Keys.ControlKey;
import visualiser.gameController.Keys.KeyFactory;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Observable;
/**
* 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
*/
@ -22,13 +22,23 @@ public class ControllerServer implements Runnable {
* Wrapper for input from client
*/
private DataInputStream inputStream;
/**
* Last received boat action
*/
private BoatActionEnum action;
/**
*
*/
private RaceLogic rc;
/**
* Initialise server-side controller with live client socket
* @param socket to client
*/
public ControllerServer(Socket socket) {
public ControllerServer(Socket socket, RaceLogic rc) {
this.socket = socket;
this.rc = rc;
this.addObserver(rc);
try {
this.inputStream = new DataInputStream(this.socket.getInputStream());
} catch (IOException e) {
@ -36,6 +46,10 @@ public class ControllerServer implements Runnable {
}
}
public BoatActionEnum getAction() {
return action;
}
/**
* Wait for controller key input from client and loop.
*/
@ -48,8 +62,10 @@ public class ControllerServer implements Runnable {
inputStream.read(message);
BinaryMessageDecoder encodedMessage = new BinaryMessageDecoder(message);
BoatActionDecoder boatActionDecoder = new BoatActionDecoder(encodedMessage.getMessageBody());
BoatActionEnum decodedMessage = boatActionDecoder.getBoatAction();
System.out.println("Received key: " + decodedMessage);
action = boatActionDecoder.getBoatAction();
this.notifyObservers();
this.setChanged();
}
} catch (IOException e) {
e.printStackTrace();

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

@ -27,7 +27,7 @@ import java.util.Map;
* Has a course, boats, boundaries, etc...
* Observes LatestMessages and updates its state based on new messages.
*/
public class VisualiserRace extends Race {
public class VisualiserRace extends Race implements Runnable {
/**

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