Removed BoatInRace from shared model, refactored

#story[778]
This commit is contained in:
Erika Savell
2017-05-01 15:06:56 +12:00
parent 7e569b59b7
commit 3f748bfdb6
15 changed files with 223 additions and 140 deletions
-1
View File
@@ -29,7 +29,6 @@ public class App extends Application {
@Override
public void start(Stage primaryStage) {
try {
RaceDataSource raceData = new RaceXMLReader("raceXML/bermuda_AC35.xml");
RegattaDataSource regattaData = new RegattaXMLReader("mockXML/regattaTest.xml");
Event raceEvent = new Event(raceData, regattaData);
@@ -4,7 +4,8 @@ import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import seng302.Exceptions.InvalidBoatDataException;
import SharedModel.BoatInRace;
import seng302.Model.Boat;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -23,10 +24,10 @@ import java.util.List;
public class BoatData {
Document doc;
private List<BoatInRace> boatData;
private List<Boat> boatData;
public BoatData(List<BoatInRace> boatData) {
public BoatData(List<Boat> boatData) {
this.boatData = boatData;
}
@@ -4,9 +4,9 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import seng302.DataInput.RaceDataSource;
import seng302.Exceptions.InvalidRaceDataException;
import SharedModel.BoatInRace;
import SharedModel.GPSCoordinate;
import SharedModel.Marker;
import seng302.Model.Boat;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@@ -129,7 +129,7 @@ public class RaceData {
private void appendParticipants() {
Element participantsElement = doc.createElement("Participants");
for (BoatInRace boat : dataSource.getBoats()) {
for (Boat boat : dataSource.getBoats()) {
Element yachtElement = doc.createElement("Yacht");
yachtElement.setAttribute("SourceID", boat.getSourceID() + "");
participantsElement.appendChild(yachtElement);
@@ -1,9 +1,9 @@
package seng302.DataInput;
import SharedModel.BoatInRace;
import SharedModel.GPSCoordinate;
import SharedModel.Leg;
import SharedModel.Marker;
import seng302.Model.Boat;
import java.util.List;
@@ -11,7 +11,7 @@ import java.util.List;
* Created by connortaylorbrown on 19/04/17.
*/
public interface RaceDataSource {
List<BoatInRace> getBoats();
List<Boat> getBoats();
List<Leg> getLegs();
@@ -4,10 +4,10 @@ import javafx.scene.paint.Color;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import SharedModel.BoatInRace;
import SharedModel.GPSCoordinate;
import SharedModel.Leg;
import SharedModel.Marker;
import seng302.Model.Boat;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
@@ -20,7 +20,7 @@ import java.util.List;
public class RaceXMLReader extends XMLReader implements RaceDataSource {
private static double COORDINATEPADDING = 0.0005;
private String raceID;
private List<BoatInRace> boats = new ArrayList<>();
private List<Boat> boats = new ArrayList<>();
private Color[] colors = {Color.BLUEVIOLET, Color.BLACK, Color.RED, Color.ORANGE, Color.DARKOLIVEGREEN, Color.LIMEGREEN};//TODO make this established in xml or come up with a better system.
private List<Leg> legs = new ArrayList<>();
private GPSCoordinate mark, startPt1, startPt2, finishPt1, finishPt2, leewardPt1, leewardPt2, windwardPt1, windwardPt2;
@@ -87,7 +87,7 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
String abbrev = getTextValueOfNode((Element) nBoats.item(i), "abbr");
double velo = Double.parseDouble(getTextValueOfNode((Element) nBoats.item(i), "speed"));
int sourceID = Integer.parseInt(getTextValueOfNode((Element) nBoats.item(i), "sourceID"));
BoatInRace boat = new BoatInRace(name, velo, colors[i], abbrev, sourceID);
Boat boat = new Boat(name, velo, abbrev, sourceID);
boat.setCurrentPosition(startPt1);
if (legs.size() > 0) {
boat.setCurrentLeg(legs.get(0));
@@ -301,7 +301,7 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
return new GPSCoordinate(startLat, startLong);
}
public List<BoatInRace> getBoats() {
public List<Boat> getBoats() {
return boats;
}
+154
View File
@@ -0,0 +1,154 @@
package seng302.Model;
import SharedModel.*;
import SharedModel.Leg;
import org.geotools.referencing.GeodeticCalculator;
/**
* Created by esa46 on 1/05/17.
*/
public class Boat {
private String name;
private double velocity;
private double scaledVelocity;
private String abbrev;
private int sourceID;
private SharedModel.Leg currentLeg;
private double distanceTravelledInLeg;
private GPSCoordinate currentPosition;
private long timeFinished = -1;
private boolean started = false;
private double heading;
/**
* Boat initialiser which keeps all of the information of the boat.
*
* @param name Name of the Boat.
* @param velocity Speed in m/s that the boat travels at.
* @param abbrev nam abbreviation
* @param sourceID id of boat
*/
public Boat(String name, double velocity, String abbrev, int sourceID) {
this.velocity = velocity;
this.abbrev = abbrev;
this.name = name;
this.sourceID = sourceID;
}
/**
* Calculates the azimuth of the travel via map coordinates of the raceMarkers
*
* @return the direction that the boat is heading towards in degrees (-180 to 180).
*/
public double calculateAzimuth() {
GeodeticCalculator calc = new GeodeticCalculator();
GPSCoordinate start = currentLeg.getStartMarker().getAverageGPSCoordinate();
GPSCoordinate end = currentLeg.getEndMarker().getAverageGPSCoordinate();
calc.setStartingGeographicPoint(start.getLongitude(), start.getLatitude());
calc.setDestinationGeographicPoint(end.getLongitude(), end.getLatitude());
return calc.getAzimuth();
}
public double calculateHeading() {
double azimuth = this.calculateAzimuth();
if (azimuth >= 0) {
return azimuth;
} else {
return azimuth + 360;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getVelocity() {
return velocity;
}
public void setVelocity(double velocity) {
this.velocity = velocity;
}
public double getScaledVelocity() {
return scaledVelocity;
}
public void setScaledVelocity(double scaledVelocity) {
this.scaledVelocity = scaledVelocity;
}
public String getAbbrev() {
return abbrev;
}
public void setAbbrev(String abbrev) {
this.abbrev = abbrev;
}
public int getSourceID() {
return sourceID;
}
public void setSourceID(int sourceID) {
this.sourceID = sourceID;
}
public Leg getCurrentLeg() {
return currentLeg;
}
public void setCurrentLeg(Leg currentLeg) {
this.currentLeg = currentLeg;
}
public double getDistanceTravelledInLeg() {
return distanceTravelledInLeg;
}
public void setDistanceTravelledInLeg(double distanceTravelledInLeg) {
this.distanceTravelledInLeg = distanceTravelledInLeg;
}
public GPSCoordinate getCurrentPosition() {
return currentPosition;
}
public void setCurrentPosition(GPSCoordinate currentPosition) {
this.currentPosition = currentPosition;
}
public long getTimeFinished() {
return timeFinished;
}
public void setTimeFinished(long timeFinished) {
this.timeFinished = timeFinished;
}
public boolean isStarted() {
return started;
}
public void setStarted(boolean started) {
this.started = started;
}
public double getHeading() {
return heading;
}
public void setHeading(double heading) {
this.heading = heading;
}
}
@@ -41,8 +41,11 @@ public class Event {
public void start() {
System.out.println("Sending Regatta");
sendRegattaData();
System.out.println("Sending Race");
sendRaceData();
System.out.println("Sending Boat");
sendBoatData();
Race newRace = new Race(raceDataSource, 15, mockOutput);
+14 -31
View File
@@ -24,8 +24,8 @@ import java.util.Random;
* Created by fwy13 on 3/03/17.
*/
public class Race implements Runnable {
//protected SharedModel.BoatInRace[] startingBoats;
protected ObservableList<SharedModel.BoatInRace> startingBoats;
//protected Boat[] startingBoats;
protected ObservableList<Boat> startingBoats;
protected List<SharedModel.Leg> legs;
protected int boatsFinished = 0;
protected long totalTimeElapsed;
@@ -44,7 +44,7 @@ public class Race implements Runnable {
* @param legs Number of marks in order that the boats pass in order to complete the race.
* @param scaleFactor for race
*/
public Race(List<SharedModel.BoatInRace> boats, List<SharedModel.Leg> legs, int scaleFactor, MockOutput mockOutput) {
public Race(List<Boat> boats, List<SharedModel.Leg> legs, int scaleFactor, MockOutput mockOutput) {
this.startingBoats = FXCollections.observableArrayList(boats);
this.legs = legs;
@@ -142,7 +142,7 @@ public class Race implements Runnable {
System.setProperty("javafx.animation.fullspeed", "true");
for (SharedModel.BoatInRace boat : startingBoats) {
for (Boat boat : startingBoats) {
boat.setStarted(true);
}
@@ -160,8 +160,8 @@ public class Race implements Runnable {
ArrayList<BoatStatusMessage> boatStatusMessages = new ArrayList<BoatStatusMessage>();
//For each boat, we update it's position, and generate a BoatLocationMessage.
for (SharedModel.BoatInRace boat : startingBoats) {
if (boat != null && !boat.isFinished()) {
for (Boat boat : startingBoats) {
if (boat != null && boat.getTimeFinished() > 0) {
//Update position.
updatePosition(boat, Math.round(1000 / lastFPS) > 20 ? 15 : Math.round(1000 / lastFPS));
checkPosition(boat, totalTimeElapsed);
@@ -179,21 +179,6 @@ public class Race implements Runnable {
}.start();
}
/**
* Update position of boats in race, no position if on starting leg or DNF.
*/
protected void updatePositions() {
FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber());
for (SharedModel.BoatInRace boat : startingBoats) {
if (boat != null) {
boat.setPosition(Integer.toString(startingBoats.indexOf(boat) + 1));
//System.out.println(boat.toString() + " " + boat.getPosition());//TEMP debug print
if (boat.getCurrentLeg().getName().equals("DNF") || boat.getCurrentLeg().getLegNumber() == 0)
boat.setPosition("-");
}
}
//System.out.println("=====");//TEMP debug print
}
public void initialiseBoats() {
SharedModel.Leg officialStart = legs.get(0);
@@ -202,7 +187,7 @@ public class Race implements Runnable {
ArrayList<SharedModel.Marker> startMarkers = getSpreadStartingPositions();
for (int i = 0; i < startingBoats.size(); i++) {
SharedModel.BoatInRace boat = startingBoats.get(i);
Boat boat = startingBoats.get(i);
if (boat != null) {
boat.setScaledVelocity(boat.getVelocity() * scaleFactor);
SharedModel.Leg startLeg = new SharedModel.Leg(name, 0);
@@ -271,7 +256,7 @@ public class Race implements Runnable {
* @param boat to be updated
* @param millisecondsElapsed since last update
*/
protected void updatePosition(SharedModel.BoatInRace boat, int millisecondsElapsed) {
protected void updatePosition(Boat boat, int millisecondsElapsed) {
//distanceTravelled = velocity (nm p hr) * time taken to update loop
double distanceTravelled = (boat.getScaledVelocity() * millisecondsElapsed) / 3600000;
@@ -289,17 +274,17 @@ public class Race implements Runnable {
}
}
protected void checkPosition(SharedModel.BoatInRace boat, long timeElapsed) {
protected void checkPosition(Boat boat, long timeElapsed) {
if (boat.getDistanceTravelledInLeg() > boat.getCurrentLeg().getDistance()) {
//boat has passed onto new leg
if (boat.getCurrentLeg().getName().equals("Finish")) {
//boat has finished
boatsFinished++;
boat.setFinished(true);
boat.setTimeFinished(timeElapsed);
boat.setTimeFinished(timeElapsed);
} else if (doNotFinish()) {
boatsFinished++;
boat.setFinished(true);
boat.setTimeFinished(timeElapsed);
boat.setCurrentLeg(new SharedModel.Leg("DNF", -1));
boat.setVelocity(0);
boat.setScaledVelocity(0);
@@ -313,19 +298,17 @@ public class Race implements Runnable {
//Add overshoot distance into the distance travelled for the next leg
boat.setDistanceTravelledInLeg(boat.getDistanceTravelledInLeg());
}
//Update the boat display table in the GUI to reflect the leg change
updatePositions();
}
}
/**
* Returns the boats that have started the race.
*
* @return ObservableList of SharedModel.BoatInRace class that participated in the race.
* @return ObservableList of Boat class that participated in the race.
* @see ObservableList
* @see SharedModel.BoatInRace
* @see Boat
*/
public ObservableList<SharedModel.BoatInRace> getStartingBoats() {
public ObservableList<Boat> getStartingBoats() {
return startingBoats;
}
}