MockRace:
Now uses RaceState as its base class, rather than Race. This aligns it with VisualiserRace. RaceState contains the shared race state between the client and server, and stores most of its data in RaceDataSource etc..., instead of maintaining its own copy. Moved collider registry member to MockRace. XMLUtilities: Added documentation to two important functions, and updated them to use generics instead of Objects. #story[1188]
This commit is contained in:
@@ -25,6 +25,8 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
@@ -118,7 +120,8 @@ public class Event {
|
||||
|
||||
}
|
||||
|
||||
this.sourceIdAllocator = new SourceIdAllocator(raceDataSource.getParticipants());
|
||||
List<Integer> potentialParticipants = new ArrayList<>(boatDataSource.getBoats().keySet());
|
||||
this.sourceIdAllocator = new SourceIdAllocator(potentialParticipants);
|
||||
this.compositeCommand = new CompositeCommand();
|
||||
this.latestMessages = new LatestMessages();
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package mock.model;
|
||||
|
||||
import javafx.animation.AnimationTimer;
|
||||
import mock.model.collider.ColliderRegistry;
|
||||
import mock.xml.*;
|
||||
import network.Messages.BoatLocation;
|
||||
import network.Messages.BoatStatus;
|
||||
@@ -13,7 +14,6 @@ import shared.exceptions.BoatNotFoundException;
|
||||
import shared.enums.RoundingType;
|
||||
import shared.model.*;
|
||||
import shared.model.Bearing;
|
||||
import shared.model.Race;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
@@ -27,7 +27,7 @@ import static java.lang.Math.cos;
|
||||
* Has a course, boats, boundaries, etc...
|
||||
* Is responsible for simulating the race, and sending messages to a MockOutput instance.
|
||||
*/
|
||||
public class MockRace extends Race {
|
||||
public class MockRace extends RaceState {
|
||||
|
||||
/**
|
||||
* An observable list of boats in the race.
|
||||
@@ -40,6 +40,12 @@ public class MockRace extends Race {
|
||||
private List<GPSCoordinate> shrinkBoundary;
|
||||
|
||||
|
||||
/**
|
||||
* Registry for all collider object in this race
|
||||
*/
|
||||
protected ColliderRegistry colliderRegistry;
|
||||
|
||||
|
||||
/**
|
||||
* The scale factor of the race.
|
||||
* See {@link Constants#RaceTimeScale}.
|
||||
@@ -51,6 +57,8 @@ public class MockRace extends Race {
|
||||
*/
|
||||
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).
|
||||
@@ -62,13 +70,15 @@ public class MockRace extends Race {
|
||||
*/
|
||||
public MockRace(BoatDataSource boatDataSource, RaceDataSource raceDataSource, RegattaDataSource regattaDataSource, Polars polars, int timeScale, WindGenerator windGenerator) {
|
||||
|
||||
super(boatDataSource, raceDataSource, regattaDataSource);
|
||||
this.setBoatDataSource(boatDataSource);
|
||||
this.setRaceDataSource(raceDataSource);
|
||||
this.setRegattaDataSource(regattaDataSource);
|
||||
|
||||
this.scaleFactor = timeScale;
|
||||
|
||||
this.boats = this.generateMockBoats(boatDataSource.getBoats(), raceDataSource.getParticipants(), polars);
|
||||
|
||||
this.shrinkBoundary = GPSCoordinate.getShrinkBoundary(this.boundary);
|
||||
this.shrinkBoundary = GPSCoordinate.getShrinkBoundary(this.getBoundary());
|
||||
|
||||
|
||||
this.windGenerator = windGenerator;
|
||||
@@ -76,9 +86,19 @@ public class MockRace extends Race {
|
||||
//Wind.
|
||||
this.setWind(windGenerator.generateBaselineWind());
|
||||
|
||||
|
||||
// Set up colliders
|
||||
this.colliderRegistry = new ColliderRegistry();
|
||||
|
||||
for(CompoundMark mark: this.getCompoundMarks()) {
|
||||
colliderRegistry.addCollider(mark.getMark1());
|
||||
if(mark.getMark2() != null) colliderRegistry.addCollider(mark.getMark2());
|
||||
}
|
||||
|
||||
this.colliderRegistry.addAllColliders(boats);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates a list of MockBoats given a list of Boats, and a list of participating boats.
|
||||
* @param boats The map of Boats describing boats that are potentially in the race. Maps boat sourceID to boat.
|
||||
@@ -108,12 +128,17 @@ public class MockRace extends Race {
|
||||
}
|
||||
|
||||
|
||||
public ColliderRegistry getColliderRegistry() {
|
||||
return colliderRegistry;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Updates the race time to a specified value, in milliseconds since the unix epoch.
|
||||
* @param currentTime Milliseconds since unix epoch.
|
||||
*/
|
||||
public void updateRaceTime(long currentTime) {
|
||||
this.raceClock.setUTCTime(currentTime);
|
||||
this.getRaceClock().setUTCTime(currentTime);
|
||||
}
|
||||
|
||||
|
||||
@@ -123,7 +148,7 @@ public class MockRace extends Race {
|
||||
public void updateRaceStatusEnum() {
|
||||
|
||||
//The millisecond duration of the race. Negative means it hasn't started, so we flip sign.
|
||||
long timeToStart = - this.raceClock.getDurationMilli();
|
||||
long timeToStart = - this.getRaceClock().getDurationMilli();
|
||||
|
||||
|
||||
if (timeToStart > Constants.RacePreStartTime) {
|
||||
@@ -194,7 +219,7 @@ public class MockRace extends Race {
|
||||
|
||||
|
||||
//The boat starts on the first leg of the race.
|
||||
boat.setCurrentLeg(this.legs.get(0));
|
||||
boat.setCurrentLeg(this.getLegs().get(0));
|
||||
|
||||
//Boats start with 0 knots speed.
|
||||
boat.setCurrentSpeed(0d);
|
||||
@@ -223,7 +248,7 @@ public class MockRace extends Race {
|
||||
private List<GPSCoordinate> getSpreadStartingPositions() {
|
||||
|
||||
//The first compound marker of the race - the starting gate.
|
||||
CompoundMark compoundMark = this.legs.get(0).getStartCompoundMark();
|
||||
CompoundMark compoundMark = this.getLegs().get(0).getStartCompoundMark();
|
||||
|
||||
//The position of the two markers from the compound marker.
|
||||
GPSCoordinate mark1Position = compoundMark.getMark1Position();
|
||||
@@ -485,7 +510,7 @@ public class MockRace extends Race {
|
||||
roundingChecks.get(0), boat.getPosition(), legBearing) &&
|
||||
gateCheck && boat.isBetweenGate(roundingMark, Mark.tempMark(roundingChecks.get(0)))) {
|
||||
boat.increaseRoundingStatus();
|
||||
if (boat.getCurrentLeg().getLegNumber() + 2 >= legs.size()){
|
||||
if (boat.getCurrentLeg().getLegNumber() + 2 >= getLegs().size()){
|
||||
//boat has finished race
|
||||
boat.increaseRoundingStatus();
|
||||
}
|
||||
@@ -503,7 +528,7 @@ public class MockRace extends Race {
|
||||
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);
|
||||
Leg nextLeg = this.getLegs().get(boat.getCurrentLeg().getLegNumber() + 1);
|
||||
boat.setCurrentLeg(nextLeg);
|
||||
break;
|
||||
}
|
||||
@@ -530,7 +555,7 @@ public class MockRace extends Race {
|
||||
gateCheck &&
|
||||
boat.isBetweenGate(roundingMark, Mark.tempMark(roundingChecks.get(0)))) {
|
||||
boat.increaseRoundingStatus();
|
||||
if (boat.getCurrentLeg().getLegNumber() + 2 >= legs.size()){
|
||||
if (boat.getCurrentLeg().getLegNumber() + 2 >= getLegs().size()){
|
||||
//boat has finished race
|
||||
boat.increaseRoundingStatus();
|
||||
}
|
||||
@@ -547,7 +572,7 @@ public class MockRace extends Race {
|
||||
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);
|
||||
Leg nextLeg = this.getLegs().get(boat.getCurrentLeg().getLegNumber() + 1);
|
||||
boat.setCurrentLeg(nextLeg);
|
||||
break;
|
||||
}
|
||||
@@ -585,7 +610,7 @@ public class MockRace extends Race {
|
||||
|
||||
GPSCoordinate roundCheck2;
|
||||
try{
|
||||
Leg nextLeg = legs.get(legs.indexOf(boat.getCurrentLeg()) + 1);
|
||||
Leg nextLeg = getLegs().get(getLegs().indexOf(boat.getCurrentLeg()) + 1);
|
||||
|
||||
GPSCoordinate startNextDirectionLinePoint = nextLeg.getStartCompoundMark().getMark1Position();
|
||||
GPSCoordinate endNextDirectionLinePoint = nextLeg.getEndCompoundMark().getMark1Position();
|
||||
@@ -681,7 +706,7 @@ public class MockRace extends Race {
|
||||
*/
|
||||
public void changeWindDirection() {
|
||||
|
||||
Wind nextWind = windGenerator.generateNextWind(raceWind.getValue());
|
||||
Wind nextWind = windGenerator.generateNextWind(windProperty().getValue());
|
||||
|
||||
setWind(nextWind);
|
||||
}
|
||||
@@ -702,13 +727,10 @@ public class MockRace extends Race {
|
||||
long timeFromNow = (long) (1000 * boat.calculateDistanceToNextMarker() / velocityToMark);
|
||||
|
||||
//Calculate time at which it will reach mark.
|
||||
ZonedDateTime timeAtMark = this.raceClock.getCurrentTime().plus(timeFromNow, ChronoUnit.MILLIS);
|
||||
ZonedDateTime timeAtMark = this.getRaceClock().getCurrentTime().plus(timeFromNow, ChronoUnit.MILLIS);
|
||||
boat.setEstimatedTimeAtNextMark(timeAtMark);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public List<CompoundMark> getCompoundMarks() {
|
||||
return compoundMarks;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,8 @@ public class RaceXMLCreator {
|
||||
public static String alterRaceToWind(String s, double degrees) throws XMLReaderException, InvalidRaceDataException, JAXBException, IOException, SAXException, ParserConfigurationException {
|
||||
RaceXMLReader reader = new RaceXMLReader(s, XMLFileType.ResourcePath);
|
||||
|
||||
XMLRace race = (XMLRace) XMLUtilities.xmlToClass(RaceXMLCreator.class.getClassLoader().getResourceAsStream(s),
|
||||
XMLRace race = XMLUtilities.xmlToClass(
|
||||
RaceXMLCreator.class.getClassLoader().getResourceAsStream(s),
|
||||
RaceXMLCreator.class.getClassLoader().getResource("mock/mockXML/schema/raceSchema.xsd"),
|
||||
XMLRace.class);
|
||||
|
||||
@@ -89,14 +90,7 @@ public class RaceXMLCreator {
|
||||
|
||||
alterRaceRotation(race, degreesToRotate);
|
||||
|
||||
JAXBContext context = JAXBContext.newInstance(XMLRace.class);
|
||||
Marshaller jaxbMarshaller = context.createMarshaller();
|
||||
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
|
||||
jaxbMarshaller.marshal(race, sw);
|
||||
return sw.toString();
|
||||
return XMLUtilities.classToXML(race);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user