mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
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:
@@ -32,7 +32,12 @@ public class ServerThread implements Runnable, Observer {
|
|||||||
public ServerThread(String threadName){
|
public ServerThread(String threadName){
|
||||||
runner = new Thread(this, threadName);
|
runner = new Thread(this, threadName);
|
||||||
serverLog("Spawning Server", 0);
|
serverLog("Spawning Server", 0);
|
||||||
|
|
||||||
raceSimulator = new Simulator(BOAT_LOCATION_PERIOD);
|
raceSimulator = new Simulator(BOAT_LOCATION_PERIOD);
|
||||||
|
raceSimulator.addObserver(this);
|
||||||
|
// run race simulator, so it can send boats' static location.
|
||||||
|
Thread raceSimulatorThread = new Thread(raceSimulator, "Race Simulator");
|
||||||
|
|
||||||
boats = raceSimulator.getBoats();
|
boats = raceSimulator.getBoats();
|
||||||
|
|
||||||
for (Boat b : boats){
|
for (Boat b : boats){
|
||||||
@@ -40,6 +45,7 @@ public class ServerThread implements Runnable, Observer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
runner.start();
|
runner.start();
|
||||||
|
raceSimulatorThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void serverLog(String message, int logLevel){
|
public static void serverLog(String message, int logLevel){
|
||||||
@@ -130,9 +136,9 @@ public class ServerThread implements Runnable, Observer {
|
|||||||
* Starts an instance of the race simulator
|
* Starts an instance of the race simulator
|
||||||
*/
|
*/
|
||||||
private void startRaceSim(){
|
private void startRaceSim(){
|
||||||
serverLog("Starting Race Simulator", 0);
|
serverLog("Starting Running Race Simulator", 0);
|
||||||
raceSimulator.addObserver(this);
|
// set race started to true, so the simulator will start moving boats
|
||||||
new Thread(raceSimulator).start();
|
raceSimulator.setRaceStarted(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -258,11 +264,12 @@ public class ServerThread implements Runnable, Observer {
|
|||||||
@Override
|
@Override
|
||||||
public void update(Observable o, Object arg) {
|
public void update(Observable o, Object arg) {
|
||||||
// Only send if server started
|
// Only send if server started
|
||||||
if(!server.isStarted()){
|
// TODO: I don't understand why i need to check server is null or not ... confused - haoming 2/5/17
|
||||||
|
if(server == null || !server.isStarted()){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Boat b : ((Simulator) o).getBoats()){
|
for (Boat b : (List<Boat>) arg){
|
||||||
try {
|
try {
|
||||||
Message m = new BoatLocationMessage(b.getSourceID(), 1, b.getLat(),
|
Message m = new BoatLocationMessage(b.getSourceID(), 1, b.getLat(),
|
||||||
b.getLng(), b.getLastPassedCorner().getBearingToNextCorner(),
|
b.getLng(), b.getLastPassedCorner().getBearingToNextCorner(),
|
||||||
@@ -273,6 +280,8 @@ public class ServerThread implements Runnable, Observer {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
catch (NullPointerException e){
|
catch (NullPointerException e){
|
||||||
|
//e.printStackTrace();
|
||||||
|
//TODO: add a method in boat to check if a boat has finished the race. - haoming 2/5/17
|
||||||
serverLog("Boat " + b.getSourceID() + " finished the race", 1);
|
serverLog("Boat " + b.getSourceID() + " finished the race", 1);
|
||||||
boatsFinished.put(b.getSourceID(), true);
|
boatsFinished.put(b.getSourceID(), true);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ public class Simulator extends Observable implements Runnable {
|
|||||||
private List<Corner> course;
|
private List<Corner> course;
|
||||||
private List<Boat> boats;
|
private List<Boat> boats;
|
||||||
private long lapse;
|
private long lapse;
|
||||||
|
private boolean isRaceStarted;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a simulator instance with given time lapse.
|
* Creates a simulator instance with given time lapse.
|
||||||
@@ -24,6 +25,7 @@ public class Simulator extends Observable implements Runnable {
|
|||||||
course = rp.getCourse();
|
course = rp.getCourse();
|
||||||
boats = rp.getBoats();
|
boats = rp.getBoats();
|
||||||
this.lapse = lapse;
|
this.lapse = lapse;
|
||||||
|
isRaceStarted = false;
|
||||||
|
|
||||||
setLegs();
|
setLegs();
|
||||||
|
|
||||||
@@ -35,7 +37,7 @@ public class Simulator extends Observable implements Runnable {
|
|||||||
boat.setLng(startLng);
|
boat.setLng(startLng);
|
||||||
boat.setLastPassedCorner(course.get(0));
|
boat.setLastPassedCorner(course.get(0));
|
||||||
boat.setHeadingCorner(course.get(1));
|
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;
|
int numOfFinishedBoats = 0;
|
||||||
|
|
||||||
while (numOfFinishedBoats < boats.size()) {
|
while (numOfFinishedBoats < boats.size()) {
|
||||||
|
|
||||||
|
// if race has started, then boat should start to move.
|
||||||
|
if (isRaceStarted) {
|
||||||
for (Boat boat : boats) {
|
for (Boat boat : boats) {
|
||||||
numOfFinishedBoats += moveBoat(boat, lapse);
|
numOfFinishedBoats += moveBoat(boat, lapse);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
//System.out.println(boats.get(0));
|
//System.out.println(boats.get(0));
|
||||||
|
|
||||||
setChanged();
|
setChanged();
|
||||||
notifyObservers(boats);
|
notifyObservers(boats);
|
||||||
|
|
||||||
// sleep for 1 second.
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(lapse);
|
Thread.sleep(lapse);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@@ -64,7 +69,6 @@ public class Simulator extends Observable implements Runnable {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Moves a boat with given time duration.
|
* Moves a boat with given time duration.
|
||||||
*
|
|
||||||
* @param boat the boat to be moved
|
* @param boat the boat to be moved
|
||||||
* @param duration the moving duration in milliseconds
|
* @param duration the moving duration in milliseconds
|
||||||
* @return 1 if the boat has reached the final line, otherwise return 0
|
* @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;
|
return boats;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setRaceStarted(boolean raceStarted) {
|
||||||
|
isRaceStarted = raceStarted;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user