Cbt multi controller Covers stories 4 and 6. See merge request !17main
commit
7966ac41fb
@ -0,0 +1,3 @@
|
|||||||
|
<component name="CopyrightManager">
|
||||||
|
<settings default="" />
|
||||||
|
</component>
|
||||||
@ -1,54 +0,0 @@
|
|||||||
package mock.app;
|
|
||||||
|
|
||||||
|
|
||||||
import javafx.application.Application;
|
|
||||||
import javafx.stage.Stage;
|
|
||||||
import mock.dataInput.PolarParser;
|
|
||||||
import mock.model.Polars;
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.xml.sax.InputSource;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
import shared.dataInput.XMLReader;
|
|
||||||
import shared.enums.XMLFileType;
|
|
||||||
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
public class App extends Application {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Entry point for running the programme
|
|
||||||
*
|
|
||||||
* @param args for starting the programme
|
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
launch(args);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void start(Stage primaryStage) {
|
|
||||||
try {
|
|
||||||
Polars boatPolars = PolarParser.parse("mock/polars/acc_polars.csv");
|
|
||||||
|
|
||||||
String regattaXML = XMLReader.readXMLFileToString("mock/mockXML/regattaTest.xml", StandardCharsets.UTF_8);
|
|
||||||
String raceXML = XMLReader.readXMLFileToString("mock/mockXML/raceTest.xml", StandardCharsets.UTF_8);
|
|
||||||
String boatXML = XMLReader.readXMLFileToString("mock/mockXML/boatTest.xml", StandardCharsets.UTF_8);
|
|
||||||
|
|
||||||
Event raceEvent = new Event(raceXML, regattaXML, boatXML, XMLFileType.Contents, boatPolars);
|
|
||||||
raceEvent.start();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
//Catch all exceptions, print, and exit.
|
|
||||||
e.printStackTrace();
|
|
||||||
System.exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -0,0 +1,214 @@
|
|||||||
|
package mock.app;
|
||||||
|
|
||||||
|
import network.Messages.Enums.XMLMessageType;
|
||||||
|
import network.Messages.LatestMessages;
|
||||||
|
import network.Messages.XMLMessage;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import visualiser.gameController.ControllerServer;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Array;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connection acceptor for multiple clients
|
||||||
|
*/
|
||||||
|
public class ConnectionAcceptor implements Runnable {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Port to expose server on.
|
||||||
|
*/
|
||||||
|
private int serverPort = 4942;
|
||||||
|
/**
|
||||||
|
* Socket used to listen for clients on.
|
||||||
|
*/
|
||||||
|
private ServerSocket serverSocket;
|
||||||
|
//mock outputs
|
||||||
|
private ArrayBlockingQueue<MockOutput> mockOutputList = new ArrayBlockingQueue<>(16, true);
|
||||||
|
//latest messages
|
||||||
|
private LatestMessages latestMessages;
|
||||||
|
//Acknowledgement number for packets
|
||||||
|
private int ackNumber = 0;
|
||||||
|
//race xml sequence number
|
||||||
|
private short raceXMLSequenceNumber;
|
||||||
|
//boat xml sequence number
|
||||||
|
private short boatXMLSequenceNumber;
|
||||||
|
//regatta xml sequence number
|
||||||
|
private short regattaXMLSequenceNumber;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connection Acceptor Constructor
|
||||||
|
* @param latestMessages Latest messages to be sent
|
||||||
|
* @throws IOException if a server socket cannot be instantiated.
|
||||||
|
*/
|
||||||
|
public ConnectionAcceptor(LatestMessages latestMessages) throws IOException {
|
||||||
|
|
||||||
|
this.latestMessages =latestMessages;
|
||||||
|
this.serverSocket = new ServerSocket(serverPort);
|
||||||
|
CheckClientConnection checkClientConnection = new CheckClientConnection(mockOutputList);
|
||||||
|
new Thread(checkClientConnection).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() throws UnknownHostException {
|
||||||
|
return InetAddress.getLocalHost().getHostAddress();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getServerPort() {
|
||||||
|
return serverPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the Acceptor
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while(true){//should be connections not filled up
|
||||||
|
try {
|
||||||
|
System.out.println("Waiting for a connection...");//TEMP DEBUG REMOVE
|
||||||
|
Socket mockSocket = serverSocket.accept();
|
||||||
|
DataOutputStream outToVisualiser = new DataOutputStream(mockSocket.getOutputStream());
|
||||||
|
MockOutput mockOutput = new MockOutput(latestMessages, outToVisualiser);
|
||||||
|
ControllerServer controllerServer = new ControllerServer(mockSocket);
|
||||||
|
new Thread(mockOutput).start();
|
||||||
|
new Thread(controllerServer).start();
|
||||||
|
mockOutputList.add(mockOutput);
|
||||||
|
System.out.println(String.format("%d number of Visualisers Connected.", mockOutputList.size()));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Nested class to remove disconnected clients
|
||||||
|
*/
|
||||||
|
class CheckClientConnection implements Runnable{
|
||||||
|
|
||||||
|
private ArrayBlockingQueue<MockOutput> mocks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
* @param mocks Mocks "connected"
|
||||||
|
*/
|
||||||
|
public CheckClientConnection(ArrayBlockingQueue<MockOutput> mocks){
|
||||||
|
this.mocks = mocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the remover.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
double timeSinceLastHeartBeat = System.currentTimeMillis();
|
||||||
|
while(true) {
|
||||||
|
//System.out.println(mocks.size());//used to see current amount of visualisers connected.
|
||||||
|
ArrayBlockingQueue<MockOutput> m = new ArrayBlockingQueue(16, true, mocks);
|
||||||
|
for (MockOutput mo : m) {
|
||||||
|
try {
|
||||||
|
mo.sendHeartBeat();
|
||||||
|
} catch (IOException e) {
|
||||||
|
mocks.remove(mo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Race XML to send.
|
||||||
|
* @param raceXml XML to send to the Client.
|
||||||
|
*/
|
||||||
|
public void setRaceXml(String raceXml) {
|
||||||
|
//Create the message.
|
||||||
|
XMLMessage message = this.createXMLMessage(raceXml, XMLMessageType.RACE);
|
||||||
|
|
||||||
|
//Place it in LatestMessages.
|
||||||
|
this.latestMessages.setRaceXMLMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Regatta XMl to send.
|
||||||
|
* @param regattaXml XML to send to Client.
|
||||||
|
*/
|
||||||
|
public void setRegattaXml(String regattaXml) {
|
||||||
|
//Create the message.
|
||||||
|
XMLMessage message = this.createXMLMessage(regattaXml, XMLMessageType.REGATTA);
|
||||||
|
|
||||||
|
//Place it in LatestMessages.
|
||||||
|
this.latestMessages.setRegattaXMLMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Boats XML to send.
|
||||||
|
* @param boatsXml XMl to send to the Client.
|
||||||
|
*/
|
||||||
|
public void setBoatsXml(String boatsXml) {
|
||||||
|
//Create the message.
|
||||||
|
XMLMessage message = this.createXMLMessage(boatsXml, XMLMessageType.BOAT);
|
||||||
|
|
||||||
|
//Place it in LatestMessages.
|
||||||
|
this.latestMessages.setBoatXMLMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an XMLMessage of a specified subtype using the xml contents string.
|
||||||
|
* @param xmlString The contents of the xml file.
|
||||||
|
* @param messageType The subtype of xml message (race, regatta, boat).
|
||||||
|
* @return The created XMLMessage object.
|
||||||
|
*/
|
||||||
|
private XMLMessage createXMLMessage(String xmlString, XMLMessageType messageType) {
|
||||||
|
|
||||||
|
//Get the correct sequence number to use, and increment it.
|
||||||
|
short sequenceNumber = 0;
|
||||||
|
if (messageType == XMLMessageType.RACE) {
|
||||||
|
sequenceNumber = this.raceXMLSequenceNumber;
|
||||||
|
this.raceXMLSequenceNumber++;
|
||||||
|
|
||||||
|
} else if (messageType == XMLMessageType.BOAT) {
|
||||||
|
sequenceNumber = this.boatXMLSequenceNumber;
|
||||||
|
this.boatXMLSequenceNumber++;
|
||||||
|
|
||||||
|
} else if (messageType == XMLMessageType.REGATTA) {
|
||||||
|
sequenceNumber = this.regattaXMLSequenceNumber;
|
||||||
|
this.regattaXMLSequenceNumber++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//Create the message.
|
||||||
|
XMLMessage message = new XMLMessage(
|
||||||
|
XMLMessage.currentVersionNumber,
|
||||||
|
getNextAckNumber(),
|
||||||
|
System.currentTimeMillis(),
|
||||||
|
messageType,
|
||||||
|
sequenceNumber,
|
||||||
|
xmlString);
|
||||||
|
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increments the ackNumber value, and returns it.
|
||||||
|
* @return Incremented ackNumber.
|
||||||
|
*/
|
||||||
|
private int getNextAckNumber(){
|
||||||
|
this.ackNumber++;
|
||||||
|
|
||||||
|
return this.ackNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
package network.MessageDecoders;
|
||||||
|
|
||||||
|
import network.Messages.Enums.BoatActionEnum;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class BoatActionDecoder {
|
||||||
|
byte byteBoatAction;
|
||||||
|
BoatActionEnum boatAction;
|
||||||
|
|
||||||
|
public BoatActionDecoder(byte[] encodedBoatAction) {
|
||||||
|
byteBoatAction = encodedBoatAction[0];
|
||||||
|
|
||||||
|
boatAction = BoatActionEnum.fromByte(byteBoatAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BoatActionEnum getBoatAction() {
|
||||||
|
return boatAction;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package network.Messages;
|
||||||
|
|
||||||
|
import network.Messages.Enums.BoatActionEnum;
|
||||||
|
import network.Messages.Enums.MessageType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by David on 10/07/2017.
|
||||||
|
*/
|
||||||
|
public class BoatAction extends AC35Data {
|
||||||
|
|
||||||
|
private byte boatAction;
|
||||||
|
|
||||||
|
public BoatAction(BoatActionEnum boatAction){
|
||||||
|
super(MessageType.BOATACTION);
|
||||||
|
this.boatAction = boatAction.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte getBoatAction() {
|
||||||
|
return boatAction;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,71 @@
|
|||||||
|
package network.Messages.Enums;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Boat actions
|
||||||
|
*/
|
||||||
|
public enum BoatActionEnum {
|
||||||
|
NOT_A_STATUS(-1),
|
||||||
|
AUTO_PILOT(1),
|
||||||
|
SAILS_IN(2),
|
||||||
|
SAILS_OUT(3),
|
||||||
|
TACK_GYBE(4),
|
||||||
|
UPWIND(5),
|
||||||
|
DOWNWIND(6);
|
||||||
|
|
||||||
|
private byte value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ctor. Creates a BoatActionEnum from a given primitive integer value, cast to a byte.
|
||||||
|
* @param value Integer, which is cast to byte, to construct from.
|
||||||
|
*/
|
||||||
|
private BoatActionEnum(int value) {
|
||||||
|
this.value = (byte) value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the primitive value of the enum.
|
||||||
|
* @return Primitive value of the enum.
|
||||||
|
*/
|
||||||
|
public byte getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stores a mapping between Byte values and BoatActionEnum values.
|
||||||
|
*/
|
||||||
|
private static final Map<Byte, BoatActionEnum> byteToStatusMap = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
Static initialization block. Initializes the byteToStatusMap.
|
||||||
|
*/
|
||||||
|
static {
|
||||||
|
for (BoatActionEnum type : BoatActionEnum.values()) {
|
||||||
|
BoatActionEnum.byteToStatusMap.put(type.value, type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the enumeration value which corresponds to a given byte value.
|
||||||
|
* @param boatActionEnum Byte value to convert to a BoatActionEnum value.
|
||||||
|
* @return The BoatActionEnum value which corresponds to the given byte value.
|
||||||
|
*/
|
||||||
|
public static BoatActionEnum fromByte(byte boatActionEnum) {
|
||||||
|
//Gets the corresponding MessageType from the map.
|
||||||
|
BoatActionEnum type = BoatActionEnum.byteToStatusMap.get(boatActionEnum);
|
||||||
|
|
||||||
|
if (type == null) {
|
||||||
|
//If the byte value wasn't found, return the NOT_A_STATUS boatActionEnum.
|
||||||
|
return BoatActionEnum.NOT_A_STATUS;
|
||||||
|
} else {
|
||||||
|
//Otherwise, return the boatActionEnum.
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,68 @@
|
|||||||
|
package visualiser.gameController;
|
||||||
|
|
||||||
|
import network.BinaryMessageEncoder;
|
||||||
|
import network.MessageEncoders.RaceVisionByteEncoder;
|
||||||
|
import network.Messages.BoatAction;
|
||||||
|
import network.Messages.Enums.BoatActionEnum;
|
||||||
|
import network.Messages.Enums.MessageType;
|
||||||
|
import visualiser.gameController.Keys.ControlKey;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic service for sending key presses to game server
|
||||||
|
*/
|
||||||
|
public class ControllerClient {
|
||||||
|
/**
|
||||||
|
* Socket to server
|
||||||
|
*/
|
||||||
|
Socket socket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Output stream wrapper for socket to server
|
||||||
|
*/
|
||||||
|
DataOutputStream outputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise controller client with live socket.
|
||||||
|
* @param socket to server
|
||||||
|
*/
|
||||||
|
public ControllerClient(Socket socket) {
|
||||||
|
this.socket = socket;
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.outputStream = new DataOutputStream(socket.getOutputStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a keypress to server
|
||||||
|
* @param key to send
|
||||||
|
* @throws IOException if socket write fails
|
||||||
|
*/
|
||||||
|
public void sendKey(ControlKey key) throws IOException {
|
||||||
|
int protocolCode = key.getProtocolCode();
|
||||||
|
if(protocolCode > -1) {
|
||||||
|
|
||||||
|
byte[] bytes = new byte[4];
|
||||||
|
ByteBuffer.wrap(bytes).putInt(key.getProtocolCode());
|
||||||
|
BoatActionEnum boatActionEnum = BoatActionEnum.fromByte(bytes[3]);
|
||||||
|
|
||||||
|
BoatAction boatAction = new BoatAction(boatActionEnum);
|
||||||
|
|
||||||
|
byte[] encodedBoatAction = RaceVisionByteEncoder.boatActionMessage(boatAction);
|
||||||
|
|
||||||
|
BinaryMessageEncoder binaryMessage = new BinaryMessageEncoder(MessageType.BOATACTION, System.currentTimeMillis(), 0,
|
||||||
|
(short) encodedBoatAction.length, encodedBoatAction);
|
||||||
|
|
||||||
|
System.out.println("Sending out key: " + boatActionEnum);
|
||||||
|
outputStream.write(binaryMessage.getFullMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,59 @@
|
|||||||
|
package visualiser.gameController;
|
||||||
|
|
||||||
|
import network.BinaryMessageDecoder;
|
||||||
|
import network.MessageDecoders.BoatActionDecoder;
|
||||||
|
import network.Messages.Enums.BoatActionEnum;
|
||||||
|
import visualiser.gameController.Keys.ControlKey;
|
||||||
|
import visualiser.gameController.Keys.KeyFactory;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service for dispatching key press data to race from client
|
||||||
|
*/
|
||||||
|
public class ControllerServer implements Runnable {
|
||||||
|
/**
|
||||||
|
* Socket to client
|
||||||
|
*/
|
||||||
|
private Socket socket;
|
||||||
|
/**
|
||||||
|
* Wrapper for input from client
|
||||||
|
*/
|
||||||
|
private DataInputStream inputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialise server-side controller with live client socket
|
||||||
|
* @param socket to client
|
||||||
|
*/
|
||||||
|
public ControllerServer(Socket socket) {
|
||||||
|
this.socket = socket;
|
||||||
|
try {
|
||||||
|
this.inputStream = new DataInputStream(this.socket.getInputStream());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wait for controller key input from client and loop.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while(true) {
|
||||||
|
byte[] message = new byte[20];
|
||||||
|
try {
|
||||||
|
if (inputStream.available() > 0) {
|
||||||
|
inputStream.read(message);
|
||||||
|
BinaryMessageDecoder encodedMessage = new BinaryMessageDecoder(message);
|
||||||
|
BoatActionDecoder boatActionDecoder = new BoatActionDecoder(encodedMessage.getMessageBody());
|
||||||
|
BoatActionEnum decodedMessage = boatActionDecoder.getBoatAction();
|
||||||
|
System.out.println("Received key: " + decodedMessage);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
package visualiser.gameController;
|
||||||
|
|
||||||
|
import javafx.animation.AnimationTimer;
|
||||||
|
import javafx.scene.Scene;
|
||||||
|
import visualiser.gameController.Keys.ControlKey;
|
||||||
|
import visualiser.gameController.Keys.KeyFactory;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import static javafx.application.Application.launch;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class for checking what keys are currently being used
|
||||||
|
*/
|
||||||
|
public class InputChecker {
|
||||||
|
private HashMap<String, Boolean> currentlyActiveKeys = new HashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller loop that detects key presses that runs parallel to the main scene.
|
||||||
|
* @param scene Scene the controller is to run in parallel with.
|
||||||
|
*/
|
||||||
|
public void runWithScene(Scene scene){
|
||||||
|
KeyFactory keyFactory = KeyFactory.getFactory();
|
||||||
|
|
||||||
|
scene.setOnKeyPressed(event -> {
|
||||||
|
String codeString = event.getCode().toString();
|
||||||
|
if (!currentlyActiveKeys.containsKey(codeString)) {
|
||||||
|
ControlKey controlKey = keyFactory.getKey(codeString);
|
||||||
|
if (controlKey != null) {
|
||||||
|
controlKey.onAction();
|
||||||
|
System.out.println(controlKey.toString() + " is Pressed.");
|
||||||
|
}
|
||||||
|
currentlyActiveKeys.put(codeString, true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
scene.setOnKeyReleased(event -> {
|
||||||
|
String codeString = event.getCode().toString();
|
||||||
|
ControlKey controlKey = keyFactory.getKey(codeString);
|
||||||
|
if (controlKey != null) {
|
||||||
|
controlKey.onRelease();
|
||||||
|
System.out.println(controlKey.toString() + " is Released.");
|
||||||
|
}
|
||||||
|
currentlyActiveKeys.remove(event.getCode().toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
new AnimationTimer() {
|
||||||
|
@Override
|
||||||
|
public void handle(long now) {
|
||||||
|
for (String key: currentlyActiveKeys.keySet()){
|
||||||
|
ControlKey controlKey = keyFactory.getKey(key);
|
||||||
|
if (controlKey != null){
|
||||||
|
controlKey.onHold();
|
||||||
|
System.out.println(controlKey.toString() + " is Held.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// for (String key : InputKeys.stringKeysMap.keySet()){
|
||||||
|
// if (removeActiveKey(key)) {
|
||||||
|
// System.out.println(key);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* removes a key from the active dictionary
|
||||||
|
* @param codeString string of the key press to remove
|
||||||
|
* @return whether or not the key has been removed or not.
|
||||||
|
*/
|
||||||
|
private boolean removeActiveKey(String codeString) {
|
||||||
|
Boolean isActive = currentlyActiveKeys.get(codeString);
|
||||||
|
|
||||||
|
if (isActive != null && isActive) {
|
||||||
|
currentlyActiveKeys.put(codeString, false);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,58 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key for the controller, part of the abstract factory KeyFactory
|
||||||
|
*/
|
||||||
|
public abstract class ControlKey {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
protected int protocolCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for key state with specified protocol code
|
||||||
|
* @param name of action
|
||||||
|
* @param protocolCode -1 if not sent
|
||||||
|
*/
|
||||||
|
public ControlKey(String name, int protocolCode) {
|
||||||
|
this.name = name;
|
||||||
|
this.protocolCode = protocolCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for key state not sent over network
|
||||||
|
* @param name name of the key
|
||||||
|
*/
|
||||||
|
public ControlKey(String name){
|
||||||
|
this.name = name;
|
||||||
|
this.protocolCode = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getProtocolCode() {
|
||||||
|
return protocolCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To String method
|
||||||
|
* @return returns the name of the key
|
||||||
|
*/
|
||||||
|
public String toString(){
|
||||||
|
return this.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What this key should do when the command is issued for it to do its job.
|
||||||
|
*/
|
||||||
|
public abstract void onAction();//may want to make it take in a visualiser and stuff in the future.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What to do when the key is held
|
||||||
|
*/
|
||||||
|
public abstract void onHold();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* What to do when the key is released.
|
||||||
|
*/
|
||||||
|
public abstract void onRelease();
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to send downwind packet to server
|
||||||
|
*/
|
||||||
|
public class DownWindKey extends ControlKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
* @param name name of the key
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public DownWindKey(String name) {
|
||||||
|
super(name, 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,51 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory for creating Keys, these could be predefined in the future.
|
||||||
|
*/
|
||||||
|
public class KeyFactory {
|
||||||
|
/**
|
||||||
|
* Retrieve command given key
|
||||||
|
*/
|
||||||
|
private Map<String, ControlKey> keyState;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton instance to enforce consistent key state
|
||||||
|
*/
|
||||||
|
private static KeyFactory theFactory = new KeyFactory();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Singleton constructor for key state, set up initial state of each action.
|
||||||
|
*/
|
||||||
|
private KeyFactory() {
|
||||||
|
this.keyState = new HashMap<>();
|
||||||
|
keyState.put("Z", new ZoomInKey("Zoom In"));
|
||||||
|
keyState.put("X", new ZoomOutKey("Zoom Out"));
|
||||||
|
keyState.put("SPACE", new VMGKey("VMG"));
|
||||||
|
keyState.put("SHIFT", new SailsToggleKey("Toggle Sails"));
|
||||||
|
keyState.put("ENTER", new TackGybeKey("Tack/Gybe"));
|
||||||
|
keyState.put("PAGE_UP", new UpWindKey("Upwind"));
|
||||||
|
keyState.put("PAGE_DOWN", new DownWindKey("Downwind"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get singleton instance of KeyFactory to interact with key state
|
||||||
|
* @return automatically constructed KeyFactory
|
||||||
|
*/
|
||||||
|
public static KeyFactory getFactory() {
|
||||||
|
return theFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the Control Key in charge of a key press
|
||||||
|
* @param key key pressed (String value of KeyCode)
|
||||||
|
* @return the Control Key behaviour of the key pressed.
|
||||||
|
*/
|
||||||
|
public ControlKey getKey(String key){
|
||||||
|
return keyState.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,34 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to toggle the sails
|
||||||
|
*/
|
||||||
|
public class SailsToggleKey extends ControlKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
* @param name name of the key
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public SailsToggleKey(String name) {
|
||||||
|
super(name, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Toggle command associated with sails key
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
protocolCode = protocolCode == 2? 3 : 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* key to toggle between tacking and gybing
|
||||||
|
*/
|
||||||
|
public class TackGybeKey extends ControlKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
* @param name name of the key
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public TackGybeKey(String name) {
|
||||||
|
super(name, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to go upwind
|
||||||
|
*/
|
||||||
|
public class UpWindKey extends ControlKey {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
* @param name name of the key
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public UpWindKey(String name) {
|
||||||
|
super(name, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,33 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to trigger auto VMG
|
||||||
|
*/
|
||||||
|
public class VMGKey extends ControlKey{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
*
|
||||||
|
* @param name name of the key
|
||||||
|
*/
|
||||||
|
public VMGKey(String name) {
|
||||||
|
super(name, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
import javafx.scene.input.KeyCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* key to zoom into the game
|
||||||
|
*/
|
||||||
|
public class ZoomInKey extends ControlKey {
|
||||||
|
|
||||||
|
public ZoomInKey(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
package visualiser.gameController.Keys;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key to zoom out of the game.
|
||||||
|
*/
|
||||||
|
public class ZoomOutKey extends ControlKey{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for Control
|
||||||
|
* @param name name of the key
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public ZoomOutKey(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onHold() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRelease() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
|
||||||
|
<BoatConfig>
|
||||||
|
<Boats>
|
||||||
|
|
||||||
|
<!--Mark Boats-->
|
||||||
|
<Boat Type="Mark" BoatName="PRO" SourceID="101" >
|
||||||
|
<GPSposition X= "-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="PIN" SourceID="102" >
|
||||||
|
<GPSposition X= "-64.855242" Y="32.293771" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="Marker1" SourceID="103" >
|
||||||
|
<GPSposition X= "-64.843983" Y="32.293039" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="WGL" SourceID="104" >
|
||||||
|
<GPSposition X= "-64.850045" Y="32.28468" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="WGR" SourceID="105" >
|
||||||
|
<GPSposition X= "-64.847591" Y="32.280164" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="LGL" SourceID="106" >
|
||||||
|
<GPSposition X= "-64.835249" Y="32.309693" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="LGR" SourceID="107" >
|
||||||
|
<GPSposition X= "-64.831785" Y="32.308046" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="FL" SourceID="108" >
|
||||||
|
<GPSposition X= "-64.839291" Y="32.317379" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat Type="Mark" BoatName="FR" SourceID="109" >
|
||||||
|
<GPSposition X= "-64.83626" Y="32.317257" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
|
||||||
|
<!--Participants-->
|
||||||
|
<Boat BoatName="Emirates Team New Zealand" HullNum="RG01" ShapeID="0" ShortName="NZL" SourceID="126" StoweName="NZL" Type="Yacht">
|
||||||
|
<GPSposition X="-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat BoatName="Land Rover BAR" HullNum="RG01" ShapeID="0" ShortName="GBR" SourceID="122" StoweName="GBR" Type="Yacht">
|
||||||
|
<GPSposition X="-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat BoatName="SoftBank Team Japan" HullNum="RG01" ShapeID="0" ShortName="JPN" SourceID="123" StoweName="JPN" Type="Yacht">
|
||||||
|
<GPSposition X="-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat BoatName="Groupama Team France" HullNum="RG01" ShapeID="0" ShortName="FRA" SourceID="124" StoweName="FRA" Type="Yacht">
|
||||||
|
<GPSposition X="-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat BoatName="Artemis Racing" HullNum="RG01" ShapeID="0" ShortName="SWE" SourceID="125" StoweName="SWE" Type="Yacht">
|
||||||
|
<GPSposition X="-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
<Boat BoatName="Emirates Team New Zealand" HullNum="RG01" ShapeID="0" ShortName="NZL" SourceID="126" StoweName="NZL" Type="Yacht">
|
||||||
|
<GPSposition X="-64.854304" Y="32.296577" Z="0"/>
|
||||||
|
</Boat>
|
||||||
|
</Boats>
|
||||||
|
</BoatConfig>
|
||||||
@ -0,0 +1,42 @@
|
|||||||
|
package visualiser.gameController;
|
||||||
|
|
||||||
|
import javafx.application.Application;
|
||||||
|
import javafx.application.Platform;
|
||||||
|
import javafx.event.EventHandler;
|
||||||
|
import javafx.scene.Scene;
|
||||||
|
import javafx.scene.layout.GridPane;
|
||||||
|
import javafx.stage.Stage;
|
||||||
|
import javafx.stage.WindowEvent;
|
||||||
|
import visualiser.gameController.InputChecker;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start to manually test the game controller
|
||||||
|
*/
|
||||||
|
public class GameControllerManualTest extends Application {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(Stage stage) throws Exception {
|
||||||
|
stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
|
||||||
|
@Override
|
||||||
|
public void handle(WindowEvent event) {
|
||||||
|
Platform.exit();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
GridPane root = new GridPane();
|
||||||
|
Scene scene = new Scene(root, 1200, 800);
|
||||||
|
|
||||||
|
InputChecker inputChecker = new InputChecker();
|
||||||
|
inputChecker.runWithScene(scene);
|
||||||
|
|
||||||
|
stage.setScene(scene);
|
||||||
|
stage.setTitle("RaceVision - Team 7 - Input Tester Manual Test");
|
||||||
|
stage.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
launch(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue