From 43a1fbcb645541c0aa107f93b6ee479b28e20924 Mon Sep 17 00:00:00 2001 From: fjc40 Date: Thu, 14 Sep 2017 14:33:21 +1200 Subject: [PATCH 01/13] Spectate mode can be selected by clients when they join a game. #story[1194] --- .../Controllers/HostGameController.java | 3 +- .../Controllers/InGameLobbyController.java | 10 +++- .../Controllers/LobbyController.java | 28 +++++++++- .../main/java/visualiser/model/ThisBoat.java | 16 +++++- .../resources/visualiser/scenes/lobby.fxml | 55 ++++++++++--------- 5 files changed, 79 insertions(+), 33 deletions(-) diff --git a/racevisionGame/src/main/java/visualiser/Controllers/HostGameController.java b/racevisionGame/src/main/java/visualiser/Controllers/HostGameController.java index 2e267031..97340012 100644 --- a/racevisionGame/src/main/java/visualiser/Controllers/HostGameController.java +++ b/racevisionGame/src/main/java/visualiser/Controllers/HostGameController.java @@ -8,6 +8,7 @@ import javafx.scene.image.Image; import javafx.scene.image.ImageView; import mock.app.Event; import mock.exceptions.EventConstructionException; +import network.Messages.Enums.RequestToJoinEnum; import visualiser.app.App; import visualiser.app.MatchBrowserSingleton; import visualiser.network.MatchBrowserInterface; @@ -95,7 +96,7 @@ public class HostGameController extends Controller { public void connectSocket(String address, int port) throws IOException { Socket socket = new Socket(address, port); InGameLobbyController iglc = (InGameLobbyController)loadScene("gameLobby.fxml"); - iglc.enterGameLobby(socket, true); + iglc.enterGameLobby(socket, true, RequestToJoinEnum.PARTICIPANT);//TODO may want to let the host spectate if they wish. } /** diff --git a/racevisionGame/src/main/java/visualiser/Controllers/InGameLobbyController.java b/racevisionGame/src/main/java/visualiser/Controllers/InGameLobbyController.java index 3366b790..a0394869 100644 --- a/racevisionGame/src/main/java/visualiser/Controllers/InGameLobbyController.java +++ b/racevisionGame/src/main/java/visualiser/Controllers/InGameLobbyController.java @@ -262,10 +262,16 @@ public class InGameLobbyController extends Controller { }.start(); } - public void enterGameLobby(Socket socket, boolean isHost){ + /** + * Joins a game and enters the game lobby for it. + * @param socket Socket to connect to. + * @param isHost Whether this client is the host. + * @param joinType How the client wishes to join (e.g., participant). + */ + public void enterGameLobby(Socket socket, boolean isHost, RequestToJoinEnum joinType){ try { - this.visualiserRaceEvent = new VisualiserRaceEvent(socket, RequestToJoinEnum.PARTICIPANT); + this.visualiserRaceEvent = new VisualiserRaceEvent(socket, joinType); this.isHost = isHost; this.controllerClient = visualiserRaceEvent.getControllerClient(); diff --git a/racevisionGame/src/main/java/visualiser/Controllers/LobbyController.java b/racevisionGame/src/main/java/visualiser/Controllers/LobbyController.java index 159af2e7..e7d45b70 100644 --- a/racevisionGame/src/main/java/visualiser/Controllers/LobbyController.java +++ b/racevisionGame/src/main/java/visualiser/Controllers/LobbyController.java @@ -7,6 +7,7 @@ import javafx.scene.control.Button; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextField; +import network.Messages.Enums.RequestToJoinEnum; import network.Messages.HostGame; import visualiser.app.MatchBrowserSingleton; import visualiser.model.RaceConnection; @@ -28,6 +29,7 @@ public class LobbyController extends Controller { private @FXML TableColumn hostNameColumn; private @FXML TableColumn statusColumn; private @FXML Button joinGameBtn; + private @FXML Button spectateButton; private @FXML TextField addressFld; private @FXML TextField portFld; @@ -53,11 +55,14 @@ public class LobbyController extends Controller { lobbyTable.getSelectionModel().selectedItemProperty().addListener((obs, prev, curr) -> { if (curr != null && curr.statusProperty().getValue().equals("Ready")) { joinGameBtn.setDisable(false); + spectateButton.setDisable(false); } else { joinGameBtn.setDisable(true); + spectateButton.setDisable(true); } }); joinGameBtn.setDisable(true); + spectateButton.setDisable(true); this.udpSocket = MatchBrowserSingleton.getInstance().getUdpSocket(); receiveMatchData(); @@ -74,21 +79,40 @@ public class LobbyController extends Controller { try { if (lobbyTable.getSelectionModel().getSelectedItem().statusProperty().getValue().equals("Ready")) { joinGameBtn.setDisable(false); + spectateButton.setDisable(false); } else { joinGameBtn.setDisable(true); + spectateButton.setDisable(true); } } catch (Exception ignored){} } /** * Connect to a connection. + * @param joinType How the client wishes to join (e.g., participant). * @throws IOException socket error */ - public void connectSocket() throws IOException { + public void connectSocket(RequestToJoinEnum joinType) throws IOException { RaceConnection connection = lobbyTable.getSelectionModel().getSelectedItem(); Socket socket = new Socket(connection.getHostname(), connection.getPort()); InGameLobbyController iglc = (InGameLobbyController)loadScene("gameLobby.fxml"); - iglc.enterGameLobby(socket, false); + iglc.enterGameLobby(socket, false, joinType); + } + + /** + * Requests to join the game as a participant. + * @throws IOException socket error. + */ + public void connectParticipate() throws IOException { + connectSocket(RequestToJoinEnum.PARTICIPANT); + } + + /** + * Requests to join the game as a spectator. + * @throws IOException socket error. + */ + public void connectSpectate() throws IOException { + connectSocket(RequestToJoinEnum.SPECTATOR); } public void menuBtnPressed() throws IOException { diff --git a/racevisionGame/src/main/java/visualiser/model/ThisBoat.java b/racevisionGame/src/main/java/visualiser/model/ThisBoat.java index 91396902..920dcbd9 100644 --- a/racevisionGame/src/main/java/visualiser/model/ThisBoat.java +++ b/racevisionGame/src/main/java/visualiser/model/ThisBoat.java @@ -14,15 +14,25 @@ public class ThisBoat { } public void setSailsOut(boolean sailsOut) { - this.boat.setSailsOut(sailsOut); + if (this.boat != null) { + this.boat.setSailsOut(sailsOut); + } } public boolean isSailsOut() { - return this.boat.isSailsOut(); + if (this.boat != null) { + return this.boat.isSailsOut(); + } else { + return true;//TODO junk value to allow the client to spectate. + } } public int getSourceID() { - return this.boat.getSourceID(); + if (this.boat != null) { + return this.boat.getSourceID(); + } else { + return 0;//TODO junk value to allow the client to spectate. + } } public void setBoat(VisualiserBoat boat) { diff --git a/racevisionGame/src/main/resources/visualiser/scenes/lobby.fxml b/racevisionGame/src/main/resources/visualiser/scenes/lobby.fxml index b9314691..6e02b4cf 100644 --- a/racevisionGame/src/main/resources/visualiser/scenes/lobby.fxml +++ b/racevisionGame/src/main/resources/visualiser/scenes/lobby.fxml @@ -10,6 +10,7 @@ + @@ -40,54 +41,58 @@ - + - - - - + + + + + + - + + - + + + + +