diff --git a/Client/src/main/java/view/menus/LobbyMenu.java b/Client/src/main/java/view/menus/LobbyMenu.java index 8c1682b..ef0cb1f 100644 --- a/Client/src/main/java/view/menus/LobbyMenu.java +++ b/Client/src/main/java/view/menus/LobbyMenu.java @@ -1,7 +1,6 @@ package view.menus; import com.google.gson.*; -import com.google.gson.reflect.TypeToken; import controller.ApplicationManger; import controller.DuelController; import controller.GamePlaySceneController; @@ -17,12 +16,10 @@ import javafx.scene.paint.Color; import model.UserData; import model.enums.ChatType; -import org.json.simple.JSONArray; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -36,6 +33,7 @@ public class LobbyMenu { public TextField editedMessage; public Label editedMessageLabel; public Button editButton; + public Button cancelButton; private ArrayList idOfMessage = new ArrayList<>(); private static int idCounter = 0; @@ -69,6 +67,7 @@ public void newGame() { startGame(response); } else if (type.equals("WAITING")) { this.message.setText(responseMessage); + cancelButton.setVisible(true); new Thread(() -> { while (!isGameStarted) { try { @@ -91,6 +90,9 @@ public void newGame() { if (type1.equals("SUCCESSFUL")) { isGameStarted = true; startGame(serverMessage); + } else if (type1.equals("CANCEL")) { + this.message.setText(""); + cancelButton.setVisible(false); } } catch (IOException e) { e.printStackTrace(); @@ -101,6 +103,19 @@ public void newGame() { } } + public void cancel() { + try { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("controller", "newGame"); + jsonObject.addProperty("method", "cancel"); + jsonObject.addProperty("token", ApplicationManger.getToken()); + ApplicationManger.getDataOutputStream().writeUTF(jsonObject.toString()); + ApplicationManger.getDataOutputStream().flush(); + } catch (IOException e) { + e.printStackTrace(); + } + } + private void startGame(String serverMessage) { System.out.println(serverMessage); @@ -172,7 +187,7 @@ public void sendMessage(ActionEvent actionEvent) { errorMessage.setTextFill(Color.GREEN); } messages.getChildren().remove(idOfMessage.indexOf(id)); - Label delete=new Label(); + Label delete = new Label(); delete.setPrefWidth(435); delete.setPrefHeight(50); delete.setAlignment(Pos.CENTER); @@ -265,7 +280,7 @@ public void refresh(ActionEvent actionEvent) { nicknameAndChatType.setText(" " + nickname + " - " + chatType); else nicknameAndChatType.setText(nickname); message1.setText(message); - if (chatType.equals("DELETED")){ + if (chatType.equals("DELETED")) { message1.setTextFill(Color.BLUE); } vBox.getChildren().add(0, nicknameAndChatType); diff --git a/Client/src/main/resources/FXML/Lobby.fxml b/Client/src/main/resources/FXML/Lobby.fxml index 15748e6..950275d 100644 --- a/Client/src/main/resources/FXML/Lobby.fxml +++ b/Client/src/main/resources/FXML/Lobby.fxml @@ -9,7 +9,7 @@ - + diff --git a/Server/src/main/java/controller/GameConnectionController.java b/Server/src/main/java/controller/GameConnectionController.java index 96fe46e..5494663 100644 --- a/Server/src/main/java/controller/GameConnectionController.java +++ b/Server/src/main/java/controller/GameConnectionController.java @@ -32,6 +32,9 @@ public String getServerMessage(String input) { if (methodName.equals("newGame")) { return newGame(input); } + if (methodName.equals("cancel")) { + return cancel(input); + } return serverMessage(MessageType.ERROR, "invalid method name", null); } @@ -55,6 +58,13 @@ private String newGame(String input) { return serverMessage(MessageType.WAITING, "waiting for a user to connect", null); } + private String cancel(String input) { + JsonObject jsonObject = JsonParser.parseString(input).getAsJsonObject(); + String token = jsonObject.get("token").getAsString(); + waitingGames.removeIf(e -> e.getUser1Token().equals(token)); + return serverMessage(MessageType.CANCEL, "game canceled", null); + } + public void addGameWaiter(Socket socket, String input) { JsonObject jsonObject = JsonParser.parseString(input).getAsJsonObject(); String token = jsonObject.get("token").getAsString(); diff --git a/Server/src/main/java/controller/ServerManager.java b/Server/src/main/java/controller/ServerManager.java index 89c661e..464f2d1 100644 --- a/Server/src/main/java/controller/ServerManager.java +++ b/Server/src/main/java/controller/ServerManager.java @@ -97,6 +97,7 @@ public void run() { String result; if (ServerManager.getIsInGame().get(socket)) break; if (input.equals("")) break; + if (input.equals("alive")) continue; if ((result = ServerController.checkToken(input)) == null) { serverController = ServerController.getController(input); if (serverController instanceof GameConnectionController) { diff --git a/Server/src/main/java/model/enums/MessageType.java b/Server/src/main/java/model/enums/MessageType.java index e4d6117..9544826 100644 --- a/Server/src/main/java/model/enums/MessageType.java +++ b/Server/src/main/java/model/enums/MessageType.java @@ -3,5 +3,6 @@ public enum MessageType { ERROR, SUCCESSFUL, - WAITING + WAITING, + CANCEL }