Refactored Race, MockRace, and VisualiserRace to use RaceClock instead of keeping their own timers.
Moved FPS tracking to Race class, so both VisualiserRace and MockRace can monitor their FPS. LatestMessages is now observable. It notifies observers when an XMLMessage is received. Boat now has StringProperty for name and country/abbreviation. Moved the MockRace timescale value to Constants.RaceTimeScale. This is passed in to MockRace on construction. Tidied up StartController. Copied the visualiser's resources into the resources folder. Refactored RaceClock. Added comments. Tidied code a bit. Moved to shared.model. Started work on RaceController.
This commit is contained in:
@@ -2,51 +2,72 @@ package visualiser.Controllers;
|
||||
|
||||
import javafx.animation.AnimationTimer;
|
||||
import javafx.application.Platform;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.TableColumn;
|
||||
import javafx.scene.control.TableView;
|
||||
import javafx.scene.control.cell.PropertyValueFactory;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.paint.Color;
|
||||
import network.Messages.Enums.RaceStatusEnum;
|
||||
import network.Messages.LatestMessages;
|
||||
import shared.dataInput.*;
|
||||
import shared.exceptions.InvalidBoatDataException;
|
||||
import shared.exceptions.InvalidRaceDataException;
|
||||
import shared.exceptions.InvalidRegattaDataException;
|
||||
import shared.exceptions.XMLReaderException;
|
||||
import visualiser.app.VisualiserInput;
|
||||
import visualiser.model.RaceClock;
|
||||
import visualiser.model.VisualiserBoat;
|
||||
import visualiser.model.VisualiserRace;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.URL;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Controller to for waiting for the race to start
|
||||
* Controller to for waiting for the race to start.
|
||||
*/
|
||||
public class StartController extends Controller implements Observer {
|
||||
|
||||
@FXML private GridPane start;
|
||||
@FXML private AnchorPane startWrapper;
|
||||
|
||||
/**
|
||||
* The name of the race/regatta.
|
||||
*/
|
||||
@FXML private Label raceTitleLabel;
|
||||
|
||||
/**
|
||||
* The time the race starts at.
|
||||
*/
|
||||
@FXML private Label raceStartLabel;
|
||||
|
||||
/**
|
||||
* The current time at the race location.
|
||||
*/
|
||||
@FXML private Label timeZoneTime;
|
||||
|
||||
/**
|
||||
* Time until the race starts.
|
||||
*/
|
||||
@FXML private Label timer;
|
||||
|
||||
@FXML private TableView<VisualiserBoat> boatNameTable;
|
||||
@FXML private TableColumn<VisualiserBoat, String> boatNameColumn;
|
||||
@FXML private TableColumn<VisualiserBoat, String> boatCodeColumn;
|
||||
@FXML private Label timeZoneTime;
|
||||
@FXML private Label timer;
|
||||
|
||||
/**
|
||||
* The status of the race.
|
||||
*/
|
||||
@FXML private Label raceStatusLabel;
|
||||
|
||||
|
||||
private RaceClock raceClock;
|
||||
|
||||
private int raceStat;
|
||||
|
||||
/**
|
||||
* The object used to read packets from the connected server.
|
||||
*/
|
||||
private VisualiserInput visualiserInput;
|
||||
|
||||
/**
|
||||
@@ -54,41 +75,159 @@ public class StartController extends Controller implements Observer {
|
||||
*/
|
||||
private VisualiserRace visualiserRace;
|
||||
|
||||
///Tracks whether the race has been started (that is, has startRaceNoScaling() be called).
|
||||
private boolean hasRaceStarted = false;
|
||||
/**
|
||||
* An array of colors used to assign colors to each boat - passed in to the VisualiserRace constructor.
|
||||
*/
|
||||
List<Color> colors = new ArrayList<>(Arrays.asList(
|
||||
Color.BLUEVIOLET,
|
||||
Color.BLACK,
|
||||
Color.RED,
|
||||
Color.ORANGE,
|
||||
Color.DARKOLIVEGREEN,
|
||||
Color.LIMEGREEN,
|
||||
Color.PURPLE,
|
||||
Color.DARKGRAY,
|
||||
Color.YELLOW
|
||||
));
|
||||
|
||||
|
||||
//Tracks whether or not a clock has been created and setup, which occurs after receiving enough information.
|
||||
private boolean hasCreatedClock = false;
|
||||
|
||||
/**
|
||||
* Begins the race with a scale factor of 1
|
||||
* Ctor.
|
||||
*/
|
||||
private void startRaceNoScaling() {
|
||||
//while(visualiserInput.getRaceStatus() == null);//TODO probably remove this.
|
||||
|
||||
countdownTimer();
|
||||
public StartController() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources){
|
||||
this.visualiserRace = new VisualiserRace();
|
||||
raceData.addObserver(this);
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Starts the race.
|
||||
* Called once we have received all XML files from the server.
|
||||
* @param latestMessages The set of latest race messages to use for race.
|
||||
* @throws XMLReaderException Thrown if XML file cannot be parsed.
|
||||
* @throws InvalidRaceDataException Thrown if XML file cannot be parsed.
|
||||
* @throws InvalidBoatDataException Thrown if XML file cannot be parsed.
|
||||
* @throws InvalidRegattaDataException Thrown if XML file cannot be parsed.
|
||||
*/
|
||||
private void startRace(LatestMessages latestMessages) throws XMLReaderException, InvalidRaceDataException, InvalidBoatDataException, InvalidRegattaDataException {
|
||||
|
||||
//Create data sources from latest messages for the race.
|
||||
RaceDataSource raceDataSource = new RaceXMLReader(latestMessages.getRaceXMLMessage().getXmlMessage());
|
||||
BoatDataSource boatDataSource = new BoatXMLReader(latestMessages.getBoatXMLMessage().getXmlMessage());
|
||||
RegattaDataSource regattaDataSource = new RegattaXMLReader(latestMessages.getRegattaXMLMessage().getXmlMessage());
|
||||
|
||||
//Create race.
|
||||
this.visualiserRace = new VisualiserRace(boatDataSource, raceDataSource, regattaDataSource, latestMessages, this.colors);
|
||||
|
||||
|
||||
//Initialise the boat table.
|
||||
initialiseBoatTable(this.visualiserRace);
|
||||
|
||||
//Initialise the race name.
|
||||
initialiseRaceName(this.visualiserRace);
|
||||
|
||||
//Initialises the race clock.
|
||||
initialiseRaceClock(this.visualiserRace);
|
||||
|
||||
//Starts the race countdown timer.
|
||||
countdownTimer();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public AnchorPane startWrapper(){
|
||||
return startWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiliases the tables that are to be shown on the pane
|
||||
*/
|
||||
private void initialiseTables() {
|
||||
List<VisualiserBoat> boats = raceData.getBoats();
|
||||
ObservableList<VisualiserBoat> observableBoats = FXCollections.observableArrayList(boats);
|
||||
|
||||
boatNameTable.setItems(observableBoats);
|
||||
boatNameColumn.setCellValueFactory(cellData -> cellData.getValue().getName());
|
||||
boatCodeColumn.setCellValueFactory(new PropertyValueFactory<>("abbrev"));
|
||||
/**
|
||||
* Initialises the boat table that is to be shown on the pane.
|
||||
* @param visualiserRace The race to get data from.
|
||||
*/
|
||||
private void initialiseBoatTable(VisualiserRace visualiserRace) {
|
||||
|
||||
//Get the boats.
|
||||
ObservableList<VisualiserBoat> boats = visualiserRace.getBoats();
|
||||
|
||||
//Populate table.
|
||||
boatNameTable.setItems(boats);
|
||||
boatNameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
|
||||
boatCodeColumn.setCellValueFactory(cellData -> cellData.getValue().countryProperty());
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialises the race name which is shown on the pane.
|
||||
* @param visualiserRace The race to get data from.
|
||||
*/
|
||||
private void initialiseRaceName(VisualiserRace visualiserRace) {
|
||||
|
||||
raceTitleLabel.setText(visualiserRace.getRegattaName());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialises the race clock/timer labels for the start time, current time, and remaining time.
|
||||
* @param visualiserRace The race to get data from.
|
||||
*/
|
||||
private void initialiseRaceClock(VisualiserRace visualiserRace) {
|
||||
|
||||
//Start time.
|
||||
initialiseRaceClockStartTime(visualiserRace);
|
||||
|
||||
//Current time.
|
||||
initialiseRaceClockCurrentTime(visualiserRace);
|
||||
|
||||
//Remaining time.
|
||||
initialiseRaceClockDuration(visualiserRace);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initialises the race current time label.
|
||||
* @param visualiserRace The race to get data from.
|
||||
*/
|
||||
private void initialiseRaceClockStartTime(VisualiserRace visualiserRace) {
|
||||
|
||||
visualiserRace.getRaceClock().startingTimeProperty().addListener((observable, oldValue, newValue) -> {
|
||||
Platform.runLater(() -> {
|
||||
raceStartLabel.setText(newValue);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initialises the race current time label.
|
||||
* @param visualiserRace The race to get data from.
|
||||
*/
|
||||
private void initialiseRaceClockCurrentTime(VisualiserRace visualiserRace) {
|
||||
|
||||
visualiserRace.getRaceClock().currentTimeProperty().addListener((observable, oldValue, newValue) -> {
|
||||
Platform.runLater(() -> {
|
||||
timeZoneTime.setText(newValue);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialises the race duration label.
|
||||
* @param visualiserRace The race to get data from.
|
||||
*/
|
||||
private void initialiseRaceClockDuration(VisualiserRace visualiserRace) {
|
||||
|
||||
visualiserRace.getRaceClock().durationProperty().addListener((observable, oldValue, newValue) -> {
|
||||
Platform.runLater(() -> {
|
||||
timer.setText(newValue);
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,103 +237,61 @@ public class StartController extends Controller implements Observer {
|
||||
new AnimationTimer() {
|
||||
@Override
|
||||
public void handle(long arg0) {
|
||||
raceStat = visualiserInput.getRaceStatus().getRaceStatus();
|
||||
raceStatusLabel.setText("Race Status: " + visualiserInput.getRaceStatus().getRaceStatus());
|
||||
if (raceStat==2 || raceStat == 3) {
|
||||
|
||||
//Get the current race status.
|
||||
RaceStatusEnum raceStatus = visualiserRace.getRaceStatusEnum();
|
||||
|
||||
//Display it.
|
||||
raceStatusLabel.setText("Race Status: " + raceStatus.name());
|
||||
|
||||
//If the race has reached the preparatory phase, or has started...
|
||||
if (raceStatus == RaceStatusEnum.PREPARATORY || raceStatus == RaceStatusEnum.STARTED) {
|
||||
//Stop this timer.
|
||||
stop();
|
||||
|
||||
//Hide this, and display the race controller.
|
||||
startWrapper.setVisible(false);
|
||||
start.setVisible(false);
|
||||
|
||||
parent.beginRace(visualiserInput, raceClock);
|
||||
parent.beginRace(visualiserInput, visualiserRace);
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the clock that displays the time of at the current race venue.
|
||||
*/
|
||||
private void setRaceClock() {
|
||||
raceClock = new RaceClock(raceData.getZonedDateTime());
|
||||
|
||||
raceClock.timeStringProperty().addListener((observable, oldValue, newValue) -> {
|
||||
Platform.runLater(() -> {
|
||||
timeZoneTime.setText(newValue);
|
||||
});
|
||||
});
|
||||
//TEMP REMOVE
|
||||
//timeZoneTime.textProperty().bind(raceClock.timeStringProperty());
|
||||
raceClock.run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the time that the race is going to start.
|
||||
* Function to handle changes in objects we observe.
|
||||
* We observe LatestMessages.
|
||||
* @param o The observed object.
|
||||
* @param arg The {@link Observable#notifyObservers(Object)} parameter.
|
||||
*/
|
||||
private void setStartingTime() {
|
||||
String dateFormat = "'Starting time:' HH:mm dd/MM/YYYY";
|
||||
Platform.runLater(()-> {
|
||||
long utcTime = visualiserInput.getRaceStatus().getExpectedStartTime();
|
||||
raceClock.setStartingTime(raceClock.getLocalTime(utcTime));
|
||||
raceStartLabel.setText(DateTimeFormatter.ofPattern(dateFormat).format(raceClock.getStartingTime()));
|
||||
|
||||
raceClock.durationProperty().addListener((observable, oldValue, newValue) -> {
|
||||
Platform.runLater(() -> {
|
||||
timer.setText(newValue);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* set the current time, may be used to update the time on the clock.
|
||||
*/
|
||||
private void setCurrentTime() {
|
||||
Platform.runLater(()->
|
||||
raceClock.setUTCTime(visualiserInput.getRaceStatus().getCurrentTime())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Observable o, Object arg) {
|
||||
if(o instanceof StreamedCourse) {
|
||||
StreamedCourse streamedCourse = (StreamedCourse) o;
|
||||
if(streamedCourse.hasReadBoats()) {
|
||||
initialiseTables();
|
||||
}
|
||||
if(streamedCourse.hasReadRegatta()) {
|
||||
Platform.runLater(() -> raceTitleLabel.setText(streamedCourse.getRegattaName()));
|
||||
}
|
||||
if (streamedCourse.hasReadCourse()) {
|
||||
|
||||
//Check that we actually have LatestMessages.
|
||||
if (o instanceof LatestMessages) {
|
||||
LatestMessages latestMessages = (LatestMessages) o;
|
||||
|
||||
//If we've received all of the xml files, start the race. Only start it if it hasn't already been created.
|
||||
if (latestMessages.hasAllXMLMessages() && this.visualiserRace == null) {
|
||||
|
||||
//Need to handle it in the javafx thread.
|
||||
Platform.runLater(() -> {
|
||||
if (!this.hasCreatedClock) {
|
||||
this.hasCreatedClock = true;
|
||||
setRaceClock();
|
||||
if (visualiserInput.getRaceStatus() == null) {
|
||||
return;//TEMP BUG FIX if the race isn't sending race status messages (our mock currently doesn't), then it would block the javafx thread with the previous while loop.
|
||||
}// TODO - replace with observer on VisualiserInput
|
||||
setStartingTime();
|
||||
setCurrentTime();
|
||||
try {
|
||||
this.startRace(latestMessages);
|
||||
|
||||
} catch (XMLReaderException | InvalidBoatDataException | InvalidRaceDataException | InvalidRegattaDataException e) {
|
||||
//We currently don't handle this in meaningful way, as it should never occur.
|
||||
//If we reach this point it means that malformed XML files were sent.
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//TODO this is a somewhat temporary fix for when not all of the race data (boats, course, regatta) is received in time.
|
||||
//Previously, startRaceNoScaling was called in the enterLobby function after the visualiserInput was started, but when connecting to the official data source it sometimes didn't send all of the race data, causing startRaceNoScaling to start, even though we didn't have enough information to start it.
|
||||
if (streamedCourse.hasReadBoats() && streamedCourse.hasReadCourse() && streamedCourse.hasReadRegatta()) {
|
||||
Platform.runLater(() -> {
|
||||
if (!this.hasRaceStarted) {
|
||||
if(visualiserInput.getRaceStatus() == null) {
|
||||
}
|
||||
else {
|
||||
this.hasRaceStarted = true;
|
||||
startRaceNoScaling();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,8 +301,12 @@ public class StartController extends Controller implements Observer {
|
||||
public void enterLobby(Socket socket) {
|
||||
startWrapper.setVisible(true);
|
||||
try {
|
||||
visualiserInput = new VisualiserInput(socket, raceData);
|
||||
new Thread(visualiserInput).start();
|
||||
//Begin reading packets from the socket/server.
|
||||
this.visualiserInput = new VisualiserInput(socket);
|
||||
//Store a reference to latestMessages so that we can observe it.
|
||||
LatestMessages latestMessages = this.visualiserInput.getLatestMessages();
|
||||
latestMessages.addObserver(this);
|
||||
new Thread(this.visualiserInput).start();
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user