You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.5 KiB

package mock.app;
import mock.model.Polars;
import network.Messages.Enums.MessageType;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.text.ParseException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
/**
* A Race Event, this holds all of the race's information as well as handling the connection to its clients.
*/
public class Event {
String raceXML;
String regattaXML;
String boatXML;
Polars boatPolars;
MockOutput mockOutput;
public Event(String raceXML, String regattaXML, String boatXML, Polars boatPolars) {
this.raceXML = getRaceXMLAtCurrentTime(raceXML);
this.boatXML = boatXML;
this.regattaXML = regattaXML;
this.boatPolars = boatPolars;
try {
mockOutput = new MockOutput();
new Thread(mockOutput).start();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Sends the initial race data and then begins race simulation
*/
public void start() {
try {
sendXMLs();
Race newRace = new Race(new RaceXMLReader(this.raceXML, new BoatXMLReader(boatXML, this.boatPolars)), mockOutput);
new Thread((newRace)).start();
} catch (ParserConfigurationException | IOException | SAXException | ParseException | StreamedCourseXMLException e) {
e.printStackTrace();
}
}
/**
* Sends out each xml string, via the mock output
*/
private void sendXMLs() {
mockOutput.setRegattaXml(regattaXML);
mockOutput.parseXMLString(regattaXML, MessageType.XMLMESSAGE.getValue());
mockOutput.setRaceXml(raceXML);
mockOutput.parseXMLString(raceXML, MessageType.XMLMESSAGE.getValue());
mockOutput.setBoatsXml(boatXML);
mockOutput.parseXMLString(boatXML, MessageType.XMLMESSAGE.getValue());
}
/**
* Sets the xml description of the race to show the race was created now, and starts in 3 minutes
* @param raceXML
* @return String containing edited xml
*/
private String getRaceXMLAtCurrentTime(String raceXML) {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
ZonedDateTime creationTime = ZonedDateTime.now();
return raceXML.replace("CREATION_TIME", dateFormat.format(creationTime))
.replace("START_TIME", dateFormat.format(creationTime.plusMinutes(3)));
}
}