From fc00b1c8fcad0844ea166433eb2b96a338a7c520 Mon Sep 17 00:00:00 2001 From: alrz1999 Date: Mon, 8 Jul 2019 22:34:28 +0430 Subject: [PATCH] bug fixed --- src/client/ResponseHandler.java | 42 ++++++++++++++++++- .../fxmlControllers/MainMenuController.java | 10 ++--- src/server/RequestHandler.java | 11 +++++ src/server/ResponseType.java | 6 ++- .../fxmls/wrapperClasses/CardContainer.java | 11 +++-- 5 files changed, 67 insertions(+), 13 deletions(-) diff --git a/src/client/ResponseHandler.java b/src/client/ResponseHandler.java index c40c363..265d72a 100644 --- a/src/client/ResponseHandler.java +++ b/src/client/ResponseHandler.java @@ -6,9 +6,11 @@ import com.google.gson.JsonStreamParser; import controller.fxmlControllers.CollectionFxmlController; import controller.fxmlControllers.LoginPageController; +import controller.fxmlControllers.MainMenuController; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.control.Alert; +import models.Enums.ErrorType; import models.Game; import server.Response; @@ -25,6 +27,7 @@ public class ResponseHandler extends Thread { private Gson gson = new Gson(); private Response response; private CollectionFxmlController collectionController; + private MainMenuController mainMenuController; public static ResponseHandler getInstance() { return RESPONSE_HANDLER; @@ -70,12 +73,27 @@ private void handleResponse() { private void handleLoginPageResponse() { if (response.getResponseType().equals(SUCCESSFUL_SIGN_IN)) { - Platform.runLater(() -> Main.getStage().getScene().setRoot(Main.getMainMenu())); + loadMainMenu(); Main.setToken(response.getAuthToken()); } else Platform.runLater(() -> Main.getLoginPageController().appearLabel(response.getResponseType().getMessage())); } + private void loadMainMenu() { + Platform.runLater(new Runnable() { + @Override + public void run() { + FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/fxmls/mainMenu.fxml")); + mainMenuController = loader.getController(); + try { + Main.getStage().getScene().setRoot(loader.load()); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + } + private void handleCollectionResponse() { if (response.getCollection() != null) CollectionFxmlController.setCollection(response.getCollection()); @@ -122,7 +140,6 @@ public void run() { }); } - private void removeDeck(String deckToRemove) { Platform.runLater(() -> { collectionController.decks.getItems().remove(deckToRemove); @@ -133,7 +150,28 @@ private void removeDeck(String deckToRemove) { } private void handleBattleResponse() { + switch (response.getResponseType()) { + case MAIN_DECK_IS_VALID: + loadBattleMenu(); + break; + case MAIN_DECK_IS_NOT_VALID: + Platform.runLater(() -> mainMenuController.appearLabel(response.getResponseType().getMessage())); + break; + } + } + private void loadBattleMenu() { + Platform.runLater(new Runnable() { + @Override + public void run() { + FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/fxmls/battleMenu.fxml")); + try { + Main.getStage().getScene().setRoot(loader.load()); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); } private void handleShopResponse() { diff --git a/src/controller/fxmlControllers/MainMenuController.java b/src/controller/fxmlControllers/MainMenuController.java index 6bc66ab..39e6e93 100644 --- a/src/controller/fxmlControllers/MainMenuController.java +++ b/src/controller/fxmlControllers/MainMenuController.java @@ -73,11 +73,9 @@ private void exit() { } private void goToBattle() { - if (!accountController.getAccount().isReadyToPlay()) { - appearLabel(ErrorType.MAIN_DECK_IS_NOT_VALID.getMessage()); - return; - } - Game.getInstance().loadPage(exitButton, "/view/fxmls/battleMenu.fxml"); + Request request = new Request(Environment.MAIN_MENU); + request.setRequestType(RequestType.ENTER_BATTLE); + RequestSender.getInstance().sendRequest(request); } private void logout() { @@ -88,7 +86,7 @@ private void showMatchHistories() { Game.getInstance().loadPage(exitButton, "/view/fxmls/matchHistories.fxml"); } - private void appearLabel(String text) { + public void appearLabel(String text) { error.setText(text); error.setStyle("-fx-background-color: rgba(255, 212, 134, 0.48)"); error.setVisible(true); diff --git a/src/server/RequestHandler.java b/src/server/RequestHandler.java index fe191c0..c86dc24 100644 --- a/src/server/RequestHandler.java +++ b/src/server/RequestHandler.java @@ -145,9 +145,20 @@ private void handleMainMenuRequest(Request request) { case ENTER_COLLECTION: sendCollection(request); break; + case ENTER_BATTLE: + enterBattle(request); } } + private void enterBattle(Request request) { + Response response = new Response(Environment.BATTLE); + if (Main.getOnlineAccounts().get(request.getOuthToken()).isReadyToPlay()) + response.setResponseType(ResponseType.MAIN_DECK_IS_VALID); + else + response.setResponseType(ResponseType.MAIN_DECK_IS_NOT_VALID); + responseSender.sendResponse(response); + } + private void sendCollection(Request request) { Response response = new Response(Environment.COLLECTION); response.setResponseType(ResponseType.ENTER_COLLECTION); diff --git a/src/server/ResponseType.java b/src/server/ResponseType.java index 5addf5c..b734d00 100644 --- a/src/server/ResponseType.java +++ b/src/server/ResponseType.java @@ -22,7 +22,11 @@ public enum ResponseType { SUCCESSFULLY_REMOVE_CARD_FROM_DECK("Selected cards removed successfully"), MAIN_DECK_SELECTED("Main deck changed"), //shop responses - ENTER_SHOP("enter shop") + ENTER_SHOP("enter shop"), + //battle responses + MAIN_DECK_IS_NOT_VALID("selected deck is invalid"), + MAIN_DECK_IS_VALID("choose single player or multi player"), + ; private String message; diff --git a/src/view/fxmls/wrapperClasses/CardContainer.java b/src/view/fxmls/wrapperClasses/CardContainer.java index 75d167f..d46f0a6 100644 --- a/src/view/fxmls/wrapperClasses/CardContainer.java +++ b/src/view/fxmls/wrapperClasses/CardContainer.java @@ -5,6 +5,10 @@ import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.AnchorPane; +import models.Card; +import models.Placeable; +import models.Shop; +import models.Spell; import models.*; @@ -54,7 +58,7 @@ private CardContainer(String name) { healthPointLabel.setLayoutX(200); healthPointLabel.setLayoutY(214); - nameLabel = new Label(name+"\n"+c.getClass().getSimpleName()); + nameLabel = new Label(name + "\n" + c.getClass().getSimpleName()); nameLabel.setStyle("-fx-font-size: 18px;-fx-text-fill: white;-fx-text-alignment: center;-fx-text-overrun: ELLIPSIS;-fx-alignment: center;" + "-fx-pref-width: 290;-fx-pref-height: 86;-fx-font-weight: bold"); nameLabel.setLayoutX(6); @@ -67,8 +71,6 @@ private CardContainer(String name) { checkBox.setLayoutX(21); checkBox.setLayoutY(390); checkBox.setStyle("-fx-font-size: 20"); - - if (!(c instanceof Item)) { imageView = new ImageView(new Image(c.getPath())); imageView.setFitHeight(191); @@ -80,7 +82,7 @@ private CardContainer(String name) { imageView.setScaleY(1.5); } anchorPane.getChildren().addAll(manaLabel, attackPointLabel, healthPointLabel, nameLabel, checkBox, imageView); - }else { + } else { anchorPane.getChildren().addAll(manaLabel, attackPointLabel, healthPointLabel, nameLabel, checkBox); } @@ -115,3 +117,4 @@ public Placeable getCard() { } +