mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Loading course mark order from RaceXML
- Re-engineered code to work with the new marks - Fixed bug where race order wasn't correct (added RacePosition class to fix) - Rewrote tests to work with new RacePosition class Tags: #story[1124] (Task 1)
This commit is contained in:
@@ -1,14 +1,5 @@
|
||||
package seng302.model.mark;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.w3c.dom.Document;
|
||||
@@ -19,11 +10,21 @@ import seng302.model.stream.xml.parser.RaceXMLData;
|
||||
import seng302.utilities.XMLGenerator;
|
||||
import seng302.utilities.XMLParser;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Class to hold the order of the marks in the race.
|
||||
*/
|
||||
public class MarkOrder {
|
||||
private List<CompoundMark> raceMarkOrder;
|
||||
private List<Mark> raceMarkOrder;
|
||||
private Logger logger = LoggerFactory.getLogger(MarkOrder.class);
|
||||
|
||||
public MarkOrder(){
|
||||
@@ -34,7 +35,7 @@ public class MarkOrder {
|
||||
* @return An ordered list of marks in the race
|
||||
* OR null if the mark order could not be loaded
|
||||
*/
|
||||
public List<CompoundMark> getMarkOrder(){
|
||||
public List<Mark> getMarkOrder(){
|
||||
if (raceMarkOrder == null){
|
||||
logger.warn("Race order accessed but not instantiated");
|
||||
return null;
|
||||
@@ -45,23 +46,25 @@ public class MarkOrder {
|
||||
|
||||
/**
|
||||
* Returns the mark in the race after the previous mark
|
||||
* @param previous The previous mark
|
||||
* @return the next mark
|
||||
* OR null if there is no next mark
|
||||
* @param position The current race position
|
||||
* @return the next race position
|
||||
* OR null if there is no position
|
||||
*/
|
||||
public CompoundMark getNextMark(CompoundMark previous){
|
||||
for (int i = 0; i < raceMarkOrder.size(); i++){
|
||||
CompoundMark mark = raceMarkOrder.get(i);
|
||||
public RacePosition getNextPosition(RacePosition position){
|
||||
Mark previousMark = position.getNextMark();
|
||||
Mark nextMark;
|
||||
|
||||
if (i + 1 >= raceMarkOrder.size()){
|
||||
return null;
|
||||
}
|
||||
if (position.getPositionIndex() + 1 >= raceMarkOrder.size() - 1){
|
||||
RacePosition nextRacePosition = new RacePosition(raceMarkOrder.size() - 1, null, previousMark);
|
||||
nextRacePosition.setFinishingLeg();
|
||||
|
||||
if (mark.equals(previous)){
|
||||
return raceMarkOrder.get(i+1);
|
||||
}
|
||||
return nextRacePosition;
|
||||
}
|
||||
return null;
|
||||
|
||||
Integer nextPositionIndex = position.getPositionIndex() + 1;
|
||||
RacePosition nextRacePosition = new RacePosition(nextPositionIndex, raceMarkOrder.get(nextPositionIndex), previousMark);
|
||||
|
||||
return nextRacePosition;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,7 +72,7 @@ public class MarkOrder {
|
||||
* @param xml An AC35 RaceXML
|
||||
* @return An ordered list of marks in the race
|
||||
*/
|
||||
private List<CompoundMark> loadRaceOrderFromXML(String xml){
|
||||
private List<Mark> loadRaceOrderFromXML(String xml){
|
||||
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder db;
|
||||
@@ -87,14 +90,32 @@ public class MarkOrder {
|
||||
|
||||
if (data != null){
|
||||
logger.debug("Loaded RaceXML for mark order");
|
||||
List<CompoundMark> course = new ArrayList<>(data.getCompoundMarks().values());
|
||||
course.sort(Comparator.comparingInt(CompoundMark::getId));
|
||||
List<Corner> corners = data.getMarkSequence();
|
||||
Map<Integer, CompoundMark> marks = data.getCompoundMarks();
|
||||
List<Mark> course = new ArrayList<>();
|
||||
|
||||
for (Corner corner : corners){
|
||||
CompoundMark compoundMark = marks.get(corner.getCompoundMarkID());
|
||||
course.add(compoundMark.getMarks().get(0));
|
||||
}
|
||||
|
||||
return course;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The first position in the race
|
||||
*/
|
||||
public RacePosition getFirstPosition(){
|
||||
if (raceMarkOrder.size() > 0){
|
||||
return new RacePosition(-1, raceMarkOrder.get(0), null);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the raceXML and mark order
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package seng302.model.mark;
|
||||
|
||||
/**
|
||||
* Represents a boats position between two marks
|
||||
*/
|
||||
public class RacePosition {
|
||||
private Integer positionIndex;
|
||||
private Mark nextMark;
|
||||
private Mark previousMark;
|
||||
private Boolean isFinishingLeg;
|
||||
|
||||
public RacePosition(Integer positionIndex, Mark nextMark, Mark previousMark){
|
||||
this.positionIndex = positionIndex;
|
||||
this.nextMark = nextMark;
|
||||
this.previousMark = previousMark;
|
||||
isFinishingLeg = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The position of the boat (0...number of marks in race - 1)
|
||||
*/
|
||||
public Integer getPositionIndex(){
|
||||
return positionIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The mark the boat is heading to
|
||||
* will return NULL if this is the finishing legg
|
||||
*/
|
||||
public Mark getNextMark(){
|
||||
return nextMark;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The mark the boat is heading away from,
|
||||
* Will return NULL if this is the starting leg
|
||||
*/
|
||||
public Mark getPreviousMark(){
|
||||
return previousMark;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a flag that this is the last leg in the race
|
||||
*/
|
||||
public void setFinishingLeg(){
|
||||
isFinishingLeg = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if this is the last leg in the race
|
||||
*/
|
||||
public boolean getIsFinishingLeg() {
|
||||
return isFinishingLeg;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user