mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 06:18:44 +00:00
Merge branch 'develop' into 1124_broadcast_mark_rounding_message
# Conflicts: # src/main/java/seng302/gameServer/ServerToClientThread.java
This commit is contained in:
@@ -69,8 +69,8 @@
|
||||
<artifactId>commons-cli</artifactId>
|
||||
<version>1.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
@@ -67,8 +67,6 @@ public class App extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage primaryStage) throws Exception {
|
||||
PolarTable.parsePolarFile(getClass().getResourceAsStream("/config/acc_polars.csv"));
|
||||
|
||||
Parent root = FXMLLoader.load(getClass().getResource("/views/StartScreenView.fxml"));
|
||||
primaryStage.setTitle("RaceVision");
|
||||
Scene scene = new Scene(root, 1530, 960);
|
||||
|
||||
@@ -4,9 +4,9 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.model.Player;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.model.mark.MarkOrder;
|
||||
@@ -56,7 +56,6 @@ public class GameState implements Runnable {
|
||||
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<>();
|
||||
@@ -75,7 +74,8 @@ public class GameState implements Runnable {
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -132,7 +132,7 @@ public class GameState implements Runnable {
|
||||
return yachts;
|
||||
}
|
||||
|
||||
public static void updateBoat(Integer sourceId, BoatActionType actionType) {
|
||||
public static void updateBoat(Integer sourceId, BoatAction actionType) {
|
||||
Yacht playerYacht = yachts.get(sourceId);
|
||||
// System.out.println("-----------------------");
|
||||
switch (actionType) {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.Observable;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import seng302.model.Player;
|
||||
import seng302.model.PolarTable;
|
||||
|
||||
/**
|
||||
* A class describing the overall server, which creates and collects server threads for each client
|
||||
@@ -31,7 +32,7 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
|
||||
} 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"));
|
||||
terminated = false;
|
||||
thread = new Thread(this);
|
||||
thread.start();
|
||||
@@ -43,6 +44,7 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
|
||||
HeartbeatThread heartbeatThread;
|
||||
|
||||
serverListenThread = new ServerListenThread(serverSocket, this);
|
||||
|
||||
heartbeatThread = new HeartbeatThread(this);
|
||||
|
||||
heartbeatThread.start();
|
||||
@@ -101,9 +103,11 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
|
||||
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();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,11 +124,15 @@ 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();
|
||||
}
|
||||
}
|
||||
serverToClientThreads.remove(closedConnection);
|
||||
setChanged();
|
||||
notifyObservers();
|
||||
}
|
||||
|
||||
@@ -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){
|
||||
|
||||
@@ -12,8 +12,6 @@ import java.net.SocketException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.CRC32;
|
||||
@@ -40,6 +38,26 @@ 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.BoatStatus;
|
||||
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.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;
|
||||
|
||||
/**
|
||||
* A class describing a single connection to a Client for the purposes of sending and receiving on
|
||||
@@ -48,6 +66,14 @@ import seng302.utilities.XMLGenerator;
|
||||
*/
|
||||
public class ServerToClientThread implements Runnable, Observer {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
@@ -66,6 +92,8 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
|
||||
private XMLGenerator xml;
|
||||
|
||||
private List<ConnectionListener> connectionListeners = new ArrayList<>();
|
||||
|
||||
public ServerToClientThread(Socket socket) {
|
||||
this.socket = socket;
|
||||
seqNo = 0;
|
||||
@@ -81,7 +109,7 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
thread.start();
|
||||
}
|
||||
|
||||
private void setUpYacht(){
|
||||
private void setUpPlayer(){
|
||||
BufferedReader fn;
|
||||
String fName = "";
|
||||
BufferedReader ln;
|
||||
@@ -144,12 +172,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() {
|
||||
@@ -180,7 +210,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);
|
||||
@@ -206,7 +236,7 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
}
|
||||
}
|
||||
|
||||
private void sendSetupMessages() {
|
||||
public void sendSetupMessages() {
|
||||
xml = new XMLGenerator();
|
||||
Race race = new Race();
|
||||
|
||||
@@ -240,7 +270,6 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private int readByte() throws Exception {
|
||||
int currentByte = -1;
|
||||
try {
|
||||
@@ -291,7 +320,6 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
public 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());
|
||||
BoatLocationMessage boatLocationMessage =
|
||||
new BoatLocationMessage(
|
||||
yacht.getSourceId(),
|
||||
@@ -312,7 +340,6 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
public void sendRaceStatusMessage() {
|
||||
// variables taken from GameServerThread
|
||||
|
||||
|
||||
List<BoatSubMessage> boatSubMessages = new ArrayList<>();
|
||||
BoatStatus boatStatus;
|
||||
RaceStatus raceStatus;
|
||||
@@ -347,4 +374,12 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
public Socket getSocket() {
|
||||
return socket;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
@@ -71,8 +71,6 @@ public final class PolarTable {
|
||||
} catch (IOException e) {
|
||||
System.out.println("[PolarTable] IO exception");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -155,7 +153,6 @@ public final class PolarTable {
|
||||
public static Double getClosestWindSpeedInPolar(Double thisWindSpeed) {
|
||||
Double smallestDif = Double.POSITIVE_INFINITY;
|
||||
Double closestWind = 0d;
|
||||
|
||||
for (Double polarWindSpeed : polarTable.keySet()) {
|
||||
Double difference = Math.abs(polarWindSpeed - thisWindSpeed);
|
||||
if (difference < smallestDif) {
|
||||
|
||||
@@ -59,7 +59,7 @@ public class Yacht extends Observable {
|
||||
private Integer legNumber = 0;
|
||||
|
||||
//SERVER SIDE
|
||||
private final Double TURN_STEP = 5.0;
|
||||
public static final Double TURN_STEP = 5.0; //This should be in some utils class somewhere 2bh. Public for tests sake.
|
||||
private Double lastHeading;
|
||||
private Boolean sailIn;
|
||||
private GeoPoint location;
|
||||
@@ -498,9 +498,9 @@ public class Yacht extends Observable {
|
||||
private void turnTowardsHeading(Double newHeading) {
|
||||
Double newVal = heading - newHeading;
|
||||
if (Math.floorMod(newVal.longValue(), 360L) > 180) {
|
||||
adjustHeading(TURN_STEP);
|
||||
adjustHeading(TURN_STEP / 5);
|
||||
} else {
|
||||
adjustHeading(-TURN_STEP);
|
||||
adjustHeading(-TURN_STEP / 5);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.zip.CRC32;
|
||||
import java.util.zip.Checksum;
|
||||
@@ -19,7 +21,12 @@ import javafx.scene.control.Alert.AlertType;
|
||||
import javafx.scene.control.ButtonType;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import seng302.gameServer.server.messages.*;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
import seng302.gameServer.server.messages.BoatActionMessage;
|
||||
import seng302.gameServer.server.messages.ClientType;
|
||||
import seng302.gameServer.server.messages.Message;
|
||||
import seng302.gameServer.server.messages.RegistrationRequestMessage;
|
||||
import seng302.gameServer.server.messages.RegistrationResponseStatus;
|
||||
import seng302.model.stream.packets.PacketType;
|
||||
import seng302.model.stream.packets.StreamPacket;
|
||||
|
||||
@@ -51,9 +58,16 @@ public class ClientToServerThread implements Runnable {
|
||||
|
||||
private Socket socket;
|
||||
private InputStream is;
|
||||
private OutputStream os;
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(ClientToServerThread.class);
|
||||
|
||||
//Output stream
|
||||
private OutputStream os;
|
||||
private Timer upWindPacketTimer = new Timer();
|
||||
private Timer downWindPacketTimer = new Timer();
|
||||
private boolean upwindTimerFlag = false, downwindTimerFlag = false;
|
||||
static public final int PACKET_SENDING_INTERVAL_MS = 100;
|
||||
|
||||
private int clientId = -1;
|
||||
|
||||
// private Boolean updateClient = true;
|
||||
@@ -141,6 +155,7 @@ public class ClientToServerThread implements Runnable {
|
||||
}
|
||||
}
|
||||
} catch (ByteReadException e) {
|
||||
e.printStackTrace();
|
||||
closeSocket();
|
||||
Platform.runLater(() -> {
|
||||
Alert alert = new Alert(AlertType.ERROR);
|
||||
@@ -151,7 +166,6 @@ public class ClientToServerThread implements Runnable {
|
||||
clientLog(e.getMessage(), 1);
|
||||
return;
|
||||
}
|
||||
// System.out.println("streamPackets = " + streamPackets.size());
|
||||
}
|
||||
closeSocket();
|
||||
clientLog("Closed connection to Server", 0);
|
||||
@@ -205,21 +219,81 @@ public class ClientToServerThread implements Runnable {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send the post-start race course information
|
||||
* @param boatActionMessage The message to send
|
||||
* Sends packets for the given boat action. Special cases are: \n
|
||||
* - DOWNWIND = Packets are sent every ClientToServerThread.PACKET_SENDING_INTERVAL_MS
|
||||
* - UPWIND = Packets are sent every ClientToServerThread.PACKET_SENDING_INTERVAL_MS
|
||||
* - MAINTAIN_HEADING = DOWNWIND and UPWIND packets stop being sent.
|
||||
* @param actionType The boat action that will dictate packets sent.
|
||||
*/
|
||||
public void sendBoatActionMessage(BoatActionMessage boatActionMessage) {
|
||||
if (clientId == -1) return;
|
||||
|
||||
try {
|
||||
os.write(boatActionMessage.getBuffer());
|
||||
} catch (IOException e) {
|
||||
clientLog("Could not write to server", 1);
|
||||
public void sendBoatAction(BoatAction actionType) {
|
||||
switch (actionType) {
|
||||
case MAINTAIN_HEADING:
|
||||
if (upwindTimerFlag) {
|
||||
cancelTimer(upWindPacketTimer);
|
||||
upwindTimerFlag = false;
|
||||
upWindPacketTimer = new Timer();
|
||||
}
|
||||
if (downwindTimerFlag) {
|
||||
cancelTimer(downWindPacketTimer);
|
||||
downwindTimerFlag = false;
|
||||
downWindPacketTimer = new Timer();
|
||||
}
|
||||
break;
|
||||
case DOWNWIND:
|
||||
if (!downwindTimerFlag) {
|
||||
downwindTimerFlag = true;
|
||||
downWindPacketTimer.scheduleAtFixedRate(
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
sendBoatAction(new BoatActionMessage(BoatAction.DOWNWIND));
|
||||
}
|
||||
}, 0, PACKET_SENDING_INTERVAL_MS
|
||||
);
|
||||
}
|
||||
break;
|
||||
case UPWIND:
|
||||
if (!upwindTimerFlag) {
|
||||
upwindTimerFlag = true;
|
||||
upWindPacketTimer.scheduleAtFixedRate(
|
||||
new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
sendBoatAction(new BoatActionMessage(BoatAction.UPWIND));
|
||||
}
|
||||
}, 0, PACKET_SENDING_INTERVAL_MS
|
||||
);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sendBoatAction(new BoatActionMessage(actionType));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels a packet sending timer.
|
||||
* @param timer The timer to cancel.
|
||||
*/
|
||||
private void cancelTimer (Timer timer) {
|
||||
timer.cancel();
|
||||
timer.purge();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a boat action of the given message type.
|
||||
* @param message The given message type.
|
||||
*/
|
||||
private void sendBoatAction(BoatActionMessage message) {
|
||||
if (clientId != -1) {
|
||||
try {
|
||||
os.write(message.getBuffer());
|
||||
} catch (IOException e) {
|
||||
clientLog("Could not write to server", 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void closeSocket() {
|
||||
try {
|
||||
@@ -273,11 +347,8 @@ public class ClientToServerThread implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
public Thread getThread() {
|
||||
return thread;
|
||||
}
|
||||
|
||||
public int getClientId () {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,8 +13,7 @@ import javafx.scene.Node;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
import javafx.scene.layout.Pane;
|
||||
import seng302.gameServer.MainServerThread;
|
||||
import seng302.gameServer.server.messages.BoatActionMessage;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
import seng302.model.RaceState;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.model.stream.packets.StreamPacket;
|
||||
@@ -31,7 +30,8 @@ import seng302.visualiser.controllers.LobbyController.CloseStatus;
|
||||
import seng302.visualiser.controllers.RaceViewController;
|
||||
|
||||
/**
|
||||
* Created by cir27 on 20/07/17.
|
||||
* This class is a client side instance of a yacht racing game in JavaFX. The game is instantiated
|
||||
* with a JavaFX Pane to insert itself into.
|
||||
*/
|
||||
public class GameClient {
|
||||
|
||||
@@ -48,13 +48,20 @@ public class GameClient {
|
||||
|
||||
private ObservableList<String> clientLobbyList = FXCollections.observableArrayList();
|
||||
|
||||
private long lastSendingTime;
|
||||
private int KEY_STROKE_SENDING_FREQUENCY = 50;
|
||||
|
||||
/**
|
||||
* Create an instance of the game client. Does not do anything until run with runAsClient()
|
||||
* runAsHost().
|
||||
* @param holder The JavaFX Pane that the visual elements for the race will be inserted into.
|
||||
*/
|
||||
public GameClient(Pane holder) {
|
||||
this.holderPane = holder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to a game at the given address and starts the visualiser.
|
||||
* @param ipAddress IP to connect to.
|
||||
* @param portNumber Port to connect to.
|
||||
*/
|
||||
public void runAsClient(String ipAddress, Integer portNumber) {
|
||||
try {
|
||||
socketThread = new ClientToServerThread(ipAddress, portNumber);
|
||||
@@ -71,6 +78,11 @@ public class GameClient {
|
||||
lobbyController.addCloseListener((exitCause) -> this.loadStartScreen());
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to a game as the host at the given address and starts the visualiser.
|
||||
* @param ipAddress IP to connect to.
|
||||
* @param portNumber Port to connect to.
|
||||
*/
|
||||
public void runAsHost(String ipAddress, Integer portNumber) {
|
||||
server = new MainServerThread();
|
||||
try {
|
||||
@@ -94,10 +106,8 @@ public class GameClient {
|
||||
|
||||
private void loadStartScreen() {
|
||||
socketThread.setSocketToClose();
|
||||
socketThread = null;
|
||||
if (server != null) {
|
||||
// TODO: 26/07/17 cir27 - handle disconnecting
|
||||
// server.shutDown();
|
||||
server.terminate();
|
||||
server = null;
|
||||
}
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(
|
||||
@@ -179,12 +189,9 @@ public class GameClient {
|
||||
StreamParser.extractXmlMessage(packet)
|
||||
);
|
||||
clientLobbyList.clear();
|
||||
allBoatsMap.forEach((id, boat) -> {
|
||||
clientLobbyList.add(id + " " + boat.getBoatName());
|
||||
// System.out.println(id + " " + boat.getBoatName());
|
||||
|
||||
});
|
||||
// startRaceIfAllDataReceived();
|
||||
allBoatsMap.forEach((id, boat) ->
|
||||
clientLobbyList.add(id + " " + boat.getBoatName())
|
||||
);
|
||||
break;
|
||||
|
||||
case RACE_START_STATUS:
|
||||
@@ -279,47 +286,34 @@ public class GameClient {
|
||||
* Handle the key-pressed event from the text field.
|
||||
* @param e The key event triggering this call
|
||||
*/
|
||||
public void keyPressed(KeyEvent e) {
|
||||
BoatActionMessage boatActionMessage;
|
||||
long currentTime = System.currentTimeMillis();
|
||||
if (currentTime - lastSendingTime > KEY_STROKE_SENDING_FREQUENCY) {
|
||||
lastSendingTime = currentTime;
|
||||
switch (e.getCode()) {
|
||||
case SPACE: // align with vmg
|
||||
boatActionMessage = new BoatActionMessage(BoatActionType.VMG);
|
||||
socketThread.sendBoatActionMessage(boatActionMessage);
|
||||
break;
|
||||
case PAGE_UP: // upwind
|
||||
boatActionMessage = new BoatActionMessage(BoatActionType.UPWIND);
|
||||
socketThread.sendBoatActionMessage(boatActionMessage);
|
||||
break;
|
||||
case PAGE_DOWN: // downwind
|
||||
boatActionMessage = new BoatActionMessage(BoatActionType.DOWNWIND);
|
||||
socketThread.sendBoatActionMessage(boatActionMessage);
|
||||
break;
|
||||
case ENTER: // tack/gybe
|
||||
boatActionMessage = new BoatActionMessage(BoatActionType.TACK_GYBE);
|
||||
socketThread.sendBoatActionMessage(boatActionMessage);
|
||||
break;
|
||||
//TODO Allow a zoom in and zoom out methods
|
||||
case Z: // zoom in
|
||||
System.out.println("Zoom in");
|
||||
break;
|
||||
case X: // zoom out
|
||||
System.out.println("Zoom out");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
private void keyPressed(KeyEvent e) {
|
||||
switch (e.getCode()) {
|
||||
//TODO 12/07/17 Determine the sail state and send the appropriate packet (eg. if sails are in, send a sail out packet)
|
||||
case SHIFT: // sails in/sails out
|
||||
BoatActionMessage boatActionMessage = new BoatActionMessage(
|
||||
BoatActionType.SAILS_IN);
|
||||
socketThread.sendBoatActionMessage(boatActionMessage);
|
||||
case SPACE: // align with vmg
|
||||
socketThread.sendBoatAction(BoatAction.VMG); break;
|
||||
case PAGE_UP: // upwind
|
||||
socketThread.sendBoatAction(BoatAction.UPWIND); break;
|
||||
case PAGE_DOWN: // downwind
|
||||
socketThread.sendBoatAction(BoatAction.DOWNWIND); break;
|
||||
case ENTER: // tack/gybe
|
||||
socketThread.sendBoatAction(BoatAction.TACK_GYBE); break;
|
||||
//TODO Allow a zoom in and zoom out methods
|
||||
case Z: // zoom in
|
||||
System.out.println("Zoom in");
|
||||
break;
|
||||
case X: // zoom out
|
||||
System.out.println("Zoom out");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void keyReleased(KeyEvent e) {
|
||||
switch (e.getCode()) {
|
||||
//TODO 12/07/17 Determine the sail state and send the appropriate packet (eg. if sails are in, send a sail out packet)
|
||||
case SHIFT: // sails in/sails out
|
||||
socketThread.sendBoatAction(BoatAction.SAILS_IN); break;
|
||||
case PAGE_UP:
|
||||
case PAGE_DOWN:
|
||||
socketThread.sendBoatAction(BoatAction.MAINTAIN_HEADING); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ public class GameView extends Pane {
|
||||
}
|
||||
}
|
||||
// Platform.runLater(() ->
|
||||
// boatObjects.forEach((boat, boatObject) -> boatObject.updateLocation())
|
||||
boatObjects.forEach((boat, boatObject) -> boatObject.updateLocation());
|
||||
// );
|
||||
}
|
||||
};
|
||||
@@ -502,7 +502,6 @@ public class GameView extends Pane {
|
||||
distanceFromReference = GeoUtility.getDistance(
|
||||
minLatPoint, new GeoPoint(unscaledLat, unscaledLon)
|
||||
);
|
||||
// System.out.println("distanceFromReference = " + distanceFromReference);
|
||||
if (angleFromReference >= 0 && angleFromReference <= Math.PI / 2) {
|
||||
xAxisLocation += Math.round(distanceScaleFactor * Math.sin(angleFromReference) * distanceFromReference);
|
||||
yAxisLocation -= Math.round(distanceScaleFactor * Math.cos(angleFromReference) * distanceFromReference);
|
||||
@@ -522,8 +521,6 @@ public class GameView extends Pane {
|
||||
if(horizontalInversion) {
|
||||
xAxisLocation = canvasWidth - bufferSize - (xAxisLocation - bufferSize);
|
||||
}
|
||||
// System.out.println("yAxisLocation = " + yAxisLocation + " " + unscaledLat);
|
||||
// System.out.println("xAxisLocation = " + xAxisLocation + " " + unscaledLon);
|
||||
return new Point2D(xAxisLocation, yAxisLocation);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,15 +4,13 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import javafx.application.Platform;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ListChangeListener;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.scene.control.TextArea;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.text.Text;
|
||||
import seng302.gameServer.GameStages;
|
||||
import seng302.gameServer.GameState;
|
||||
@@ -33,28 +31,26 @@ public class LobbyController {
|
||||
void notify(CloseStatus exitCause);
|
||||
}
|
||||
|
||||
@FXML
|
||||
private GridPane lobbyScreen;
|
||||
@FXML
|
||||
private Text lobbyIpText;
|
||||
@FXML
|
||||
private Button readyButton;
|
||||
@FXML
|
||||
private ListView<String> firstListView;
|
||||
private TextArea playerOneTxt;
|
||||
@FXML
|
||||
private ListView secondListView;
|
||||
private TextArea playerTwoTxt;
|
||||
@FXML
|
||||
private ListView thirdListView;
|
||||
private TextArea playerThreeTxt;
|
||||
@FXML
|
||||
private ListView fourthListView;
|
||||
private TextArea playerFourTxt;
|
||||
@FXML
|
||||
private ListView fifthListView;
|
||||
private TextArea playerFiveTxt;
|
||||
@FXML
|
||||
private ListView sixthListView;
|
||||
private TextArea playerSixTxt;
|
||||
@FXML
|
||||
private ListView seventhListView;
|
||||
private TextArea playerSevenTxt;
|
||||
@FXML
|
||||
private ListView eighthListView;
|
||||
private TextArea playerEightTxt;
|
||||
@FXML
|
||||
private ImageView firstImageView;
|
||||
@FXML
|
||||
@@ -72,79 +68,67 @@ public class LobbyController {
|
||||
@FXML
|
||||
private ImageView eighthImageView;
|
||||
|
||||
private List<ObservableList<String>> competitors = new ArrayList<>();
|
||||
private ObservableList<String> firstCompetitor = FXCollections.observableArrayList();
|
||||
private ObservableList<String> secondCompetitor = FXCollections.observableArrayList();
|
||||
private ObservableList<String> thirdCompetitor = FXCollections.observableArrayList();
|
||||
private ObservableList<String> fourthCompetitor = FXCollections.observableArrayList();
|
||||
private ObservableList<String> fifthCompetitor = FXCollections.observableArrayList();
|
||||
private ObservableList<String> sixthCompetitor = FXCollections.observableArrayList();
|
||||
private ObservableList<String> seventhCompetitor = FXCollections.observableArrayList();
|
||||
private ObservableList<String> eighthCompetitor = FXCollections.observableArrayList();
|
||||
|
||||
private List<ImageView> imageViews = new ArrayList<>();
|
||||
private List<ListView> listViews;
|
||||
private List<TextArea> listViews = new ArrayList<>();
|
||||
|
||||
private int MAX_NUM_PLAYERS = 8;
|
||||
|
||||
private List<LobbyCloseListener> lobbyListeners = new ArrayList<>();
|
||||
private ObservableList<String> players = FXCollections.observableArrayList();
|
||||
private ObservableList<String> players;
|
||||
|
||||
/**
|
||||
* Add all FXObjects to lists and initialize images.
|
||||
*/
|
||||
public void initialize() {
|
||||
imageViews = new ArrayList<>();
|
||||
Collections
|
||||
.addAll(imageViews, firstImageView, secondImageView, thirdImageView, fourthImageView,
|
||||
fifthImageView, sixthImageView, seventhImageView, eighthImageView);
|
||||
listViews = new ArrayList<>();
|
||||
Collections.addAll(listViews, firstListView, secondListView, thirdListView, fourthListView, fifthListView,
|
||||
sixthListView, seventhListView, eighthListView);
|
||||
competitors = new ArrayList<>();
|
||||
Collections.addAll(competitors, firstCompetitor, secondCompetitor, thirdCompetitor,
|
||||
fourthCompetitor, fifthCompetitor, sixthCompetitor, seventhCompetitor, eighthCompetitor);
|
||||
|
||||
Collections.addAll(listViews,
|
||||
playerOneTxt, playerTwoTxt, playerThreeTxt, playerFourTxt, playerFiveTxt, playerSixTxt,
|
||||
playerSevenTxt, playerEightTxt
|
||||
);
|
||||
Collections.addAll(imageViews,
|
||||
firstImageView, secondImageView, thirdImageView, fourthImageView,
|
||||
fifthImageView, sixthImageView, seventhImageView, eighthImageView
|
||||
);
|
||||
initialiseImageView();
|
||||
}
|
||||
|
||||
private void initialiseListView() {
|
||||
listViews.forEach(listView -> listView.getItems().clear());
|
||||
imageViews.forEach(gif -> gif.setVisible(false));
|
||||
competitors.forEach(ol -> ol.removeAll());
|
||||
/**
|
||||
* Updates player names.
|
||||
*/
|
||||
private void updatePlayers() {
|
||||
//Update players if one added.
|
||||
for (int i = 0; i < players.size(); i++) {
|
||||
competitors.get(i).add(players.get(i));
|
||||
listViews.get(i).setItems(competitors.get(i));
|
||||
listViews.get(i).setText(players.get(i));
|
||||
imageViews.get(i).setVisible(true);
|
||||
}
|
||||
//Update empty text fields if player left.
|
||||
for (int i = MAX_NUM_PLAYERS-1; i >= players.size(); i--) {
|
||||
listViews.get(i).setText("");
|
||||
imageViews.get(i).setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets all images and hides them till players join.
|
||||
*/
|
||||
private void initialiseImageView() {
|
||||
imageViews.add(firstImageView);
|
||||
imageViews.add(secondImageView);
|
||||
imageViews.add(thirdImageView);
|
||||
imageViews.add(fourthImageView);
|
||||
imageViews.add(fifthImageView);
|
||||
imageViews.add(sixthImageView);
|
||||
imageViews.add(seventhImageView);
|
||||
imageViews.add(eighthImageView);
|
||||
for (int i = 0; i < MAX_NUM_PLAYERS; i++) {
|
||||
imageViews.get(i).setImage(
|
||||
for (ImageView viewer : imageViews) {
|
||||
viewer.setImage(
|
||||
new Image(
|
||||
RaceViewController.class.getResourceAsStream(
|
||||
"/pics/sail.png")
|
||||
)
|
||||
);
|
||||
viewer.setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void leaveLobbyButtonPressed() {
|
||||
// TODO: 10/07/17 wmu16 - Finish function!
|
||||
// setContentPane("/views/StartScreenView.fxml");
|
||||
GameState.setCurrentStage(GameStages.CANCELLED);
|
||||
// TODO: 20/07/17 wmu16 - Implement some way of terminating the game
|
||||
// ClientState.setConnectedToHost(false);
|
||||
for (LobbyCloseListener readyListener : lobbyListeners)
|
||||
readyListener.notify(CloseStatus.LEAVE);
|
||||
|
||||
}
|
||||
|
||||
@FXML
|
||||
@@ -154,32 +138,6 @@ public class LobbyController {
|
||||
readyListener.notify(CloseStatus.READY);
|
||||
}
|
||||
|
||||
|
||||
// private static MediaPlayer mediaPlayer;
|
||||
//
|
||||
// private void playTheme() {
|
||||
// Random random = new Random(System.currentTimeMillis());
|
||||
// Integer rand = random.nextInt();
|
||||
// if(rand == 10) {
|
||||
// URL file = getClass().getResource("/music/Disturbed - down with the sickness.mp3");
|
||||
// Media hit = new Media(file.toString());
|
||||
// mediaPlayer = new MediaPlayer(hit);
|
||||
// mediaPlayer.play();
|
||||
// } else if(rand == 9) {
|
||||
// URL file = getClass().getResource("/music/Owl City - Fireflies.mp3");
|
||||
// Media hit = new Media(file.toString());
|
||||
// mediaPlayer = new MediaPlayer(hit);
|
||||
// mediaPlayer.play();
|
||||
// }
|
||||
// }
|
||||
|
||||
// private void switchToRaceView() {
|
||||
// if (!switchedPane) {
|
||||
// switchedPane = true;
|
||||
// setContentPane("/views/RaceView.fxml");
|
||||
// }
|
||||
// }
|
||||
// TODO: 26/07/17 cir27 - Could probably be done in a cleaner way.
|
||||
public void setTitle (String title) {
|
||||
lobbyIpText.setText(title);
|
||||
}
|
||||
@@ -191,12 +149,13 @@ public class LobbyController {
|
||||
public void setPlayerListSource (ObservableList<String> players) {
|
||||
this.players = players;
|
||||
players.addListener((ListChangeListener<? super String>) (lcl) ->
|
||||
Platform.runLater(this::initialiseListView)
|
||||
Platform.runLater(this::updatePlayers)
|
||||
);
|
||||
Platform.runLater(this::initialiseListView);
|
||||
Platform.runLater(this::updatePlayers);
|
||||
}
|
||||
|
||||
public void disableReadyButton () {
|
||||
readyButton.setDisable(true);
|
||||
readyButton.setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,15 +242,15 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
|
||||
}
|
||||
});
|
||||
// Adds the new data series to the sparkline (and set the colour of the series)
|
||||
Platform.runLater(() -> {
|
||||
Platform.runLater(() ->
|
||||
sparkLineData
|
||||
.stream()
|
||||
.filter(spark -> !raceSparkLine.getData().contains(spark))
|
||||
.forEach(spark -> {
|
||||
raceSparkLine.getData().add(spark);
|
||||
spark.getNode().lookup(".chart-series-line").setStyle("-fx-stroke:" + getBoatColorAsRGB(spark.getName()));
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private void initialiseSparkLine() {
|
||||
|
||||
@@ -268,7 +268,7 @@ public class BoatObject extends Group {
|
||||
*/
|
||||
public void setAsPlayer() {
|
||||
boatPoly.getPoints().setAll(
|
||||
-BOAT_WIDTH / 1.75, BOAT_HEIGHT / 1.75,
|
||||
-BOAT_WIDTH / 1.75, BOAT_HEIGHT / 1.75,
|
||||
0.0, -BOAT_HEIGHT / 1.75,
|
||||
BOAT_WIDTH / 1.75, BOAT_HEIGHT / 1.75
|
||||
);
|
||||
|
||||
@@ -52,7 +52,7 @@ public class Wake extends Group {
|
||||
arc.setFill(new Color(0.0, 0.0, 0.0, 0.0));
|
||||
arcs[i] = arc;
|
||||
arc.getTransforms().setAll(
|
||||
new Rotate(1)
|
||||
new Rotate(1)
|
||||
);
|
||||
}
|
||||
super.getChildren().addAll(arcs);
|
||||
@@ -60,15 +60,15 @@ public class Wake extends Group {
|
||||
|
||||
void setRotation (double rotation, double velocity) {
|
||||
// if (Math.abs(rotations[0] - rotation) > 20) {
|
||||
Platform.runLater(() -> {
|
||||
rotate(rotation);
|
||||
double rad = (14 / numWakes) + velocity;
|
||||
for (Arc arc : arcs) {
|
||||
arc.setRadiusX(rad);
|
||||
arc.setRadiusY(rad);
|
||||
rad += (14 / numWakes) + (velocity / 2.5);
|
||||
}
|
||||
});
|
||||
Platform.runLater(() -> {
|
||||
rotate(rotation);
|
||||
double rad = (14 / numWakes) + velocity;
|
||||
for (Arc arc : arcs) {
|
||||
arc.setRadiusX(rad);
|
||||
arc.setRadiusY(rad);
|
||||
rad += (14 / numWakes) + (velocity / 2.5);
|
||||
}
|
||||
});
|
||||
// } else {
|
||||
// rotations[0] = rotation;
|
||||
// ((Rotate) arcs[0].getTransforms().get(0)).setAngle(rotation);
|
||||
|
||||
@@ -28,7 +28,7 @@ public class MercatorProjection {
|
||||
* @param geo GeoPoint (lat, lng) location to be projected
|
||||
* @return the projection Point2D (x, y) on planar
|
||||
*/
|
||||
static Point2D toMapPoint(GeoPoint geo) {
|
||||
public static Point2D toMapPoint(GeoPoint geo) {
|
||||
double x, y;
|
||||
Point2D origin = new Point2D(MERCATOR_RANGE / 2.0, MERCATOR_RANGE / 2.0);
|
||||
x = (origin.getX() + geo.getLng() * pixelsPerLngDegree);
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
|
||||
<?import java.lang.*?>
|
||||
<?import javafx.geometry.*?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.image.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
<?import javafx.scene.text.*?>
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.ListView?>
|
||||
@@ -11,6 +16,7 @@
|
||||
<?import javafx.scene.layout.RowConstraints?>
|
||||
<?import javafx.scene.text.Font?>
|
||||
<?import javafx.scene.text.Text?>
|
||||
|
||||
<GridPane fx:id="lobbyScreen" nodeOrientation="LEFT_TO_RIGHT" prefHeight="960.0" prefWidth="1530.0" style="-fx-background-color: #2C2c36;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.visualiser.controllers.LobbyController">
|
||||
<columnConstraints>
|
||||
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
|
||||
@@ -53,41 +59,14 @@
|
||||
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
|
||||
</rowConstraints>
|
||||
<children>
|
||||
<ListView fx:id="firstListView" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1">
|
||||
<GridPane.margin>
|
||||
<Insets bottom="10.0" left="40.0" right="40.0" top="10.0" />
|
||||
</GridPane.margin>
|
||||
</ListView>
|
||||
<ListView fx:id="secondListView" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2">
|
||||
<GridPane.margin>
|
||||
<Insets bottom="10.0" left="40.0" right="40.0" top="10.0" />
|
||||
</GridPane.margin>
|
||||
</ListView>
|
||||
<ListView fx:id="thirdListView" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="3">
|
||||
<GridPane.margin>
|
||||
<Insets bottom="10.0" left="40.0" right="40.0" top="10.0" />
|
||||
</GridPane.margin></ListView>
|
||||
<ListView fx:id="fourthListView" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="4">
|
||||
<GridPane.margin>
|
||||
<Insets bottom="10.0" left="40.0" right="40.0" top="10.0" />
|
||||
</GridPane.margin>
|
||||
</ListView>
|
||||
<ListView fx:id="eighthListView" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="4" GridPane.rowIndex="1">
|
||||
<GridPane.margin>
|
||||
<Insets bottom="10.0" left="40.0" right="40.0" top="10.0" />
|
||||
</GridPane.margin></ListView>
|
||||
<ListView fx:id="fifthListView" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="1">
|
||||
<GridPane.margin>
|
||||
<Insets bottom="10.0" left="40.0" right="40.0" top="10.0" />
|
||||
</GridPane.margin></ListView>
|
||||
<ListView fx:id="sixthListView" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1">
|
||||
<GridPane.margin>
|
||||
<Insets bottom="10.0" left="40.0" right="40.0" top="10.0" />
|
||||
</GridPane.margin></ListView>
|
||||
<ListView fx:id="seventhListView" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="3" GridPane.rowIndex="1">
|
||||
<GridPane.margin>
|
||||
<Insets bottom="10.0" left="40.0" right="40.0" top="10.0" />
|
||||
</GridPane.margin></ListView>
|
||||
<TextArea fx:id="playerFourTxt" editable="false" maxHeight="211.0" maxWidth="175.0" minHeight="211.0" minWidth="175.0" prefHeight="211.0" prefWidth="175.0" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
|
||||
<TextArea fx:id="playerEightTxt" editable="false" maxHeight="211.0" maxWidth="175.0" minHeight="211.0" minWidth="175.0" prefHeight="211.0" prefWidth="175.0" GridPane.columnIndex="4" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
|
||||
<TextArea fx:id="playerSevenTxt" editable="false" maxHeight="211.0" maxWidth="175.0" minHeight="211.0" minWidth="175.0" prefHeight="211.0" prefWidth="175.0" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
|
||||
<TextArea fx:id="playerThreeTxt" editable="false" maxHeight="211.0" maxWidth="175.0" minHeight="211.0" minWidth="175.0" prefHeight="211.0" prefWidth="175.0" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
|
||||
<TextArea fx:id="playerSixTxt" editable="false" maxHeight="211.0" maxWidth="175.0" minHeight="211.0" minWidth="175.0" prefHeight="211.0" prefWidth="175.0" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
|
||||
<TextArea fx:id="playerFiveTxt" editable="false" maxHeight="211.0" maxWidth="175.0" minHeight="211.0" minWidth="175.0" prefHeight="211.0" prefWidth="175.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER" />
|
||||
<TextArea fx:id="playerOneTxt" editable="false" maxHeight="211.0" maxWidth="175.0" minHeight="211.0" minWidth="175.0" prefHeight="211.0" prefWidth="175.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
|
||||
<TextArea fx:id="playerTwoTxt" editable="false" maxHeight="211.0" maxWidth="175.0" minHeight="211.0" minWidth="175.0" prefHeight="211.0" prefWidth="175.0" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
|
||||
<ImageView fx:id="firstImageView" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.valignment="BOTTOM">
|
||||
<GridPane.margin>
|
||||
<Insets bottom="10.0" />
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package seng302;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class TestRaceTimer {
|
||||
@Test
|
||||
public void testPositiveTimeString(){
|
||||
// RaceViewController controller = new RaceViewController();
|
||||
// String result = controller.convertTimeToMinutesSeconds(61);
|
||||
//
|
||||
// assertTrue(result.equals("01:01"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeTimeString(){
|
||||
// RaceViewController controller = new RaceViewController();
|
||||
// String result = controller.convertTimeToMinutesSeconds(-61);
|
||||
//
|
||||
// assertTrue(result.equals("-01:01"));
|
||||
}
|
||||
}
|
||||
@@ -37,12 +37,12 @@ public class YachtTest {
|
||||
for (Double begin : values.keySet()) {
|
||||
y1.setHeading(begin);
|
||||
y1.tackGybe(windDirection);
|
||||
for (int i = 0; i < 50; i++) {
|
||||
|
||||
for (int i = 0; i < 200; i++) {
|
||||
y1.runAutoPilot();
|
||||
}
|
||||
assertEquals(values.get(begin), y1.getHeading(), 5.0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -67,12 +67,10 @@ public class YachtTest {
|
||||
values.put(350d, downwindRight);
|
||||
|
||||
for (Double begin : values.keySet()) {
|
||||
System.out.println(begin);
|
||||
y1.setHeading(begin);
|
||||
y1.turnToVMG();
|
||||
for (int i = 0; i < 50; i++) {
|
||||
for (int i = 0; i < 200; i++) {
|
||||
y1.runAutoPilot();
|
||||
System.out.println(y1.getHeading());
|
||||
}
|
||||
y1.disableAutoPilot();
|
||||
assertEquals(values.get(begin), y1.getHeading(), 5.0);
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package seng302.visualiser.ClientToServerTests;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import seng302.gameServer.GameStages;
|
||||
import seng302.gameServer.GameState;
|
||||
import seng302.gameServer.MainServerThread;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.visualiser.ClientToServerThread;
|
||||
|
||||
/**
|
||||
* Test for checking how regularly packets are sent from ClientToServer Thread.
|
||||
*/
|
||||
public class RegularPacketsTest {
|
||||
|
||||
private MainServerThread serverThread;
|
||||
private ClientToServerThread clientThread;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
new GameState("localhost");
|
||||
serverThread = new MainServerThread();
|
||||
clientThread = new ClientToServerThread("localhost", 4942);
|
||||
GameState.setCurrentStage(GameStages.RACING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void packetsSentAtRegularIntervals () throws Exception {
|
||||
final double TEST_DISTANCE = 10.0;
|
||||
serverThread.startGame();
|
||||
SleepThreadMaxDelay();
|
||||
Yacht yacht = new ArrayList<>(GameState.getYachts().values()).get(0);
|
||||
double startAngle = yacht.getHeading();
|
||||
long startTime = System.currentTimeMillis();
|
||||
clientThread.sendBoatAction(BoatAction.UPWIND);
|
||||
Thread.sleep(200);
|
||||
while (Math.abs(yacht.getHeading() - startAngle) < TEST_DISTANCE) {
|
||||
Thread.sleep(1);
|
||||
}
|
||||
clientThread.sendBoatAction(BoatAction.MAINTAIN_HEADING);
|
||||
long endTime = System.currentTimeMillis();
|
||||
SleepThreadMaxDelay();
|
||||
//Allowed to be two loops of delay due to loop delay and processing delay at client + server ends.
|
||||
Assert.assertEquals(TEST_DISTANCE / Yacht.TURN_STEP * ClientToServerThread.PACKET_SENDING_INTERVAL_MS,
|
||||
(endTime - startTime), 2 * ClientToServerThread.PACKET_SENDING_INTERVAL_MS);
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void testArbitraryPacketSent() throws Exception {
|
||||
// serverThread.startGame();
|
||||
// SleepThreadMaxDelay();
|
||||
// Yacht yacht = new ArrayList<>(GameState.getYachts().values()).get(0);
|
||||
// boolean startState = yacht.getSailIn();
|
||||
// clientThread.sendBoatAction(BoatAction.SAILS_IN);
|
||||
// SleepThreadMaxDelay();
|
||||
// Assert.assertEquals(startState, !yacht.getSailIn());
|
||||
// }
|
||||
|
||||
/**
|
||||
* Give time for processing and packet sending. 200ms listed as absolute maximum for an
|
||||
* acceptable delay.
|
||||
* @throws Exception Thrown if thread crashes or something
|
||||
*/
|
||||
private void SleepThreadMaxDelay() throws Exception {
|
||||
Thread.sleep(200);
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown () throws Exception {
|
||||
clientThread.setSocketToClose();
|
||||
serverThread.terminate();
|
||||
GameState.setCurrentStage(GameStages.LOBBYING);
|
||||
for (int i = 0; i<20; i++)
|
||||
SleepThreadMaxDelay(); //Make sure socket is closed and toolkit remade.
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package seng302.visualizer.annotations;
|
||||
package seng302.visualiser.annotations;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
Reference in New Issue
Block a user