mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Added method to check if a boat is finished the race or not.
- updated server if all boats finish the race, then start sending race finished message. - race simulator will terminate if all boats finish the race, and prints log. #story[715]
This commit is contained in:
@@ -3,21 +3,16 @@ package seng302.server;
|
||||
import seng302.server.messages.*;
|
||||
import seng302.server.simulator.Boat;
|
||||
import seng302.server.simulator.Simulator;
|
||||
import sun.misc.IOUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
|
||||
public class ServerThread implements Runnable, Observer {
|
||||
private Thread runner;
|
||||
private StreamingServerSocket server;
|
||||
private long startTime;
|
||||
boolean raceStarted = false;
|
||||
Map<Integer,Boolean> boatsFinished = new HashMap<>();
|
||||
private boolean raceStarted = false;
|
||||
private Map<Integer,Boolean> boatsFinished = new HashMap<>();
|
||||
private List<Boat> boats;
|
||||
private Simulator raceSimulator;
|
||||
private boolean sendingRaceFinishedLocationMessages = true;
|
||||
@@ -31,7 +26,7 @@ public class ServerThread implements Runnable, Observer {
|
||||
private static final int LOG_LEVEL = 1;
|
||||
|
||||
public ServerThread(String threadName){
|
||||
runner = new Thread(this, threadName);
|
||||
Thread runner = new Thread(this, threadName);
|
||||
runner.setDaemon(true);
|
||||
|
||||
serverLog("Spawning Server", 0);
|
||||
@@ -51,7 +46,7 @@ public class ServerThread implements Runnable, Observer {
|
||||
raceSimulatorThread.start();
|
||||
}
|
||||
|
||||
public static void serverLog(String message, int logLevel){
|
||||
static void serverLog(String message, int logLevel){
|
||||
if(logLevel <= LOG_LEVEL){
|
||||
System.out.println("[SERVER] " + message);
|
||||
}
|
||||
@@ -63,7 +58,7 @@ public class ServerThread implements Runnable, Observer {
|
||||
* @param type The XML Message type
|
||||
* @return The XML Message
|
||||
*/
|
||||
public Message getXmlMessage(String fileName, XMLMessageSubType type){
|
||||
private Message getXmlMessage(String fileName, XMLMessageSubType type){
|
||||
String fileContents = null;
|
||||
|
||||
try {
|
||||
@@ -85,8 +80,8 @@ public class ServerThread implements Runnable, Observer {
|
||||
/**
|
||||
* @return Get a race status message for the current race
|
||||
*/
|
||||
public Message getRaceStatusMessage(){
|
||||
List<BoatSubMessage> boatSubMessages = new ArrayList<BoatSubMessage>();
|
||||
private Message getRaceStatusMessage(){
|
||||
List<BoatSubMessage> boatSubMessages = new ArrayList<>();
|
||||
BoatStatus boatStatus;
|
||||
RaceStatus raceStatus;
|
||||
boolean thereAreBoatsNotFinished = false;
|
||||
@@ -289,6 +284,7 @@ public class ServerThread implements Runnable, Observer {
|
||||
* @param arg .
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void update(Observable o, Object arg) {
|
||||
// Only send if server started
|
||||
// TODO: I don't understand why i need to check server is null or not ... confused - haoming 2/5/17
|
||||
@@ -296,22 +292,32 @@ public class ServerThread implements Runnable, Observer {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Boat b : (List<Boat>) arg){
|
||||
int numOfBoatsFinished = 0;
|
||||
for (Boat boat : (List<Boat>) arg){
|
||||
try {
|
||||
Message m = new BoatLocationMessage(b.getSourceID(), server.getSequenceNumber(), b.getLat(),
|
||||
b.getLng(), b.getLastPassedCorner().getBearingToNextCorner(),
|
||||
((long) b.getSpeed()));
|
||||
if (boat.isFinished()) {
|
||||
numOfBoatsFinished ++;
|
||||
if (!boatsFinished.get(boat.getSourceID())) {
|
||||
boatsFinished.put(boat.getSourceID(), true);
|
||||
serverLog("Boat " + boat.getSourceID() + " finished the race", 1);
|
||||
}
|
||||
}
|
||||
Message m = new BoatLocationMessage(boat.getSourceID(), 1, boat.getLat(),
|
||||
boat.getLng(), boat.getLastPassedCorner().getBearingToNextCorner(),
|
||||
((long) boat.getSpeed()));
|
||||
server.send(m);
|
||||
} catch (IOException e) {
|
||||
serverLog("Couldn't send a boat status message", 3);
|
||||
return;
|
||||
}
|
||||
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);
|
||||
boatsFinished.put(b.getSourceID(), true);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (numOfBoatsFinished == ((List<Boat>) arg).size()) {
|
||||
startSendingRaceFinishedBoatPostions();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,12 +10,14 @@ public class Boat {
|
||||
private double lng;
|
||||
private double speed; // in mm/sec
|
||||
private String boatName, shortName, shorterName;
|
||||
private boolean isFinished;
|
||||
|
||||
private Corner lastPassedCorner, headingCorner;
|
||||
|
||||
public Boat(int sourceID, String boatName) {
|
||||
this.sourceID = sourceID;
|
||||
this.boatName = boatName;
|
||||
this.isFinished = false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,4 +108,12 @@ public class Boat {
|
||||
public void setHeadingCorner(Corner headingCorner) {
|
||||
this.headingCorner = headingCorner;
|
||||
}
|
||||
|
||||
public boolean isFinished() {
|
||||
return isFinished;
|
||||
}
|
||||
|
||||
public void setFinished(boolean finished) {
|
||||
isFinished = finished;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +65,8 @@ public class Simulator extends Observable implements Runnable {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("[SERVER] Race simulator has been terminated");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,7 +91,10 @@ public class Simulator extends Observable implements Runnable {
|
||||
boat.setHeadingCorner(boat.getLastPassedCorner().getNextCorner());
|
||||
|
||||
// heading corner == null means boat has reached the final mark
|
||||
if (boat.getHeadingCorner() == null) return 1;
|
||||
if (boat.getHeadingCorner() == null) {
|
||||
boat.setFinished(true);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// move compensate distance for the mark just passed
|
||||
Position pos = GeoUtility.getGeoCoordinate(
|
||||
|
||||
Reference in New Issue
Block a user