Fix bugs that server doesn't send boat location before and after the race.

- server will sends boat location not only during the race, but also before the race and after all boats have finished the race.
- refactored simulator so that it runs at the begining to send boat location, and if its "isStarted" set to true, then it starts moving the boats.

#story[715]
This commit is contained in:
Haoming Yin
2017-05-02 22:51:35 +12:00
parent 772ece25a0
commit f0d6312fa5
2 changed files with 26 additions and 10 deletions
@@ -14,6 +14,7 @@ public class Simulator extends Observable implements Runnable {
private List<Corner> course;
private List<Boat> boats;
private long lapse;
private boolean isRaceStarted;
/**
* Creates a simulator instance with given time lapse.
@@ -24,6 +25,7 @@ public class Simulator extends Observable implements Runnable {
course = rp.getCourse();
boats = rp.getBoats();
this.lapse = lapse;
isRaceStarted = false;
setLegs();
@@ -35,7 +37,7 @@ public class Simulator extends Observable implements Runnable {
boat.setLng(startLng);
boat.setLastPassedCorner(course.get(0));
boat.setHeadingCorner(course.get(1));
boat.setSpeed(ThreadLocalRandom.current().nextInt(40000, 60000 + 1));
boat.setSpeed(ThreadLocalRandom.current().nextInt(400000, 600000 + 1));
}
}
@@ -45,15 +47,18 @@ public class Simulator extends Observable implements Runnable {
int numOfFinishedBoats = 0;
while (numOfFinishedBoats < boats.size()) {
for (Boat boat : boats) {
numOfFinishedBoats += moveBoat(boat, lapse);
// if race has started, then boat should start to move.
if (isRaceStarted) {
for (Boat boat : boats) {
numOfFinishedBoats += moveBoat(boat, lapse);
}
}
//System.out.println(boats.get(0));
setChanged();
notifyObservers(boats);
// sleep for 1 second.
try {
Thread.sleep(lapse);
} catch (InterruptedException e) {
@@ -64,7 +69,6 @@ public class Simulator extends Observable implements Runnable {
/**
* Moves a boat with given time duration.
*
* @param boat the boat to be moved
* @param duration the moving duration in milliseconds
* @return 1 if the boat has reached the final line, otherwise return 0
@@ -126,4 +130,7 @@ public class Simulator extends Observable implements Runnable {
return boats;
}
public void setRaceStarted(boolean raceStarted) {
isRaceStarted = raceStarted;
}
}