Merge branch 'develop' into 1124_switching_to_finish_screen

# 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/model/Yacht.java
#	src/main/java/seng302/visualiser/GameClient.java
#	src/main/java/seng302/visualiser/GameView.java
#	src/main/java/seng302/visualiser/controllers/RaceViewController.java
#	src/test/java/seng302/visualiser/ClientToServerTests/RegularPacketsTest.java
This commit is contained in:
Calum
2017-08-15 23:45:50 +12:00
27 changed files with 1602 additions and 653 deletions
@@ -1,21 +1,29 @@
package seng302.gameServer;
import com.sun.corba.se.spi.activation.Server;
import java.io.IOException;
import java.net.ServerSocket;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;
import seng302.gameServer.server.messages.Message;
import seng302.model.GeoPoint;
import seng302.model.Player;
import seng302.model.Yacht;
import seng302.model.mark.CompoundMark;
import seng302.utilities.GeoUtility;
import seng302.visualiser.GameClient;
import seng302.model.PolarTable;
/**
* 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 extends Observable implements Runnable, ClientConnectionDelegate,
Observer {
private static final int PORT = 4942;
private static final Integer CLIENT_UPDATES_PER_SECOND = 10;
@@ -27,7 +35,10 @@ 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) {
@@ -46,7 +57,6 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
HeartbeatThread heartbeatThread;
serverListenThread = new ServerListenThread(serverSocket, this);
heartbeatThread = new HeartbeatThread(this);
heartbeatThread.start();
@@ -97,14 +107,16 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
}
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
@@ -120,6 +132,7 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
/**
* A player has left the game, remove the player from the GameState
*
* @param player The player that left
*/
@Override
@@ -141,11 +154,12 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
}
}
serverToClientThreads.remove(closedConnection);
setChanged();
notifyObservers();
}
public void startGame() {
initialiseBoatPositions();
setupYachtObserver();
Timer t = new Timer();
t.schedule(new TimerTask() {
@@ -162,4 +176,69 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
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 DISTANCEFACTOR = 50.0; // distance apart in meters
int boatIndex = 0;
for (Yacht yacht : GameState.getYachts().values()) {
int distanceApart = boatIndex / 2;
if (boatIndex % 2 == 1 && boatIndex != 0) {
distanceApart++;
distanceApart *= -1;
}
GeoPoint spawnMark = GeoUtility
.getGeoCoordinate(midpoint, perpendicularAngle, distanceApart * DISTANCEFACTOR);
if (yacht.getHeading() < perpendicularAngle) {
spawnMark = GeoUtility
.getGeoCoordinate(spawnMark, perpendicularAngle + 90, DISTANCEFACTOR);
} else {
spawnMark = GeoUtility
.getGeoCoordinate(spawnMark, perpendicularAngle + 270, DISTANCEFACTOR);
}
yacht.setLocation(spawnMark);
boatIndex++;
}
}
@Override
public void update(Observable o, Object arg) {
for (ServerToClientThread serverToClientThread : serverToClientThreads) {
serverToClientThread.sendCollisionMessage((Integer) arg);
}
}
private void setupYachtObserver() {
for (ServerToClientThread serverToClientThread : serverToClientThreads) {
serverToClientThread.getYacht().addObserver(this);
}
}
}