Created methods to initialise and change wind direction throughout race.

-Method initialiseWinDir assigns an unsigned short value to wind direction.
-Method changeWindDir randomly changes the wind direction throughout race.
-Issue found regarding showing wind direction.

#story[882]
main
zwu18 9 years ago
parent 3bc8ad0a07
commit 8390b19299

@ -8,10 +8,12 @@
</parent> </parent>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>mock</name> <name>visualiser</name>
<artifactId>mock</artifactId> <artifactId>visualiser</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<url>https://eng-git.canterbury.ac.nz/SENG302-2016/team-7</url>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
@ -52,6 +54,12 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
<dependency>
<groupId>eu.hansolo</groupId>
<artifactId>Medusa</artifactId>
<version>7.9</version>
</dependency>
</dependencies> </dependencies>
@ -162,3 +170,4 @@
</plugins> </plugins>
</reporting> </reporting>
</project> </project>

@ -41,6 +41,7 @@ public class Race implements Runnable {
private MockOutput mockOutput; private MockOutput mockOutput;
private static int boatOffset = 0; private static int boatOffset = 0;
private int finished = 0; private int finished = 0;
private int windDir;
/** /**
@ -100,10 +101,36 @@ public class Race implements Runnable {
* Runnable for the thread. * Runnable for the thread.
*/ */
public void run() { public void run() {
initialiseWindDir();
initialiseBoats(); initialiseBoats();
countdownTimer(); countdownTimer();
} }
/**
* Initiate a random wind direction
*/
protected void initialiseWindDir(){
windDir = new Random().nextInt(65535);
}
/**
* Changes the wind direction. Checks to make sure wind direction is between 0 - 65535
*/
protected void changeWindDir(){
int r = new Random().nextInt(3) + 1;
if (r == 1){
windDir+=250;
} else if (r == 2) {
windDir-=250;
}
if (windDir > 65535){
windDir -= 65535;
}
if (windDir < 0){
windDir += 65535;
}
}
/** /**
* Starts the heartbeat timer, which sends a heartbeat message every so often (i.e., 5 seconds). * Starts the heartbeat timer, which sends a heartbeat message every so often (i.e., 5 seconds).
*/ */
@ -134,7 +161,7 @@ public class Race implements Runnable {
boatOffset = (boatOffset + 1) % (startingBoats.size()); boatOffset = (boatOffset + 1) % (startingBoats.size());
if (timeLeft <= 60000/scaleFactor && timeLeft > 0) { if (timeLeft <= 60000/scaleFactor && timeLeft > 0) {
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 2, startTime, 0, 2300, 1, boatStatuses); RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 2, startTime, windDir, 2300, 1, boatStatuses);
mockOutput.parseRaceStatus(raceStatus); mockOutput.parseRaceStatus(raceStatus);
} }
else if (timeLeft <= 0) { else if (timeLeft <= 0) {
@ -145,7 +172,7 @@ public class Race implements Runnable {
stop(); stop();
} }
else { else {
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 1, startTime, 0, 2300,1, boatStatuses); RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 1, startTime, windDir, 2300,1, boatStatuses);
mockOutput.parseRaceStatus(raceStatus); mockOutput.parseRaceStatus(raceStatus);
} }
currentTime = System.currentTimeMillis(); currentTime = System.currentTimeMillis();
@ -173,6 +200,7 @@ public class Race implements Runnable {
@Override @Override
public void handle(long arg0) { public void handle(long arg0) {
System.out.println(windDir);
if (boatsFinished < startingBoats.size()) { if (boatsFinished < startingBoats.size()) {
//Get the current time. //Get the current time.
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
@ -199,7 +227,7 @@ public class Race implements Runnable {
boat.getCurrentLeg().getLegNumber() >= 0 ? BoatStatusEnum.RACING : BoatStatusEnum.DNF, boat.getCurrentLeg().getLegNumber())); boat.getCurrentLeg().getLegNumber() >= 0 ? BoatStatusEnum.RACING : BoatStatusEnum.DNF, boat.getCurrentLeg().getLegNumber()));
} }
if (startingBoats.size()==finished){ if (startingBoats.size()==finished){
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 4, startTime, 0, 2300, 2, boatStatuses);//TODO FIX the second currentTime is a placeholder! Also, replace magic values. RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 4, startTime, windDir, 2300, 2, boatStatuses);//TODO FIX the second currentTime is a placeholder! Also, replace magic values.
mockOutput.parseRaceStatus(raceStatus); mockOutput.parseRaceStatus(raceStatus);
} }
} else { } else {
@ -207,9 +235,10 @@ public class Race implements Runnable {
} }
} }
boatOffset = (boatOffset + 1) % (startingBoats.size()); boatOffset = (boatOffset + 1) % (startingBoats.size());
RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 3, startTime, 0, 2300, 2, boatStatuses);//TODO FIX the second currentTime is a placeholder! Also, replace magic values. RaceStatus raceStatus = new RaceStatus(System.currentTimeMillis(), raceId, 3, startTime, windDir, 2300, 2, boatStatuses);//TODO FIX the second currentTime is a placeholder! Also, replace magic values.
mockOutput.parseRaceStatus(raceStatus); mockOutput.parseRaceStatus(raceStatus);
} }
changeWindDir();
} }
}.start(); }.start();
} }

Loading…
Cancel
Save