From b550afcca7a06b6508b7cc3fa9bc8332f4eb1198 Mon Sep 17 00:00:00 2001 From: Fan-Wu Yang Date: Wed, 20 Sep 2017 15:05:08 +1200 Subject: [PATCH] Merged race scaling with Http Match Discovery. --- .../main/java/mock/xml/RaceXMLCreator.java | 27 ++++++++++++++++--- .../main/java/shared/utils/JsonReader.java | 22 ++++++++++++++- .../network/HttpMatchBrowserClient.java | 3 +++ .../network/HttpMatchBrowserHost.java | 16 ++++++++++- 4 files changed, 63 insertions(+), 5 deletions(-) diff --git a/racevisionGame/src/main/java/mock/xml/RaceXMLCreator.java b/racevisionGame/src/main/java/mock/xml/RaceXMLCreator.java index cb508aff..b7b17319 100644 --- a/racevisionGame/src/main/java/mock/xml/RaceXMLCreator.java +++ b/racevisionGame/src/main/java/mock/xml/RaceXMLCreator.java @@ -154,6 +154,12 @@ public class RaceXMLCreator { } } + /** + * Gets an estimate of how long a race will take using an average Speed + * @param race race to estimate + * @param averageSpeed average speed that the boats move at + * @return the estimated amount of time it will take a boat to finish the race (skewed to minimum). + */ public static double getRaceLength(XMLRace race, double averageSpeed){ double raceRoundingTime = 5000; //5 seconds to round a mark double totalDistance = 0; //in nautical miles @@ -175,13 +181,24 @@ public class RaceXMLCreator { return totalTime; } + /** + * gets the destance between two marks + * @param a mark 1 + * @param b mark 2 + * @return + */ private static double getDistance(XMLMark a, XMLMark b){ GPSCoordinate coorda = new GPSCoordinate(a.getTargetLat(), a.getTargetLng()); GPSCoordinate coordb = new GPSCoordinate(b.getTargetLat(), b.getTargetLng()); return GPSCoordinate.calculateDistanceNauticalMiles(coorda, coordb); } - + /** + * Scales the race based on the windspeed the race is running at and the amount of time it should be completed in. + * @param race Race to scale + * @param windSpeed windspeed of the race, this is used with the polars + * @param milliseconds milliseconds the race should take. + */ private static void scaleRace(XMLRace race, double windSpeed, double milliseconds) { GPSCoordinate center = getCenter(race); //sort the compound marks @@ -214,8 +231,12 @@ public class RaceXMLCreator { } } - - + /** + * Scales a point from the the center(pivot) + * @param mark mark the scale + * @param center center as pivot + * @param scale scale to scale at. + */ private static void scalePoint(XMLMark mark, GPSCoordinate center, double scale){ double latDiff = mark.getTargetLat() - center.getLatitude(); double longDiff = mark.getTargetLng() - center.getLongitude(); diff --git a/racevisionGame/src/main/java/shared/utils/JsonReader.java b/racevisionGame/src/main/java/shared/utils/JsonReader.java index 476c3fed..44e55c98 100644 --- a/racevisionGame/src/main/java/shared/utils/JsonReader.java +++ b/racevisionGame/src/main/java/shared/utils/JsonReader.java @@ -9,10 +9,16 @@ import java.net.URL; import java.nio.charset.Charset; /** - * Created by Gondr on 23/05/2017. + * A helper class that has functions to read information from a url to json object. */ public class JsonReader { + /** + * Reads all data from a Reader + * @param rd reader to read from + * @return string that the reader has currently read + * @throws IOException if the reader is invalid + */ private static String readAll(Reader rd) throws IOException { StringBuilder sb = new StringBuilder(); int cp; @@ -22,6 +28,13 @@ public class JsonReader { return sb.toString(); } + /** + * Reads a Json Object from a URL + * @param url url to read from + * @return JSONObject that has been read + * @throws IOException if the reader cannot obtain information + * @throws JSONException if the read information is not json parsable. + */ public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException { InputStream is = new URL(url).openStream(); try { @@ -36,6 +49,13 @@ public class JsonReader { } } + /** + * Reads a Json Array from a URL + * @param url url to read from + * @return JSONArray that has been read + * @throws IOException if the reader cannot obtain information + * @throws JSONException if the read information is not json parsable. + */ public static JSONArray readJsonFromUrlArray(String url) throws IOException, JSONException { InputStream is = new URL(url).openStream(); try { diff --git a/racevisionGame/src/main/java/visualiser/network/HttpMatchBrowserClient.java b/racevisionGame/src/main/java/visualiser/network/HttpMatchBrowserClient.java index d6e41530..6384025f 100644 --- a/racevisionGame/src/main/java/visualiser/network/HttpMatchBrowserClient.java +++ b/racevisionGame/src/main/java/visualiser/network/HttpMatchBrowserClient.java @@ -15,6 +15,9 @@ import java.io.IOException; public class HttpMatchBrowserClient extends Thread { public ObservableList connections = FXCollections.observableArrayList(); + /** + * Get all the matches that have been running in the past 5 seconds. + */ @Override public void run() { while(!Thread.interrupted()) { diff --git a/racevisionGame/src/main/java/visualiser/network/HttpMatchBrowserHost.java b/racevisionGame/src/main/java/visualiser/network/HttpMatchBrowserHost.java index c0ad21d1..1e32e46c 100644 --- a/racevisionGame/src/main/java/visualiser/network/HttpMatchBrowserHost.java +++ b/racevisionGame/src/main/java/visualiser/network/HttpMatchBrowserHost.java @@ -17,7 +17,7 @@ import java.util.ArrayList; import java.util.List; /** - * Created by Gondr on 19/09/2017. + * Creates an Http connection that hosts a game */ public class HttpMatchBrowserHost extends Thread { private HttpClient httpClient; @@ -25,6 +25,12 @@ public class HttpMatchBrowserHost extends Thread { public static HttpMatchBrowserHost httpMatchBrowserHost = null; + /** + * Constructor, this sends out the creation message of the race. + * the thread should be run as soon as possible as the race is only valid for 5 seconds + * until it requires a heartbeat from the start function. + * @throws IOException if the hosting url is unreachable. + */ public HttpMatchBrowserHost() throws IOException { httpMatchBrowserHost = this; httpClient = HttpClients.createDefault(); @@ -38,6 +44,11 @@ public class HttpMatchBrowserHost extends Thread { System.out.println("Register Match"); } + /** + * Sends a post to a server. + * @param domain url of to send to + * @throws IOException if the url is unreachable. + */ public void sendHttp(String domain) throws IOException { HttpPost httppost = new HttpPost(domain); httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); @@ -58,6 +69,9 @@ public class HttpMatchBrowserHost extends Thread { } } + /** + * THe host starts sending out heartbeat messages every 2 seconds. + */ @Override public void run() { while(!Thread.interrupted()){