From 48d58ea6602992c035d288ddf5ce68706938cb06 Mon Sep 17 00:00:00 2001 From: Kusal Ekanayake Date: Fri, 19 May 2017 18:08:26 +1200 Subject: [PATCH] Sparklines now update when a boat passes a mark. There are issue of initialising the sparkline before the race starts and when the race is being viewed when it starts. It works great though when you join a race halfway. #story[952] --- src/main/java/seng302/App.java | 2 +- .../controllers/RaceViewController.java | 54 +++++++++---------- src/main/java/seng302/models/Yacht.java | 16 ++---- 3 files changed, 32 insertions(+), 40 deletions(-) diff --git a/src/main/java/seng302/App.java b/src/main/java/seng302/App.java index 108d44aa..bd39a80d 100644 --- a/src/main/java/seng302/App.java +++ b/src/main/java/seng302/App.java @@ -62,7 +62,7 @@ public class App extends Application { } //Change the StreamReceiver in this else block to change the default data source. else{ - //sr = new StreamReceiver("localhost", 4949, "RaceStream"); +// sr = new StreamReceiver("localhost", 4949, "RaceStream"); // sr = new StreamReceiver("csse-s302staff.canterbury.ac.nz", 4941, "RaceStream"); sr = new StreamReceiver("livedata.americascup.com", 4941, "RaceStream"); } diff --git a/src/main/java/seng302/controllers/RaceViewController.java b/src/main/java/seng302/controllers/RaceViewController.java index 6eb3439f..5adba02c 100644 --- a/src/main/java/seng302/controllers/RaceViewController.java +++ b/src/main/java/seng302/controllers/RaceViewController.java @@ -70,7 +70,7 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel private Timeline timerTimeline; private Race race; private Stage stage; - private ArrayList> sparklineData = new ArrayList<>(); + private static HashMap> sparklineData = new HashMap<>(); private ArrayList positions; private int time; @@ -193,25 +193,39 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel for (Yacht yacht: startingBoats){ XYChart.Series yachtData = new XYChart.Series<>(); yachtData.setName(yacht.getBoatName()); - yachtData.getData().add(new XYChart.Data<>(Integer.toString(time), 1 + startingBoats.size() - Double.parseDouble(yacht.getPosition()))); - sparklineData.add(yachtData); + yachtData.getData().add(new XYChart.Data<>(Integer.toString(yacht.getLegNumber()), Double.parseDouble(yacht.getPosition()))); + sparklineData.put(yacht.getSourceID(),yachtData); } - for (Series spark: sparklineData){ + List> positions = new ArrayList>(sparklineData.values()); + + Collections.sort(positions, new Comparator() { + @Override + public int compare(Series o1, Series o2) { + Integer leg1 = Integer.parseInt( ((XYChart.Data) o1.getData().get(o1.getData().size()-1)).getXValue()); + Integer leg2 = Integer.parseInt( ((XYChart.Data) o2.getData().get(o2.getData().size()-1)).getXValue()); + if (leg2 < leg1){ + return 1; + } else { + return -1; + } + } + }); + for (Series spark: positions){ raceSparkLine.getData().add(spark); } - Set nodes = raceSparkLine.lookupAll(".series"); - for (Node n : nodes) { - StringBuilder style = new StringBuilder(); - style.append("-fx-stroke: red; -fx-background-color: red, white; "); - n.setStyle(style.toString()); - } - time++; + + } + + + public static void updateYachtPositionSparkline(Yacht yacht, Integer legNumber){ + XYChart.Series positionData = sparklineData.get(yacht.getSourceID()); + positionData.getData().add(new XYChart.Data<>(Integer.toString(legNumber), Double.parseDouble(yacht.getPosition()))); } public void checkForPositionChange(){ // initialize position array - if (positions.size() == 0 && startingBoats.size() == 6){ + if (startingBoats.size() == 6 && raceSparkLine.getData().size() != 6){ Boolean positionsAssigned = true; for (Yacht yacht: startingBoats){ if (Objects.equals(yacht.getPosition(), "-")) { @@ -220,28 +234,12 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel } if (positionsAssigned){ initializeSparkline(); - positions.addAll(startingBoats.stream().map(Yacht::getPosition).collect(Collectors.toList())); } - } else { - ArrayList currentPositions = startingBoats.stream().map(Yacht::getPosition).collect(Collectors.toCollection(ArrayList::new)); - if (!positions.equals(currentPositions)){ - updateSparkLine(); - } - positions.clear(); - positions.addAll(startingBoats.stream().map(Yacht::getPosition).collect(Collectors.toList())); } } - public void updateSparkLine(){ - for (int i = 0; i < startingBoats.size();i++){ - if (!Objects.equals(startingBoats.get(i).getPosition(), "-")) { - sparklineData.get(i).getData().add(new XYChart.Data<>(Integer.toString(time), 1 + startingBoats.size() - Double.parseDouble(startingBoats.get(i).getPosition()))); - } - } - time++; - } /** * Initalises a timer which updates elements of the RaceView such as wind direction, boat diff --git a/src/main/java/seng302/models/Yacht.java b/src/main/java/seng302/models/Yacht.java index 5777fc25..ec1110a3 100644 --- a/src/main/java/seng302/models/Yacht.java +++ b/src/main/java/seng302/models/Yacht.java @@ -4,6 +4,7 @@ import java.util.concurrent.ConcurrentLinkedQueue; import javafx.scene.chart.XYChart; import javafx.scene.chart.XYChart.Series; import javafx.scene.paint.Color; +import seng302.controllers.RaceViewController; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -20,7 +21,6 @@ public class Yacht { private double velocity; private int index; - private ConcurrentLinkedQueue dataQ1; private String boatType; private Integer sourceID; @@ -62,7 +62,6 @@ public class Yacht { this.shortName = shortName; this.sourceID = id; index = 0; - dataQ1 = new ConcurrentLinkedQueue<>(); } @@ -74,7 +73,6 @@ public class Yacht { this.boatName = boatName; this.country = country; index = 0; - dataQ1 = new ConcurrentLinkedQueue<>(); } public String getBoatType() { @@ -109,7 +107,11 @@ public class Yacht { } public void setLegNumber(Integer legNumber) { + if (legNumber != this.legNumber && colour != null && position != "-") { + RaceViewController.updateYachtPositionSparkline(this, legNumber); + } this.legNumber = legNumber; + } public Integer getPenaltiesAwarded() { @@ -152,14 +154,6 @@ public class Yacht { } public void setPosition(String position) { - try { - if (Integer.parseInt(position) != Integer.parseInt(this.position)){ - dataQ1.add(Integer.parseInt(position)); - System.out.println("new position found for " + boatName + " in position " + position + " old(" + this.position + ")"); - } - } catch (NumberFormatException e) { - System.out.println("No position found"); - } this.position = position; }