Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsen5980 committed Jul 21, 2021
2 parents fb1b42b + bc03d55 commit 638aba0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
25 changes: 20 additions & 5 deletions Client/src/main/java/view/menus/LobbyMenu.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -36,6 +33,7 @@ public class LobbyMenu {
public TextField editedMessage;
public Label editedMessageLabel;
public Button editButton;
public Button cancelButton;
private ArrayList<Integer> idOfMessage = new ArrayList<>();

private static int idCounter = 0;
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions Client/src/main/resources/FXML/Lobby.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="900.0" prefWidth="1600.0" styleClass="background1" stylesheets="@../CSS/LobbyStyle.css" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.menus.LobbyMenu">
<AnchorPane prefHeight="900.0" prefWidth="1600.0" styleClass="background1" stylesheets="@../CSS/LobbyStyle.css" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.menus.LobbyMenu">
<children>
<Button layoutX="60.0" layoutY="794.0" mnemonicParsing="false" onAction="#back" styleClass="back" text="Back" />
<Button layoutX="993.0" layoutY="410.0" mnemonicParsing="false" onAction="#newGame" styleClass="back" text="New Game">
Expand All @@ -26,11 +26,16 @@
<Font size="35.0" />
</font>
</CheckBox>
<Label fx:id="message" alignment="CENTER" layoutX="969.0" layoutY="555.0" prefHeight="50.0" prefWidth="500.0" text="" textAlignment="CENTER">
<Label fx:id="message" alignment="CENTER" layoutX="969.0" layoutY="555.0" prefHeight="50.0" prefWidth="500.0" style="-fx-background-color: rgb(191, 214, 224);" text="" textAlignment="CENTER">
<font>
<Font size="25.0" />
</font>
</Label>
<Button fx:id="cancelButton" layoutX="1120.0" layoutY="630.0" mnemonicParsing="false" onAction="#cancel" styleClass="back" text="Cancel" visible="false">
<font>
<Font size="25.0" />
</font>
</Button>
<ScrollPane layoutX="70.0" layoutY="38.0" prefHeight="453.0" prefWidth="502.0">
<content>
<AnchorPane prefHeight="472.0" prefWidth="500.0" style="-fx-background-color: rgb(191, 214, 224);">
Expand Down
10 changes: 10 additions & 0 deletions Server/src/main/java/controller/GameConnectionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions Server/src/main/java/controller/ServerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion Server/src/main/java/model/enums/MessageType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public enum MessageType {
ERROR,
SUCCESSFUL,
WAITING
WAITING,
CANCEL
}

0 comments on commit 638aba0

Please sign in to comment.