mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Merge remote-tracking branch 'origin/develop' into Story66_Collision
# 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/model/mark/MarkOrder.java # src/main/java/seng302/visualiser/GameView.java # src/main/java/seng302/visualiser/controllers/RaceViewController.java
This commit is contained in:
@@ -23,5 +23,5 @@ Haoming Yin <hyi25@uclive.ac.nz> <haoming.y@icloud.com>
|
||||
Peter Galloway <ptg19@uclive.ac.nz> Peter <ptg19@uclive.ac.nz>
|
||||
Zhi You Tan <zyt10@uclive.ac.nz> zyt10 <zyt10@uclive.ac.nz>
|
||||
Zhi You Tan <zyt10@uclive.ac.nz> Ryan Tan <ryan_zhiyou@hotmail.com>
|
||||
Alistair McIntyre <ajm412@uclive.ac.nz> alistairjmcintyre <alistairjmcintyre@gmail.com>
|
||||
Alistair McIntyre <ajm412@uclive.ac.nz> <alistairjmcintyre@gmail.com>
|
||||
Calum <cir27@uclive.ac.nz> cir27 <cir27@uclive.ac.nz>
|
||||
@@ -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);
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
package seng302.gameServer;
|
||||
|
||||
|
||||
import java.util.*;
|
||||
import seng302.gameServer.server.messages.BoatAction;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import java.util.*;
|
||||
|
||||
import seng302.model.Player;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.model.mark.MarkOrder;
|
||||
import javafx.application.Platform;
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import seng302.model.GeoPoint;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.model.Player;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.model.mark.MarkOrder;
|
||||
import seng302.model.mark.CompoundMark;
|
||||
import seng302.model.mark.Mark;
|
||||
import seng302.utilities.GeoUtility;
|
||||
@@ -25,6 +25,7 @@ public class GameState implements Runnable {
|
||||
private Logger logger = LoggerFactory.getLogger(MarkOrder.class);
|
||||
|
||||
private static Integer STATE_UPDATES_PER_SECOND = 60;
|
||||
public static Integer MAX_PLAYERS = 8;
|
||||
|
||||
private static Long previousUpdateTime;
|
||||
public static Double windDirection;
|
||||
@@ -61,10 +62,8 @@ 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<>();
|
||||
markOrder = new MarkOrder(); //This could be instantiated at some point with a select map?
|
||||
|
||||
new Thread(this).start(); //Run the auto updates on the game state
|
||||
@@ -86,7 +85,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);
|
||||
}
|
||||
|
||||
@@ -143,7 +143,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() {
|
||||
|
||||
@@ -15,6 +15,7 @@ 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
|
||||
@@ -36,12 +37,13 @@ public class MainServerThread extends Observable implements Runnable, ClientConn
|
||||
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"));
|
||||
terminated = false;
|
||||
thread = new Thread(this);
|
||||
thread.start();
|
||||
@@ -113,9 +115,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("send setup message");
|
||||
serverToClientThread.addConnectionListener(() -> {
|
||||
for (ServerToClientThread thread : serverToClientThreads) {
|
||||
thread.sendSetupMessages();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -133,11 +137,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("send setup message");
|
||||
}
|
||||
|
||||
@@ -1,37 +1,26 @@
|
||||
package seng302.gameServer;
|
||||
|
||||
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 = bytesToLong(Arrays.copyOfRange(payload, 0, 1));
|
||||
return BoatActionType.getType((int) actionTypeValue);
|
||||
long actionTypeValue = Message.bytesToLong(Arrays.copyOfRange(payload, 0, 1));
|
||||
return BoatAction.getType((int) actionTypeValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* takes an array of up to 7 bytes and returns a positive
|
||||
* long constructed from the input bytes
|
||||
*
|
||||
* @return a positive long if there is less than 7 bytes -1 otherwise
|
||||
*/
|
||||
private static long bytesToLong(byte[] bytes) {
|
||||
long partialLong = 0;
|
||||
int index = 0;
|
||||
for (byte b : bytes) {
|
||||
if (index > 6) {
|
||||
return -1;
|
||||
}
|
||||
partialLong = partialLong | (b & 0xFFL) << (index * 8);
|
||||
index++;
|
||||
}
|
||||
return partialLong;
|
||||
public static ClientType extractClientType(StreamPacket packet){
|
||||
byte[] payload = packet.getPayload();
|
||||
long value = Message.bytesToLong(Arrays.copyOfRange(payload, 0, 1));
|
||||
return ClientType.getClientType((int) value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
@@ -26,23 +24,41 @@ 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.BoatActionType;
|
||||
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
|
||||
* its own thread. All server threads created and owned by the server thread handler which can
|
||||
* trigger client updates on its threads Created by wmu16 on 13/07/17.
|
||||
*/
|
||||
public class ServerToClientThread implements Runnable, Observer {
|
||||
public class ServerToClientThread implements Runnable {
|
||||
|
||||
/**
|
||||
* Called to notify listeners when this thread receives a connection correctly.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
interface ConnectionListener {
|
||||
void notifyConnection ();
|
||||
}
|
||||
|
||||
private static final Integer LOG_LEVEL = 1;
|
||||
private static final Integer MAX_ID_ATTEMPTS = 10;
|
||||
@@ -55,27 +71,41 @@ 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;
|
||||
|
||||
private ClientType clientType;
|
||||
private Boolean isRegistered = false;
|
||||
|
||||
private XMLGenerator xml;
|
||||
|
||||
private List<ConnectionListener> connectionListeners = new ArrayList<>();
|
||||
|
||||
private Yacht yacht;
|
||||
|
||||
public ServerToClientThread(Socket socket) {
|
||||
this.socket = socket;
|
||||
seqNo = 0;
|
||||
|
||||
try{
|
||||
is = socket.getInputStream();
|
||||
os = socket.getOutputStream();
|
||||
} catch (IOException e) {
|
||||
return;
|
||||
}
|
||||
|
||||
thread = new Thread(this);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
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(
|
||||
@@ -102,20 +132,16 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
if (threeWayHandshake(sourceId)) {
|
||||
serverLog("Successful handshake. Client allocated id: " + sourceId, 0);
|
||||
yacht = new Yacht(
|
||||
);
|
||||
all = ln.lines().collect(Collectors.toList());
|
||||
lName = all.get(ThreadLocalRandom.current().nextInt(0, all.size()));
|
||||
|
||||
|
||||
Yacht yacht = new Yacht(
|
||||
"Yacht", sourceId, sourceId.toString(), fName, fName + " " + lName, "NZ"
|
||||
);
|
||||
// Yacht yacht = new Yacht("Kappa", "Kap", new GeoPoint(57.6708220, 11.8321340), 90.0);
|
||||
GameState.addYacht(sourceId, yacht);
|
||||
GameState.addPlayer(new Player(socket, yacht));
|
||||
} else {
|
||||
serverLog("Unsuccessful handshake. Connection rejected", 1);
|
||||
closeSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
seqNo = 0;
|
||||
thread = new Thread(this);
|
||||
thread.start();
|
||||
}
|
||||
|
||||
static void serverLog(String message, int logLevel) {
|
||||
@@ -132,6 +158,35 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
}
|
||||
}
|
||||
|
||||
private void completeRegistration(ClientType clientType) throws IOException {
|
||||
// Fail if not a player
|
||||
if (!clientType.equals(ClientType.PLAYER)){
|
||||
RegistrationResponseMessage responseMessage = new RegistrationResponseMessage(0, RegistrationResponseStatus.FAILURE_GENERAL);
|
||||
os.write(responseMessage.getBuffer());
|
||||
return;
|
||||
}
|
||||
|
||||
if (GameState.getPlayers().size() >= GameState.MAX_PLAYERS){
|
||||
RegistrationResponseMessage responseMessage = new RegistrationResponseMessage(0, RegistrationResponseStatus.FAILURE_FULL);
|
||||
os.write(responseMessage.getBuffer());
|
||||
return;
|
||||
}
|
||||
|
||||
Integer sourceId = GameState.getUniquePlayerID();
|
||||
RegistrationResponseMessage responseMessage = new RegistrationResponseMessage(sourceId, RegistrationResponseStatus.SUCCESS_PLAYING);
|
||||
|
||||
this.clientType = clientType;
|
||||
this.sourceId = sourceId;
|
||||
isRegistered = true;
|
||||
os.write(responseMessage.getBuffer());
|
||||
|
||||
setUpPlayer();
|
||||
|
||||
for (ConnectionListener listener : connectionListeners) {
|
||||
listener.notifyConnection();
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
int sync1;
|
||||
int sync2;
|
||||
@@ -140,20 +195,6 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
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();
|
||||
@@ -173,11 +214,18 @@ 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);
|
||||
break;
|
||||
|
||||
case RACE_REGISTRATION_REQUEST:
|
||||
ClientType requestedType = ServerPacketParser.extractClientType(
|
||||
new StreamPacket(type, payloadLength, timeStamp, payload));
|
||||
|
||||
completeRegistration(requestedType);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
serverLog("Packet has been dropped", 1);
|
||||
@@ -192,7 +240,7 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
}
|
||||
}
|
||||
|
||||
private void sendSetupMessages() {
|
||||
public void sendSetupMessages() {
|
||||
xml = new XMLGenerator();
|
||||
Race race = new Race();
|
||||
|
||||
@@ -220,40 +268,6 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
|
||||
public void updateClient() {
|
||||
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) {
|
||||
Integer confirmationID = null;
|
||||
Integer identificationAttempt = 0;
|
||||
while (!userIdentified) {
|
||||
try {
|
||||
os.write(id); //Send out new ID looking for echo
|
||||
confirmationID = is.read();
|
||||
} catch (IOException e) {
|
||||
serverLog("Three way handshake failed", 1);
|
||||
}
|
||||
|
||||
if (id.equals(confirmationID)) { //ID is echoed back. Connection is a client
|
||||
return true;
|
||||
} else if (identificationAttempt > MAX_ID_ATTEMPTS) { //No response. not a client. tidy up and go home.
|
||||
return false;
|
||||
}
|
||||
identificationAttempt++;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void closeSocket() {
|
||||
@@ -264,7 +278,6 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private int readByte() throws Exception {
|
||||
int currentByte = -1;
|
||||
try {
|
||||
@@ -314,7 +327,6 @@ 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());
|
||||
BoatLocationMessage boatLocationMessage =
|
||||
new BoatLocationMessage(
|
||||
yacht.getSourceId(),
|
||||
@@ -335,7 +347,6 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
public void sendRaceStatusMessage() {
|
||||
// variables taken from GameServerThread
|
||||
|
||||
|
||||
List<BoatSubMessage> boatSubMessages = new ArrayList<>();
|
||||
BoatStatus boatStatus;
|
||||
RaceStatus raceStatus;
|
||||
@@ -378,4 +389,12 @@ public class ServerToClientThread implements Runnable, Observer {
|
||||
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();
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package seng302.gameServer.server.messages;
|
||||
|
||||
public enum ClientType {
|
||||
SPECTATOR(0x00),
|
||||
PLAYER(0x01),
|
||||
CONTROL_TUTORIAL(0x02),
|
||||
GHOST_MODE(0x03);
|
||||
|
||||
private int type;
|
||||
|
||||
ClientType(int type){
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getCode(){
|
||||
return type;
|
||||
}
|
||||
|
||||
public static ClientType getClientType(int typeCode){
|
||||
switch (typeCode){
|
||||
case 0x00:
|
||||
return SPECTATOR;
|
||||
case 0x01:
|
||||
return PLAYER;
|
||||
case 0x02:
|
||||
return CONTROL_TUTORIAL;
|
||||
case 0x03:
|
||||
return GHOST_MODE;
|
||||
default:
|
||||
return PLAYER;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,9 @@ public enum MessageType {
|
||||
MARK_ROUNDING(38),
|
||||
COURSE_WIND(44),
|
||||
AVERAGE_WIND(47),
|
||||
BOAT_ACTION(100);
|
||||
BOAT_ACTION(100),
|
||||
REGISTRATION_REQUEST(101),
|
||||
REGISTRATION_RESPONSE(102);
|
||||
|
||||
private int code;
|
||||
|
||||
@@ -32,4 +34,6 @@ public enum MessageType {
|
||||
int getCode(){
|
||||
return this.code;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package seng302.gameServer.server.messages;
|
||||
|
||||
|
||||
public class RegistrationRequestMessage extends Message {
|
||||
private static int MESSAGE_LENGTH = 2;
|
||||
|
||||
public RegistrationRequestMessage(ClientType type){
|
||||
setHeader(new Header(MessageType.REGISTRATION_REQUEST, 1, (short) getSize()));
|
||||
|
||||
allocateBuffer();
|
||||
writeHeaderToBuffer();
|
||||
|
||||
putInt(type.getCode(), 2);
|
||||
|
||||
writeCRC();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return MESSAGE_LENGTH;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package seng302.gameServer.server.messages;
|
||||
|
||||
public class RegistrationResponseMessage extends Message{
|
||||
|
||||
public RegistrationResponseMessage(int clientSourceID, RegistrationResponseStatus status){
|
||||
setHeader(new Header(MessageType.REGISTRATION_RESPONSE, 1, (short) getSize()));
|
||||
allocateBuffer();
|
||||
writeHeaderToBuffer();
|
||||
|
||||
putInt(clientSourceID, 4);
|
||||
putInt(status.getCode(), 1);
|
||||
|
||||
writeCRC();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return 5;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package seng302.gameServer.server.messages;
|
||||
|
||||
public enum RegistrationResponseStatus {
|
||||
SUCCESS_SPECTATING(0x00),
|
||||
SUCCESS_PLAYING(0x01),
|
||||
SUCCESS_TUTORIAL(0x02),
|
||||
SUCCESS_GHOSTING(0x03),
|
||||
|
||||
FAILURE_GENERAL(0x10),
|
||||
FAILURE_FULL(0x11);
|
||||
|
||||
private int code;
|
||||
|
||||
RegistrationResponseStatus(int code){
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the message code (From the API Spec)
|
||||
* @return the message code
|
||||
*/
|
||||
int getCode(){
|
||||
return this.code;
|
||||
}
|
||||
|
||||
public static RegistrationResponseStatus getResponseStatus(int typeCode){
|
||||
switch (typeCode){
|
||||
case 0x00:
|
||||
return SUCCESS_SPECTATING;
|
||||
case 0x01:
|
||||
return SUCCESS_PLAYING;
|
||||
case 0x02:
|
||||
return SUCCESS_TUTORIAL;
|
||||
case 0x03:
|
||||
return SUCCESS_GHOSTING;
|
||||
case 0x10:
|
||||
return FAILURE_GENERAL;
|
||||
case 0x11:
|
||||
return FAILURE_FULL;
|
||||
default:
|
||||
return FAILURE_GENERAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,10 @@ 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;
|
||||
import seng302.model.mark.CompoundMark;
|
||||
import seng302.model.mark.Mark;
|
||||
|
||||
/**
|
||||
* Parses the race xml file to get course details
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -32,7 +32,7 @@ public class Yacht extends Observable {
|
||||
|
||||
@FunctionalInterface
|
||||
public interface YachtLocationListener {
|
||||
void notifyLocation(Yacht yacht, double lat, double lon, double heading, double velocity);
|
||||
void notifyLocation(Yacht yacht, double lat, double lon, double heading, double velocity, boolean sailIn);
|
||||
}
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(Yacht.class);
|
||||
@@ -62,12 +62,14 @@ 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 Boolean sailIn = false;
|
||||
private GeoPoint location;
|
||||
private Integer boatStatus;
|
||||
private Double velocity;
|
||||
private Boolean isAuto;
|
||||
private Double autoHeading;
|
||||
|
||||
//MARK ROUNDING INFO
|
||||
private GeoPoint lastLocation; //For purposes of mark rounding calculations
|
||||
@@ -85,6 +87,7 @@ public class Yacht extends Observable {
|
||||
private CompoundMark lastMarkRounded;
|
||||
private Integer positionInt = 0;
|
||||
private Color colour;
|
||||
private Boolean clientSailsIn = true;
|
||||
|
||||
public Yacht(String boatType, Integer sourceId, String hullID, String shortName,
|
||||
String boatName, String country) {
|
||||
@@ -95,6 +98,7 @@ public class Yacht extends Observable {
|
||||
this.boatName = boatName;
|
||||
this.country = country;
|
||||
this.sailIn = false;
|
||||
this.isAuto = false;
|
||||
this.location = new GeoPoint(57.670341, 11.826856);
|
||||
this.lastLocation = location;
|
||||
this.heading = 120.0; //In degrees
|
||||
@@ -152,6 +156,11 @@ public class Yacht extends Observable {
|
||||
}
|
||||
}
|
||||
|
||||
runAutoPilot();
|
||||
|
||||
//UPDATE BOAT LOCATION
|
||||
lastLocation = location;
|
||||
location = GeoUtility.getGeoCoordinate(location, heading, velocity * secondsElapsed);
|
||||
Double metersCovered = velocity * secondsElapsed;
|
||||
GeoPoint calculatedPoint = getGeoCoordinate(location, heading, metersCovered);
|
||||
|
||||
@@ -390,15 +399,60 @@ public class Yacht extends Observable {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adjusts the heading of the boat by a given amount, while recording the boats
|
||||
* last heading.
|
||||
*
|
||||
* @param amount the amount by which to adjust the boat heading.
|
||||
*/
|
||||
public void adjustHeading(Double amount) {
|
||||
Double newVal = heading + amount;
|
||||
lastHeading = heading;
|
||||
heading = (double) Math.floorMod(newVal.longValue(), 360L);
|
||||
}
|
||||
|
||||
/**
|
||||
* Swaps the boats direction from one side of the wind to the other.
|
||||
*/
|
||||
public void tackGybe(Double windDirection) {
|
||||
if (isAuto) {
|
||||
disableAutoPilot();
|
||||
} else {
|
||||
Double normalizedHeading = normalizeHeading();
|
||||
adjustHeading(-2 * normalizedHeading);
|
||||
Double newVal = (-2 * normalizedHeading) + heading;
|
||||
Double newHeading = (double) Math.floorMod(newVal.longValue(), 360L);
|
||||
setAutoPilot(newHeading);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables the boats auto pilot feature, which will move the boat towards a given heading.
|
||||
* @param thisHeading The heading to move the boat towards.
|
||||
*/
|
||||
private void setAutoPilot(Double thisHeading) {
|
||||
isAuto = true;
|
||||
autoHeading = thisHeading;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables the auto pilot function.
|
||||
*/
|
||||
public void disableAutoPilot() {
|
||||
isAuto = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves the boat towards the given heading when the auto pilot was set. Disables the auto pilot
|
||||
* in the event that the boat is within the range of 1 turn step of its goal.
|
||||
*/
|
||||
public void runAutoPilot() {
|
||||
if (isAuto) {
|
||||
turnTowardsHeading(autoHeading);
|
||||
if (Math.abs(heading - autoHeading)
|
||||
<= TURN_STEP) { //Cancel when within 1 turn step of target.
|
||||
isAuto = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void toggleSailIn() {
|
||||
@@ -406,6 +460,7 @@ public class Yacht extends Observable {
|
||||
}
|
||||
|
||||
public void turnUpwind() {
|
||||
disableAutoPilot();
|
||||
Double normalizedHeading = normalizeHeading();
|
||||
if (normalizedHeading == 0) {
|
||||
if (lastHeading < 180) {
|
||||
@@ -427,6 +482,7 @@ public class Yacht extends Observable {
|
||||
}
|
||||
|
||||
public void turnDownwind() {
|
||||
disableAutoPilot();
|
||||
Double normalizedHeading = normalizeHeading();
|
||||
if (normalizedHeading == 0) {
|
||||
if (lastHeading < 180) {
|
||||
@@ -447,38 +503,59 @@ public class Yacht extends Observable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the VMG from the polartable for upwind or downwind depending on the boats direction,
|
||||
* and uses this to calculate a heading to move the yacht towards.
|
||||
*/
|
||||
public void turnToVMG() {
|
||||
if (isAuto) {
|
||||
disableAutoPilot();
|
||||
} else {
|
||||
Double normalizedHeading = normalizeHeading();
|
||||
Double optimalHeading;
|
||||
HashMap<Double, Double> optimalPolarMap;
|
||||
|
||||
if (normalizedHeading >= 90 && normalizedHeading <= 270) { // Downwind
|
||||
optimalPolarMap = PolarTable.getOptimalDownwindVMG(GameState.getWindSpeedKnots());
|
||||
optimalHeading = optimalPolarMap.keySet().iterator().next();
|
||||
} else {
|
||||
optimalPolarMap = PolarTable.getOptimalUpwindVMG(GameState.getWindSpeedKnots());
|
||||
}
|
||||
optimalHeading = optimalPolarMap.keySet().iterator().next();
|
||||
|
||||
if (normalizedHeading > 180) {
|
||||
optimalHeading = 360 - optimalHeading;
|
||||
}
|
||||
// Take optimal heading and turn into correct
|
||||
|
||||
// Take optimal heading and turn into a boat heading rather than a wind heading.
|
||||
optimalHeading =
|
||||
optimalHeading + (double) Math.floorMod(GameState.getWindDirection().longValue(), 360L);
|
||||
|
||||
turnTowardsHeading(optimalHeading);
|
||||
optimalHeading + GameState.getWindDirection();
|
||||
|
||||
setAutoPilot(optimalHeading);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes a given heading and rotates the boat towards that heading.
|
||||
* This does not care about being upwind or downwind, just which direction will reach a given
|
||||
* heading faster.
|
||||
*
|
||||
* @param newHeading The heading to turn the yacht towards.
|
||||
*/
|
||||
private void turnTowardsHeading(Double newHeading) {
|
||||
if (heading < 90 && newHeading > 270) {
|
||||
adjustHeading(-TURN_STEP);
|
||||
Double newVal = heading - newHeading;
|
||||
if (Math.floorMod(newVal.longValue(), 360L) > 180) {
|
||||
adjustHeading(TURN_STEP / 5);
|
||||
} else {
|
||||
if (heading < newHeading) {
|
||||
adjustHeading(TURN_STEP);
|
||||
} else {
|
||||
adjustHeading(-TURN_STEP);
|
||||
}
|
||||
adjustHeading(-TURN_STEP / 5);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a heading normalized for the wind direction. Heading direction into the wind is 0,
|
||||
* directly away is 180.
|
||||
*
|
||||
* @return The normalized heading accounting for wind direction.
|
||||
*/
|
||||
private Double normalizeHeading() {
|
||||
Double normalizedHeading = heading - GameState.windDirection;
|
||||
normalizedHeading = (double) Math.floorMod(normalizedHeading.longValue(), 360L);
|
||||
@@ -654,6 +731,9 @@ public class Yacht extends Observable {
|
||||
this.colour = colour;
|
||||
}
|
||||
|
||||
public void toggleClientSail() {
|
||||
clientSailsIn = !clientSailsIn;
|
||||
}
|
||||
|
||||
public Double getVelocity() {
|
||||
return velocity;
|
||||
@@ -667,13 +747,17 @@ public class Yacht extends Observable {
|
||||
return distanceToCurrentMark;
|
||||
}
|
||||
|
||||
public Boolean getClientSailsIn(){
|
||||
return clientSailsIn;
|
||||
}
|
||||
|
||||
public void updateLocation(double lat, double lng, double heading, double velocity) {
|
||||
setLocation(lat, lng);
|
||||
this.heading = heading;
|
||||
this.velocity = velocity;
|
||||
updateVelocityProperty(velocity);
|
||||
for (YachtLocationListener yll : locationListeners) {
|
||||
yll.notifyLocation(this, lat, lng, heading, velocity);
|
||||
yll.notifyLocation(this, lat, lng, heading, velocity, clientSailsIn);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package seng302.model.mark;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.w3c.dom.Document;
|
||||
@@ -9,12 +14,6 @@ import seng302.model.stream.xml.generator.Race;
|
||||
import seng302.model.stream.xml.parser.RaceXMLData;
|
||||
import seng302.utilities.XMLGenerator;
|
||||
import seng302.utilities.XMLParser;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -53,8 +52,7 @@ public class MarkOrder {
|
||||
/**
|
||||
* @param currentSeqID The seqID of the current mark the boat is heading to
|
||||
* @return The mark last passed
|
||||
* @throws IndexOutOfBoundsException if there is no next mark.
|
||||
* Check seqID != 0 first
|
||||
* @throws IndexOutOfBoundsException if there is no next mark. Check seqID != 0 first
|
||||
*/
|
||||
public CompoundMark getPreviousMark(Integer currentSeqID) throws IndexOutOfBoundsException {
|
||||
return raceMarkOrder.get(currentSeqID - 1);
|
||||
@@ -67,8 +65,8 @@ public class MarkOrder {
|
||||
/**
|
||||
* @param currentSeqID The seqID of the current mark the boat is heading to
|
||||
* @return The mark following the mark that the boat is heading to
|
||||
* @throws IndexOutOfBoundsException if there is no next mark.
|
||||
* Check using {@link #isLastMark(Integer)}
|
||||
* @throws IndexOutOfBoundsException if there is no next mark. Check using {@link
|
||||
* #isLastMark(Integer)}
|
||||
*/
|
||||
public CompoundMark getNextMark(Integer currentSeqID) throws IndexOutOfBoundsException {
|
||||
return raceMarkOrder.get(currentSeqID + 1);
|
||||
|
||||
@@ -19,7 +19,9 @@ public enum PacketType {
|
||||
COURSE_WIND,
|
||||
AVG_WIND,
|
||||
BOAT_ACTION,
|
||||
OTHER;
|
||||
OTHER,
|
||||
RACE_REGISTRATION_REQUEST,
|
||||
RACE_REGISTRATION_RESPONSE;
|
||||
|
||||
public static PacketType assignPacketType(int packetType, byte[] payload){
|
||||
switch(packetType){
|
||||
@@ -56,6 +58,10 @@ public enum PacketType {
|
||||
return AVG_WIND;
|
||||
case 100:
|
||||
return BOAT_ACTION;
|
||||
case 101:
|
||||
return RACE_REGISTRATION_REQUEST;
|
||||
case 102:
|
||||
return RACE_REGISTRATION_RESPONSE;
|
||||
default:
|
||||
}
|
||||
return OTHER;
|
||||
|
||||
@@ -7,17 +7,28 @@ import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.time.LocalDateTime;
|
||||
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;
|
||||
import javafx.application.Platform;
|
||||
import javafx.scene.control.Alert;
|
||||
import javafx.scene.control.Alert.AlertType;
|
||||
import seng302.model.stream.packets.StreamPacket;
|
||||
import javafx.scene.control.ButtonType;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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;
|
||||
|
||||
/**
|
||||
* A class describing a single connection to a Server for the purposes of sending and receiving on
|
||||
@@ -25,6 +36,8 @@ import seng302.gameServer.server.messages.Message;
|
||||
*/
|
||||
public class ClientToServerThread implements Runnable {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Functional interface for receiving packets from client socket.
|
||||
*/
|
||||
@@ -47,9 +60,17 @@ public class ClientToServerThread implements Runnable {
|
||||
|
||||
private Socket socket;
|
||||
private InputStream is;
|
||||
private OutputStream os;
|
||||
|
||||
private int clientId;
|
||||
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;
|
||||
private ByteArrayOutputStream crcBuffer;
|
||||
@@ -71,15 +92,8 @@ public class ClientToServerThread implements Runnable {
|
||||
socket = new Socket(ipAddress, portNumber);
|
||||
is = socket.getInputStream();
|
||||
os = socket.getOutputStream();
|
||||
Integer allocatedID = threeWayHandshake();
|
||||
if (allocatedID != null) {
|
||||
clientId = allocatedID;
|
||||
clientLog("Successful handshake. Allocated ID: " + clientId, 1);
|
||||
} else {
|
||||
clientLog("Unsuccessful handshake", 1);
|
||||
closeSocket();
|
||||
return;
|
||||
}
|
||||
|
||||
sendRegistrationRequest();
|
||||
|
||||
thread = new Thread(this);
|
||||
thread.start();
|
||||
@@ -128,15 +142,22 @@ public class ClientToServerThread implements Runnable {
|
||||
if (streamPackets.size() > 0) {
|
||||
streamPackets.add(new StreamPacket(type, payloadLength, timeStamp, payload));
|
||||
} else {
|
||||
if (PacketType.RACE_REGISTRATION_RESPONSE == PacketType.assignPacketType(type, payload)){
|
||||
processRegistrationResponse(new StreamPacket(type, payloadLength, timeStamp, payload));
|
||||
}
|
||||
else {
|
||||
if (clientId == -1) continue; // Do not continue if not registered
|
||||
streamPackets.add(new StreamPacket(type, payloadLength, timeStamp, payload));
|
||||
for (ClientSocketListener csl : listeners)
|
||||
csl.newPacket();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
clientLog("Packet has been dropped", 1);
|
||||
}
|
||||
}
|
||||
} catch (ByteReadException e) {
|
||||
e.printStackTrace();
|
||||
closeSocket();
|
||||
Platform.runLater(() -> {
|
||||
Alert alert = new Alert(AlertType.ERROR);
|
||||
@@ -147,7 +168,6 @@ public class ClientToServerThread implements Runnable {
|
||||
clientLog(e.getMessage(), 1);
|
||||
return;
|
||||
}
|
||||
// System.out.println("streamPackets = " + streamPackets.size());
|
||||
}
|
||||
closeSocket();
|
||||
clientLog("Closed connection to Server", 0);
|
||||
@@ -155,43 +175,127 @@ public class ClientToServerThread implements Runnable {
|
||||
|
||||
|
||||
/**
|
||||
* Listens for an allocated sourceID and returns it to the server
|
||||
*
|
||||
* @return the sourceID allocated to us by the server
|
||||
* Sends a request to the server asking for a source ID
|
||||
*/
|
||||
private Integer threeWayHandshake() {
|
||||
Integer ourSourceID = null;
|
||||
while (true) {
|
||||
try {
|
||||
ourSourceID = is.read();
|
||||
} catch (IOException e) {
|
||||
clientLog("Three way handshake failed", 1);
|
||||
}
|
||||
if (ourSourceID != null) {
|
||||
try {
|
||||
os.write(ourSourceID);
|
||||
return ourSourceID;
|
||||
} catch (IOException e) {
|
||||
clientLog("Three way handshake failed", 1);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private void sendRegistrationRequest() {
|
||||
RegistrationRequestMessage requestMessage = new RegistrationRequestMessage(ClientType.PLAYER);
|
||||
|
||||
try {
|
||||
os.write(requestMessage.getBuffer());
|
||||
} catch (IOException e) {
|
||||
logger.error("Could not send registration request. Exiting");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the post-start race course information
|
||||
* @param boatActionMessage The message to send
|
||||
* Accepts a response to the registration request message, and updates the client OR quits
|
||||
* @param packet The registration requests packet
|
||||
*/
|
||||
public void sendBoatActionMessage(BoatActionMessage boatActionMessage) {
|
||||
private void processRegistrationResponse(StreamPacket packet){
|
||||
int sourceId = (int) Message.bytesToLong(Arrays.copyOfRange(packet.getPayload(), 0, 3));
|
||||
int statusCode = (int) Message.bytesToLong(Arrays.copyOfRange(packet.getPayload(), 4,5));
|
||||
|
||||
RegistrationResponseStatus status = RegistrationResponseStatus.getResponseStatus(statusCode);
|
||||
|
||||
if (status.equals(RegistrationResponseStatus.SUCCESS_PLAYING)){
|
||||
clientId = sourceId;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
logger.error("Server Denied Connection, Exiting");
|
||||
|
||||
final String alertErrorText;
|
||||
|
||||
if (status.equals(RegistrationResponseStatus.FAILURE_FULL)){
|
||||
alertErrorText = "Server is full";
|
||||
}
|
||||
else{
|
||||
alertErrorText = "Could not connect to server";
|
||||
}
|
||||
|
||||
Platform.runLater(() -> {
|
||||
new Alert(AlertType.ERROR, alertErrorText, ButtonType.OK).showAndWait();
|
||||
System.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 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(boatActionMessage.getBuffer());
|
||||
os.write(message.getBuffer());
|
||||
} catch (IOException e) {
|
||||
clientLog("Could not write to server", 1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void closeSocket() {
|
||||
try {
|
||||
@@ -245,11 +349,8 @@ public class ClientToServerThread implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
public Thread getThread() {
|
||||
return thread;
|
||||
}
|
||||
|
||||
public int getClientId () {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,6 +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.BoatAction;
|
||||
import seng302.model.RaceState;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.model.stream.packets.StreamPacket;
|
||||
@@ -23,8 +24,6 @@ import seng302.model.stream.parser.RaceStatusData;
|
||||
import seng302.model.stream.parser.YachtEventData;
|
||||
import seng302.model.stream.xml.parser.RaceXMLData;
|
||||
import seng302.model.stream.xml.parser.RegattaXMLData;
|
||||
import seng302.gameServer.server.messages.BoatActionMessage;
|
||||
import seng302.gameServer.server.messages.BoatActionType;
|
||||
import seng302.utilities.StreamParser;
|
||||
import seng302.utilities.XMLParser;
|
||||
import seng302.visualiser.controllers.LobbyController;
|
||||
@@ -32,7 +31,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 {
|
||||
|
||||
@@ -49,13 +49,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);
|
||||
@@ -63,6 +70,7 @@ public class GameClient {
|
||||
ioe.printStackTrace();
|
||||
System.out.println("Unable to connect to host...");
|
||||
}
|
||||
|
||||
socketThread.addStreamObserver(this::parsePackets);
|
||||
LobbyController lobbyController = loadLobby();
|
||||
lobbyController.setPlayerListSource(clientLobbyList);
|
||||
@@ -71,6 +79,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 {
|
||||
@@ -96,10 +109,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(
|
||||
@@ -182,12 +193,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:
|
||||
@@ -258,6 +266,8 @@ public class GameClient {
|
||||
private void processRaceStatusUpdate(RaceStatusData data) {
|
||||
if (allXMLReceived()) {
|
||||
raceState.updateState(data);
|
||||
if (raceView != null)
|
||||
raceView.getGameView().setWindDir(raceState.getWindDirection());
|
||||
for (long[] boatData : data.getBoatData()) {
|
||||
Yacht yacht = allBoatsMap.get((int) boatData[0]);
|
||||
yacht.setEstimateTimeTillNextMark(raceState.getRaceTime() - boatData[1]);
|
||||
@@ -289,28 +299,16 @@ public class GameClient {
|
||||
*
|
||||
* @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;
|
||||
private void keyPressed(KeyEvent e) {
|
||||
switch (e.getCode()) {
|
||||
case SPACE: // align with vmg
|
||||
boatActionMessage = new BoatActionMessage(BoatActionType.VMG);
|
||||
socketThread.sendBoatActionMessage(boatActionMessage);
|
||||
break;
|
||||
socketThread.sendBoatAction(BoatAction.VMG); break;
|
||||
case PAGE_UP: // upwind
|
||||
boatActionMessage = new BoatActionMessage(BoatActionType.UPWIND);
|
||||
socketThread.sendBoatActionMessage(boatActionMessage);
|
||||
break;
|
||||
socketThread.sendBoatAction(BoatAction.UPWIND); break;
|
||||
case PAGE_DOWN: // downwind
|
||||
boatActionMessage = new BoatActionMessage(BoatActionType.DOWNWIND);
|
||||
socketThread.sendBoatActionMessage(boatActionMessage);
|
||||
break;
|
||||
socketThread.sendBoatAction(BoatAction.DOWNWIND); break;
|
||||
case ENTER: // tack/gybe
|
||||
boatActionMessage = new BoatActionMessage(BoatActionType.TACK_GYBE);
|
||||
socketThread.sendBoatActionMessage(boatActionMessage);
|
||||
break;
|
||||
socketThread.sendBoatAction(BoatAction.TACK_GYBE); break;
|
||||
//TODO Allow a zoom in and zoom out methods
|
||||
case Z: // zoom in
|
||||
System.out.println("Zoom in");
|
||||
@@ -320,16 +318,17 @@ public class GameClient {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
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
|
||||
BoatActionMessage boatActionMessage = new BoatActionMessage(
|
||||
BoatActionType.SAILS_IN);
|
||||
socketThread.sendBoatActionMessage(boatActionMessage);
|
||||
socketThread.sendBoatAction(BoatAction.SAILS_IN);
|
||||
raceView.getGameView().getPlayerYacht().toggleClientSail();
|
||||
break;
|
||||
case PAGE_UP:
|
||||
case PAGE_DOWN:
|
||||
socketThread.sendBoatAction(BoatAction.MAINTAIN_HEADING); break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,9 @@ import javafx.geometry.Point2D;
|
||||
import javafx.scene.Group;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.image.ImageView;
|
||||
import javafx.scene.input.KeyCode;
|
||||
import javafx.scene.input.KeyEvent;
|
||||
import javafx.scene.input.ScrollEvent;
|
||||
import javafx.scene.layout.AnchorPane;
|
||||
import javafx.scene.layout.Pane;
|
||||
import javafx.scene.paint.Color;
|
||||
@@ -64,6 +67,8 @@ public class GameView extends Pane {
|
||||
private double referencePointX, referencePointY;
|
||||
private double metersPerPixelX, metersPerPixelY;
|
||||
|
||||
final double SCALE_DELTA = 1.1;
|
||||
|
||||
private Text fpsDisplay = new Text();
|
||||
private Polygon raceBorder = new CourseBoundary();
|
||||
|
||||
@@ -91,6 +96,26 @@ public class GameView extends Pane {
|
||||
private Double frameRate = 60.0;
|
||||
private int frameTimeIndex = 0;
|
||||
private boolean arrayFilled = false;
|
||||
private Yacht playerYacht;
|
||||
private double windDir = 0.0;
|
||||
|
||||
double scaleFactor = 1;
|
||||
|
||||
public void zoomOut() {
|
||||
scaleFactor = 0.95;
|
||||
for (Node child : getChildren()) {
|
||||
child.setScaleX(child.getScaleX() * scaleFactor);
|
||||
child.setScaleY(child.getScaleY() * scaleFactor);
|
||||
}
|
||||
}
|
||||
|
||||
public void zoomIn() {
|
||||
scaleFactor = 1.05;
|
||||
for (Node child : getChildren()) {
|
||||
child.setScaleX(child.getScaleX() * scaleFactor);
|
||||
child.setScaleY(child.getScaleY() * scaleFactor);
|
||||
}
|
||||
}
|
||||
|
||||
private enum ScaleDirection {
|
||||
HORIZONTAL,
|
||||
@@ -125,8 +150,7 @@ public class GameView extends Pane {
|
||||
if (lastTime == 0) {
|
||||
lastTime = now;
|
||||
} else {
|
||||
if (now - lastTime >= (1e8
|
||||
/ 60)) { //Fix for framerate going above 60 when minimized
|
||||
if (now - lastTime >= (1e8 / 60)) { //Fix for framerate going above 60 when minimized
|
||||
long oldFrameTime = frameTimes[frameTimeIndex];
|
||||
frameTimes[frameTimeIndex] = now;
|
||||
frameTimeIndex = (frameTimeIndex + 1) % frameTimes.length;
|
||||
@@ -147,7 +171,7 @@ public class GameView extends Pane {
|
||||
}
|
||||
}
|
||||
// Platform.runLater(() ->
|
||||
// boatObjects.forEach((boat, boatObject) -> boatObject.updateLocation())
|
||||
boatObjects.forEach((boat, boatObject) -> boatObject.updateLocation());
|
||||
// );
|
||||
}
|
||||
};
|
||||
@@ -337,10 +361,10 @@ public class GameView extends Pane {
|
||||
boatObjectGroup.getChildren().add(newBoat);
|
||||
trails.getChildren().add(newBoat.getTrail());
|
||||
// TODO: 1/08/17 Make this less vile to look at.
|
||||
yacht.addLocationListener((boat, lat, lon, heading, velocity) -> {
|
||||
yacht.addLocationListener((boat, lat, lon, heading, velocity, sailIn) ->{
|
||||
BoatObject bo = boatObjects.get(boat);
|
||||
Point2D p2d = findScaledXY(lat, lon);
|
||||
bo.moveTo(p2d.getX(), p2d.getY(), heading, velocity);
|
||||
bo.moveTo(p2d.getX(), p2d.getY(), heading, velocity, sailIn, windDir);
|
||||
// annotations.get(boat).setLayoutX(p2d.getX());
|
||||
// annotations.get(boat).setLayoutY(p2d.getY());
|
||||
// annotations.get(boat).setLocation(100d, 100d);
|
||||
@@ -512,7 +536,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);
|
||||
@@ -540,8 +563,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);
|
||||
}
|
||||
|
||||
@@ -593,11 +614,23 @@ public class GameView extends Pane {
|
||||
timer.stop();
|
||||
}
|
||||
|
||||
|
||||
public void setWindDir(double windDir) {
|
||||
this.windDir = windDir;
|
||||
}
|
||||
|
||||
public void startRace() {
|
||||
timer.start();
|
||||
}
|
||||
|
||||
public void setBoatAsPlayer(Yacht playerYacht) {
|
||||
public Yacht getPlayerYacht() {
|
||||
return playerYacht;
|
||||
}
|
||||
|
||||
public void setBoatAsPlayer (Yacht playerYacht) {
|
||||
this.playerYacht = playerYacht;
|
||||
this.playerYacht.toggleClientSail();
|
||||
boatObjects.get(playerYacht).setAsPlayer();
|
||||
annotations.get(playerYacht).addAnnotation(
|
||||
"velocity",
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,7 +243,7 @@ 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))
|
||||
@@ -251,8 +251,8 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
|
||||
raceSparkLine.getData().add(spark);
|
||||
spark.getNode().lookup(".chart-series-line")
|
||||
.setStyle("-fx-stroke:" + getBoatColorAsRGB(spark.getName()));
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private void initialiseSparkLine() {
|
||||
@@ -613,4 +613,8 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
|
||||
public void showCollision(Long subjectId) {
|
||||
gameView.drawCollision(participants.get((int) (long) subjectId).getLocation());
|
||||
}
|
||||
|
||||
public GameView getGameView() {
|
||||
return gameView;
|
||||
}
|
||||
}
|
||||
@@ -66,7 +66,7 @@ public class StartScreenController implements Initializable {
|
||||
*/
|
||||
@FXML
|
||||
public void hostButtonPressed() {
|
||||
new GameState(getLocalHostIp());
|
||||
// new GameState(getLocalHostIp());
|
||||
gameClient = new GameClient(holder);
|
||||
gameClient.runAsHost(getLocalHostIp(), 4942);
|
||||
// try {
|
||||
|
||||
@@ -30,9 +30,11 @@ public class BoatObject extends Group {
|
||||
private double xVelocity;
|
||||
private double yVelocity;
|
||||
private double lastHeading;
|
||||
private double sailState;
|
||||
//Graphical objects
|
||||
private Polyline trail = new Polyline();
|
||||
private Polygon boatPoly;
|
||||
private Polygon sail;
|
||||
private Wake wake;
|
||||
private Line leftLayLine;
|
||||
private Line rightLayline;
|
||||
@@ -94,7 +96,16 @@ public class BoatObject extends Group {
|
||||
trail.setCache(true);
|
||||
wake = new Wake(0, -BOAT_HEIGHT);
|
||||
wake.setVisible(true);
|
||||
super.getChildren().addAll(boatPoly);//, annotationBox);
|
||||
|
||||
sail = new Polygon(0.0,BOAT_HEIGHT / 4,
|
||||
0.0, BOAT_HEIGHT);
|
||||
sailState = 0;
|
||||
sail.setStrokeWidth(2.0);
|
||||
sail.setStroke(Color.BLACK);
|
||||
sail.setFill(Color.TRANSPARENT);
|
||||
sail.setCache(true);
|
||||
super.getChildren().clear();
|
||||
super.getChildren().addAll(boatPoly, sail);
|
||||
}
|
||||
|
||||
public void setFill (Paint value) {
|
||||
@@ -105,19 +116,30 @@ public class BoatObject extends Group {
|
||||
|
||||
/**
|
||||
* Moves the boat and its children annotations to coordinates specified
|
||||
*
|
||||
* @param x The X coordinate to move the boat to
|
||||
* @param y The Y coordinate to move the boat to
|
||||
* @param rotation The rotation by which the boat moves
|
||||
* @param velocity The velocity the boat is moving
|
||||
* @param sailIn
|
||||
*/
|
||||
public void moveTo(double x, double y, double rotation, double velocity) {
|
||||
public void moveTo(double x, double y, double rotation, double velocity, Boolean sailIn, double windDir) {
|
||||
Double dx = Math.abs(boatPoly.getLayoutX() - x);
|
||||
Double dy = Math.abs(boatPoly.getLayoutY() - y);
|
||||
Platform.runLater(() -> {
|
||||
rotateTo(rotation);
|
||||
rotateTo(rotation, sailIn, windDir);
|
||||
boatPoly.setLayoutX(x);
|
||||
boatPoly.setLayoutY(y);
|
||||
if (sailIn) {
|
||||
// sail.getPoints().clear();
|
||||
// sail.getPoints().addAll(0.0, 0.0, 4.0, 1.5, 8.0, 3.0, 12.0, 3.5, 16.0, 3.0, 20.0, 1.5, 24.0, 0.0);
|
||||
// sail.getPoints().addAll(0.0, 0.0, 24.0, 0.0);
|
||||
sail.setLayoutX(x);
|
||||
sail.setLayoutY(y);
|
||||
} else {
|
||||
animateSail();
|
||||
sail.setLayoutX(x);
|
||||
sail.setLayoutY(y);
|
||||
}
|
||||
wake.setLayoutX(x);
|
||||
wake.setLayoutY(y);
|
||||
});
|
||||
@@ -142,8 +164,65 @@ public class BoatObject extends Group {
|
||||
}
|
||||
}
|
||||
|
||||
private void rotateTo(double rotation) {
|
||||
boatPoly.getTransforms().setAll(new Rotate(rotation));
|
||||
private Double normalizeHeading(double heading, double windDirection) {
|
||||
Double normalizedHeading = heading - windDirection;
|
||||
normalizedHeading = (double) Math.floorMod(normalizedHeading.longValue(), 360L);
|
||||
return normalizedHeading;
|
||||
}
|
||||
|
||||
|
||||
private void rotateTo(double heading, boolean sailsIn, double windDir) {
|
||||
boatPoly.getTransforms().setAll(new Rotate(heading));
|
||||
if (sailsIn) {
|
||||
Double sailWindOffset = 30.0;
|
||||
Double upwindAngleLimit = 15.0;
|
||||
Double downwindAngleLimit = 10.0; //Upwind from normalised horizontal
|
||||
Double normalizedHeading = normalizeHeading(heading, windDir);
|
||||
if (normalizedHeading < 180) {
|
||||
sail.getTransforms().setAll(new Rotate(windDir + 90 + sailWindOffset));
|
||||
sail.getPoints().clear();
|
||||
sail.getPoints().addAll(0.0, 0.0, 4.0, -1.5, 8.0, -3.0, 12.0, -3.5, 16.0, -3.0, 20.0, -1.5, 24.0, 0.0);
|
||||
if (normalizedHeading > 90 + sailWindOffset){
|
||||
sail.getTransforms().setAll(new Rotate(heading + downwindAngleLimit));
|
||||
}
|
||||
if (normalizedHeading < sailWindOffset + upwindAngleLimit){
|
||||
sail.getTransforms().setAll(new Rotate(heading + 90 - upwindAngleLimit));
|
||||
}
|
||||
} else {
|
||||
sail.getTransforms().setAll(new Rotate(windDir + 90 - sailWindOffset));
|
||||
sail.getPoints().clear();
|
||||
sail.getPoints().addAll(0.0, 0.0, 4.0, 1.5, 8.0, 3.0, 12.0, 3.5, 16.0, 3.0, 20.0, 1.5, 24.0, 0.0);
|
||||
if (normalizedHeading < 270 - sailWindOffset){
|
||||
sail.getTransforms().setAll(new Rotate(heading + 180 - downwindAngleLimit));
|
||||
}
|
||||
if (normalizedHeading > 360 - (sailWindOffset + upwindAngleLimit)){
|
||||
sail.getTransforms().setAll(new Rotate(heading + 90 + upwindAngleLimit));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sail.getTransforms().setAll(new Rotate(windDir));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void animateSail(){
|
||||
Double[] points = new Double[200];
|
||||
double amplitude = 2.0;
|
||||
double period = 10;
|
||||
for (int i = 0; i < 50; i++) {
|
||||
points[i * 2] = amplitude * Math.sin(((Math.PI * i) / period + sailState));
|
||||
points[i * 2 + 1] = (BOAT_HEIGHT * i) / BOAT_HEIGHT / 2;
|
||||
points[199 - (i * 2)] = (BOAT_HEIGHT * i) / BOAT_HEIGHT / 2;
|
||||
points[199 - (i * 2 + 1)] = amplitude * Math.sin(((Math.PI * i) / period + sailState));
|
||||
}
|
||||
if (sailState == - 2 * Math.PI) {
|
||||
sailState = 0;
|
||||
} else {
|
||||
sailState = sailState - Math.PI / 5;
|
||||
}
|
||||
sail.getPoints().clear();
|
||||
sail.getPoints().addAll(points);
|
||||
|
||||
}
|
||||
|
||||
public void updateLocation() {
|
||||
@@ -275,11 +354,12 @@ public class BoatObject extends Group {
|
||||
boatPoly.setStroke(Color.BLACK);
|
||||
boatPoly.setStrokeWidth(3);
|
||||
isPlayer = true;
|
||||
animateSail();
|
||||
}
|
||||
|
||||
public void setTrajectory(double heading, double velocity) {
|
||||
public void setTrajectory(double heading, double velocity, double windDir) {
|
||||
wake.setRotation(lastHeading - heading, velocity);
|
||||
rotateTo(heading);
|
||||
rotateTo(heading, false, windDir);
|
||||
xVelocity = Math.cos(Math.toRadians(heading)) * velocity;
|
||||
yVelocity = Math.sin(Math.toRadians(heading)) * velocity;
|
||||
lastHeading = heading;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -4,6 +4,6 @@
|
||||
<race-name>AC35</race-name>
|
||||
<race-size>6</race-size>
|
||||
<time-scale>10.0</time-scale>
|
||||
<wind-direction>135</wind-direction>
|
||||
<windDir-direction>135</windDir-direction>
|
||||
</configurations>
|
||||
|
||||
|
||||
@@ -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" />
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import cucumber.api.CucumberOptions;
|
||||
import cucumber.api.junit.Cucumber;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
/**
|
||||
* Created by kre39 on 7/08/17.
|
||||
*/
|
||||
|
||||
@RunWith(Cucumber.class)
|
||||
@CucumberOptions(features = "src/test/java/features")
|
||||
public class RunCucumberTests {
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
Feature: SailsToggle
|
||||
Scenario: User toggles in sail
|
||||
Given The game is running
|
||||
When the user has pressed "shift"
|
||||
Then the sails are "in"
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package seng302.model.mark;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
package seng302.models;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import seng302.model.mark.CompoundMark;
|
||||
import seng302.model.mark.Mark;
|
||||
import seng302.model.mark.MarkOrder;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
public class MarkOrderTest {
|
||||
private static MarkOrder markOrder;
|
||||
private static Integer currentSeqID;
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package seng302.models;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.HashMap;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import seng302.gameServer.GameState;
|
||||
import seng302.model.PolarTable;
|
||||
import seng302.model.Yacht;
|
||||
|
||||
|
||||
public class YachtTest {
|
||||
|
||||
private static Yacht y1;
|
||||
//Yacht y2;
|
||||
private static Double windDirection = 180d;
|
||||
private static Double windSpeed = 20d;
|
||||
private static GameState gs;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() {
|
||||
y1 = new Yacht("Yacht", 101, "Y1", "Y1", "Yacht 1", "C1");
|
||||
gs = new GameState("localhost");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void tackGybeTest() {
|
||||
HashMap<Double, Double> values = new HashMap<>();
|
||||
values.put(280.0, 80.0);
|
||||
values.put(270.0, 90.0);
|
||||
values.put(359.0, 1.0);
|
||||
values.put(180.0, 180.0);
|
||||
values.put(75.0, 285.0);
|
||||
|
||||
for (Double begin : values.keySet()) {
|
||||
y1.setHeading(begin);
|
||||
y1.tackGybe(windDirection);
|
||||
|
||||
for (int i = 0; i < 200; i++) {
|
||||
y1.runAutoPilot();
|
||||
}
|
||||
assertEquals(values.get(begin), y1.getHeading(), 5.0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void vmgTest() {
|
||||
|
||||
PolarTable.parsePolarFile(getClass().getResourceAsStream("/config/acc_polars.csv"));
|
||||
Double upwind = PolarTable.getOptimalUpwindVMG(windSpeed).keySet().iterator().next();
|
||||
Double downwind = PolarTable.getOptimalDownwindVMG(windSpeed).keySet().iterator().next();
|
||||
|
||||
HashMap<Double, Double> values = new HashMap<>();
|
||||
|
||||
upwind = (double) Math.floorMod(upwind.longValue() + windDirection.longValue(), 360L);
|
||||
Double upwindRight = upwind;
|
||||
Double upwindLeft = 360 - upwindRight;
|
||||
downwind = (double) Math.floorMod(downwind.longValue() + windDirection.longValue(), 360L);
|
||||
Double downwindRight = downwind;
|
||||
Double downwindLeft = 360 - downwindRight;
|
||||
|
||||
values.put(190d, upwindRight);
|
||||
values.put(170d, upwindLeft);
|
||||
values.put(10d, downwindLeft);
|
||||
values.put(350d, downwindRight);
|
||||
|
||||
for (Double begin : values.keySet()) {
|
||||
y1.setHeading(begin);
|
||||
y1.turnToVMG();
|
||||
for (int i = 0; i < 200; i++) {
|
||||
y1.runAutoPilot();
|
||||
}
|
||||
y1.disableAutoPilot();
|
||||
assertEquals(values.get(begin), y1.getHeading(), 5.0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() {
|
||||
y1 = null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package seng302.visualiser.map;
|
||||
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import seng302.model.Yacht;
|
||||
import seng302.visualiser.fxObjects.BoatObject;
|
||||
|
||||
/**
|
||||
* Created by kre39 on 6/08/17.
|
||||
*/
|
||||
public class BoatSailAnimationToggleTest {
|
||||
|
||||
private Yacht yacht;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception{
|
||||
yacht = new Yacht("Yacht", 1, "YACHT", "YAC", "Test Yacht", "NZ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sailToggleTest() throws Exception {
|
||||
assertFalse(yacht.getSailIn());
|
||||
yacht.toggleClientSail();
|
||||
assertFalse(yacht.getSailIn());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package steps;
|
||||
|
||||
import cucumber.api.java.en.Given;
|
||||
import cucumber.api.java.en.Then;
|
||||
import cucumber.api.java.en.When;
|
||||
import java.util.ArrayList;
|
||||
import org.junit.Assert;
|
||||
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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by kre39 on 7/08/17.
|
||||
*/
|
||||
public class ToggleSailSteps {
|
||||
|
||||
|
||||
MainServerThread mst;
|
||||
ClientToServerThread client;
|
||||
boolean sailsIn = false;
|
||||
long startTime;
|
||||
private Yacht yacht;
|
||||
|
||||
|
||||
|
||||
@Given("^The game is running$")
|
||||
public void the_game_is_running() throws Throwable {
|
||||
mst = new MainServerThread();
|
||||
client = new ClientToServerThread("localhost", 4942);
|
||||
GameState.setCurrentStage(GameStages.RACING);
|
||||
Thread.sleep(200); // Sleep needed to help the threads all be up to speed with each other
|
||||
Yacht yacht = (new ArrayList<>(GameState.getYachts().values())).get(0);
|
||||
Assert.assertFalse(yacht.getSailIn());
|
||||
}
|
||||
|
||||
|
||||
@When("^the user has pressed \"([^\"]*)\"$")
|
||||
public void the_user_has_pressed(String arg1) throws Throwable {
|
||||
startTime = System.currentTimeMillis();
|
||||
if (arg1 == "shift") {
|
||||
client.sendBoatAction(BoatAction.SAILS_IN);
|
||||
}
|
||||
}
|
||||
|
||||
@Then("^the sails are \"([^\"]*)\"$")
|
||||
public void the_sails_are(String arg1) throws Throwable {
|
||||
Thread.sleep(200); // Sleep needed to help the threads all be up to speed with each other
|
||||
Yacht yacht = (new ArrayList<>(GameState.getYachts().values())).get(0);
|
||||
if (arg1 == "in") {
|
||||
Assert.assertTrue(yacht.getSailIn());
|
||||
} else {
|
||||
Assert.assertFalse(yacht.getSailIn());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user