Refactored RaceClock to act as central source of time and duration information throughout visualiser.

- StartController and RaceController use same RaceClock instance
- RaceClock updates automatically as Runnable for consistent operation
- Duration between starting and current time is now an observable property of RaceClock

#story[782]
main
cbt24 9 years ago
parent 12e457824d
commit a6adfb4960

@ -2,6 +2,7 @@ package seng302.Controllers;
import javafx.fxml.FXML;
import javafx.scene.layout.AnchorPane;
import seng302.Model.RaceClock;
import seng302.RaceDataSource;
import seng302.VisualiserInput;
@ -20,8 +21,8 @@ public class MainController extends Controller {
@FXML
ConnectionController connectionController;
public void beginRace(VisualiserInput visualiserInput) {
raceController.startRace(visualiserInput);
public void beginRace(VisualiserInput visualiserInput, RaceClock raceClock) {
raceController.startRace(visualiserInput, raceClock);
}
public void enterLobby(Socket socket) {

@ -121,7 +121,7 @@ public class RaceController extends Controller {
*
* @param visualiserInput input from network
*/
public void startRace(VisualiserInput visualiserInput) {
public void startRace(VisualiserInput visualiserInput, RaceClock raceClock) {
StreamedRace newRace = new StreamedRace(visualiserInput, this);
//newRace.initialiseBoats();
@ -146,8 +146,8 @@ public class RaceController extends Controller {
race.setVisible(true);
//Initialize save annotation array, fps listener, and annotation listeners
RaceClock raceClock = new RaceClock(visualiserInput.getCourse().getZonedDateTime());
timeZone.setText(raceClock.getTimeZone());
timer.textProperty().bind(raceClock.durationProperty());
initializeFPS();
initializeAnnotations();
@ -161,7 +161,7 @@ public class RaceController extends Controller {
* @param time time that the label will be updated to
*/
public void setTimer(String time) {
timer.setText(time);
//timer.setText(time);
}
/**

@ -22,6 +22,8 @@ import java.net.Socket;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
@ -41,14 +43,14 @@ public class StartController extends Controller implements Observer {
@FXML private Label timeZoneTime;
@FXML private Label timer;
@FXML private Label raceStatusLabel;
@FXML private int PRERACE_TIME = 10;
private ZonedDateTime startingTime;
//@FXML Button fifteenMinButton;
private RaceClock raceClock;
private StreamedCourse raceData;
private long timeLeft = 1;
private int raceStat;
private VisualiserInput visualiserInput;
@ -86,42 +88,21 @@ public class StartController extends Controller implements Observer {
boatCodeColumn.setCellValueFactory(new PropertyValueFactory<>("abbrev"));
}
/**
* Updates the calculated time to the timer label
*
* @param time The calculated time from calcTimer() method
*/
protected void updateTime(String time) {
Platform.runLater(() -> {
timer.setText(time);
});
}
/**
* Countdown timer until race starts.
*/
protected void countdownTimer() {
new AnimationTimer() {
long currentTime = System.currentTimeMillis();
long startTime = currentTime + PRERACE_TIME;
DateFormat timerFormat = new SimpleDateFormat("'Race Clock:' -HH:mm:ss");
@Override
public void handle(long arg0) {
timeLeft = startTime - currentTime;
raceStat = visualiserInput.getRaceStatus().getRaceStatus();
raceStatusLabel.setText("Race Status: " + visualiserInput.getRaceStatus().getRaceStatus());
if (raceStat==2 || raceStat == 3) {
updateTime("Race is starting...");
stop();
parent.beginRace(visualiserInput);
parent.beginRace(visualiserInput, raceClock);
startWrapper.setVisible(false);
start.setVisible(false);
} else {
updateTime(timerFormat.format(currentTime));
}
currentTime = System.currentTimeMillis();
}
}.start();
}
@ -129,26 +110,22 @@ public class StartController extends Controller implements Observer {
private void setRaceClock() {
raceClock = new RaceClock(raceData.getZonedDateTime());
timeZoneTime.textProperty().bind(raceClock.timeStringProperty());
new AnimationTimer() {
@Override
public void handle(long arg0) {
raceClock.updateTime();
}
}.start();
raceClock.run();
}
private void setStartingTime() {
String dateFormat = "'Starting time:' HH:mm dd/MM/YYYY";
Platform.runLater(()-> {
long utcTime = visualiserInput.getRaceStatus().getExpectedStartTime();
raceStartLabel.setText(DateTimeFormatter.ofPattern(dateFormat).format(raceClock.getLocalTime(utcTime)));
raceClock.setStartingTime(raceClock.getLocalTime(utcTime));
raceStartLabel.setText(DateTimeFormatter.ofPattern(dateFormat).format(raceClock.getStartingTime()));
timer.textProperty().bind(raceClock.durationProperty());
});
}
private void setCurrentTime() {
Platform.runLater(()->
raceClock.setTime(raceClock.getLocalTime(visualiserInput.getRaceStatus().getCurrentTime()))
raceClock.setUTCTime(visualiserInput.getRaceStatus().getCurrentTime())
);
}
@ -163,8 +140,8 @@ public class StartController extends Controller implements Observer {
}
if (((StreamedCourse) o).hasReadCourse()) {
Platform.runLater(() -> {
while(visualiserInput.getRaceStatus() == null);
setRaceClock();
while(visualiserInput.getRaceStatus() == null); // TODO - replace with observer on VisualiserInput
setStartingTime();
setCurrentTime();
});

@ -2,6 +2,7 @@ package seng302.Model;
import com.github.bfsmith.geotimezone.TimeZoneLookup;
import com.github.bfsmith.geotimezone.TimeZoneResult;
import javafx.animation.AnimationTimer;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import seng302.GPSCoordinate;
@ -17,15 +18,18 @@ import java.util.Date;
/**
* Created by Gondr on 19/04/2017.
*/
public class RaceClock {
public class RaceClock implements Runnable {
private long lastTime;
private ZoneId zoneId;
private ZonedDateTime time;
private ZonedDateTime startingTime;
private StringProperty timeString;
private StringProperty duration;
public RaceClock(ZonedDateTime zonedDateTime) {
this.zoneId = zonedDateTime.getZone();
this.timeString = new SimpleStringProperty();
this.duration = new SimpleStringProperty();
this.time = zonedDateTime;
setTime(time);
}
@ -44,6 +48,15 @@ public class RaceClock {
return LocalDateTime.now(zone).atZone(zone);
}
public void run() {
new AnimationTimer() {
@Override
public void handle(long now) {
updateTime();
}
}.start();
}
/**
* Sets time to arbitrary zoned time.
*
@ -53,6 +66,13 @@ public class RaceClock {
this.time = time;
this.timeString.set(DateTimeFormatter.ofPattern("HH:mm:ss dd/MM/YYYY Z").format(time));
this.lastTime = System.currentTimeMillis();
if(startingTime != null) {
long seconds = Duration.between(startingTime.toLocalDateTime(), time.toLocalDateTime()).getSeconds();
if(seconds < 0)
duration.set(String.format(String.format("Starting in: %02d:%02d:%02d", -seconds/3600, -(seconds%3600)/60, -seconds%60)));
else
duration.set(String.format(String.format("Time: %02d:%02d:%02d", seconds/3600, (seconds%3600)/60, seconds%60)));
}
}
/**
@ -64,6 +84,14 @@ public class RaceClock {
setTime(utcTime.toInstant().atZone(this.zoneId));
}
public ZonedDateTime getStartingTime() {
return startingTime;
}
public void setStartingTime(ZonedDateTime startingTime) {
this.startingTime = startingTime;
}
/**
* Get ZonedDateTime corresponding to local time zone and given UTC time.
* @param time time in mills
@ -83,6 +111,14 @@ public class RaceClock {
setTime(time);
}
public String getDuration() {
return duration.get();
}
public StringProperty durationProperty() {
return duration;
}
public String getTimeZone() {
return zoneId.toString();
}

Loading…
Cancel
Save