Skip to content

Commit

Permalink
Merge branch 'akhoonak(client)' of https://github.com/Advanced-Progra…
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-alir committed Jul 21, 2021
2 parents 00ab519 + 7918b05 commit dfa924c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 27 deletions.
18 changes: 17 additions & 1 deletion src/main/java/sample/view/gameboardview/GameBoardHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class GameBoardHandler implements Runnable{
private Stage stage;
private Communicator communicator;
private GameBoardView gameBoardView;
private boolean isRunning;

public GameBoardHandler(Stage stage){
this.stage = stage;
Expand All @@ -34,13 +35,21 @@ public void setGameBoardView(GameBoardView gameBoardView) {
this.gameBoardView = gameBoardView;
}

public boolean getIsRunning(){
return isRunning;
}

@Override
public void run() {
isRunning = true;
String message;
while (run){
message = sender.receive();
handle(message);
if (message.equals("break"))
break;
}
isRunning = false;
}

protected void handle(String message){
Expand Down Expand Up @@ -99,7 +108,14 @@ else if (message.startsWith("reduceHealth")){
} else if (message.startsWith("actions")){
strings= message.replaceAll("actions\\*", "").split("\\*");
gameBoardView.setActions(strings[0], strings[1], Integer.parseInt(strings[2]), strings[3].equalsIgnoreCase("true"));
}
} else if (message.startsWith("message"))
gameBoardView.addTextToChat(message.replaceAll("^message\\*", ""));
else if (message.equals("pause"))
gameBoardView.pause();
else if (message.equals("resume"))
gameBoardView.resume();
else if (message.equals("break"))
gameBoardView.shutdown(stage, true);
else {
handleSelecting(message);
}
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/sample/view/gameboardview/GameBoardView.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,25 +309,28 @@ private void setPause(Button button){
if (button.getText().equals("pause")) {
button.setText("resume");
pause();
opponentCommunicator.pause();
sender.send("-GPC-pause");
} else {
button.setText("pause");
resume();
opponentCommunicator.resume();
sender.send("-GPC-resume");
}
});
}

public void pause(){
blocked = true;
pauseButton.setText("resume");
anchorPane.getChildren().add(darkMode);
Platform.runLater(() -> pauseButton.setText("resume"));
if (!anchorPane.getChildren().contains(darkMode))
Platform.runLater(() -> anchorPane.getChildren().add(darkMode));
}

public void resume(){
blocked = false;
pauseButton.setText("pause");
anchorPane.getChildren().remove(darkMode);
Platform.runLater(() -> {
pauseButton.setText("pause");
anchorPane.getChildren().remove(darkMode);
});
}

private void setNextPhase(Rectangle... rectangles){
Expand Down Expand Up @@ -377,12 +380,13 @@ private void sendMessage() {
messageField.clear();
text = "\n" + myNicknameString + " : " + text;
chatTexts.appendText(text);
opponentCommunicator.sendMessage(text);
sender.send("-GPC-message*" + text);
}
}

public void addTextToChat(String message){
chatTexts.appendText(message);
if (message.startsWith("\n" + enemyNickname.getText()))
chatTexts.appendText(message);
}

