Changed main loop so boats are looped through with a different starting boat

-This was necessary because one boat (the last one to be processed) would lag and jump, as sometimes there was not enough time for all location messages to be sent before the next lot were added.
- The solution was to add a boatOffset, which means while the boats are still  processed with3 coming after 2 coming after 1 etc, the loop starts with boat 0, then the next run through it wiull start with boat 1 and so on.

-This eliminated the lag (or at least spread it evenly across all the boats)

#story[778]
main
Erika Savell 9 years ago
parent 42ad1f78fc
commit 049f0f7331

@ -93,7 +93,7 @@ public class RaceData {
*/
private void appendRaceId() {
Element raceIdElement = doc.createElement("RaceID");
raceIdElement.appendChild(doc.createTextNode(dataSource.getRaceId()));
raceIdElement.appendChild(doc.createTextNode(Integer.toString(dataSource.getRaceId())));
rootElement.appendChild(raceIdElement);
}

@ -20,7 +20,7 @@ public interface RaceDataSource {
List<Marker> getMarkers();
String getRaceId();
int getRaceId();
String getRaceType();

@ -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 int raceID;
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<>();
@ -70,10 +70,8 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
}
public void readID() {
NodeList race = doc.getElementsByTagName("race");
raceID = getTextValueOfNode((Element) race.item(0), "raceId");
raceID = Integer.parseInt(getTextValueOfNode((Element) race.item(0), "raceId"));
}
/**
@ -358,7 +356,7 @@ public class RaceXMLReader extends XMLReader implements RaceDataSource {
return mapBottomRight;
}
public String getRaceId() {
public int getRaceId() {
return raceID;
}

@ -38,6 +38,7 @@ public class Race implements Runnable {
private int raceId;
private int dnfChance = 0; //percentage chance a boat fails at each checkpoint
private MockOutput mockOutput;
private static int boatOffset = 0;
/**
@ -47,11 +48,12 @@ 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<Boat> boats, List<Leg> legs, int scaleFactor, MockOutput mockOutput) {
public Race(List<Boat> boats, List<Leg> legs, int raceID, int scaleFactor, MockOutput mockOutput) {
this.startingBoats = FXCollections.observableArrayList(boats);
this.legs = legs;
this.legs.add(new Leg("Finish", this.legs.size()));
this.raceId = raceID;
this.scaleFactor = scaleFactor;
this.mockOutput = mockOutput;
@ -63,7 +65,7 @@ public class Race implements Runnable {
}
public Race(RaceDataSource raceData, int scaleFactor, MockOutput mockOutput) {
this(raceData.getBoats(), raceData.getLegs(), scaleFactor, mockOutput);
this(raceData.getBoats(), raceData.getLegs(), raceData.getRaceId(), scaleFactor, mockOutput);
}
/**
@ -163,7 +165,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 (Boat boat : startingBoats) {
for (int i = 0; i < startingBoats.size(); i++) {
Boat boat = startingBoats.get((i + boatOffset) % startingBoats.size());
if (boat != null) {
//Update position.
if (boat.getTimeFinished() < 0) {
@ -177,6 +180,7 @@ public class Race implements Runnable {
stop();
}
}
boatOffset = (boatOffset + 1) % (startingBoats.size());
RaceStatus raceStatus = new RaceStatus(totalTimeElapsed, raceId, 3, 2, boatStatusMessages);
mockOutput.parseRaceStatus(raceStatus);
}

@ -1,52 +0,0 @@
package seng302.Model;
import com.github.bfsmith.geotimezone.TimeZoneLookup;
import com.github.bfsmith.geotimezone.TimeZoneResult;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
/**
* Created by Gondr on 19/04/2017.
*/
public class RaceClock {
private StringProperty time;
private DateTimeFormatter dateTimeFormatter;
private String timeZone;
private ZoneId zoneId;
public RaceClock(GPSCoordinate gpsCoordinate) {
TimeZoneLookup timeZoneLookup = new TimeZoneLookup();
TimeZoneResult timeZoneResult = timeZoneLookup.getTimeZone(gpsCoordinate.getLatitude(), gpsCoordinate.getLongitude());
zoneId = ZoneId.of(timeZoneResult.getResult());
LocalDateTime localDateTime = LocalDateTime.now(zoneId);
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM HH:mm:ss z");
time = new SimpleStringProperty(dateTimeFormatter.format(zonedDateTime));
DateTimeFormatter timeZoneFormatter = DateTimeFormatter.ofPattern("z");
timeZone = timeZoneFormatter.format(zonedDateTime);
}
public void updateTime() {
LocalDateTime localDateTime = LocalDateTime.now(zoneId);
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
time.setValue(dateTimeFormatter.format(zonedDateTime));
}
public String getTime() {
return time.get();
}
public StringProperty timeProperty() {
return time;
}
public String getTimeZone() {
return timeZone;
}
}

@ -75,7 +75,7 @@ public class RaceDataTest {
String newId = result.substring(result.indexOf("<RaceID>") + 8, result.indexOf("</RaceID>"));
String newRaceType = result.substring(result.indexOf("<RaceType>") + 10, result.indexOf("</RaceType>"));
assertTrue(raceDataSource.getRaceId().equals(newId));
assertTrue(raceDataSource.getRaceId() == Integer.parseInt(newId));
assertTrue(raceDataSource.getRaceType().equals(newRaceType));
}

@ -30,7 +30,6 @@ public class AC35DumpReader {
packets = new ArrayList<>();
System.out.println(dump.length);
readAllPackets();
}

Loading…
Cancel
Save