Created a new chat history class that allows for rich formatting of individual messages. Current implementation allows for bold and coloured text only. Has been manually tested. Fixed 3/5 chat command tests.

#implement #test #story[1246]
This commit is contained in:
Calum
2017-09-04 23:10:54 +12:00
parent 5026c568a7
commit 47f3f6e27d
9 changed files with 220 additions and 180 deletions
@@ -698,7 +698,6 @@ public class GameState implements Runnable {
public static void endRace () { public static void endRace () {
yachts.forEach((id, yacht) -> yacht.setBoatStatus(BoatStatus.FINISHED)); yachts.forEach((id, yacht) -> yacht.setBoatStatus(BoatStatus.FINISHED));
currentStage = GameStages.FINISHED; currentStage = GameStages.FINISHED;
System.out.println("FOR FUCKS SAKE YOU FUCKING DEGENERATE");
} }
public static void setSpeedMultiplier (double multiplier) { public static void setSpeedMultiplier (double multiplier) {
@@ -61,7 +61,6 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
//You should handle interrupts in some way, so that the thread won't keep on forever if you exit the app. //You should handle interrupts in some way, so that the thread won't keep on forever if you exit the app.
while (!terminated) { while (!terminated) {
System.out.println("CUNT GF" + GameState.getCurrentStage());
try { try {
Thread.sleep(1000 / CLIENT_UPDATES_PER_SECOND); Thread.sleep(1000 / CLIENT_UPDATES_PER_SECOND);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@@ -88,9 +87,8 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
//FINISHED //FINISHED
else if (GameState.getCurrentStage() == GameStages.FINISHED) { else if (GameState.getCurrentStage() == GameStages.FINISHED) {
broadcastMessage(makeRaceStatusMessage()); broadcastMessage(makeRaceStatusMessage());
System.out.println("BUT I WAS HERE CUNTFACE");
try { try {
Thread.sleep(100); //Hackish fix to make sure all threads have broadcasted Thread.sleep(1000); //Hackish fix to make sure all threads have sent closing RaceStatus
terminate(); terminate();
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
serverLog("Thread interrupted while waiting to terminate clients", 1); serverLog("Thread interrupted while waiting to terminate clients", 1);
@@ -102,7 +100,6 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
serverToClientThread.terminate(); serverToClientThread.terminate();
} }
serverSocket.close(); serverSocket.close();
System.out.println("closed");
} catch (IOException e) { } catch (IOException e) {
System.out.println("IO error in server thread handler upon closing socket"); System.out.println("IO error in server thread handler upon closing socket");
} }
@@ -245,7 +242,6 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
for (Player player : GameState.getPlayers()) { for (Player player : GameState.getPlayers()) {
ServerYacht y = player.getYacht(); ServerYacht y = player.getYacht();
System.out.println(y.getBoatStatus());
BoatSubMessage m = new BoatSubMessage(y.getSourceId(), y.getBoatStatus(), BoatSubMessage m = new BoatSubMessage(y.getSourceId(), y.getBoatStatus(),
y.getLegNumber(), y.getLegNumber(),
0, 0, 1234L, 0, 0, 1234L,
@@ -268,7 +264,6 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
raceStatus = RaceStatus.PREPARATORY; raceStatus = RaceStatus.PREPARATORY;
} }
} else if (GameState.getCurrentStage() == GameStages.FINISHED) { } else if (GameState.getCurrentStage() == GameStages.FINISHED) {
System.out.println("WHAT THE FUCKING FUCK");
raceStatus = RaceStatus.TERMINATED; raceStatus = RaceStatus.TERMINATED;
} else { } else {
raceStatus = RaceStatus.STARTED; raceStatus = RaceStatus.STARTED;
@@ -281,7 +276,6 @@ public class MainServerThread implements Runnable, ClientConnectionDelegate {
} }
public void terminate() { public void terminate() {
System.out.println("done");
terminated = true; terminated = true;
} }
@@ -386,9 +386,6 @@ public class ServerToClientThread implements Runnable, Observer {
Arrays.copyOfRange(chatterPayload, 3, 3 + chatterPayload.length) Arrays.copyOfRange(chatterPayload, 3, 3 + chatterPayload.length)
); );
String[] words = chatterText.split("\\s+"); String[] words = chatterText.split("\\s+");
for (String s : words) {
System.out.println(s);
}
if (words.length > 2 && isHost) { if (words.length > 2 && isHost) {
switch (words[2].trim()) { switch (words[2].trim()) {
case ">speed": case ">speed":
@@ -396,19 +393,18 @@ public class ServerToClientThread implements Runnable, Observer {
GameState.setSpeedMultiplier(Double.valueOf(words[3])); GameState.setSpeedMultiplier(Double.valueOf(words[3]));
GameState.broadcastChatter(new ChatterMessage( GameState.broadcastChatter(new ChatterMessage(
Byte.toUnsignedInt(chatterPayload[1]), Byte.toUnsignedInt(chatterPayload[1]),
words[0] + "Host has set speed modifier to x" + words[3] "SERVER: Speed modifier set to x" + words[3]
)); ));
} catch (Exception e) { } catch (Exception e) {
logger.error("cannot parse >speed value"); logger.error("cannot parse >speed value");
} }
return; return;
case ">finish": case ">finish":
System.out.println(words[2].trim());
GameState.endRace();
GameState.broadcastChatter(new ChatterMessage( GameState.broadcastChatter(new ChatterMessage(
chatterPayload[1], chatterPayload[1],
words[0] + "Host has ended the game" "SERVER: Game will now finish"
)); ));
GameState.endRace();
return; return;
} }
} }
@@ -26,7 +26,6 @@ import javafx.scene.control.Button;
import javafx.scene.control.CheckBox; import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox; import javafx.scene.control.ComboBox;
import javafx.scene.control.Slider; import javafx.scene.control.Slider;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
@@ -50,6 +49,7 @@ import seng302.visualiser.controllers.annotations.ImportantAnnotationController;
import seng302.visualiser.controllers.annotations.ImportantAnnotationDelegate; import seng302.visualiser.controllers.annotations.ImportantAnnotationDelegate;
import seng302.visualiser.controllers.annotations.ImportantAnnotationsState; import seng302.visualiser.controllers.annotations.ImportantAnnotationsState;
import seng302.visualiser.fxObjects.BoatObject; import seng302.visualiser.fxObjects.BoatObject;
import seng302.visualiser.fxObjects.ChatHistory;
/** /**
* Controller class that manages the display of a race * Controller class that manages the display of a race
@@ -63,7 +63,7 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
@FXML @FXML
private Button chatSend; private Button chatSend;
@FXML @FXML
private TextArea chatHistory; private Pane chatHistoryHolder;
@FXML @FXML
private TextField chatInput; private TextField chatInput;
@FXML @FXML
@@ -98,6 +98,8 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
private GameView gameView; private GameView gameView;
private RaceState raceState; private RaceState raceState;
private ChatHistory chatHistory;
private Timeline timerTimeline; private Timeline timerTimeline;
private Timer timer = new Timer(); private Timer timer = new Timer();
private List<Series<String, Double>> sparkLineData = new ArrayList<>(); private List<Series<String, Double>> sparkLineData = new ArrayList<>();
@@ -122,9 +124,19 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
chatInput.setText(chatInput.getText().substring(0, CHAT_LIMIT)); chatInput.setText(chatInput.getText().substring(0, CHAT_LIMIT));
} }
}); });
chatHistory.textProperty().addListener((obs, oldValue, newValue) -> { chatHistory = new ChatHistory();
chatHistory.setScrollTop(Double.MAX_VALUE); chatHistoryHolder.getChildren().addAll(chatHistory);
}); chatHistory.prefWidthProperty().bind(
chatHistoryHolder.widthProperty()
);
chatHistory.prefHeightProperty().bind(
chatHistoryHolder.heightProperty()
);
// chatHistory.setFitToWidth(true);
// chatHistory.setFitToHeight(true);
// chatHistory.textProperty().addListener((obs, oldValue, newValue) -> {
// chatHistory.setScrollTop(Double.MAX_VALUE);
// });
} }
public void loadRace ( public void loadRace (
@@ -136,12 +148,6 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
this.markers = raceData.getCompoundMarks(); this.markers = raceData.getCompoundMarks();
this.raceState = raceState; this.raceState = raceState;
initializeUpdateTimer();
initialiseFPSCheckBox();
initialiseAnnotationSlider();
initialiseBoatSelectionComboBox();
initialiseSparkLine();
raceState.getPlayerPositions().addListener((ListChangeListener<ClientYacht>) c -> { raceState.getPlayerPositions().addListener((ListChangeListener<ClientYacht>) c -> {
while (c.next()) { while (c.next()) {
if (c.wasPermutated()) { if (c.wasPermutated()) {
@@ -181,6 +187,12 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
gameView.enableZoom(); gameView.enableZoom();
} }
}); });
initializeUpdateTimer();
initialiseFPSCheckBox();
initialiseAnnotationSlider();
initialiseBoatSelectionComboBox();
initialiseSparkLine();
} }
/** /**
@@ -665,7 +677,8 @@ public class RaceViewController extends Thread implements ImportantAnnotationDel
} }
public void updateChatHistory(Paint playerColour, String newMessage) { public void updateChatHistory(Paint playerColour, String newMessage) {
Platform.runLater(() -> chatHistory.appendText(newMessage + "\n\n")); // Platform.runLater(() -> chatHistory.appendText(newMessage + "\n\n"));
Platform.runLater(() -> chatHistory.addMessage(playerColour, newMessage));
} }
} }
@@ -10,8 +10,6 @@ import javafx.fxml.FXML;
import javafx.fxml.Initializable; import javafx.fxml.Initializable;
import javafx.scene.control.TextField; import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane; import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import seng302.gameServer.GameState;
import seng302.visualiser.GameClient; import seng302.visualiser.GameClient;
/** /**
@@ -23,111 +21,32 @@ public class StartScreenController implements Initializable {
@FXML @FXML
private TextField ipTextField; private TextField ipTextField;
@FXML @FXML
private TextField portTextField;
@FXML
private GridPane startScreen2;
@FXML
private AnchorPane holder; private AnchorPane holder;
GameClient gameClient; private GameClient gameClient;
public void initialize(URL url, ResourceBundle resourceBundle) { public void initialize(URL url, ResourceBundle resourceBundle) {
// gameClient = new GameClient(holder);
}
//
// /**
// * Loads the fxml content into the parent pane
// * @param jfxUrl
// * @return the controller of the fxml
// */
// private Object setContentPane(String jfxUrl) {
// try {
// AnchorPane contentPane = (AnchorPane) startScreen2.getParent();
// contentPane.getChildren().removeAll();
// contentPane.getChildren().clear();
// contentPane.getStylesheets().add(getClass().getResource("/css/master.css").toString());
// FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(jfxUrl));
// contentPane.getChildren().addAll((Pane) fxmlLoader.load());
//
// return fxmlLoader.getController();
// } catch (IOException e) {
// e.printStackTrace();
// }
// return null;
// }
}
/** /**
* ATTEMPTS TO: * Creates an instance of GameClient and runs it as a host.
* Sets up a new game state with your IP address as designated as the host.
* Starts a thread to listen for incoming connections.
* Starts a client to server thread and connects to own ip.
* Switches to the lobby screen
*/ */
@FXML @FXML
public void hostButtonPressed() { public void hostButtonPressed() {
// new GameState(getLocalHostIp());
gameClient = new GameClient(holder); gameClient = new GameClient(holder);
gameClient.runAsHost(getLocalHostIp(), 4942); gameClient.runAsHost(getLocalHostIp(), 4942);
// try {
//// String ipAddress = InetAddress.getLocalHost().getHostAddress();
//// new GameState(ipAddress);
//// new MainServerThread();
//// ClientToServerThread clientToServerThread = new ClientToServerThread("localhost", 4950);
//// controller.setClientToServerThread(clientToServerThread);
// // get the lobby controller so that we can pass the game server thread to it
// new GameState(getLocalHostIp());
// MainServerThread mainServerThread = new MainServerThread();
//// ClientState.setHost(true);
// // host will connect and handshake to itself after setting up the server
// // TODO: 24/07/17 wmu16 - Make port number some static global type constant?
//// ClientToServerThread clientToServerThread = new ClientToServerThread(ClientState.getHostIp(), 4942);
//// ClientState.setConnectedToHost(true);
//// controller.setClientToServerThread(clientToServerThread);
// LobbyController lobbyController = (LobbyController) setContentPane("/views/LobbyView.fxml");
// lobbyController.setMainServerThread(mainServerThread);
// } catch (Exception e) {
// Alert alert = new Alert(AlertType.ERROR);
// alert.setHeaderText("Cannot host");
// alert.setContentText("Oops, failed to host, try to restart.");
// alert.showAndWait();
// e.printStackTrace();
// }
} }
/** /**
* ATTEMPTS TO: * Creates an instance of GameClient and runs it has a client.
* Connect to an ip address and port using the ip and port specified on start screen.
* Starts a Client To Server Thread to maintain connection to host.
* Switch view to lobby view.
*/ */
@FXML @FXML
public void connectButtonPressed() { public void connectButtonPressed() {
// TODO: 10/07/17 wmu16 - Finish function
gameClient = new GameClient(holder); gameClient = new GameClient(holder);
gameClient.runAsClient(ipTextField.getText().trim().toLowerCase(), 4942); gameClient.runAsClient(ipTextField.getText().trim().toLowerCase(), 4942);
// try {
// String ipAddress = ipTextField.getText().trim().toLowerCase();
// Integer port = Integer.valueOf(portTextField.getText().trim());
//
//// ClientToServerThread clientToServerThread = new ClientToServerThread(ipAddress, port);
//// ClientState.setHost(false);
//// ClientState.setConnectedToHost(true);
//
//// controller.setClientToServerThread(clientToServerThread);
//// setContentPane("/views/LobbyView.fxml");
// } catch (Exception e) {
// Alert alert = new Alert(AlertType.ERROR);
// alert.setHeaderText("Cannot reach the host");
// alert.setContentText("Please check your host IP address.");
// alert.showAndWait();
// }
} }
// public void setController(Controller controller) {
// this.controller = controller;
// }
/** /**
* Gets the local host ip address and sets this ip to ClientState. * Gets the local host ip address and sets this ip to ClientState.
@@ -162,7 +81,6 @@ public class StartScreenController implements Initializable {
if (ipAddress == null) { if (ipAddress == null) {
System.out.println("[HOST] Cannot obtain local host ip address."); System.out.println("[HOST] Cannot obtain local host ip address.");
} }
// ClientState.setHostIp(ipAddress);
return ipAddress; return ipAddress;
} }
} }
@@ -0,0 +1,59 @@
package seng302.visualiser.fxObjects;
import java.util.Arrays;
import javafx.collections.ListChangeListener;
import javafx.scene.Node;
import javafx.scene.control.ScrollPane;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
/**
* Extension of a ScrollPane that contains a TextFlow. Has an addMessage() function to parse and
* display chatter text.
*/
public class ChatHistory extends ScrollPane {
private TextFlow textFlow = new TextFlow();
public ChatHistory() {
this.setContent(textFlow);
this.setFitToWidth(true);
this.setFitToHeight(true);
this.setMaxHeight(Double.MAX_VALUE);
this.setMaxWidth(Double.MAX_VALUE);
this.setVbarPolicy(ScrollBarPolicy.ALWAYS);
this.setHbarPolicy(ScrollBarPolicy.NEVER);
textFlow.getChildren().addListener((ListChangeListener<Node>) c -> {
this.setVvalue(1.0);
});
}
/**
* Adds a message to chat history. Messages should be either of the form:
* "[HH:MM:ss] \<player_name\>: \<message_text\>" or
* "SERVER: \<message_text\>"
* @param colour The colour of the user sending the message
* @param Text The chatter text message to be displayed
*/
public void addMessage (Paint colour, String Text) {
String[] words = Text.split(":");
if (words[0].trim().equals("SERVER")) {
Text text = new Text(Text + "\n\n");
text.setStyle("-fx-font-weight: bolder");
textFlow.getChildren().add(text);
} else {
Text timePlayer = new Text(
String.join(":", Arrays.copyOfRange(words, 0, 3)) + ":"
);
timePlayer.setStyle("-fx-font-weight: bold");
timePlayer.setFill(colour);
Text message = new Text(
String.join(":", Arrays.copyOfRange(words, 3, words.length)) + "\n\n"
);
message.wrappingWidthProperty().bind(this.widthProperty());
textFlow.getChildren().addAll(timePlayer, message);
}
}
}
+13 -32
View File
@@ -9,47 +9,29 @@
<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 fx:id="basePane" layoutX="322.0" layoutY="130.0" prefHeight="998.0" <AnchorPane fx:id="basePane" layoutX="322.0" layoutY="130.0" prefHeight="998.0" prefWidth="1281.0" style="-fx-background-color: skyblue;" AnchorPane.bottomAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
prefWidth="1281.0" style="-fx-background-color: skyblue;" AnchorPane.bottomAnchor="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" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
<columnConstraints> <columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="630.0" minWidth="10.0" <ColumnConstraints hgrow="SOMETIMES" maxWidth="630.0" minWidth="10.0" prefWidth="68.0" />
prefWidth="68.0"/> <ColumnConstraints hgrow="SOMETIMES" maxWidth="1213.0" minWidth="10.0" prefWidth="1213.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="1213.0" minWidth="10.0"
prefWidth="1213.0"/>
</columnConstraints> </columnConstraints>
<rowConstraints> <rowConstraints>
<RowConstraints maxHeight="489.0" minHeight="1.0" prefHeight="24.0" <RowConstraints maxHeight="489.0" minHeight="1.0" prefHeight="24.0" vgrow="SOMETIMES" />
vgrow="SOMETIMES"/> <RowConstraints maxHeight="997.0" minHeight="10.0" prefHeight="974.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="997.0" minHeight="10.0" prefHeight="974.0"
vgrow="SOMETIMES"/>
</rowConstraints> </rowConstraints>
<children> <children>
<AnchorPane fx:id="contentAnchorPane" prefHeight="200.0" prefWidth="200.0" <AnchorPane fx:id="contentAnchorPane" prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="2" GridPane.rowSpan="2">
GridPane.columnSpan="2" GridPane.rowSpan="2">
<children> <children>
<AnchorPane layoutX="799.0" layoutY="770.0" prefHeight="214.0" <AnchorPane layoutX="799.0" layoutY="770.0" prefHeight="214.0" prefWidth="468.0">
prefWidth="468.0">
<children> <children>
<VBox prefHeight="214.0" prefWidth="468.0" <VBox prefHeight="214.0" prefWidth="468.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children> <children>
<TextArea fx:id="chatHistory" editable="false" <Pane fx:id="chatHistoryHolder" maxHeight="9.9999999999E10" maxWidth="1.7976931348623157E308" prefHeight="9.9999999999E10" />
focusTraversable="false" wrapText="true"
VBox.vgrow="ALWAYS"/>
<HBox VBox.vgrow="NEVER"> <HBox VBox.vgrow="NEVER">
<children> <children>
<TextField fx:id="chatInput" <TextField fx:id="chatInput" focusTraversable="false" prefHeight="25.0" HBox.hgrow="ALWAYS" />
focusTraversable="false" prefHeight="25.0" <Button fx:id="chatSend" focusTraversable="false" mnemonicParsing="false" text="Send" />
HBox.hgrow="ALWAYS"/>
<Button fx:id="chatSend"
focusTraversable="false"
mnemonicParsing="false" text="Send"/>
</children> </children>
</HBox> </HBox>
</children> </children>
@@ -58,8 +40,7 @@
</AnchorPane> </AnchorPane>
</children> </children>
</AnchorPane> </AnchorPane>
<Text fx:id="fpsDisplay" strokeType="OUTSIDE" strokeWidth="0.0" text="60 FPS" <Text fx:id="fpsDisplay" strokeType="OUTSIDE" strokeWidth="0.0" text="60 FPS" GridPane.halignment="CENTER" GridPane.valignment="CENTER" />
GridPane.halignment="CENTER" GridPane.valignment="CENTER"/>
</children> </children>
</GridPane> </GridPane>
</children> </children>
@@ -0,0 +1 @@
Feature: Sending chatter packets over the web.
@@ -19,25 +19,28 @@ public class ChatCommandsTest {
// @Rule // @Rule
// public Timeout globalTimeout = new Timeout(3, TimeUnit.SECONDS); // public Timeout globalTimeout = new Timeout(3, TimeUnit.SECONDS);
private boolean dcSent = false;
private ClientToServerThread client;
private ClientToServerThread host;
private MainServerThread mst;
@Test @Test
public void sendFinishAsHost () { public void sendFinishAsHost () {
try { try {
final MainServerThread mst = new MainServerThread(); dcSent = false;
final ClientToServerThread host = new ClientToServerThread("localhost", 4942); new GameState("localhost");
mst = new MainServerThread();
host = new ClientToServerThread("localhost", 4942);
host.addStreamObserver(() -> { host.addStreamObserver(() -> {
while (host.getPacketQueue().peek() != null) { while (host.getPacketQueue().peek() != null) {
StreamPacket packet = host.getPacketQueue().poll(); StreamPacket packet = host.getPacketQueue().poll();
switch (packet.getType()) { switch (packet.getType()) {
case RACE_STATUS: case RACE_STATUS:
RaceStatusData rsd = StreamParser.extractRaceStatus(packet); RaceStatusData rsd = StreamParser.extractRaceStatus(packet);
System.out.println("yas");
System.out.println(rsd.getBoatData().get(0)[4]);
if (rsd.getBoatData().get(0)[4] == BoatStatus.FINISHED.getCode()) { if (rsd.getBoatData().get(0)[4] == BoatStatus.FINISHED.getCode()) {
System.out.println("why tho");
System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAA");
mst.terminate(); mst.terminate();
host.setSocketToClose(); // host.setSocketToClose();
Assert.assertTrue(true); Assert.assertTrue(dcSent);
} }
break; break;
default: default:
@@ -45,13 +48,27 @@ public class ChatCommandsTest {
} }
} }
}); });
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
mst.startGame(); mst.startGame();
try { try {
Thread.sleep(1000); Thread.sleep(100);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
ie.printStackTrace(); ie.printStackTrace();
} }
host.sendChatterMessage("[time_prefix] <name_prefix> >finish"); host.sendChatterMessage("[time_prefix] <name_prefix> >finish");
dcSent = true;
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
host = null;
client = null;
mst = null;
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); ioe.printStackTrace();
} }
@@ -59,76 +76,126 @@ public class ChatCommandsTest {
@Test @Test
public void sendSpeedAsHostValid () { public void sendSpeedAsHostValid () {
MainServerThread mst = new MainServerThread(); new GameState("localhost");
ClientToServerThread host = null; mst = new MainServerThread();
host = null;
try { try {
host = new ClientToServerThread("localhost", 4942); host = new ClientToServerThread("localhost", 4942);
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); ioe.printStackTrace();
} }
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
mst.startGame(); mst.startGame();
host.sendChatterMessage("[time_prefix] <name_prefix> >speed 5.0"); host.sendChatterMessage("[time_prefix] <name_prefix> >speed 5.0");
try { try {
Thread.sleep(1000); Thread.sleep(100);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
ie.printStackTrace(); ie.printStackTrace();
} }
mst.terminate();
host.setSocketToClose();
Assert.assertEquals(5.0, GameState.getSpeedMultiplier(), 0.00001); Assert.assertEquals(5.0, GameState.getSpeedMultiplier(), 0.00001);
mst.terminate();
// host.setSocketToClose();
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
host = null;
client = null;
mst = null;
} }
@Test public void sendSpeedAsHostInvalid () { @Test
MainServerThread mst = new MainServerThread(); public void sendSpeedAsHostInvalid () {
ClientToServerThread host = null; new GameState("localhost");
mst = new MainServerThread();
host = null;
try { try {
host = new ClientToServerThread("localhost", 4942); host = new ClientToServerThread("localhost", 4942);
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); ioe.printStackTrace();
} }
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
mst.startGame(); mst.startGame();
host.sendChatterMessage("[time_prefix] <name_prefix> >speed fdgdgdfg"); host.sendChatterMessage("[time_prefix] <name_prefix> >speed fdgdgdfg");
try { try {
Thread.sleep(1000); Thread.sleep(100);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
ie.printStackTrace(); ie.printStackTrace();
} }
mst.terminate(); mst.terminate();
host.setSocketToClose(); // host.setSocketToClose();
Assert.assertEquals(1.0, GameState.getSpeedMultiplier(), 0.00001); Assert.assertEquals(1.0, GameState.getSpeedMultiplier(), 0.00001);
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
} }
@Test @Test
public void sendFinishAsClient () { public void sendCommandAsClient () {
MainServerThread mst = new MainServerThread(); new GameState("localhost");
ClientToServerThread host = null; mst = new MainServerThread();
ClientToServerThread client = null; host = null;
client = null;
try { try {
host = new ClientToServerThread("localhost", 4942); host = new ClientToServerThread("localhost", 4942);
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
client = new ClientToServerThread("localhost", 4942); client = new ClientToServerThread("localhost", 4942);
System.out.println("done client and host assigning");
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); ioe.printStackTrace();
} }
mst.startGame();
client.sendChatterMessage("[time_prefix] <name_prefix> >speed 5.0");
try { try {
Thread.sleep(1000); Thread.sleep(100);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
ie.printStackTrace(); ie.printStackTrace();
} }
mst.startGame();
try {
Thread.sleep(200);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
client.sendChatterMessage("[time_prefix] <name_prefix> >speed 5.0");
try {
Thread.sleep(200);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
Assert.assertEquals(1.0, GameState.getSpeedMultiplier(), 0.00001);
mst.terminate(); mst.terminate();
host.setSocketToClose(); host.setSocketToClose();
client.setSocketToClose(); client.setSocketToClose();
Assert.assertEquals(1.0, GameState.getSpeedMultiplier(), 0.00001); try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
} }
@Test @Test
public void receiveFinishedAsClient () { public void receiveFinishedAsClient () {
MainServerThread mst = new MainServerThread(); new GameState("localhost");
ClientToServerThread host = null; dcSent = false;
mst = new MainServerThread();
host = null;
try { try {
host = new ClientToServerThread("localhost", 4942); host = new ClientToServerThread("localhost", 4942);
ClientToServerThread client = new ClientToServerThread("localhost", 4942); client = new ClientToServerThread("localhost", 4942);
client.addStreamObserver(() -> { client.addStreamObserver(() -> {
while (client.getPacketQueue().peek() != null) { while (client.getPacketQueue().peek() != null) {
StreamPacket packet = client.getPacketQueue().poll(); StreamPacket packet = client.getPacketQueue().poll();
@@ -137,8 +204,9 @@ public class ChatCommandsTest {
RaceStatusData rsd = StreamParser.extractRaceStatus(packet); RaceStatusData rsd = StreamParser.extractRaceStatus(packet);
if (rsd.getBoatData().get(0)[4] == BoatStatus.FINISHED.getCode()) { if (rsd.getBoatData().get(0)[4] == BoatStatus.FINISHED.getCode()) {
mst.terminate(); mst.terminate();
client.setSocketToClose(); // client.setSocketToClose();
Assert.assertTrue(true); // host.setSocketToClose();
Assert.assertTrue(dcSent);
} }
break; break;
default: default:
@@ -149,18 +217,29 @@ public class ChatCommandsTest {
} catch (IOException ioe) { } catch (IOException ioe) {
ioe.printStackTrace(); ioe.printStackTrace();
} }
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
mst.startGame(); mst.startGame();
try { try {
Thread.sleep(1000); Thread.sleep(100);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
ie.printStackTrace(); ie.printStackTrace();
} }
host.sendChatterMessage("[time_prefix] <name_prefix> >finish"); host.sendChatterMessage("[time_prefix] <name_prefix> >finish");
dcSent = true;
try { try {
Thread.sleep(1000); Thread.sleep(200);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
// host.setSocketToClose();
try {
Thread.sleep(2000);
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
ie.printStackTrace(); ie.printStackTrace();
} }
host.setSocketToClose();
} }
} }