private void initializeCardPlaces(HashMap<Integer, CardView> places, AnchorPane anchorPane, boolean enemy){
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/sample/view/mainMenu/MainMenuController.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void initialize(URL url, ResourceBundle resourceBundle){
logoutButton.setStyle("-fx-border-width: 0 0 0 0.5; -fx-border-color: #ff5959");
searchPlayerToPlayTextArea.setStyle("-fx-prompt-text-fill: white; -fx-text-fill: #ffff22");
allUsersTextArea.setStyle("-fx-text-fill: white");
tvButton.setStyle("-fx-border-width: 0 0 0 0.4; -fx-border-color: radial-gradient(aqua, #ff5bec)");
// tvButton.setStyle("-fx-border-width: 0 0 0 0.4; -fx-border-color: radial-gradient(aqua, #ff5bec)");
}

public void onMouseEnteredNewGameButton(MouseEvent e){
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/sample/view/mainMenu/MainMenuFxml.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<Image url="@../../../../resources/media/images/others/swordIcon.png" />
</image>
</ImageView>
<JFXButton fx:id="newGameButton" buttonType="RAISED" layoutX="76.0" layoutY="251.0" onMouseEntered="#onMouseEnteredNewGameButton" onMouseExited="#onMouseExitedNewGameButton" prefHeight="300.0" prefWidth="200.0" text="New Game" textFill="#ffff44" wrapText="true">
<JFXButton fx:id="newGameButton" onAction="#switchToGameScene" buttonType="RAISED" layoutX="76.0" layoutY="251.0" onMouseEntered="#onMouseEnteredNewGameButton" onMouseExited="#onMouseExitedNewGameButton" prefHeight="300.0" prefWidth="200.0" text="New Game" textFill="#ffff44" wrapText="true">
<font>
<Font size="25.0" />
</font>
Expand Down Expand Up @@ -216,11 +216,11 @@
<!-- </LinearGradient>-->
<!-- </ripplerFill>-->
<!-- </JFXButton>-->
<JFXButton fx:id="tvButton" buttonType="RAISED" layoutX="0" layoutY="0" onAction="#switchToTv" prefHeight="45.0" prefWidth="380.0" text="TV" textFill="#ffff44">
<font>
<Font size="25.0" />
</font>
</JFXButton>
<!-- <JFXButton fx:id="tvButton" buttonType="RAISED" layoutX="0" layoutY="0" onAction="#switchToTv" prefHeight="45.0" prefWidth="380.0" text="TV" textFill="#ffff44">-->
<!-- <font>-->
<!-- <Font size="25.0" />-->
<!-- </font>-->
<!-- </JFXButton>-->
<ImageView fx:id="leftHandImageHelper" fitHeight="202.0" fitWidth="219.0" layoutX="91.0" layoutY="461.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="rightHandImageHelper" fitHeight="320.0" fitWidth="300.0" layoutX="867.0" layoutY="436.0" pickOnBounds="true" preserveRatio="true" />
</children>
Expand Down
37 changes: 26 additions & 11 deletions src/main/java/sample/view/newduel/DuelController.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,28 @@ public void initialize(URL url, ResourceBundle resourceBundle) {

@FXML
private void startGame(ActionEvent actionEvent){
errorLabel.setText(sender.getResponse(sender.setMessageWithToken("-ND-", "startANewGame", rounds)));
GameBoardHandler gameBoardHandler = new GameBoardHandler((Stage) ((Node) actionEvent.getSource()).getScene().getWindow());
this.gameBoardHandler = gameBoardHandler;
Thread thread = new Thread(gameBoardHandler);
thread.setDaemon(true);
thread.start();
// }
if (gameBoardHandler == null) {
errorLabel.setText(sender.getResponse(sender.setMessageWithToken("-ND-", "startANewGame", rounds)));
GameBoardHandler gameBoardHandler = new GameBoardHandler((Stage) ((Node) actionEvent.getSource()).getScene().getWindow());
this.gameBoardHandler = gameBoardHandler;
Thread thread = new Thread(gameBoardHandler);
thread.setDaemon(true);
thread.start();
}
}

@FXML
private void changeRounds(ActionEvent actionEvent){
if (oneRound.isSelected())
rounds = "1";
else rounds = "3";
reset();
}

@FXML
private void back(ActionEvent actionEvent){
try {
if (gameBoardHandler != null){
gameBoardHandler.setRun(false);
}
sender.send(sender.setMessageWithToken("-ND-", "end", rounds));
reset();
Parent root = FXMLLoader.load(new File("./src/main/java/sample/view/mainMenu/MainMenuFxml.fxml").toURI().toURL());
Scene scene = new Scene(root);
Stage stage = (Stage) ((Node) actionEvent.getSource()).getScene().getWindow();
Expand All @@ -86,4 +85,20 @@ private void back(ActionEvent actionEvent){
ioException.printStackTrace();
}
}

private void reset() {
if (gameBoardHandler != null){
gameBoardHandler.setRun(false);
sender.send(sender.setMessageWithToken("-ND-", "dummy", "dummy"));
while (gameBoardHandler.getIsRunning()) {
try {
Thread.sleep(100);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
}
this.gameBoardHandler = null;
}
sender.getResponse(sender.setMessageWithToken("-ND-", "end", rounds));
}
}
1 change: 1 addition & 0 deletions src/main/java/sample/view/sender/Sender.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public String receive(){
message = dataInputStream.readUTF();
} catch (IOException ioException) {
ioException.printStackTrace();
message = "break";
}
return message;
}
Expand Down

0 comments on commit dfa924c

Please sign in to comment.