From 322ff740e2bfc408513c92533a24b2643c2708fd Mon Sep 17 00:00:00 2001 From: Michael Rausch Date: Thu, 20 Jul 2017 14:28:59 +1200 Subject: [PATCH] Added race start time to race XML - Added race start time to race XML - Added documentation - Removed test code in MainServerThread Tags: #story[984] --- .../MainServerThread.java | 30 -------------- src/main/java/seng302/models/xml/Boats.java | 11 ------ src/main/java/seng302/models/xml/Race.java | 30 ++++++++++++++ src/main/java/seng302/models/xml/Regatta.java | 3 ++ .../java/seng302/models/xml/XMLGenerator.java | 39 ++++++++++++++++++- .../server_config/xml_templates/race.ftlh | 4 +- 6 files changed, 72 insertions(+), 45 deletions(-) delete mode 100644 src/main/java/seng302/models/xml/Boats.java diff --git a/src/main/java/seng302/gameServerWithThreading/MainServerThread.java b/src/main/java/seng302/gameServerWithThreading/MainServerThread.java index e643738b..226e1dc8 100644 --- a/src/main/java/seng302/gameServerWithThreading/MainServerThread.java +++ b/src/main/java/seng302/gameServerWithThreading/MainServerThread.java @@ -2,17 +2,9 @@ package seng302.gameServerWithThreading; import seng302.gameServer.GameStages; import seng302.gameServer.GameState; -import seng302.models.Yacht; -import seng302.models.mark.Mark; -import seng302.models.mark.MarkType; -import seng302.models.mark.SingleMark; import seng302.models.stream.PacketBufferDelegate; import seng302.models.stream.StreamParser; import seng302.models.stream.packets.StreamPacket; -import seng302.models.xml.Boats; -import seng302.models.xml.Race; -import seng302.models.xml.Regatta; -import seng302.models.xml.XMLGenerator; import java.io.IOException; import java.net.ServerSocket; @@ -48,28 +40,6 @@ public class MainServerThread extends Thread implements PacketBufferDelegate{ public void run() { - // @TODO remove before commit - XMLGenerator g = new XMLGenerator(); - - Regatta r = new Regatta("Test regatta", 1.123, 4.456); - r.setMagneticVariation(12.1); - r.setUtcOffset(12); - - g.setRegatta(r); - - Race b = new Race(); - b.addBoat(new Yacht("SomeType", 123, "hid", "NZL", - "Emirates Team New Zealand", "NZL")); - - g.setRace(b); - - System.out.println("g.getBoatsAsXml() = " + g.getBoatsAsXml()); - g.getBoatsAsXml(); - - System.out.println("g.getRegattaAsXml() = " + g.getRegattaAsXml()); - - System.out.println("g.ragce() = " + g.getRaceAsXml()); - //You should handle interrupts in some way, so that the thread won't keep on forever if you exit the app. while (!isInterrupted()) { try { diff --git a/src/main/java/seng302/models/xml/Boats.java b/src/main/java/seng302/models/xml/Boats.java deleted file mode 100644 index ed913873..00000000 --- a/src/main/java/seng302/models/xml/Boats.java +++ /dev/null @@ -1,11 +0,0 @@ -package seng302.models.xml; - -import seng302.models.Yacht; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class Boats { - -} diff --git a/src/main/java/seng302/models/xml/Race.java b/src/main/java/seng302/models/xml/Race.java index b5047071..9be61f37 100644 --- a/src/main/java/seng302/models/xml/Race.java +++ b/src/main/java/seng302/models/xml/Race.java @@ -2,22 +2,52 @@ package seng302.models.xml; import seng302.models.Yacht; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.List; +/** + * A Race object that can be parsed into XML + */ public class Race { private List yachts; + private LocalDateTime startTime; public Race(){ yachts = new ArrayList<>(); + startTime = LocalDateTime.now(); } + /** + * Add a boat to the race + * @param yacht The boat to add + */ public void addBoat(Yacht yacht){ yachts.add(yacht); } + /** + * Get a list of boats in the race + * @return A List of boats + */ public List getBoats(){ return Collections.unmodifiableList(yachts); } + + /** + * Set the time until the race starts + * @param seconds The time in seconds until the race starts + */ + public void setRaceStartDelay(Integer seconds){ + startTime = startTime.plusMinutes(seconds); + } + + /** + * Get the time the race starts + * @return The time the race starts + */ + public String getRaceStartTime(){ + return startTime.toString(); + } } diff --git a/src/main/java/seng302/models/xml/Regatta.java b/src/main/java/seng302/models/xml/Regatta.java index 7c9dd448..733b7a0a 100644 --- a/src/main/java/seng302/models/xml/Regatta.java +++ b/src/main/java/seng302/models/xml/Regatta.java @@ -1,5 +1,8 @@ package seng302.models.xml; +/** + * A Race regatta that can be parsed into XML + */ public class Regatta { private final Double DEFAULT_ALTITUDE = 0d; private final Integer DEFAULT_REGATTA_ID = 0; diff --git a/src/main/java/seng302/models/xml/XMLGenerator.java b/src/main/java/seng302/models/xml/XMLGenerator.java index 918748e3..d74dfb31 100644 --- a/src/main/java/seng302/models/xml/XMLGenerator.java +++ b/src/main/java/seng302/models/xml/XMLGenerator.java @@ -8,6 +8,9 @@ import seng302.server.messages.XMLMessageSubType; import java.io.*; import java.net.URISyntaxException; +/** + * An XML generator to generate the Race, Boat, and Regatta XML dynamically + */ public class XMLGenerator { private static final String XML_TEMPLATE_DIR = "/server_config/xml_templates"; private static final String REGATTA_TEMPLATE_NAME = "regatta.ftlh"; @@ -34,18 +37,36 @@ public class XMLGenerator { } } + /** + * Create an instance of the XML Generator + */ public XMLGenerator(){ setupConfiguration(); } + /** + * Set the race regatta to send to players + * Note: This must be set before a regatta message can be generated + * @param regatta The race regatta + */ public void setRegatta(Regatta regatta){ this.regatta = regatta; } - + + /** + * Set the race to send to players + * Note: This must be set before a boat or race message can be generated + * @param race The race + */ public void setRace(Race race){ this.race = race; } + /** + * Parse an XML template and generate the output as a string + * @param templateName The templates file name + * @param type The XML message sub type + */ private String parseToXmlString(String templateName, XMLMessageSubType type) throws IOException, TemplateException { Template template; ByteArrayOutputStream os = new ByteArrayOutputStream(); @@ -78,6 +99,11 @@ public class XMLGenerator { } } + /** + * Get the race regatta as a string + * Note: Regatta must be set before calling this + * @return String containing the regatta XML, null if there was an error + */ public String getRegattaAsXml(){ String result = null; @@ -94,7 +120,11 @@ public class XMLGenerator { return result; } - + /** + * Get the boats XML as a string + * Note: Race must be set before calling this + * @return String containing the boats XML, null if there was an error + */ public String getBoatsAsXml() { String result = null; @@ -111,6 +141,11 @@ public class XMLGenerator { return result; } + /** + * Get the race XML as a string + * Note: Race must be set before calling this + * @return String containing the race XML, null if there was an error + */ public String getRaceAsXml() { String result = null; diff --git a/src/main/resources/server_config/xml_templates/race.ftlh b/src/main/resources/server_config/xml_templates/race.ftlh index 2a091262..6bdb6ef5 100644 --- a/src/main/resources/server_config/xml_templates/race.ftlh +++ b/src/main/resources/server_config/xml_templates/race.ftlh @@ -1,7 +1,7 @@ - 2015-08-29T11:27:15+02:00 - + ${raceStartTime} + 15082901 Fleet