package seng302.Model; import com.sun.org.apache.xpath.internal.SourceTree; import javafx.animation.AnimationTimer; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import seng302.RaceDataSource; import java.util.Arrays; import java.util.List; /** * Parent class for races * Created by fwy13 on 3/03/17. */ public abstract class Race implements Runnable { //protected BoatInRace[] startingBoats; protected ObservableList startingBoats; protected List legs; protected int boatsFinished = 0; protected long totalTimeElapsed; private int lastFPS = 20; private int dnfChance = 0; //percentage chance a boat fails at each checkpoint protected int scaleFactor; protected int PRERACE_TIME = 120000; //time in milliseconds to pause during pre-race /** * Initailiser for Race * * @param boats Takes in an array of boats that are participating in the race. * @param legs Number of marks in order that the boats pass in order to complete the race. * @param scaleFactor for race */ public Race(List boats, List legs, int scaleFactor) { this.startingBoats = FXCollections.observableArrayList(boats); this.legs = legs; this.legs.add(new Leg("Finish", this.legs.size())); this.scaleFactor = scaleFactor; if (startingBoats != null && startingBoats.size() > 0) { initialiseBoats(); } } public Race(BoatInRace[] startingBoats, List legs, int scaleFactor) { this(Arrays.asList(startingBoats), legs, scaleFactor); } public Race(RaceDataSource raceData, int scaleFactor) { this(raceData.getBoats(), raceData.getLegs(), scaleFactor); } public abstract void initialiseBoats(); /** * Randomly generate number to see if boat fails * @return True if number lower than dnfChance else false */ protected abstract boolean doNotFinish(); /** * Checks the position of the boat, this updates the boats current position. * * @param boat Boat that the position is to be updated for. * @param timeElapsed Time that has elapse since the start of the the race. * @see BoatInRace */ protected abstract void checkPosition(BoatInRace boat, long timeElapsed); /** * Updates the boat's gps coordinates depending on time elapsed * * @param boat to be updated * @param millisecondsElapsed time since last update */ protected abstract void updatePosition(BoatInRace boat, int millisecondsElapsed); /** * Runnable for the thread. */ public void run() { initialiseBoats(); countdownTimer(); } /** * Countdown timer until race starts. Use PRERACE_TIME to set countdown duration. */ protected void countdownTimer() { AnimationTimer timer = new AnimationTimer() { long currentTime = System.currentTimeMillis(); long startTime = currentTime + (PRERACE_TIME/scaleFactor); long minutes; long currentTimeInSeconds; long remainingSeconds; long hours; long timeLeft; @Override public void handle(long arg0) { timeLeft = startTime - currentTime; if (timeLeft <= 0) { stop(); simulateRace(); } else { currentTimeInSeconds = (timeLeft*scaleFactor) / 1000; minutes = currentTimeInSeconds / 60; remainingSeconds = currentTimeInSeconds % 60; hours = minutes / 60; minutes = minutes % 60; } currentTime = System.currentTimeMillis(); } }; timer.start(); } /** * Takes total time elapsed and format to hour:minute:second * * @return Formatted time as string */ protected String calcTimer() { long minutes; long currentTimeInSeconds; long remainingSeconds; long hours; currentTimeInSeconds = (totalTimeElapsed * scaleFactor) / 1000; minutes = currentTimeInSeconds / 60; remainingSeconds = currentTimeInSeconds % 60; hours = minutes / 60; minutes = minutes % 60; return String.format("Race clock: %02d:%02d:%02d", hours, minutes, remainingSeconds); } /** * Starts the Race Simulation, playing the race start to finish with the timescale. * This prints the boats participating, the order that the events occur in time order, and the respective information of the events. */ private void simulateRace() { System.setProperty("javafx.animation.fullspeed", "true"); for (BoatInRace boat : startingBoats) { boat.setStarted(true); } new AnimationTimer() { long timeRaceStarted = System.currentTimeMillis(); //start time of loop @Override public void handle(long arg0) { if (boatsFinished < startingBoats.size()) { totalTimeElapsed = System.currentTimeMillis() - timeRaceStarted; for (BoatInRace boat : startingBoats) { if (boat != null && !boat.isFinished()) { updatePosition(boat, Math.round(1000 / lastFPS) > 20 ? 15 : Math.round(1000 / lastFPS)); checkPosition(boat, totalTimeElapsed); } } } } }.start(); } /** * Update position of boats in race, no position if on starting leg or DNF. */ protected void updatePositions() { FXCollections.sort(startingBoats, (a, b) -> b.getCurrentLeg().getLegNumber() - a.getCurrentLeg().getLegNumber()); for(BoatInRace boat: startingBoats) { if(boat != null) { boat.setPosition(Integer.toString(startingBoats.indexOf(boat) + 1)); System.out.println(boat.getName() + boat.getPosition()); if (boat.getCurrentLeg().getName().equals("DNF") || boat.getCurrentLeg().getLegNumber() == 0) boat.setPosition("-"); } } } /** * Returns the boats that have started the race. * * @return ObservableList of BoatInRace class that participated in the race. * @see ObservableList * @see BoatInRace */ public ObservableList getStartingBoats() { return startingBoats; } }