sending chatter packets to server #story[1246]

This commit is contained in:
Peter Galloway
2017-08-30 19:14:47 +12:00
parent 75155fe481
commit 353dd48829
5 changed files with 63 additions and 13 deletions
@@ -11,9 +11,11 @@ public class ChatterMessage extends Message {
private int message_size = 21; private int message_size = 21;
private String message; private String message;
public ChatterMessage(int message_type, int message_size, String message) { public ChatterMessage(int message_type, String message) {
byte[] byteMessage = message.getBytes();
this.message_type = message_type; this.message_type = message_type;
this.message_size = message_size; this.message_size = byteMessage.length;
this.message = message; this.message = message;
setHeader(new Header(MessageType.CHATTER_TEXT, 1, (short) getSize())); setHeader(new Header(MessageType.CHATTER_TEXT, 1, (short) getSize()));
@@ -23,7 +25,7 @@ public class ChatterMessage extends Message {
putByte((byte) MESSAGE_VERSION_NUMBER); putByte((byte) MESSAGE_VERSION_NUMBER);
putInt(message_type, 1); putInt(message_type, 1);
putInt(message_size, 1); putInt(message_size, 1);
putBytes(message.getBytes()); putBytes(byteMessage);
writeCRC(); writeCRC();
rewind(); rewind();
@@ -18,6 +18,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import seng302.gameServer.messages.BoatAction; import seng302.gameServer.messages.BoatAction;
import seng302.gameServer.messages.BoatActionMessage; import seng302.gameServer.messages.BoatActionMessage;
import seng302.gameServer.messages.ChatterMessage;
import seng302.gameServer.messages.ClientType; import seng302.gameServer.messages.ClientType;
import seng302.gameServer.messages.CustomizeRequestMessage; import seng302.gameServer.messages.CustomizeRequestMessage;
import seng302.gameServer.messages.CustomizeRequestType; import seng302.gameServer.messages.CustomizeRequestType;
@@ -283,9 +284,17 @@ public class ClientToServerThread implements Runnable {
* @param message The given message type. * @param message The given message type.
*/ */
private void sendBoatActionMessage(BoatActionMessage message) { private void sendBoatActionMessage(BoatActionMessage message) {
sendByteBuffer(message.getBuffer());
}
public void sendChatterMessage(String message) {
sendByteBuffer(new ChatterMessage(5, message).getBuffer());
}
private void sendByteBuffer(byte[] bytes) {
if (clientId != -1) { if (clientId != -1) {
try { try {
os.write(message.getBuffer()); os.write(bytes);
} catch (IOException e) { } catch (IOException e) {
logger.warn("IOException on attempting to sendBoatAction from Client"); logger.warn("IOException on attempting to sendBoatAction from Client");
notifyDisconnectListeners("Cannot communicate with server"); notifyDisconnectListeners("Cannot communicate with server");
@@ -12,6 +12,7 @@ import javafx.fxml.FXMLLoader;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.control.Alert; import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType; import javafx.scene.control.Alert.AlertType;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent; import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import seng302.gameServer.GameState; import seng302.gameServer.GameState;
@@ -195,8 +196,14 @@ public class GameClient {
raceView = fxmlLoader.getController(); raceView = fxmlLoader.getController();
ClientYacht player = allBoatsMap.get(socketThread.getClientId()); ClientYacht player = allBoatsMap.get(socketThread.getClientId());
raceView.loadRace(allBoatsMap, courseData, raceState, player); raceView.loadRace(allBoatsMap, courseData, raceState, player);
raceView.getSendPressedProperty().addListener((obs, old, isPressed) -> {
if (isPressed) {
socketThread.sendChatterMessage(raceView.readChatInput());
}
});
} }
private void loadFinishScreenView() { private void loadFinishScreenView() {
FXMLLoader fxmlLoader = loadFXMLToHolder("/views/FinishScreenView.fxml"); FXMLLoader fxmlLoader = loadFXMLToHolder("/views/FinishScreenView.fxml");
FinishScreenViewController controller = fxmlLoader.getController(); FinishScreenViewController controller = fxmlLoader.getController();
@@ -373,6 +380,12 @@ public class GameClient {
* @param e The key event triggering this call * @param e The key event triggering this call
*/ */
private void keyPressed(KeyEvent e) { private void keyPressed(KeyEvent e) {
if (raceView.isChatInputFocused()) {
if (e.getCode() == KeyCode.ENTER) {
socketThread.sendChatterMessage(raceView.readChatInput());
}
return;
}
switch (e.getCode()) { switch (e.getCode()) {
case SPACE: // align with vmg case SPACE: // align with vmg
socketThread.sendBoatAction(BoatAction.VMG); break; socketThread.sendBoatAction(BoatAction.VMG); break;
@@ -381,12 +394,16 @@ public class GameClient {
case PAGE_DOWN: // downwind case PAGE_DOWN: // downwind
socketThread.sendBoatAction(BoatAction.DOWNWIND); break; socketThread.sendBoatAction(BoatAction.DOWNWIND); break;
case ENTER: // tack/gybe case ENTER: // tack/gybe
// if chat box is active take whatever is in there and send it to server
socketThread.sendBoatAction(BoatAction.TACK_GYBE); break; socketThread.sendBoatAction(BoatAction.TACK_GYBE); break;
} }
} }
private void keyReleased(KeyEvent e) { private void keyReleased(KeyEvent e) {
if (raceView.isChatInputFocused()) {
return;
}
switch (e.getCode()) { 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) //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 case SHIFT: // sails in/sails out
@@ -9,6 +9,8 @@ import java.util.TimerTask;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javafx.animation.Timeline; import javafx.animation.Timeline;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.collections.FXCollections; import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener; import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
@@ -55,6 +57,8 @@ import seng302.visualiser.fxObjects.BoatObject;
*/ */
public class RaceViewController extends Thread implements ImportantAnnotationDelegate { public class RaceViewController extends Thread implements ImportantAnnotationDelegate {
@FXML
private Pane basePane;
@FXML @FXML
private Button chatSend; private Button chatSend;
@FXML @FXML
@@ -631,8 +635,24 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
gameView.updateBorder(raceData.getCourseLimit()); gameView.updateBorder(raceData.getCourseLimit());
} }
@FXML public ReadOnlyBooleanProperty getSendPressedProperty() {
public void onSendAction() { return chatSend.pressedProperty();
chatHistory.setText(chatHistory.getText() + chatInput.getText() + '\n');
} }
public boolean isChatInputFocused() {
return chatInput.focusedProperty().getValue();
}
public String readChatInput() {
String chat = chatInput.getText();
chatInput.clear();
basePane.requestFocus();
return chat;
}
// @FXML
// public void onSendAction() {
//
// chatHistory.setText(chatHistory.getText() + chatInput.getText() + '\n');
// }
} }
+8 -6
View File
@@ -26,8 +26,8 @@
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="998.0" prefWidth="1530.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.visualiser.controllers.RaceViewController"> <AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="998.0" prefWidth="1530.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="seng302.visualiser.controllers.RaceViewController">
<children> <children>
<AnchorPane layoutX="322.0" layoutY="130.0" prefHeight="998.0" prefWidth="1281.0" <AnchorPane fx:id="basePane" layoutX="322.0" layoutY="130.0" prefHeight="998.0"
style="-fx-background-color: skyblue;" AnchorPane.bottomAnchor="0.0" prefWidth="1281.0" style="-fx-background-color: skyblue;" AnchorPane.bottomAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children> <children>
<GridPane prefHeight="998.0" prefWidth="1281.0" AnchorPane.bottomAnchor="0.0" <GridPane prefHeight="998.0" prefWidth="1281.0" AnchorPane.bottomAnchor="0.0"
@@ -56,14 +56,16 @@
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children> <children>
<TextArea fx:id="chatHistory" VBox.vgrow="ALWAYS"/> <TextArea fx:id="chatHistory" editable="false"
focusTraversable="false" VBox.vgrow="ALWAYS"/>
<HBox VBox.vgrow="NEVER"> <HBox VBox.vgrow="NEVER">
<children> <children>
<TextField fx:id="chatInput" <TextField fx:id="chatInput"
prefHeight="25.0" HBox.hgrow="ALWAYS"/> focusTraversable="false" prefHeight="25.0"
HBox.hgrow="ALWAYS"/>
<Button fx:id="chatSend" <Button fx:id="chatSend"
mnemonicParsing="false" focusTraversable="false"
onAction="#onSendAction" text="Send"/> mnemonicParsing="false" text="Send"/>
</children> </children>
</HBox> </HBox>
</children> </children>