mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Loading course mark order from RaceXML
- Mark order is read from the generated RaceXML and stored - Added .getNextMark() to get the next mark in the race - Added .equals() and .hashCode() for Marks Tags: #story[1124] (Task 1)
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
package seng302.gameServer;
|
||||
|
||||
import seng302.client.ClientPacketParser;
|
||||
import seng302.models.Player;
|
||||
import seng302.models.Yacht;
|
||||
import seng302.server.messages.BoatActionType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import seng302.client.ClientPacketParser;
|
||||
import seng302.models.Player;
|
||||
import seng302.models.Yacht;
|
||||
import seng302.server.messages.BoatActionType;
|
||||
|
||||
/**
|
||||
* A Static class to hold information about the current state of the game (model)
|
||||
@@ -43,7 +44,6 @@ public class GameState implements Runnable {
|
||||
yachts = new HashMap<>();
|
||||
|
||||
new Thread(this).start();
|
||||
|
||||
}
|
||||
|
||||
public static String getHostIpAddress() {
|
||||
|
||||
@@ -7,8 +7,6 @@ import seng302.utilities.GeoPoint;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
import java.lang.Math;
|
||||
|
||||
/**
|
||||
* CanvasMap retrieves a map image with given geo boundary from Google Map server.
|
||||
* By passing a rectangle like geo boundary, it returns a map image with the
|
||||
|
||||
@@ -145,4 +145,42 @@ public abstract class Mark {
|
||||
public int getCompoundMarkID() {
|
||||
return compoundMarkID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if (other == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!(other instanceof Mark)){
|
||||
return false;
|
||||
}
|
||||
|
||||
Mark otherMark = (Mark) other;
|
||||
|
||||
if (otherMark.getLatitude() != getLatitude() || otherMark.getLongitude() != getLongitude()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (otherMark.getCompoundMarkID() != getCompoundMarkID()){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (otherMark.getId() != getId()){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!otherMark.getName().equals(name)){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getName().hashCode() + getMarkType().hashCode() +
|
||||
Integer.hashCode(getCompoundMarkID()) + Double.hashCode(getLatitude()) +
|
||||
Double.hashCode(getLongitude());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
package seng302.models.mark;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import seng302.models.stream.XMLParser;
|
||||
import seng302.models.xml.Race;
|
||||
import seng302.models.xml.XMLGenerator;
|
||||
import seng302.server.messages.XMLMessageSubType;
|
||||
|
||||
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.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Class to hold the order of the marks in the race.
|
||||
*/
|
||||
public class MarkOrder {
|
||||
private List<Mark> raceMarkOrder;
|
||||
private Logger logger = LoggerFactory.getLogger(MarkOrder.class);
|
||||
|
||||
public MarkOrder(){
|
||||
loadRaceProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return An ordered list of marks in the race
|
||||
* OR null if the mark order could not be loaded
|
||||
*/
|
||||
public List<Mark> getMarkOrder(){
|
||||
if (raceMarkOrder == null){
|
||||
logger.warn("Race order accessed but not instantiated");
|
||||
return null;
|
||||
}
|
||||
|
||||
return Collections.unmodifiableList(raceMarkOrder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public Mark getNextMark(Mark previous){
|
||||
for (int i = 0; i < raceMarkOrder.size(); i++){
|
||||
Mark mark = raceMarkOrder.get(i);
|
||||
|
||||
if (i + 1 >= raceMarkOrder.size()){
|
||||
return null;
|
||||
}
|
||||
|
||||
if (mark.equals(previous)){
|
||||
return raceMarkOrder.get(i+1);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the race order from an XML string
|
||||
* @param xml An AC35 RaceXML
|
||||
* @return An ordered list of marks in the race
|
||||
*/
|
||||
private List<Mark> loadRaceOrderFromXML(String xml){
|
||||
XMLParser xmlParser = new XMLParser();
|
||||
XMLParser.RaceXMLObject raceXMLObject;
|
||||
|
||||
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder db;
|
||||
Document doc;
|
||||
|
||||
try {
|
||||
db = dbf.newDocumentBuilder();
|
||||
doc = db.parse(new InputSource(new StringReader(xml)));
|
||||
} catch (ParserConfigurationException | IOException | SAXException e) {
|
||||
logger.error("Failed to read generated race XML");
|
||||
return null;
|
||||
}
|
||||
|
||||
xmlParser.constructXML(doc , XMLMessageSubType.RACE.getType());
|
||||
raceXMLObject = xmlParser.getRaceXML();
|
||||
|
||||
if (raceXMLObject != null){
|
||||
logger.debug("Loaded RaceXML for mark order");
|
||||
return raceXMLObject.getNonDupCompoundMarks();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the raceXML and mark order
|
||||
*/
|
||||
private void loadRaceProperties(){
|
||||
XMLGenerator generator = new XMLGenerator();
|
||||
|
||||
generator.setRace(new Race());
|
||||
|
||||
String raceXML = generator.getRaceAsXml();
|
||||
|
||||
if (raceXML == null){
|
||||
logger.error("Failed to generate raceXML (for race properties)");
|
||||
return;
|
||||
}
|
||||
|
||||
raceMarkOrder = loadRaceOrderFromXML(raceXML);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package seng302.server.messages;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.Arrays;
|
||||
|
||||
Reference in New Issue
Block a user