mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Merge branch 'develop' into StartScreen
# Conflicts: # src/main/java/seng302/gameServer/GameState.java # src/main/java/seng302/gameServer/MainServerThread.java # src/main/java/seng302/gameServer/ServerToClientThread.java # src/main/java/seng302/utilities/StreamParser.java # src/main/java/seng302/visualiser/GameClient.java # src/main/java/seng302/visualiser/controllers/LobbyController.java
This commit is contained in:
@@ -1,26 +1,51 @@
|
||||
package seng302.gameServer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
import seng302.gameServer.server.messages.BoatStatus;
|
||||
import seng302.gameServer.server.messages.MarkRoundingMessage;
|
||||
import seng302.gameServer.server.messages.MarkType;
|
||||
import seng302.gameServer.server.messages.Message;
|
||||
import seng302.gameServer.server.messages.RoundingBoatStatus;
|
||||
import seng302.gameServer.server.messages.YachtEventCodeMessage;
|
||||
import seng302.model.GeoPoint;
|
||||
import seng302.model.Player;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.model.PolarTable;
|
||||
import seng302.model.ServerYacht;
|
||||
import seng302.model.mark.CompoundMark;
|
||||
import seng302.model.mark.Mark;
|
||||
import seng302.model.mark.MarkOrder;
|
||||
import seng302.utilities.GeoUtility;
|
||||
|
||||
/**
|
||||
* A Static class to hold information about the current state of the game (model)
|
||||
* Also contains logic for updating itself on regular time intervals on its own thread
|
||||
* Created by wmu16 on 10/07/17.
|
||||
*/
|
||||
public class GameState implements Runnable {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(MarkOrder.class);
|
||||
@FunctionalInterface
|
||||
interface NewMessageListener {
|
||||
void notify(Message message);
|
||||
}
|
||||
|
||||
private static Integer STATE_UPDATES_PER_SECOND = 60;
|
||||
private Logger logger = LoggerFactory.getLogger(GameState.class);
|
||||
|
||||
private static final Integer STATE_UPDATES_PER_SECOND = 60;
|
||||
public static Integer MAX_PLAYERS = 8;
|
||||
public static Double ROUNDING_DISTANCE = 50d; // TODO: 14/08/17 wmu16 - Look into this value further
|
||||
public static final Double MARK_COLLISION_DISTANCE = 15d;
|
||||
public static final Double YACHT_COLLISION_DISTANCE = 25.0;
|
||||
public static final Double BOUNCE_DISTANCE_MARK = 20.0;
|
||||
public static final Double BOUNCE_DISTANCE_YACHT = 30.0;
|
||||
public static final Double COLLISION_VELOCITY_PENALTY = 0.3;
|
||||
|
||||
private static Long previousUpdateTime;
|
||||
public static Double windDirection;
|
||||
@@ -28,11 +53,14 @@ public class GameState implements Runnable {
|
||||
|
||||
private static String hostIpAddress;
|
||||
private static List<Player> players;
|
||||
private static Map<Integer, Yacht> yachts;
|
||||
private static Map<Integer, ServerYacht> yachts;
|
||||
private static Boolean isRaceStarted;
|
||||
private static GameStages currentStage;
|
||||
private static MarkOrder markOrder;
|
||||
private static long startTime;
|
||||
private static Set<Mark> marks;
|
||||
|
||||
private static List<NewMessageListener> markListeners;
|
||||
|
||||
private static Map<Player, String> playerStringMap = new HashMap<>();
|
||||
/*
|
||||
@@ -53,40 +81,47 @@ public class GameState implements Runnable {
|
||||
yachts = new HashMap<>();
|
||||
players = new ArrayList<>();
|
||||
GameState.hostIpAddress = hostIpAddress;
|
||||
players = new ArrayList<>();
|
||||
;
|
||||
currentStage = GameStages.LOBBYING;
|
||||
isRaceStarted = false;
|
||||
yachts = new HashMap<>();
|
||||
//set this when game stage changes to prerace
|
||||
previousUpdateTime = System.currentTimeMillis();
|
||||
yachts = new HashMap<>();
|
||||
markOrder = new MarkOrder(); //This could be instantiated at some point with a select map?
|
||||
markListeners = new ArrayList<>();
|
||||
|
||||
startTime = System.currentTimeMillis() + 60000;
|
||||
|
||||
new Thread(this).start(); //Run the auto updates on the game state
|
||||
new Thread(this, "GameState").start(); //Run the auto updates on the game state
|
||||
|
||||
marks = new MarkOrder().getAllMarks();
|
||||
}
|
||||
|
||||
public static String getHostIpAddress() {
|
||||
return hostIpAddress;
|
||||
}
|
||||
|
||||
public static Set<Mark> getMarks(){
|
||||
return Collections.unmodifiableSet(marks);
|
||||
}
|
||||
|
||||
public static List<Player> getPlayers() {
|
||||
return players;
|
||||
}
|
||||
|
||||
public static void addPlayer(Player player) {
|
||||
players.add(player);
|
||||
String playerText = player.getYacht().getSourceId() + " " + player.getYacht().getBoatName() + " " + player.getYacht().getCountry();
|
||||
String playerText = player.getYacht().getSourceId() + " " + player.getYacht().getBoatName()
|
||||
+ " " + player.getYacht().getCountry();
|
||||
playerStringMap.put(player, playerText);
|
||||
}
|
||||
|
||||
|
||||
public static void removePlayer(Player player) {
|
||||
players.remove(player);
|
||||
playerStringMap.remove(player);
|
||||
}
|
||||
|
||||
public static void addYacht(Integer sourceId, Yacht yacht) {
|
||||
public static void addYacht(Integer sourceId, ServerYacht yacht) {
|
||||
yachts.put(sourceId, yacht);
|
||||
}
|
||||
|
||||
@@ -123,59 +158,17 @@ public class GameState implements Runnable {
|
||||
}
|
||||
|
||||
public static Double getWindSpeedKnots() {
|
||||
return windSpeed / 1000 * 1.943844492; // TODO: 26/07/17 cir27 - remove magic numbers
|
||||
return GeoUtility.mmsToKnots(windSpeed); // TODO: 26/07/17 cir27 - remove magic numbers
|
||||
}
|
||||
|
||||
public static Map<Integer, Yacht> getYachts() {
|
||||
public static Map<Integer, ServerYacht> getYachts() {
|
||||
return yachts;
|
||||
}
|
||||
|
||||
public static void updateBoat(Integer sourceId, BoatActionType actionType) {
|
||||
Yacht playerYacht = yachts.get(sourceId);
|
||||
// System.out.println("-----------------------");
|
||||
switch (actionType) {
|
||||
case VMG:
|
||||
playerYacht.turnToVMG();
|
||||
// System.out.println("Snapping to VMG");
|
||||
break;
|
||||
case SAILS_IN:
|
||||
playerYacht.toggleSailIn();
|
||||
// System.out.println("Toggling Sails");
|
||||
break;
|
||||
case SAILS_OUT:
|
||||
playerYacht.toggleSailIn();
|
||||
// System.out.println("Toggling Sails");
|
||||
break;
|
||||
case TACK_GYBE:
|
||||
playerYacht.tackGybe(windDirection);
|
||||
// System.out.println("Tack/Gybe");
|
||||
break;
|
||||
case UPWIND:
|
||||
playerYacht.turnUpwind();
|
||||
// System.out.println("Moving upwind");
|
||||
break;
|
||||
case DOWNWIND:
|
||||
playerYacht.turnDownwind();
|
||||
// System.out.println("Moving downwind");
|
||||
break;
|
||||
}
|
||||
|
||||
// printBoatStatus(playerYacht);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
Long timeInterval = System.currentTimeMillis() - previousUpdateTime;
|
||||
previousUpdateTime = System.currentTimeMillis();
|
||||
if (System.currentTimeMillis() > startTime) {
|
||||
GameState.setCurrentStage(GameStages.RACING);
|
||||
}
|
||||
for (Yacht yacht : yachts.values()) {
|
||||
yacht.update(timeInterval);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a new ID based off the size of current players + 1
|
||||
*
|
||||
* @return a playerID to be allocated to a new connetion
|
||||
*/
|
||||
public static Integer getUniquePlayerID() {
|
||||
@@ -190,30 +183,412 @@ public class GameState implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
while(true) {
|
||||
while (currentStage != GameStages.FINISHED) {
|
||||
try {
|
||||
Thread.sleep(1000 / STATE_UPDATES_PER_SECOND);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("[GameState] interrupted exception");
|
||||
}
|
||||
if (currentStage == GameStages.PRE_RACE || currentStage == GameStages.LOBBYING) {
|
||||
if (currentStage == GameStages.PRE_RACE || curentStage == GameStages.RACING) {
|
||||
update();
|
||||
}
|
||||
|
||||
//RACING
|
||||
if (currentStage == GameStages.RACING) {
|
||||
update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void printBoatStatus(Yacht playerYacht) {
|
||||
System.out.println("-----------------------");
|
||||
System.out.println("Sails are in: " + playerYacht.getSailIn());
|
||||
System.out.println("Heading: " + playerYacht.getHeading());
|
||||
System.out.println("Velocity: " + playerYacht.getVelocityMMS() / 1000);
|
||||
System.out.println("Lat: " + playerYacht.getLocation().getLat());
|
||||
System.out.println("Lng: " + playerYacht.getLocation().getLng());
|
||||
System.out.println("-----------------------\n");
|
||||
public static void updateBoat(Integer sourceId, BoatAction actionType) {
|
||||
ServerYacht playerYacht = yachts.get(sourceId);
|
||||
switch (actionType) {
|
||||
case VMG:
|
||||
playerYacht.turnToVMG();
|
||||
break;
|
||||
case SAILS_IN:
|
||||
playerYacht.toggleSailIn();
|
||||
break;
|
||||
case SAILS_OUT:
|
||||
playerYacht.toggleSailIn();
|
||||
break;
|
||||
case TACK_GYBE:
|
||||
playerYacht.tackGybe(windDirection);
|
||||
break;
|
||||
case UPWIND:
|
||||
playerYacht.turnUpwind();
|
||||
break;
|
||||
case DOWNWIND:
|
||||
playerYacht.turnDownwind();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Called periodically in this GameState thread to update the GameState values
|
||||
*/
|
||||
public void update() {
|
||||
Boolean raceFinished = true;
|
||||
|
||||
Double timeInterval = (System.currentTimeMillis() - previousUpdateTime) / 1000000.0;
|
||||
previousUpdateTime = System.currentTimeMillis();
|
||||
if (System.currentTimeMillis() > startTime) {
|
||||
GameState.setCurrentStage(GameStages.RACING);
|
||||
}
|
||||
for (ServerYacht yacht : yachts.values()) {
|
||||
updateVelocity(yacht);
|
||||
yacht.runAutoPilot();
|
||||
yacht.updateLocation(timeInterval);
|
||||
if (yacht.getBoatStatus() != BoatStatus.FINISHED) {
|
||||
checkForCollision(yacht);
|
||||
checkForLegProgression(yacht);
|
||||
raceFinished = false;
|
||||
}
|
||||
}
|
||||
for (Yacht yacht : yachts.values()) {
|
||||
yacht.update(timeInterval);
|
||||
}
|
||||
}
|
||||
|
||||
if (raceFinished) {
|
||||
currentStage = GameStages.FINISHED;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void checkForCollision(ServerYacht serverYacht) {
|
||||
ServerYacht collidedYacht = checkCollision(serverYacht);
|
||||
if (collidedYacht != null) {
|
||||
GeoPoint originalLocation = serverYacht.getLocation();
|
||||
serverYacht.setLocation(
|
||||
calculateBounceBack(serverYacht, originalLocation, BOUNCE_DISTANCE_YACHT)
|
||||
);
|
||||
serverYacht.setCurrentVelocity(
|
||||
serverYacht.getCurrentVelocity() * COLLISION_VELOCITY_PENALTY
|
||||
);
|
||||
collidedYacht.setLocation(
|
||||
calculateBounceBack(collidedYacht, originalLocation, BOUNCE_DISTANCE_YACHT)
|
||||
);
|
||||
collidedYacht.setCurrentVelocity(
|
||||
collidedYacht.getCurrentVelocity() * COLLISION_VELOCITY_PENALTY
|
||||
);
|
||||
notifyMessageListeners(
|
||||
new YachtEventCodeMessage(serverYacht.getSourceId())
|
||||
);
|
||||
} else {
|
||||
Mark collidedMark = markCollidedWith(serverYacht);
|
||||
if (collidedMark != null) {
|
||||
serverYacht.setLocation(
|
||||
calculateBounceBack(serverYacht, collidedMark, BOUNCE_DISTANCE_MARK)
|
||||
);
|
||||
serverYacht.setCurrentVelocity(
|
||||
serverYacht.getCurrentVelocity() * COLLISION_VELOCITY_PENALTY
|
||||
);
|
||||
notifyMessageListeners(
|
||||
new YachtEventCodeMessage(serverYacht.getSourceId())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void updateVelocity(ServerYacht yacht) {
|
||||
Double velocity = yacht.getCurrentVelocity();
|
||||
Double trueWindAngle = Math.abs(windDirection - yacht.getHeading());
|
||||
Double boatSpeedInKnots = PolarTable.getBoatSpeed(getWindSpeedKnots(), trueWindAngle);
|
||||
Double maxBoatSpeed = GeoUtility.knotsToMMS(boatSpeedInKnots);
|
||||
// TODO: 15/08/17 remove magic numbers from these equations.
|
||||
if (yacht.getSailIn()) {
|
||||
if (velocity < maxBoatSpeed - 500) {
|
||||
yacht.changeVelocity(maxBoatSpeed / 100);
|
||||
} else if (velocity > maxBoatSpeed + 500) {
|
||||
yacht.changeVelocity(-maxBoatSpeed / 100);
|
||||
} else {
|
||||
yacht.setCurrentVelocity(maxBoatSpeed);
|
||||
}
|
||||
} else {
|
||||
if (velocity > 3000) {
|
||||
yacht.changeVelocity(-velocity / 200);
|
||||
} else if (velocity > 100) {
|
||||
yacht.changeVelocity(-velocity / 50);
|
||||
} else if (velocity <= 100){
|
||||
yacht.setCurrentVelocity(0d);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculates the distance to the next mark (closest of the two if a gate mark). For purposes of
|
||||
* mark rounding
|
||||
*
|
||||
* @return A distance in metres. Returns -1 if there is no next mark
|
||||
* @throws IndexOutOfBoundsException If the next mark is null (ie the last mark in the race)
|
||||
* Check first using {@link seng302.model.mark.MarkOrder#isLastMark(Integer)}
|
||||
*/
|
||||
private Double calcDistanceToCurrentMark(ServerYacht yacht) throws IndexOutOfBoundsException {
|
||||
Integer currentMarkSeqID = yacht.getCurrentMarkSeqID();
|
||||
CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID);
|
||||
GeoPoint location = yacht.getLocation();
|
||||
|
||||
if (currentMark.isGate()) {
|
||||
Mark sub1 = currentMark.getSubMark(1);
|
||||
Mark sub2 = currentMark.getSubMark(2);
|
||||
Double distance1 = GeoUtility.getDistance(location, sub1);
|
||||
Double distance2 = GeoUtility.getDistance(location, sub2);
|
||||
if (distance1 < distance2) {
|
||||
yacht.setClosestCurrentMark(sub1);
|
||||
return distance1;
|
||||
} else {
|
||||
yacht.setClosestCurrentMark(sub2);
|
||||
return distance2;
|
||||
}
|
||||
} else {
|
||||
yacht.setClosestCurrentMark(currentMark.getSubMark(1));
|
||||
return GeoUtility.getDistance(location, currentMark.getSubMark(1));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 4 Different cases of progression in the race 1 - Passing the start line 2 - Passing any
|
||||
* in-race Gate 3 - Passing any in-race Mark 4 - Passing the finish line
|
||||
* @param yacht the current yacht to check for progression
|
||||
*/
|
||||
private void checkForLegProgression(ServerYacht yacht) {
|
||||
Integer currentMarkSeqID = yacht.getCurrentMarkSeqID();
|
||||
CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID);
|
||||
// System.out.println(yacht.getCurrentMarkSeqID());
|
||||
|
||||
Boolean hasProgressed;
|
||||
if (currentMarkSeqID == 0) {
|
||||
hasProgressed = checkStartLineCrossing(yacht);
|
||||
} else if (markOrder.isLastMark(currentMarkSeqID)) {
|
||||
hasProgressed = checkFinishLineCrossing(yacht);
|
||||
} else if (currentMark.isGate()) {
|
||||
hasProgressed = checkGateRounding(yacht);
|
||||
} else {
|
||||
hasProgressed = checkMarkRounding(yacht);
|
||||
}
|
||||
|
||||
if (hasProgressed) {
|
||||
sendMarkRoundingMessage(yacht);
|
||||
logMarkRounding(yacht);
|
||||
yacht.setHasPassedLine(false);
|
||||
yacht.setHasEnteredRoundingZone(false);
|
||||
yacht.setHasPassedThroughGate(false);
|
||||
if (!markOrder.isLastMark(currentMarkSeqID)) {
|
||||
yacht.incrementMarkSeqID();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* If we pass the start line gate in the correct direction, progress
|
||||
*
|
||||
* @param yacht The current yacht to check for
|
||||
*/
|
||||
private Boolean checkStartLineCrossing(ServerYacht yacht) {
|
||||
Integer currentMarkSeqID = yacht.getCurrentMarkSeqID();
|
||||
CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID);
|
||||
GeoPoint lastLocation = yacht.getLastLocation();
|
||||
GeoPoint location = yacht.getLocation();
|
||||
|
||||
Mark mark1 = currentMark.getSubMark(1);
|
||||
Mark mark2 = currentMark.getSubMark(2);
|
||||
CompoundMark nextMark = markOrder.getNextMark(currentMarkSeqID);
|
||||
|
||||
Integer crossedLine = GeoUtility.checkCrossedLine(mark1, mark2, lastLocation, location);
|
||||
if (crossedLine > 0) {
|
||||
Boolean isClockwiseCross = GeoUtility.isClockwise(mark1, mark2, nextMark.getMidPoint());
|
||||
if (crossedLine == 2 && isClockwiseCross || crossedLine == 1 && !isClockwiseCross) {
|
||||
yacht.setClosestCurrentMark(mark1);
|
||||
yacht.setBoatStatus(BoatStatus.RACING);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This algorithm checks for mark rounding. And increments the currentMarSeqID number attribute
|
||||
* of the yacht if so. A visual representation of this algorithm can be seen on the Wiki under
|
||||
* 'mark passing algorithm'
|
||||
*
|
||||
* @param yacht The current yacht to check for
|
||||
*/
|
||||
private Boolean checkMarkRounding(ServerYacht yacht) {
|
||||
Integer currentMarkSeqID = yacht.getCurrentMarkSeqID();
|
||||
CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID);
|
||||
GeoPoint lastLocation = yacht.getLastLocation();
|
||||
GeoPoint location = yacht.getLocation();
|
||||
GeoPoint nextPoint = markOrder.getNextMark(currentMarkSeqID).getMidPoint();
|
||||
GeoPoint prevPoint = markOrder.getPreviousMark(currentMarkSeqID).getMidPoint();
|
||||
GeoPoint midPoint = GeoUtility.getDirtyMidPoint(nextPoint, prevPoint);
|
||||
|
||||
if (calcDistanceToCurrentMark(yacht) < ROUNDING_DISTANCE) {
|
||||
yacht.setHasEnteredRoundingZone(true);
|
||||
}
|
||||
|
||||
//In case current mark is a gate, loop through all marks just in case
|
||||
for (Mark thisCurrentMark : currentMark.getMarks()) {
|
||||
if (GeoUtility.isPointInTriangle(lastLocation, location, midPoint, thisCurrentMark)) {
|
||||
yacht.setHasPassedLine(true);
|
||||
}
|
||||
}
|
||||
|
||||
return yacht.hasPassedLine() && yacht.hasEnteredRoundingZone();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks if a gate line has been crossed and in the correct direction
|
||||
*
|
||||
* @param yacht The current yacht to check for
|
||||
*/
|
||||
private Boolean checkGateRounding(ServerYacht yacht) {
|
||||
Integer currentMarkSeqID = yacht.getCurrentMarkSeqID();
|
||||
CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID);
|
||||
GeoPoint lastLocation = yacht.getLastLocation();
|
||||
GeoPoint location = yacht.getLocation();
|
||||
|
||||
Mark mark1 = currentMark.getSubMark(1);
|
||||
Mark mark2 = currentMark.getSubMark(2);
|
||||
CompoundMark prevMark = markOrder.getPreviousMark(currentMarkSeqID);
|
||||
CompoundMark nextMark = markOrder.getNextMark(currentMarkSeqID);
|
||||
|
||||
Integer crossedLine = GeoUtility.checkCrossedLine(mark1, mark2, lastLocation, location);
|
||||
|
||||
//We have crossed the line
|
||||
if (crossedLine > 0) {
|
||||
Boolean isClockwiseCross = GeoUtility.isClockwise(mark1, mark2, prevMark.getMidPoint());
|
||||
|
||||
//Check we cross the line in the correct direction
|
||||
if (crossedLine == 1 && isClockwiseCross || crossedLine == 2 && !isClockwiseCross) {
|
||||
yacht.setHasPassedThroughGate(true);
|
||||
}
|
||||
}
|
||||
|
||||
Boolean prevMarkSide = GeoUtility.isClockwise(mark1, mark2, prevMark.getMidPoint());
|
||||
Boolean nextMarkSide = GeoUtility.isClockwise(mark1, mark2, nextMark.getMidPoint());
|
||||
|
||||
if (yacht.hasPassedThroughGate()) {
|
||||
//Check if we need to round this gate after passing through
|
||||
if (prevMarkSide == nextMarkSide) {
|
||||
return checkMarkRounding(yacht);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* If we pass the finish gate in the correct direction
|
||||
*
|
||||
* @param yacht The current yacht to check for
|
||||
*/
|
||||
private Boolean checkFinishLineCrossing(ServerYacht yacht) {
|
||||
Integer currentMarkSeqID = yacht.getCurrentMarkSeqID();
|
||||
CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID);
|
||||
GeoPoint lastLocation = yacht.getLastLocation();
|
||||
GeoPoint location = yacht.getLocation();
|
||||
|
||||
Mark mark1 = currentMark.getSubMark(1);
|
||||
Mark mark2 = currentMark.getSubMark(2);
|
||||
CompoundMark prevMark = markOrder.getPreviousMark(currentMarkSeqID);
|
||||
|
||||
Integer crossedLine = GeoUtility.checkCrossedLine(mark1, mark2, lastLocation, location);
|
||||
if (crossedLine > 0) {
|
||||
Boolean isClockwiseCross = GeoUtility.isClockwise(mark1, mark2, prevMark.getMidPoint());
|
||||
if (crossedLine == 1 && isClockwiseCross || crossedLine == 2 && !isClockwiseCross) {
|
||||
yacht.setClosestCurrentMark(mark1);
|
||||
yacht.setBoatStatus(BoatStatus.FINISHED);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private static Mark markCollidedWith(ServerYacht yacht) {
|
||||
Set<Mark> marksInRace = GameState.getMarks();
|
||||
for (Mark mark : marksInRace) {
|
||||
if (GeoUtility.getDistance(yacht.getLocation(), mark)
|
||||
<= MARK_COLLISION_DISTANCE) {
|
||||
return mark;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the new position of the boat after it has had a collision
|
||||
*
|
||||
* @return The boats new position
|
||||
*/
|
||||
private static GeoPoint calculateBounceBack(ServerYacht yacht, GeoPoint collidedWith, Double bounceDistance) {
|
||||
Double heading = GeoUtility.getBearing(yacht.getLocation(), collidedWith);
|
||||
// Invert heading
|
||||
heading -= 180;
|
||||
Integer newHeading = Math.floorMod(heading.intValue(), 360);
|
||||
return GeoUtility.getGeoCoordinate(yacht.getLocation(), newHeading.doubleValue(), bounceDistance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collision detection which iterates through all the yachts and check if any yacht collided
|
||||
* with this yacht. Return collided yacht or null if no collision.
|
||||
*
|
||||
* @return yacht to compare to all other yachts.
|
||||
*/
|
||||
private static ServerYacht checkCollision(ServerYacht yacht) {
|
||||
|
||||
for (ServerYacht otherYacht : GameState.getYachts().values()) {
|
||||
if (otherYacht != yacht) {
|
||||
Double distance = GeoUtility.getDistance(otherYacht.getLocation(), yacht.getLocation());
|
||||
if (distance < YACHT_COLLISION_DISTANCE) {
|
||||
return otherYacht;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void sendMarkRoundingMessage(ServerYacht yacht) {
|
||||
Integer sourceID = yacht.getSourceId();
|
||||
Integer currentMarkSeqID = yacht.getCurrentMarkSeqID();
|
||||
CompoundMark currentMark = markOrder.getCurrentMark(currentMarkSeqID);
|
||||
MarkType markType = (currentMark.isGate()) ? MarkType.GATE : MarkType.ROUNDING_MARK;
|
||||
Mark roundingMark = yacht.getClosestCurrentMark();
|
||||
|
||||
// TODO: 13/8/17 figure out the rounding side, rounded mark source ID and boat status.
|
||||
Message markRoundingMessage = new MarkRoundingMessage(0, 0,
|
||||
sourceID, RoundingBoatStatus.RACING, roundingMark.getRoundingSide(), markType,
|
||||
roundingMark.getSourceID());
|
||||
|
||||
notifyMessageListeners(markRoundingMessage);
|
||||
}
|
||||
|
||||
private static void notifyMessageListeners(Message message) {
|
||||
for (NewMessageListener mpl : markListeners) {
|
||||
mpl.notify(message);
|
||||
}
|
||||
}
|
||||
|
||||
private void logMarkRounding(ServerYacht yacht) {
|
||||
Mark roundingMark = yacht.getClosestCurrentMark();
|
||||
logger.debug(
|
||||
String.format("Yacht srcID(%d) passed Mark srcID(%d)", yacht.getSourceId(),
|
||||
roundingMark.getSourceID()));
|
||||
}
|
||||
|
||||
|
||||
public static void addMarkPassListener(NewMessageListener listener) {
|
||||
markListeners.add(listener);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,6 @@ public class HeartbeatThread extends Thread{
|
||||
*/
|
||||
private void sendHeartbeatToAllPlayers(){
|
||||
Message heartbeat = new Heartbeat(seqNum);
|
||||
|
||||
for (Player player : GameState.getPlayers()){
|
||||
if (!player.getSocket().isConnected()) {
|
||||
playerLostConnection(player);
|
||||
@@ -54,7 +53,6 @@ public class HeartbeatThread extends Thread{
|
||||
playerLostConnection(player);
|
||||
}
|
||||
}
|
||||
|
||||
updateDelegate();
|
||||
seqNum++;
|
||||
}
|
||||
@@ -71,7 +69,6 @@ public class HeartbeatThread extends Thread{
|
||||
|
||||
public void run(){
|
||||
Timer t = new Timer();
|
||||
|
||||
t.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
@@ -1,19 +1,31 @@
|
||||
package seng302.gameServer;
|
||||
|
||||
import gherkin.lexer.Fi;
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Observable;
|
||||
import java.util.List;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import seng302.gameServer.server.messages.BoatSubMessage;
|
||||
import seng302.gameServer.server.messages.Message;
|
||||
import seng302.gameServer.server.messages.RaceStatus;
|
||||
import seng302.gameServer.server.messages.RaceStatusMessage;
|
||||
import seng302.gameServer.server.messages.RaceType;
|
||||
import seng302.model.GeoPoint;
|
||||
import seng302.model.Player;
|
||||
import seng302.model.PolarTable;
|
||||
import seng302.model.ServerYacht;
|
||||
import seng302.model.mark.CompoundMark;
|
||||
import seng302.utilities.GeoUtility;
|
||||
import seng302.visualiser.GameClient;
|
||||
|
||||
/**
|
||||
* A class describing the overall server, which creates and collects server threads for each client
|
||||
* Created by wmu16 on 13/07/17.
|
||||
*/
|
||||
public class MainServerThread extends Observable implements Runnable, ClientConnectionDelegate{
|
||||
public class MainServerThread implements Runnable, ClientConnectionDelegate {
|
||||
|
||||
private static final int PORT = 4942;
|
||||
private static final Integer CLIENT_UPDATES_PER_SECOND = 10;
|
||||
@@ -25,15 +37,19 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
|
||||
private ServerSocket serverSocket = null;
|
||||
private ArrayList<ServerToClientThread> serverToClientThreads = new ArrayList<>();
|
||||
|
||||
private GameClient gameClient;
|
||||
|
||||
public MainServerThread() {
|
||||
new GameState("localhost");
|
||||
try {
|
||||
serverSocket = new ServerSocket(PORT);
|
||||
} catch (IOException e) {
|
||||
serverLog("IO error in server thread handler upon trying to make new server socket", 0);
|
||||
}
|
||||
|
||||
PolarTable.parsePolarFile(getClass().getResourceAsStream("/config/acc_polars.csv"));
|
||||
GameState.addMarkPassListener(this::broadcastMessage);
|
||||
terminated = false;
|
||||
thread = new Thread(this);
|
||||
thread = new Thread(this, "MainServer");
|
||||
thread.start();
|
||||
}
|
||||
|
||||
@@ -67,7 +83,7 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
|
||||
|
||||
//FINISHED
|
||||
else if (GameState.getCurrentStage() == GameStages.FINISHED) {
|
||||
|
||||
terminate();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,32 +98,43 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
|
||||
|
||||
public void updateClients() {
|
||||
for (ServerToClientThread serverToClientThread : serverToClientThreads) {
|
||||
serverToClientThread.updateClient();
|
||||
serverToClientThread.sendBoatLocationPackets();
|
||||
}
|
||||
}
|
||||
|
||||
private void broadcastMessage(Message message) {
|
||||
for (ServerToClientThread serverToClientThread : serverToClientThreads) {
|
||||
serverToClientThread.sendMessage(message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void serverLog(String message, int logLevel){
|
||||
if(logLevel <= LOG_LEVEL){
|
||||
System.out.println("[SERVER " + LocalDateTime.now().toLocalTime().toString() + "] " + message);
|
||||
static void serverLog(String message, int logLevel) {
|
||||
if (logLevel <= LOG_LEVEL) {
|
||||
System.out.println(
|
||||
"[SERVER " + LocalDateTime.now().toLocalTime().toString() + "] " + message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A client has tried to connect to the server
|
||||
*
|
||||
* @param serverToClientThread The player that connected
|
||||
*/
|
||||
@Override
|
||||
public void clientConnected(ServerToClientThread serverToClientThread) {
|
||||
serverLog("Player Connected From " + serverToClientThread.getThread().getName(), 0);
|
||||
serverToClientThreads.add(serverToClientThread);
|
||||
this.addObserver(serverToClientThread);
|
||||
setChanged();
|
||||
notifyObservers();
|
||||
serverToClientThread.addConnectionListener(() -> {
|
||||
for (ServerToClientThread thread : serverToClientThreads) {
|
||||
thread.sendSetupMessages();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* A player has left the game, remove the player from the GameState
|
||||
*
|
||||
* @param player The player that left
|
||||
*/
|
||||
@Override
|
||||
@@ -120,29 +147,108 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
|
||||
serverLog("Player " + player.getYacht().getSourceId() + "'s socket disconnected", 0);
|
||||
GameState.removeYacht(player.getYacht().getSourceId());
|
||||
GameState.removePlayer(player);
|
||||
ServerToClientThread closedConnection = null;
|
||||
for (ServerToClientThread serverToClientThread : serverToClientThreads) {
|
||||
if (serverToClientThread.getSocket() == player.getSocket()) {
|
||||
this.deleteObserver(serverToClientThread);
|
||||
closedConnection = serverToClientThread;
|
||||
} else {
|
||||
serverToClientThread.sendSetupMessages();
|
||||
}
|
||||
}
|
||||
setChanged();
|
||||
notifyObservers();
|
||||
serverToClientThreads.remove(closedConnection);
|
||||
}
|
||||
|
||||
public void startGame() {
|
||||
initialiseBoatPositions();
|
||||
Timer t = new Timer();
|
||||
|
||||
t.schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (ServerToClientThread serverToClientThread : serverToClientThreads) {
|
||||
serverToClientThread.sendRaceStatusMessage();
|
||||
}
|
||||
broadcastMessage(makeRaceStatusMessage());
|
||||
}
|
||||
}, 0, 500);
|
||||
}
|
||||
|
||||
private RaceStatusMessage makeRaceStatusMessage() {
|
||||
// variables taken from GameServerThread
|
||||
|
||||
List<BoatSubMessage> boatSubMessages = new ArrayList<>();
|
||||
RaceStatus raceStatus;
|
||||
|
||||
for (Player player : GameState.getPlayers()) {
|
||||
ServerYacht y = player.getYacht();
|
||||
BoatSubMessage m = new BoatSubMessage(y.getSourceId(), y.getBoatStatus(), 0,
|
||||
0, 0, 1234L,
|
||||
1234L);
|
||||
boatSubMessages.add(m);
|
||||
}
|
||||
|
||||
if (GameState.getCurrentStage() == GameStages.RACING) {
|
||||
raceStatus = RaceStatus.STARTED;
|
||||
} else {
|
||||
raceStatus = RaceStatus.WARNING;
|
||||
}
|
||||
|
||||
return new RaceStatusMessage(1, raceStatus, GameState.getStartTime(),
|
||||
GameState.getWindDirection(),
|
||||
GameState.getWindSpeedMMS().longValue(), GameState.getPlayers().size(),
|
||||
RaceType.MATCH_RACE, 1, boatSubMessages);
|
||||
}
|
||||
|
||||
public void terminate() {
|
||||
terminated = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass GameClient to main server thread so it can access the properties inside.
|
||||
*
|
||||
* @param gameClient gameClient
|
||||
*/
|
||||
public void setGameClient(GameClient gameClient) {
|
||||
this.gameClient = gameClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise boats to specific spaced out geopoints behind starting line.
|
||||
*/
|
||||
private void initialiseBoatPositions() {
|
||||
// Getting the start line compound marks
|
||||
CompoundMark cm = gameClient.getCourseData().getCompoundMarks().get(1);
|
||||
GeoPoint startMark1 = new GeoPoint(cm.getMarks().get(0).getLat(),
|
||||
cm.getMarks().get(0).getLng());
|
||||
GeoPoint startMark2 = new GeoPoint(cm.getMarks().get(1).getLat(),
|
||||
cm.getMarks().get(1).getLng());
|
||||
|
||||
// Calculating midpoint
|
||||
Double perpendicularAngle = GeoUtility.getBearing(startMark1, startMark2);
|
||||
Double length = GeoUtility.getDistance(startMark1, startMark2);
|
||||
GeoPoint midpoint = GeoUtility.getGeoCoordinate(startMark1, perpendicularAngle, length / 2);
|
||||
|
||||
// Setting each boats position side by side
|
||||
double DISTANCE_FACTOR = 50.0; // distance apart in meters
|
||||
int boatIndex = 0;
|
||||
for (ServerYacht yacht : GameState.getYachts().values()) {
|
||||
int distanceApart = boatIndex / 2;
|
||||
|
||||
if (boatIndex % 2 == 1 && boatIndex != 0) {
|
||||
distanceApart++;
|
||||
distanceApart *= -1;
|
||||
}
|
||||
|
||||
GeoPoint spawnMark = GeoUtility
|
||||
.getGeoCoordinate(midpoint, perpendicularAngle, distanceApart * DISTANCE_FACTOR);
|
||||
|
||||
if (yacht.getHeading() < perpendicularAngle) {
|
||||
spawnMark = GeoUtility
|
||||
.getGeoCoordinate(spawnMark, perpendicularAngle + 90, DISTANCE_FACTOR);
|
||||
} else {
|
||||
spawnMark = GeoUtility
|
||||
.getGeoCoordinate(spawnMark, perpendicularAngle + 270, DISTANCE_FACTOR);
|
||||
}
|
||||
|
||||
yacht.setLocation(spawnMark);
|
||||
boatIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,16 +5,16 @@ import java.util.Arrays;
|
||||
import seng302.gameServer.server.messages.ClientType;
|
||||
import seng302.gameServer.server.messages.Message;
|
||||
import seng302.model.stream.packets.StreamPacket;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
|
||||
|
||||
public class ServerPacketParser {
|
||||
|
||||
public static BoatActionType extractBoatAction(StreamPacket packet) {
|
||||
public static BoatAction extractBoatAction(StreamPacket packet) {
|
||||
byte[] payload = packet.getPayload();
|
||||
int messageVersionNo = payload[0];
|
||||
long actionTypeValue = Message.bytesToLong(Arrays.copyOfRange(payload, 0, 1));
|
||||
return BoatActionType.getType((int) actionTypeValue);
|
||||
return BoatAction.getType((int) actionTypeValue);
|
||||
}
|
||||
|
||||
public static ClientType extractClientType(StreamPacket packet){
|
||||
|
||||
@@ -9,7 +9,6 @@ import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Observable;
|
||||
@@ -18,15 +17,28 @@ import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.CRC32;
|
||||
import java.util.zip.Checksum;
|
||||
|
||||
import seng302.gameServer.server.messages.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import seng302.gameServer.server.messages.YachtEventCodeMessage;
|
||||
import seng302.model.Player;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.model.stream.packets.PacketType;
|
||||
import seng302.model.stream.packets.StreamPacket;
|
||||
import seng302.model.stream.xml.generator.Race;
|
||||
import seng302.model.stream.xml.generator.Regatta;
|
||||
import seng302.utilities.XMLGenerator;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
import seng302.gameServer.server.messages.BoatLocationMessage;
|
||||
import seng302.gameServer.server.messages.BoatSubMessage;
|
||||
import seng302.gameServer.server.messages.ClientType;
|
||||
import seng302.gameServer.server.messages.Message;
|
||||
import seng302.gameServer.server.messages.RaceStatus;
|
||||
import seng302.gameServer.server.messages.RaceStatusMessage;
|
||||
import seng302.gameServer.server.messages.RaceType;
|
||||
import seng302.gameServer.server.messages.RegistrationResponseMessage;
|
||||
import seng302.gameServer.server.messages.RegistrationResponseStatus;
|
||||
import seng302.gameServer.server.messages.XMLMessage;
|
||||
import seng302.gameServer.server.messages.XMLMessageSubType;
|
||||
import seng302.model.ServerYacht;
|
||||
|
||||
/**
|
||||
* A class describing a single connection to a Client for the purposes of sending and receiving on
|
||||
@@ -35,8 +47,15 @@ import seng302.utilities.XMLGenerator;
|
||||
*/
|
||||
public class ServerToClientThread implements Runnable, Observer {
|
||||
|
||||
private static final Integer LOG_LEVEL = 1;
|
||||
private static final Integer MAX_ID_ATTEMPTS = 10;
|
||||
/**
|
||||
* Called to notify listeners when this thread receives a connection correctly.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
interface ConnectionListener {
|
||||
void notifyConnection ();
|
||||
}
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(ServerToClientThread.class);
|
||||
|
||||
private Thread thread;
|
||||
|
||||
@@ -46,11 +65,6 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
|
||||
private ByteArrayOutputStream crcBuffer;
|
||||
|
||||
private Boolean userIdentified = false;
|
||||
private Boolean connected = true;
|
||||
private Boolean updateClient = true;
|
||||
// private Boolean initialisedRace = true;
|
||||
|
||||
private Integer seqNo;
|
||||
private Integer sourceId;
|
||||
|
||||
@@ -64,6 +78,10 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
private static final int PREPATORY_TIME = 10 * -1000;
|
||||
|
||||
|
||||
private List<ConnectionListener> connectionListeners = new ArrayList<>();
|
||||
|
||||
private ServerYacht yacht;
|
||||
|
||||
public ServerToClientThread(Socket socket) {
|
||||
this.socket = socket;
|
||||
seqNo = 0;
|
||||
@@ -75,58 +93,51 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
return;
|
||||
}
|
||||
|
||||
thread = new Thread(this);
|
||||
thread = new Thread(this, "ServerToClient");
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private void setUpYacht(){
|
||||
private void setUpPlayer(){
|
||||
BufferedReader fn;
|
||||
String fName = "";
|
||||
BufferedReader ln;
|
||||
String lName = "";
|
||||
try {
|
||||
is = socket.getInputStream();
|
||||
os = socket.getOutputStream();
|
||||
fn = new BufferedReader(
|
||||
new InputStreamReader(
|
||||
ServerToClientThread.class.getResourceAsStream(
|
||||
"/server_config/CSV_Database_of_First_Names.csv"
|
||||
)
|
||||
)
|
||||
);
|
||||
List<String> all = fn.lines().collect(Collectors.toList());
|
||||
fName = all.get(ThreadLocalRandom.current().nextInt(0, all.size()));
|
||||
ln = new BufferedReader(
|
||||
new InputStreamReader(
|
||||
ServerToClientThread.class.getResourceAsStream(
|
||||
"/server_config/CSV_Database_of_Last_Names.csv"
|
||||
)
|
||||
)
|
||||
);
|
||||
all = ln.lines().collect(Collectors.toList());
|
||||
lName = all.get(ThreadLocalRandom.current().nextInt(0, all.size()));
|
||||
} catch (IOException e) {
|
||||
serverLog("IO error in server thread upon grabbing streams", 1);
|
||||
}
|
||||
|
||||
Yacht yacht = new Yacht(
|
||||
fn = new BufferedReader(
|
||||
new InputStreamReader(
|
||||
ServerToClientThread.class.getResourceAsStream(
|
||||
"/server_config/CSV_Database_of_First_Names.csv"
|
||||
)
|
||||
)
|
||||
);
|
||||
List<String> all = fn.lines().collect(Collectors.toList());
|
||||
fName = all.get(ThreadLocalRandom.current().nextInt(0, all.size()));
|
||||
ln = new BufferedReader(
|
||||
new InputStreamReader(
|
||||
ServerToClientThread.class.getResourceAsStream(
|
||||
"/server_config/CSV_Database_of_Last_Names.csv"
|
||||
)
|
||||
)
|
||||
);
|
||||
all = ln.lines().collect(Collectors.toList());
|
||||
lName = all.get(ThreadLocalRandom.current().nextInt(0, all.size()));
|
||||
|
||||
ServerYacht yacht = new ServerYacht(
|
||||
"Yacht", sourceId, sourceId.toString(), fName, fName + " " + lName, "NZ"
|
||||
);
|
||||
|
||||
yacht.addObserver(this); // TODO: yacht can notify mark rounding message hyi25 13/8/17
|
||||
GameState.addYacht(sourceId, yacht);
|
||||
GameState.addPlayer(new Player(socket, yacht));
|
||||
}
|
||||
|
||||
static void serverLog(String message, int logLevel) {
|
||||
if (logLevel <= LOG_LEVEL) {
|
||||
System.out.println(
|
||||
"[SERVER " + LocalDateTime.now().toLocalTime().toString() + "] " + message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Observable o, Object arg) {
|
||||
sendSetupMessages();
|
||||
if (arg != null) {
|
||||
sendMessage((Message) arg);
|
||||
} else {
|
||||
sendSetupMessages();
|
||||
}
|
||||
}
|
||||
|
||||
private void completeRegistration(ClientType clientType) throws IOException {
|
||||
@@ -148,12 +159,14 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
|
||||
this.clientType = clientType;
|
||||
this.sourceId = sourceId;
|
||||
setUpYacht();
|
||||
|
||||
isRegistered = true;
|
||||
os.write(responseMessage.getBuffer());
|
||||
sendSetupMessages();
|
||||
|
||||
setUpPlayer();
|
||||
|
||||
for (ConnectionListener listener : connectionListeners) {
|
||||
listener.notifyConnection();
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
@@ -161,24 +174,9 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
int sync2;
|
||||
// TODO: 14/07/17 wmu16 - Work out how to fix this while loop
|
||||
|
||||
|
||||
while (socket.isConnected()) {
|
||||
|
||||
try {
|
||||
//Perform a write if it is time to as delegated by the MainServerThread
|
||||
if (updateClient) {
|
||||
// TODO: 13/07/17 wmu16 - Write out game state - some function that would write all appropriate messages to this output stream
|
||||
// ChatterMessage chatterMessage = new ChatterMessage(4, 14, "Hello, it's me");
|
||||
// sendMessage(chatterMessage);
|
||||
// try {
|
||||
// GameState.outputState(os);
|
||||
// } catch (IOException e) {
|
||||
// System.out.println("IO error in server thread upon writing to output stream");
|
||||
// }
|
||||
// sendBoatLocationPackets();
|
||||
updateClient = false;
|
||||
}
|
||||
|
||||
crcBuffer = new ByteArrayOutputStream();
|
||||
sync1 = readByte();
|
||||
sync2 = readByte();
|
||||
@@ -198,7 +196,7 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
//System.out.println("RECEIVED A PACKET");
|
||||
switch (PacketType.assignPacketType(type, payload)) {
|
||||
case BOAT_ACTION:
|
||||
BoatActionType actionType = ServerPacketParser
|
||||
BoatAction actionType = ServerPacketParser
|
||||
.extractBoatAction(
|
||||
new StreamPacket(type, payloadLength, timeStamp, payload));
|
||||
GameState.updateBoat(sourceId, actionType);
|
||||
@@ -212,7 +210,7 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
serverLog("Packet has been dropped", 1);
|
||||
logger.warn("Packet has been dropped", 1);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -222,11 +220,11 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
}
|
||||
}
|
||||
|
||||
private void sendSetupMessages() {
|
||||
public void sendSetupMessages() {
|
||||
xml = new XMLGenerator();
|
||||
Race race = new Race();
|
||||
|
||||
for (Yacht yacht : GameState.getYachts().values()) {
|
||||
for (ServerYacht yacht : GameState.getYachts().values()) {
|
||||
race.addBoat(yacht);
|
||||
}
|
||||
|
||||
@@ -248,28 +246,6 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
sendMessage(xmlMessage);
|
||||
}
|
||||
|
||||
public void updateClient() {
|
||||
sendRaceStatusMessage();
|
||||
sendBoatLocationPackets();
|
||||
updateClient = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tries to confirm the connection just accepted.
|
||||
* Sends ID, expects that ID echoed for confirmation,
|
||||
* if so, sends a confirmation packet back to that connection
|
||||
* Creates a player instance with that ID and this thread and adds it to the GameState
|
||||
* If not, close the socket and end the threads execution
|
||||
*
|
||||
* @param id the id to try and assign to the connection
|
||||
* @return A boolean indicating if it was a successful handshake
|
||||
*/
|
||||
private Boolean threeWayHandshake(Integer id) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void closeSocket() {
|
||||
try {
|
||||
socket.close();
|
||||
@@ -278,7 +254,6 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private int readByte() throws Exception {
|
||||
int currentByte = -1;
|
||||
try {
|
||||
@@ -287,7 +262,7 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
crcBuffer.write(currentByte);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
serverLog("Socket read failed", 1);
|
||||
logger.warn("Socket read failed", 1);
|
||||
}
|
||||
if (currentByte == -1) {
|
||||
throw new Exception();
|
||||
@@ -316,7 +291,7 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
//serverLog("Player " + sourceId + " side socket disconnected", 1);
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
serverLog("Message send failed", 1);
|
||||
logger.warn("Message send failed", 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,10 +301,9 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
}
|
||||
|
||||
|
||||
private void sendBoatLocationPackets() {
|
||||
ArrayList<Yacht> yachts = new ArrayList<>(GameState.getYachts().values());
|
||||
for (Yacht yacht : yachts) {
|
||||
// System.out.println("[SERVER] Lat: " + yacht.getLocation().getLat() + " Lon: " + yacht.getLocation().getLng());
|
||||
public void sendBoatLocationPackets() {
|
||||
ArrayList<ServerYacht> yachts = new ArrayList<>(GameState.getYachts().values());
|
||||
for (ServerYacht yacht : yachts) {
|
||||
BoatLocationMessage boatLocationMessage =
|
||||
new BoatLocationMessage(
|
||||
yacht.getSourceId(),
|
||||
@@ -337,7 +311,7 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
yacht.getLocation().getLat(),
|
||||
yacht.getLocation().getLng(),
|
||||
yacht.getHeading(),
|
||||
yacht.getVelocity().longValue());
|
||||
yacht.getCurrentVelocity().longValue());
|
||||
|
||||
sendMessage(boatLocationMessage);
|
||||
}
|
||||
@@ -347,64 +321,23 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
return thread;
|
||||
}
|
||||
|
||||
public void sendRaceStatusMessage() {
|
||||
// variables taken from GameServerThread
|
||||
|
||||
|
||||
List<BoatSubMessage> boatSubMessages = new ArrayList<>();
|
||||
BoatStatus boatStatus;
|
||||
RaceStatus raceStatus;
|
||||
|
||||
for (Player player : GameState.getPlayers()) {
|
||||
Yacht y = player.getYacht();
|
||||
|
||||
if (GameState.getCurrentStage() == GameStages.PRE_RACE) {
|
||||
boatStatus = BoatStatus.PRESTART;
|
||||
} else if (GameState.getCurrentStage() == GameStages.RACING) {
|
||||
boatStatus = BoatStatus.RACING;
|
||||
} else {
|
||||
boatStatus = BoatStatus.UNDEFINED;
|
||||
}
|
||||
|
||||
BoatSubMessage m = new BoatSubMessage(y.getSourceId(), boatStatus, 0, 0, 0, 1234l,
|
||||
1234l);
|
||||
boatSubMessages.add(m);
|
||||
}
|
||||
|
||||
long timeTillStart = System.currentTimeMillis() - GameState.getStartTime();
|
||||
|
||||
if (GameState.getCurrentStage() == GameStages.LOBBYING) {
|
||||
raceStatus = RaceStatus.PRESTART;
|
||||
} else if (GameState.getCurrentStage() == GameStages.PRE_RACE) {
|
||||
raceStatus = RaceStatus.PRESTART;
|
||||
|
||||
if (timeTillStart > WARNING_TIME) {
|
||||
raceStatus = RaceStatus.WARNING;
|
||||
}
|
||||
|
||||
if (timeTillStart > PREPATORY_TIME) {
|
||||
raceStatus = RaceStatus.PREPARATORY;
|
||||
}
|
||||
} else {
|
||||
raceStatus = RaceStatus.STARTED;
|
||||
}
|
||||
|
||||
System.out.println("raceStatus.ger = " + raceStatus.getCode());
|
||||
|
||||
sendMessage(new RaceStatusMessage(1, raceStatus, GameState.getStartTime(), GameState.getWindDirection(),
|
||||
GameState.getWindSpeedMMS().longValue(), GameState.getPlayers().size(),
|
||||
RaceType.MATCH_RACE, 1, boatSubMessages));
|
||||
|
||||
if (GameState.getCurrentStage() == GameStages.PRE_RACE || GameState.getCurrentStage() == GameStages.LOBBYING){
|
||||
Long raceStartTime = GameState.getStartTime();
|
||||
|
||||
sendMessage(new RaceStartStatusMessage(1, raceStartTime ,
|
||||
1, RaceStartNotificationType.SET_RACE_START_TIME));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
public ServerYacht getYacht() {
|
||||
return yacht;
|
||||
}
|
||||
|
||||
public void sendCollisionMessage(Integer yachtId) {
|
||||
sendMessage(new YachtEventCodeMessage(yachtId));
|
||||
}
|
||||
|
||||
public void addConnectionListener(ConnectionListener listener) {
|
||||
connectionListeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeConnectionListener(ConnectionListener listener) {
|
||||
connectionListeners.remove(listener);
|
||||
}
|
||||
}
|
||||
|
||||
+7
-6
@@ -6,29 +6,30 @@ import java.util.Map;
|
||||
/**
|
||||
* Created by kre39 on 12/07/17.
|
||||
*/
|
||||
public enum BoatActionType {
|
||||
public enum BoatAction {
|
||||
|
||||
VMG(1),
|
||||
SAILS_IN(2),
|
||||
SAILS_OUT(3),
|
||||
TACK_GYBE(4),
|
||||
UPWIND(5),
|
||||
DOWNWIND(6);
|
||||
DOWNWIND(6),
|
||||
MAINTAIN_HEADING(7);
|
||||
|
||||
private final int type;
|
||||
private static final Map<Integer, BoatActionType> intToTypeMap = new HashMap<>();
|
||||
private static final Map<Integer, BoatAction> intToTypeMap = new HashMap<>();
|
||||
|
||||
static {
|
||||
for (BoatActionType type : BoatActionType.values()) {
|
||||
for (BoatAction type : BoatAction.values()) {
|
||||
intToTypeMap.put(type.getValue(), type);
|
||||
}
|
||||
}
|
||||
|
||||
BoatActionType(int type){
|
||||
BoatAction(int type){
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public static BoatActionType getType(int value) {
|
||||
public static BoatAction getType(int value) {
|
||||
return intToTypeMap.get(value);
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ package seng302.gameServer.server.messages;
|
||||
public class BoatActionMessage extends Message{
|
||||
private final MessageType MESSAGE_TYPE = MessageType.BOAT_ACTION;
|
||||
private final int MESSAGE_SIZE = 1;
|
||||
private BoatActionType actionType;
|
||||
private BoatAction actionType;
|
||||
|
||||
public BoatActionMessage(BoatActionType actionType) {
|
||||
public BoatActionMessage(BoatAction actionType) {
|
||||
this.actionType = actionType;
|
||||
setHeader(new Header(MessageType.BOAT_ACTION, 0, (short) 1)); // the second variable is the source id
|
||||
allocateBuffer();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package seng302.gameServer.server.messages;
|
||||
|
||||
import seng302.gameServer.GameState;
|
||||
|
||||
public class MarkRoundingMessage extends Message{
|
||||
private final long MESSAGE_VERSION_NUMBER = 1;
|
||||
private final int MESSAGE_SIZE = 21;
|
||||
@@ -18,14 +20,14 @@ public class MarkRoundingMessage extends Message{
|
||||
* The purpose of this is to record the time when yachts cross marks
|
||||
* @param ackNumber ackNumber
|
||||
* @param raceId raceId
|
||||
* @param sourceId sourceId
|
||||
* @param sourceId boatSourceId
|
||||
* @param roundingBoatStatus roundingBoatStatus
|
||||
* @param roundingSide roundingSide
|
||||
* @param markId markId
|
||||
*/
|
||||
public MarkRoundingMessage(int ackNumber, int raceId, int sourceId, RoundingBoatStatus roundingBoatStatus,
|
||||
RoundingSide roundingSide, int markId){
|
||||
this.time = System.currentTimeMillis() / 1000L;
|
||||
RoundingSide roundingSide, MarkType markType, int markId) {
|
||||
this.time = System.currentTimeMillis();
|
||||
this.ackNumber = ackNumber;
|
||||
this.raceId = raceId;
|
||||
this.sourceId = sourceId;
|
||||
@@ -44,6 +46,7 @@ public class MarkRoundingMessage extends Message{
|
||||
putInt((int) sourceId, 4);
|
||||
putByte((byte) boatStatus.getCode());
|
||||
putByte((byte) roundingSide.getCode());
|
||||
putByte((byte) markType.getCode());
|
||||
putByte((byte) markId);
|
||||
|
||||
writeCRC();
|
||||
|
||||
@@ -4,17 +4,49 @@ package seng302.gameServer.server.messages;
|
||||
* The side the boat rounded the mark
|
||||
*/
|
||||
public enum RoundingSide {
|
||||
UNKNOWN(0),
|
||||
PORT(1),
|
||||
STARBOARD(2);
|
||||
UNKNOWN(0, "Unknown"),
|
||||
PORT(1, "Port"),
|
||||
STARBOARD(2, "Stbd"),
|
||||
SP(3, "SP"),
|
||||
PS(4, "PS");
|
||||
|
||||
|
||||
private long code;
|
||||
private String name;
|
||||
|
||||
RoundingSide(long code) {
|
||||
RoundingSide(long code, String name) {
|
||||
this.code = code;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getCode(){
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public static RoundingSide getRoundingSide(String identifier) {
|
||||
RoundingSide roundingSide = UNKNOWN;
|
||||
switch (identifier) {
|
||||
case "Unknown":
|
||||
roundingSide = UNKNOWN;
|
||||
break;
|
||||
case "Port":
|
||||
roundingSide = PORT;
|
||||
break;
|
||||
case "Stbd":
|
||||
roundingSide = STARBOARD;
|
||||
break;
|
||||
case "SP":
|
||||
roundingSide = SP;
|
||||
break;
|
||||
case "PS":
|
||||
roundingSide = PS;
|
||||
break;
|
||||
}
|
||||
|
||||
return roundingSide;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package seng302.gameServer.server.messages;
|
||||
|
||||
/**
|
||||
* Created by zyt10 on 10/08/17.
|
||||
*/
|
||||
public class YachtEventCodeMessage extends Message {
|
||||
|
||||
private final MessageType MESSAGE_TYPE = MessageType.YACHT_EVENT_CODE;
|
||||
private final int MESSAGE_VERSION = 1; //Always set to 1
|
||||
private final int MESSAGE_SIZE = 22;
|
||||
|
||||
// Message fields
|
||||
private long timeStamp;
|
||||
private long ack = 0x00; //Unused
|
||||
private int raceId;
|
||||
private int destSourceId;
|
||||
private int incidentId;
|
||||
private int eventId;
|
||||
|
||||
|
||||
public YachtEventCodeMessage(Integer subjectId) {
|
||||
timeStamp = System.currentTimeMillis() / 1000L;
|
||||
ack = 0;
|
||||
raceId = 1;
|
||||
destSourceId = subjectId; // collision boat source id
|
||||
incidentId = 0;
|
||||
eventId = 33;
|
||||
|
||||
setHeader(new Header(MESSAGE_TYPE, 0x01, (short) getSize()));
|
||||
allocateBuffer();
|
||||
writeHeaderToBuffer();
|
||||
|
||||
// Write message fields
|
||||
putUnsignedByte((byte) MESSAGE_VERSION);
|
||||
putInt((int) timeStamp, 6);
|
||||
putInt((int) ack, 2);
|
||||
putInt((int) raceId, 4);
|
||||
putInt((int) destSourceId, 4);
|
||||
putInt((int) incidentId, 4);
|
||||
putInt((int) eventId, 1);
|
||||
|
||||
writeCRC();
|
||||
rewind();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The length of this message
|
||||
*/
|
||||
public int getSize() {
|
||||
return MESSAGE_SIZE;
|
||||
}
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
package seng302.gameServer.server.simulator;
|
||||
|
||||
import seng302.model.GeoPoint;
|
||||
import seng302.utilities.GeoUtility;
|
||||
|
||||
public class Boat {
|
||||
|
||||
private int sourceID;
|
||||
private double lat;
|
||||
private double lng;
|
||||
private double speed; // in mm/sec
|
||||
private String boatName, shortName, shorterName;
|
||||
private boolean isFinished;
|
||||
private long estimatedTimeTillFinish;
|
||||
|
||||
private Corner lastPassedCorner, headingCorner;
|
||||
|
||||
public Boat(int sourceID, String boatName) {
|
||||
this.sourceID = sourceID;
|
||||
this.boatName = boatName;
|
||||
this.isFinished = false;
|
||||
estimatedTimeTillFinish = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves boat to the heading direction for a given time duration
|
||||
* @param heading moving direction in degree.
|
||||
* @param duration moving duration in millisecond.
|
||||
*/
|
||||
public void move(double heading, double duration) {
|
||||
Double distance = speed * duration / 1000000; // convert mm to meter
|
||||
GeoPoint originPos = new GeoPoint(lat, lng);
|
||||
GeoPoint newPos = GeoUtility.getGeoCoordinate(originPos, heading, distance);
|
||||
this.lat = newPos.getLat();
|
||||
this.lng = newPos.getLng();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return String.format("Boat (%d): lat: %f, lng: %f", sourceID, lat, lng);
|
||||
}
|
||||
|
||||
public int getSourceID() {
|
||||
return sourceID;
|
||||
}
|
||||
|
||||
public void setSourceID(int sourceID) {
|
||||
this.sourceID = sourceID;
|
||||
}
|
||||
|
||||
public double getLat() {
|
||||
return lat;
|
||||
}
|
||||
|
||||
public void setLat(double lat) {
|
||||
this.lat = lat;
|
||||
}
|
||||
|
||||
public double getLng() {
|
||||
return lng;
|
||||
}
|
||||
|
||||
public void setLng(double lng) {
|
||||
this.lng = lng;
|
||||
}
|
||||
|
||||
public double getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
public void setSpeed(double speed) {
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
public String getBoatName() {
|
||||
return boatName;
|
||||
}
|
||||
|
||||
public void setBoatName(String boatName) {
|
||||
this.boatName = boatName;
|
||||
}
|
||||
|
||||
public String getShortName() {
|
||||
return shortName;
|
||||
}
|
||||
|
||||
public void setShortName(String shortName) {
|
||||
this.shortName = shortName;
|
||||
}
|
||||
|
||||
public String getShorterName() {
|
||||
return shorterName;
|
||||
}
|
||||
|
||||
public void setShorterName(String shorterName) {
|
||||
this.shorterName = shorterName;
|
||||
}
|
||||
|
||||
public Corner getLastPassedCorner() {
|
||||
return lastPassedCorner;
|
||||
}
|
||||
|
||||
public void setLastPassedCorner(Corner lastPassedCorner) {
|
||||
this.lastPassedCorner = lastPassedCorner;
|
||||
}
|
||||
|
||||
public Corner getHeadingCorner() {
|
||||
return headingCorner;
|
||||
}
|
||||
|
||||
public void setHeadingCorner(Corner headingCorner) {
|
||||
this.headingCorner = headingCorner;
|
||||
}
|
||||
|
||||
public boolean isFinished() {
|
||||
return isFinished;
|
||||
}
|
||||
|
||||
public void setFinished(boolean finished) {
|
||||
isFinished = finished;
|
||||
}
|
||||
|
||||
public long getEstimatedTimeTillFinish(){
|
||||
return (long) (-getSpeed()) + System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
package seng302.gameServer.server.simulator;
|
||||
|
||||
import seng302.model.mark.CompoundMark;
|
||||
|
||||
public class Corner {
|
||||
|
||||
private int seqID;
|
||||
private CompoundMark compoundMark;
|
||||
//private int CompoundMarkID;
|
||||
private RoundingType roundingType;
|
||||
private int zoneSize; // size of the zone around a mark in boat-lengths.
|
||||
|
||||
// TODO: this shouldn't be used in the future!!!!
|
||||
private double bearingToNextCorner, distanceToNextCorner;
|
||||
private Corner nextCorner;
|
||||
|
||||
public Corner(int seqID, CompoundMark compoundMark, RoundingType roundingType, int zoneSize) {
|
||||
this.seqID = seqID;
|
||||
this.compoundMark = compoundMark;
|
||||
this.roundingType = roundingType;
|
||||
this.zoneSize = zoneSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints out corner's info and its compound mark, good for testing
|
||||
* @return a string showing its details
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Corner: %d - %s - %d, %s\n",
|
||||
seqID, roundingType.getType(), zoneSize, compoundMark.toString());
|
||||
}
|
||||
|
||||
public int getSeqID() {
|
||||
return seqID;
|
||||
}
|
||||
|
||||
public void setSeqID(int seqID) {
|
||||
this.seqID = seqID;
|
||||
}
|
||||
|
||||
public CompoundMark getCompoundMark() {
|
||||
return compoundMark;
|
||||
}
|
||||
|
||||
public void setCompoundMark(CompoundMark compoundMark) {
|
||||
this.compoundMark = compoundMark;
|
||||
}
|
||||
|
||||
public RoundingType getRoundingType() {
|
||||
return roundingType;
|
||||
}
|
||||
|
||||
public void setRoundingType(RoundingType roundingType) {
|
||||
this.roundingType = roundingType;
|
||||
}
|
||||
|
||||
public int getZoneSize() {
|
||||
return zoneSize;
|
||||
}
|
||||
|
||||
public void setZoneSize(int zoneSize) {
|
||||
this.zoneSize = zoneSize;
|
||||
}
|
||||
|
||||
|
||||
// TODO: next six setters & getters shouldn't be used in the future.
|
||||
public double getBearingToNextCorner() {
|
||||
return bearingToNextCorner;
|
||||
}
|
||||
|
||||
public void setBearingToNextCorner(double bearingToNextCorner) {
|
||||
this.bearingToNextCorner = bearingToNextCorner;
|
||||
}
|
||||
|
||||
public double getDistanceToNextCorner() {
|
||||
return distanceToNextCorner;
|
||||
}
|
||||
|
||||
public void setDistanceToNextCorner(double distanceToNextCorner) {
|
||||
this.distanceToNextCorner = distanceToNextCorner;
|
||||
}
|
||||
|
||||
public Corner getNextCorner() {
|
||||
return nextCorner;
|
||||
}
|
||||
|
||||
public void setNextCorner(Corner nextCorner) {
|
||||
this.nextCorner = nextCorner;
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package seng302.gameServer.server.simulator;
|
||||
|
||||
public enum RoundingType {
|
||||
|
||||
// the mark should be rounded to port (boat's left)
|
||||
PORT("Port"),
|
||||
|
||||
// the mark should be rounded to starboard (boat's right)
|
||||
STARBOARD("Stbd"),
|
||||
|
||||
// the boat within the compound mark with the SeqID of 1 should be rounded
|
||||
// to starboard and the boat within the compound mark with the SeqID of 2
|
||||
// should be rounded to port.
|
||||
SP("SP"),
|
||||
|
||||
// the opposite of SP
|
||||
PS("PS");
|
||||
|
||||
private String type;
|
||||
|
||||
RoundingType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public static RoundingType typeOf(String type) {
|
||||
switch (type) {
|
||||
case "Port":
|
||||
return PORT;
|
||||
case "Stbd":
|
||||
return STARBOARD;
|
||||
case "SP":
|
||||
return SP;
|
||||
case "PS":
|
||||
return PS;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package seng302.gameServer.server.simulator.parsers;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
|
||||
/**
|
||||
* Parses the race xml file to get course details
|
||||
* Created by Haoming Yin (hyi25) on 16/3/2017
|
||||
*/
|
||||
public class BoatsParser extends FileParser {
|
||||
|
||||
private Document doc;
|
||||
|
||||
public BoatsParser(String path) {
|
||||
super(path);
|
||||
this.doc = this.parseFile();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
package seng302.gameServer.server.simulator.parsers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import seng302.model.mark.CompoundMark;
|
||||
import seng302.gameServer.server.simulator.Corner;
|
||||
import seng302.model.mark.Mark;
|
||||
import seng302.gameServer.server.simulator.RoundingType;
|
||||
|
||||
/**
|
||||
* Parses the race xml file to get course details
|
||||
* Created by Haoming Yin (hyi25) on 16/3/2017
|
||||
*/
|
||||
public class CourseParser extends FileParser {
|
||||
|
||||
private Document doc;
|
||||
private Map<Integer, CompoundMark> compoundMarksMap;
|
||||
|
||||
public CourseParser(String path) {
|
||||
super(path);
|
||||
this.doc = this.parseFile();
|
||||
}
|
||||
|
||||
// TODO: should handle error / invalid file gracefully
|
||||
protected List<Corner> getCourse() {
|
||||
compoundMarksMap = getCompoundMarks(doc.getDocumentElement());
|
||||
List<Corner> corners = new ArrayList<>();
|
||||
NodeList cMarksSequence = doc.getElementsByTagName("Corner");
|
||||
|
||||
for (int i = 0; i < cMarksSequence.getLength(); i++) {
|
||||
corners.add(getCorner(cMarksSequence.item(i)));
|
||||
}
|
||||
return corners;
|
||||
}
|
||||
|
||||
|
||||
private Corner getCorner(Node node) {
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element e = (Element) node;
|
||||
|
||||
Integer seqId = Integer.valueOf(e.getAttribute("SeqID"));
|
||||
Integer cMarkId = Integer.valueOf(e.getAttribute("CompoundMarkID"));
|
||||
CompoundMark cMark = compoundMarksMap.get(cMarkId);
|
||||
RoundingType roundingType = RoundingType.typeOf(e.getAttribute("Rounding"));
|
||||
Integer zoneSize = Integer.valueOf(e.getAttribute("ZoneSize"));
|
||||
|
||||
return new Corner(seqId, cMark, roundingType, zoneSize);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Map<Integer, CompoundMark> getCompoundMarks(Node node) {
|
||||
Map<Integer, CompoundMark> compoundMarksMap = new HashMap<>();
|
||||
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element element = (Element) node;
|
||||
NodeList cMarks = element.getElementsByTagName("CompoundMark");
|
||||
|
||||
// loop through all compound marks who are the children of course node
|
||||
for (int i = 0; i < cMarks.getLength(); i++) {
|
||||
CompoundMark cMark = getCompoundMark(cMarks.item(i));
|
||||
if (cMark != null)
|
||||
compoundMarksMap.put(cMark.getId(), cMark);
|
||||
}
|
||||
|
||||
return compoundMarksMap;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private CompoundMark getCompoundMark(Node node) {
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element e = (Element) node;
|
||||
Integer markID = Integer.valueOf(e.getAttribute("CompoundMarkID"));
|
||||
String name = e.getAttribute("Name");
|
||||
|
||||
NodeList marks = e.getElementsByTagName("Mark");
|
||||
List<Mark> subMarks = new ArrayList<>();
|
||||
for (int i = 0; i < marks.getLength(); i++) {
|
||||
Mark mark = getMark(marks.item(i));
|
||||
if (mark != null) {
|
||||
subMarks.add(mark);
|
||||
}
|
||||
}
|
||||
|
||||
return new CompoundMark(markID, name, subMarks);
|
||||
}
|
||||
System.out.println("Failed to create compound mark.");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private Mark getMark(Node node) {
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element e = (Element) node;
|
||||
Integer seqId = Integer.valueOf(e.getAttribute("SeqID"));
|
||||
String name = e.getAttribute("Name");
|
||||
Double lat = Double.valueOf(e.getAttribute("TargetLat"));
|
||||
Double lng = Double.valueOf(e.getAttribute("TargetLng"));
|
||||
Integer sourceId = Integer.valueOf(e.getAttribute("SourceID"));
|
||||
|
||||
Mark mark = new Mark(name, lat, lng, sourceId);
|
||||
mark.setSeqID(seqId);
|
||||
|
||||
return mark;
|
||||
}
|
||||
System.out.println("Failed to create mark.");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package seng302.gameServer.server.simulator.parsers;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.StringReader;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
/**
|
||||
* Created by Haoming Yin (hyi25) on 16/3/2017
|
||||
*/
|
||||
public abstract class FileParser {
|
||||
|
||||
private String filePath;
|
||||
|
||||
public FileParser() {}
|
||||
|
||||
public FileParser(String path) {
|
||||
this.filePath = path;
|
||||
}
|
||||
|
||||
protected Document parseFile() {
|
||||
try {
|
||||
InputStream is = getClass().getResourceAsStream(this.filePath);
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document doc = builder.parse(is);
|
||||
// optional, in order to recover info from broken line.
|
||||
doc.getDocumentElement().normalize();
|
||||
return doc;
|
||||
} catch (Exception e) {
|
||||
System.out.println("[FileParser] Exception");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected Document parseFile(String xmlString) {
|
||||
try {
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder builder = factory.newDocumentBuilder();
|
||||
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
|
||||
// optional, in order to recover info from broken line.
|
||||
doc.getDocumentElement().normalize();
|
||||
return doc;
|
||||
} catch (Exception e) {
|
||||
System.out.println("[FileParser] Exception");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
package seng302.gameServer.server.simulator.parsers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import seng302.gameServer.server.simulator.Boat;
|
||||
import seng302.gameServer.server.simulator.Corner;
|
||||
|
||||
/**
|
||||
* Parses the race xml file to get course details
|
||||
* Created by Haoming Yin (hyi25) on 16/3/2017
|
||||
*/
|
||||
public class RaceParser extends FileParser {
|
||||
|
||||
private Document doc;
|
||||
private String path;
|
||||
|
||||
public RaceParser(String path) {
|
||||
super(path);
|
||||
this.path = path;
|
||||
this.doc = this.parseFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses race.xml file and returns a list of corner which is the race course.
|
||||
* @return a list of ordered corner to represent the course.
|
||||
*/
|
||||
public List<Corner> getCourse() {
|
||||
CourseParser cp = new CourseParser(path);
|
||||
return cp.getCourse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses race.xml file and return a list of boats which will compete in the
|
||||
* race.
|
||||
* @return a list of boats that are going to compete in the race.
|
||||
*/
|
||||
public List<Boat> getBoats() {
|
||||
NodeList yachts = doc.getDocumentElement().getElementsByTagName("Yacht");
|
||||
List<Boat> boats = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < yachts.getLength(); i++) {
|
||||
boats.add(getBoat(yachts.item(i)));
|
||||
}
|
||||
return boats;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a single boat from the given node
|
||||
* @param node a node within a boat tag
|
||||
* @return a boat instance parsed from the given node
|
||||
*/
|
||||
private Boat getBoat(Node node) {
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element e = (Element) node;
|
||||
|
||||
Integer sourceId = Integer.valueOf(e.getAttribute("SourceID"));
|
||||
return new Boat(sourceId, "Test Boat");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user