From b5ba5daddcede18086d76d6ce5375b0ae579acbc Mon Sep 17 00:00:00 2001 From: AhmadZafarani Date: Mon, 8 Jul 2019 23:45:51 +0430 Subject: [PATCH] shop finished --- src/client/ResponseHandler.java | 115 +++++++++++------- .../fxmlControllers/CardInShopController.java | 22 ++-- .../fxmlControllers/CustomCardController.java | 2 +- .../fxmlControllers/MainMenuController.java | 17 --- .../fxmlControllers/ShopFxmlController.java | 46 +++---- .../logicController/AccountController.java | 3 - .../logicController/ShopController.java | 70 ----------- src/models/Shop.java | 4 + src/server/Request.java | 57 ++++++--- src/server/RequestHandler.java | 64 +++++++++- src/server/RequestType.java | 5 +- src/server/Response.java | 81 ++++++++---- src/server/ResponseType.java | 6 +- src/view/fxmls/shop.fxml | 2 +- .../fxmls/wrapperClasses/CardContainer.java | 71 ++++++----- 15 files changed, 316 insertions(+), 249 deletions(-) delete mode 100644 src/controller/logicController/ShopController.java diff --git a/src/client/ResponseHandler.java b/src/client/ResponseHandler.java index c40c363..5ef966b 100644 --- a/src/client/ResponseHandler.java +++ b/src/client/ResponseHandler.java @@ -6,18 +6,24 @@ import com.google.gson.JsonStreamParser; import controller.fxmlControllers.CollectionFxmlController; import controller.fxmlControllers.LoginPageController; +import controller.fxmlControllers.ShopFxmlController; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.control.Alert; -import models.Game; +import javafx.scene.control.Label; +import models.*; import server.Response; +import server.ResponseType; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.Timer; +import java.util.TimerTask; -import static server.ResponseType.SUCCESSFUL_SIGN_IN; +import static models.Enums.ErrorType.NO_ERROR; +import static server.ResponseType.*; public class ResponseHandler extends Thread { private static ResponseHandler RESPONSE_HANDLER = new ResponseHandler(); @@ -25,6 +31,7 @@ public class ResponseHandler extends Thread { private Gson gson = new Gson(); private Response response; private CollectionFxmlController collectionController; + private ShopFxmlController shopFxmlController; public static ResponseHandler getInstance() { return RESPONSE_HANDLER; @@ -73,36 +80,26 @@ private void handleLoginPageResponse() { Platform.runLater(() -> Main.getStage().getScene().setRoot(Main.getMainMenu())); Main.setToken(response.getAuthToken()); } else - Platform.runLater(() -> Main.getLoginPageController().appearLabel(response.getResponseType().getMessage())); + Platform.runLater(() -> Main.getLoginPageController().appearLabel(((ResponseType) response.getResponseType()).getMessage())); } private void handleCollectionResponse() { if (response.getCollection() != null) CollectionFxmlController.setCollection(response.getCollection()); - switch (response.getResponseType()) { - case CREATE_DECK_SUCCESSFULLY: - Platform.runLater(() -> collectionController.decks.getItems().add(response.getDeckToAdd())); - break; - case SUCCESSFULLY_REMOVE_DECK: - removeDeck(response.getDeckToRemove()); - break; - case DUPLICATE_DECK: - Platform.runLater(() -> collectionController.makeAlert("Error while making deck", "This name was used before!", Alert.AlertType.ERROR)); - break; - case MORE_THAN_ONE_HERO_ERROR: - case MORE_THAN_20_NORMAL_CARD_ERROR: - case MORE_THAN_ONE_ITEM_ERROR: - Platform.runLater(() -> collectionController.makeAlert("Error while adding cards to deck", response.getResponseType().getMessage(), Alert.AlertType.ERROR)); - break; - case SUCCESSFULLY_MOVE_CARD_TO_DECK: - case SUCCESSFULLY_REMOVE_CARD_FROM_DECK: - Platform.runLater(() -> collectionController.updateDeckCards()); - break; - case MAIN_DECK_SELECTED: - Platform.runLater(() -> collectionController.makeAlert("new main deck selected", null, Alert.AlertType.INFORMATION)); - break; - case ENTER_COLLECTION: - loadCollection(); + if (CREATE_DECK_SUCCESSFULLY.equals(response.getResponseType())) { + Platform.runLater(() -> collectionController.decks.getItems().add(response.getDeckToAdd())); + } else if (SUCCESSFULLY_REMOVE_DECK.equals(response.getResponseType())) { + removeDeck(response.getDeckToRemove()); + } else if (DUPLICATE_DECK.equals(response.getResponseType())) { + Platform.runLater(() -> collectionController.makeAlert("Error while making deck", "This name was used before!", Alert.AlertType.ERROR)); + } else if (MORE_THAN_ONE_HERO_ERROR.equals(response.getResponseType()) || MORE_THAN_20_NORMAL_CARD_ERROR.equals(response.getResponseType()) || MORE_THAN_ONE_ITEM_ERROR.equals(response.getResponseType())) { + Platform.runLater(() -> collectionController.makeAlert("Error while adding cards to deck", ((ResponseType) response.getResponseType()).getMessage(), Alert.AlertType.ERROR)); + } else if (SUCCESSFULLY_MOVE_CARD_TO_DECK.equals(response.getResponseType()) || SUCCESSFULLY_REMOVE_CARD_FROM_DECK.equals(response.getResponseType())) { + Platform.runLater(() -> collectionController.updateDeckCards()); + } else if (MAIN_DECK_SELECTED.equals(response.getResponseType())) { + Platform.runLater(() -> collectionController.makeAlert("new main deck selected", null, Alert.AlertType.INFORMATION)); + } else if (ENTER_COLLECTION.equals(response.getResponseType())) { + loadCollection(); } } @@ -122,7 +119,6 @@ public void run() { }); } - private void removeDeck(String deckToRemove) { Platform.runLater(() -> { collectionController.decks.getItems().remove(deckToRemove); @@ -137,9 +133,42 @@ private void handleBattleResponse() { } private void handleShopResponse() { - switch (response.getResponseType()) { - case ENTER_SHOP: - Platform.runLater(this::initializeShop); + if (ACCOUNT_MONEY.equals(response.getResponseType())) { + Platform.runLater(() -> shopFxmlController.money.setText(String.valueOf(response.getMoney()))); + } else if (SEARCH_IN_SHOP.equals(response.getResponseType())) { + Platform.runLater(() -> shopFxmlController.shop.setVvalue(response.getvValue())); + } else if (GET_SHOP_CARDS.equals(response.getResponseType())) { + Platform.runLater(this::getShopCards); + } else if (SUCCESSFULL_SELL.equals(response.getResponseType())) { + Platform.runLater(this::sold); + } else if (NO_ERROR.equals(response.getResponseType())) { + Platform.runLater(this::bought); + } else Platform.runLater(() -> viewMessage(((ResponseType) response.getResponseType()).getMessage())); + } + + private void bought() { + Placeable cardToBuy = response.getCardToBuy(); + viewMessage("you bought \n" + cardToBuy.getName()); + shopFxmlController.fillPanes(cardToBuy, shopFxmlController.collectionPane, false); + shopFxmlController.money.setText(String.valueOf(response.getMoney())); + } + + private void sold() { + viewMessage("you sold\n" + response.getCardToSell()); + shopFxmlController.collectionPane.getChildren().remove(response.getPaneToRemove()); + shopFxmlController.money.setText(String.valueOf(response.getMoney())); + } + + private void getShopCards() { + for (Placeable c : response.getShopCards()) { + shopFxmlController.fillPanes(c, shopFxmlController.pane, true); + } + Collection collection = response.getCollection(); + for (Placeable p : collection.getCollectionCards()) { + System.out.println(p instanceof Card); + } + for (Placeable c : collection.getCollectionCards()) { + shopFxmlController.fillPanes(c, shopFxmlController.collectionPane, false); } } @@ -151,16 +180,20 @@ public void setCollectionController(CollectionFxmlController collectionControlle this.collectionController = collectionController; } - private void initializeShop() { -// ShopFxmlController controller = MainMenuController.getShopFxmlController(); -// controller.money.setText(String.valueOf(shopController.getShop().getAccount().getMoney())); -// shopController.setShopFxmlController(this); -// controller.back.setOnAction(actionEvent -> Game.getInstance().loadPage(controller.back, "/view/fxmls/mainMenu.fxml")); -// controller.craftGraphics(); -// controller.search.setOnKeyPressed(actionEvent -> { -// if (actionEvent.getCode() == KeyCode.ENTER) -// controller.searchInShop(); -// }); + public void setShopFxmlController(ShopFxmlController shopFxmlController) { + this.shopFxmlController = shopFxmlController; + } + + private void viewMessage(String message) { + Label message1 = shopFxmlController.message; + message1.setText(message); + message1.setVisible(true); + new Timer().schedule(new TimerTask() { + @Override + public void run() { + message1.setVisible(false); + } + }, 1000); } } diff --git a/src/controller/fxmlControllers/CardInShopController.java b/src/controller/fxmlControllers/CardInShopController.java index a30f2f5..0fea583 100644 --- a/src/controller/fxmlControllers/CardInShopController.java +++ b/src/controller/fxmlControllers/CardInShopController.java @@ -1,18 +1,19 @@ package controller.fxmlControllers; -import controller.logicController.ShopController; -import javafx.fxml.FXML; +import client.RequestSender; import javafx.fxml.Initializable; import javafx.scene.control.Label; import javafx.scene.image.ImageView; import javafx.scene.layout.AnchorPane; +import server.Environment; +import server.Request; +import server.RequestType; import java.net.URL; import java.util.ResourceBundle; public class CardInShopController implements Initializable { - @FXML public AnchorPane pane; public Label mana; public Label AP; @@ -23,12 +24,17 @@ public class CardInShopController implements Initializable { @Override public void initialize(URL url, ResourceBundle resourceBundle) { - ShopController shopController = ShopController.getInstance(); + Request request = new Request(Environment.SHOP); pane.setOnMouseClicked(mouseEvent -> { - if (buy) - shopController.buyCard(name.getText()); - else - shopController.sellCard(name.getText(), pane); + if (buy) { + request.setRequestType(RequestType.BUY); + request.setCardToBuy(name.getText()); + } else { + request.setRequestType(RequestType.SELL); + request.setCardToSell(name.getText()); + request.setPaneToSell(pane); + } + RequestSender.getInstance().sendRequest(request); }); } diff --git a/src/controller/fxmlControllers/CustomCardController.java b/src/controller/fxmlControllers/CustomCardController.java index 8da7dd0..b3d3632 100644 --- a/src/controller/fxmlControllers/CustomCardController.java +++ b/src/controller/fxmlControllers/CustomCardController.java @@ -31,7 +31,7 @@ public class CustomCardController implements Initializable { public Button back; public Label message; public ComboBox specialPower; - private Shop shop = Shop.getInstance(); + private Shop shop = new Shop(); @Override public void initialize(URL url, ResourceBundle resourceBundle) { diff --git a/src/controller/fxmlControllers/MainMenuController.java b/src/controller/fxmlControllers/MainMenuController.java index 6bc66ab..38d0f6c 100644 --- a/src/controller/fxmlControllers/MainMenuController.java +++ b/src/controller/fxmlControllers/MainMenuController.java @@ -3,7 +3,6 @@ import Main.Main; import client.RequestSender; import controller.logicController.AccountController; -import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.Label; @@ -13,7 +12,6 @@ import server.Request; import server.RequestType; -import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; @@ -28,7 +26,6 @@ public class MainMenuController implements Initializable { public Label error; public Button customCard; private AccountController accountController = AccountController.getInstance(); - private static ShopFxmlController shopFxmlController = new ShopFxmlController(); @Override public void initialize(URL location, ResourceBundle resources) { @@ -49,16 +46,6 @@ private void goToCollection() { } private void goToShop() { - Request request = new Request(Environment.MAIN_MENU); - request.setRequestType(RequestType.ENTER_SHOP); - RequestSender.getInstance().sendRequest(request); - FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/fxmls/shop.fxml")); - loader.setController(shopFxmlController); - try { - exitButton.getScene().setRoot(loader.load()); - } catch (IOException e) { - e.printStackTrace(); - } Game.getInstance().loadPage(exitButton, "/view/fxmls/shop.fxml"); } @@ -99,8 +86,4 @@ private void createCustomCard() { Game.getInstance().loadPage(error, "/view/fxmls/customCard.fxml"); } - public static ShopFxmlController getShopFxmlController() { - return shopFxmlController; - } - } diff --git a/src/controller/fxmlControllers/ShopFxmlController.java b/src/controller/fxmlControllers/ShopFxmlController.java index 444a356..daa584e 100644 --- a/src/controller/fxmlControllers/ShopFxmlController.java +++ b/src/controller/fxmlControllers/ShopFxmlController.java @@ -1,6 +1,7 @@ package controller.fxmlControllers; -import controller.logicController.ShopController; +import client.RequestSender; +import client.ResponseHandler; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable; import javafx.scene.Node; @@ -13,9 +14,11 @@ import javafx.scene.input.KeyCode; import javafx.scene.layout.*; import models.*; +import server.Environment; +import server.Request; +import server.RequestType; import java.net.URL; -import java.util.ArrayList; import java.util.ResourceBundle; public class ShopFxmlController implements Initializable { @@ -27,16 +30,13 @@ public class ShopFxmlController implements Initializable { public Label message; public ScrollPane shop; public TextField search; - private ShopController shopController = ShopController.getInstance(); @Override public void initialize(URL location, ResourceBundle resources) { - try { - money.setText(String.valueOf(shopController.getShop().getAccount().getMoney())); - } catch (NullPointerException e) { - - } - shopController.setShopFxmlController(this); + ResponseHandler.getInstance().setShopFxmlController(this); + Request request = new Request(Environment.SHOP); + request.setRequestType(RequestType.ACCOUNT_MONEY); + RequestSender.getInstance().sendRequest(request); back.setOnAction(actionEvent -> Game.getInstance().loadPage(back, "/view/fxmls/mainMenu.fxml")); craftGraphics(); search.setOnKeyPressed(actionEvent -> { @@ -46,30 +46,16 @@ public void initialize(URL location, ResourceBundle resources) { } private void searchInShop() { - Placeable p = shopController.getShop().getCard(search.getText()); - if (p != null) { - ArrayList cards = shopController.getShop().getCards(); - double x = (double) (cards.indexOf(p) + 1) / cards.size(); - shop.setVvalue(x); - } + Request request = new Request(Environment.SHOP); + request.setRequestType(RequestType.SEARCH_IN_SHOP); + request.setSearchedString(search.getText()); + RequestSender.getInstance().sendRequest(request); } private void craftGraphics() { - Shop shop = Shop.getInstance(); - for (Placeable c : shop.getCards()) { - fillPanes(c, pane, true); - } - Account account = shop.getAccount(); - if (account == null) { - return; - } - Collection collection = account.getCollection(); - for (Placeable p : collection.getCollectionCards()) { - System.out.println(p instanceof Card); - } - for (Placeable c : collection.getCollectionCards()) { - fillPanes(c, collectionPane, false); - } + Request request = new Request(Environment.SHOP); + request.setRequestType(RequestType.GET_SHOP_CARDS); + RequestSender.getInstance().sendRequest(request); } public void fillPanes(Placeable c, FlowPane flowPane, boolean buy) { diff --git a/src/controller/logicController/AccountController.java b/src/controller/logicController/AccountController.java index ce17344..143712c 100644 --- a/src/controller/logicController/AccountController.java +++ b/src/controller/logicController/AccountController.java @@ -135,9 +135,6 @@ private boolean savedBefore(Account[] accounts) { } //---------------------------------------------------------------------- - public void enterShop() { - ShopController.getInstance().getShop().setAccount(account); - } public void setAccount(Account account) { this.account = account; diff --git a/src/controller/logicController/ShopController.java b/src/controller/logicController/ShopController.java deleted file mode 100644 index 02d7dee..0000000 --- a/src/controller/logicController/ShopController.java +++ /dev/null @@ -1,70 +0,0 @@ -package controller.logicController; - -import controller.fxmlControllers.ShopFxmlController; -import javafx.scene.control.Label; -import javafx.scene.layout.AnchorPane; -import models.Enums.ErrorType; -import models.Shop; - -import java.util.Timer; -import java.util.TimerTask; - -import static models.Enums.ErrorType.NO_ERROR; - -public class ShopController { - private static ShopController shopController = new ShopController(); - private Shop shop = Shop.getInstance(); - private ShopFxmlController shopFxmlController; - - private ShopController() { - } - - public static ShopController getInstance() { - return shopController; - } - - public void setShopFxmlController(ShopFxmlController shopFxmlController) { - this.shopFxmlController = shopFxmlController; - } - - public void sellCard(String cardName, AnchorPane pane) { - cardName = cardName.split("\n")[0]; - boolean isDone = shop.sell(shop.getAccount().getCollection().getCardIDInCollection(cardName)); - if (isDone) { - viewMessage("you sold\n" + cardName); - shopFxmlController.collectionPane.getChildren().remove(pane); - } else - viewMessage("sell failed!!!"); - shopFxmlController.money.setText(String.valueOf(shop.getAccount().getMoney())); - } - - public void buyCard(String cardName) { - cardName = cardName.split("\n")[0]; - ErrorType error = shop.buy(cardName); - if (error != NO_ERROR) { - viewMessage(error.getMessage()); - } else { - viewMessage("you bought \n" + cardName); - shopFxmlController.fillPanes(shop.getCard(cardName), shopFxmlController.collectionPane, - false); - } - shopFxmlController.money.setText(String.valueOf(shop.getAccount().getMoney())); - } - - private void viewMessage(String message) { - Label message1 = shopFxmlController.message; - message1.setText(message); - message1.setVisible(true); - new Timer().schedule(new TimerTask() { - @Override - public void run() { - message1.setVisible(false); - } - }, 1000); - } - - public Shop getShop() { - return shop; - } - -} diff --git a/src/models/Shop.java b/src/models/Shop.java index c010e2b..eac5aab 100644 --- a/src/models/Shop.java +++ b/src/models/Shop.java @@ -56,6 +56,9 @@ private Shop() { this.cards = cards; } + /** + * use this method as server's shop, but get new instances for clients + */ public static Shop getInstance() { return shop; } @@ -87,6 +90,7 @@ public ErrorType buy(String cardName) { } else { account.getCollection().addCardToCollection(card); account.decreaseMoney(card.getCost()); + remainingCard.put(card.getName(), remainingCard.get(card.getName()) - 1); return ErrorType.NO_ERROR; } } else { diff --git a/src/server/Request.java b/src/server/Request.java index b7bfad8..10ec0eb 100644 --- a/src/server/Request.java +++ b/src/server/Request.java @@ -1,5 +1,7 @@ package server; +import javafx.scene.layout.AnchorPane; + import java.util.ArrayList; public class Request { @@ -8,8 +10,8 @@ public class Request { private String outhToken; private String username; private String password; - private ArrayList cardsToSell; - private ArrayList cardsToBuy; + private String cardToSell; + private String cardToBuy; private String deckToAdd; private String mainDeck; private String deckToRemove; @@ -20,6 +22,8 @@ public class Request { private String importedDeck; private ArrayList cardsToRemoveFromDeck; private ArrayList cardsToAddToDeck; + private String searchedString; + private AnchorPane paneToSell; public Request(Environment environment) { this.environment = environment; @@ -45,22 +49,6 @@ public void setOuthToken(String outhToken) { this.outhToken = outhToken; } - public ArrayList getCardsToSell() { - return cardsToSell; - } - - public void setCardsToSell(ArrayList cardsToSell) { - this.cardsToSell = cardsToSell; - } - - public ArrayList getCardsToBuy() { - return cardsToBuy; - } - - public void setCardsToBuy(ArrayList cardsToBuy) { - this.cardsToBuy = cardsToBuy; - } - public String getMainDeck() { return mainDeck; } @@ -156,4 +144,37 @@ public String getImportedDeck() { public void setImportedDeck(String importedDeck) { this.importedDeck = importedDeck; } + + public String getSearchedString() { + return searchedString; + } + + public void setSearchedString(String searchedString) { + this.searchedString = searchedString; + } + + public String getCardToBuy() { + return cardToBuy; + } + + public void setCardToBuy(String cardToBuy) { + this.cardToBuy = cardToBuy; + } + + public String getCardToSell() { + return cardToSell; + } + + public void setCardToSell(String cardToSell) { + this.cardToSell = cardToSell; + } + + public AnchorPane getPaneToSell() { + return paneToSell; + } + + public void setPaneToSell(AnchorPane paneToSell) { + this.paneToSell = paneToSell; + } + } diff --git a/src/server/RequestHandler.java b/src/server/RequestHandler.java index fe191c0..c096628 100644 --- a/src/server/RequestHandler.java +++ b/src/server/RequestHandler.java @@ -5,13 +5,14 @@ import controller.logicController.CollectionController; import controller.logicController.AccountController; import controller.logicController.GameController; -import models.Collection; -import models.Game; +import models.*; +import models.Enums.ErrorType; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.Socket; +import java.util.ArrayList; public class RequestHandler extends Thread { private JsonStreamParser parser; @@ -44,7 +45,7 @@ public void run() { private void handleRequest(Request request) { switch (request.getEnvironment()) { case SHOP: - handleShopRequest(); + handleShopRequest(request); break; case BATTLE: handleBattleRequest(); @@ -117,7 +118,13 @@ private void handleCollectionRequest(Request request) { case EXPORT_DECK: controller.exportDeck(request.getExportedDeck(), responseSender); break; - + case GET_CARD: + Placeable placeable = Shop.getInstance().getCard(request.getCardToBuy()); + Response response = new Response(Environment.COLLECTION); + response.setResponseType(RequestType.GET_CARD); + response.setCardToBuy(placeable); + responseSender.sendResponse(response); + break; } } @@ -126,8 +133,55 @@ private void handleBattleRequest() { } - private void handleShopRequest() { + private void handleShopRequest(Request request) { + Response response = new Response(Environment.SHOP); + Shop shop = Shop.getInstance(); + Account account = Main.getOnlineAccounts().get(request.getOuthToken()); + switch (request.getRequestType()) { + case ACCOUNT_MONEY: + response.setMoney(account.getMoney()); + response.setResponseType(ResponseType.ACCOUNT_MONEY); + break; + case SEARCH_IN_SHOP: + searchInShop(request, response, shop); + break; + case GET_SHOP_CARDS: + getShopCards(request, response, shop); + break; + case BUY: + String cardName = request.getCardToBuy().split("\n")[0]; + ErrorType error = shop.buy(cardName); + response.setResponseType(error); + response.setMoney(account.getMoney()); + response.setCardToBuy(shop.getCard(cardName)); + break; + case SELL: + cardName = request.getCardToSell().split("\n")[0]; + if (shop.sell(account.getCollection().getCardIDInCollection(cardName))) { + response.setResponseType(ResponseType.SUCCESSFULL_SELL); + response.setMoney(account.getMoney()); + response.setPaneToRemove(request.getPaneToSell()); + response.setCardToSell(cardName); + } + } + responseSender.sendResponse(response); + } + private void getShopCards(Request request, Response response, Shop instance) { + response.setShopCards(instance.getCards()); + response.setResponseType(ResponseType.GET_SHOP_CARDS); + response.setCollection(Main.getOnlineAccounts().get(request.getOuthToken()).getCollection()); + responseSender.sendResponse(response); + } + + private void searchInShop(Request request, Response response, Shop instance) { + Placeable p = instance.getCard(request.getSearchedString()); + if (p != null) { + ArrayList cards = instance.getCards(); + double x = (double) (cards.indexOf(p) + 1) / cards.size(); + response.setResponseType(ResponseType.SEARCH_IN_SHOP); + response.setvValue(x); + } } private void handleLeaderboardRequest() { diff --git a/src/server/RequestType.java b/src/server/RequestType.java index 86a2917..77af1f0 100644 --- a/src/server/RequestType.java +++ b/src/server/RequestType.java @@ -4,7 +4,6 @@ public enum RequestType { SIGN_UP, SIGN_IN, SAVE, - ENTER_SHOP, LOG_OUT, BUY, SELL, @@ -13,12 +12,16 @@ public enum RequestType { REMOVE_CARD_FROM_DECK, ADD_CARD_TO_DECK, REMOVE_DECK, + GET_SHOP_CARDS, ENTER_BATTLE, + SEARCH_IN_SHOP, SHOW_LEADER_BOARD, ENTER_COLLECTION, + ACCOUNT_MONEY, //close the client connection with server CLOSE_CONNECTION, IMPORT_DECK, EXPORT_DECK, + GET_CARD } diff --git a/src/server/Response.java b/src/server/Response.java index 3404587..f87728a 100644 --- a/src/server/Response.java +++ b/src/server/Response.java @@ -1,15 +1,18 @@ package server; +import javafx.scene.layout.AnchorPane; +import models.Card; import models.Collection; +import models.Placeable; import java.util.ArrayList; import java.util.HashMap; public class Response { - private ResponseType responseType; + private Enum responseType; private Environment environment; - private ArrayList cardsToSell; - private ArrayList cardsToBuy; + private String cardToSell; + private Placeable cardToBuy; private String mainDeck; private String deckToRemove; private String deckToRemoveCardFrom; @@ -20,12 +23,16 @@ public class Response { private String authToken; private HashMap remainingCardsInShop; private Collection collection; + private int money; + private double vValue; + private ArrayList shopCards; + private AnchorPane paneToRemove; public Response(Environment environment) { this.environment = environment; } - public ResponseType getResponseType() { + public Enum getResponseType() { return responseType; } @@ -33,7 +40,7 @@ public Environment getEnvironment() { return environment; } - public void setResponseType(ResponseType responseType) { + public void setResponseType(Enum responseType) { this.responseType = responseType; } @@ -49,22 +56,6 @@ public void setEnvironment(Environment environment) { this.environment = environment; } - public ArrayList getCardsToSell() { - return cardsToSell; - } - - public void setCardsToSell(ArrayList cardsToSell) { - this.cardsToSell = cardsToSell; - } - - public ArrayList getCardsToBuy() { - return cardsToBuy; - } - - public void setCardsToBuy(ArrayList cardsToBuy) { - this.cardsToBuy = cardsToBuy; - } - public String getMainDeck() { return mainDeck; } @@ -137,4 +128,52 @@ public void setCollection(Collection collection) { this.collection = collection; } + public int getMoney() { + return money; + } + + public void setMoney(int money) { + this.money = money; + } + + public double getvValue() { + return vValue; + } + + public void setvValue(double vValue) { + this.vValue = vValue; + } + + public ArrayList getShopCards() { + return shopCards; + } + + public void setShopCards(ArrayList shopCards) { + this.shopCards = shopCards; + } + + public AnchorPane getPaneToRemove() { + return paneToRemove; + } + + public void setPaneToRemove(AnchorPane paneToRemove) { + this.paneToRemove = paneToRemove; + } + + public String getCardToSell() { + return cardToSell; + } + + public void setCardToSell(String cardToSell) { + this.cardToSell = cardToSell; + } + + public Placeable getCardToBuy() { + return cardToBuy; + } + + public void setCardToBuy(Placeable cardToBuy) { + this.cardToBuy = cardToBuy; + } + } diff --git a/src/server/ResponseType.java b/src/server/ResponseType.java index 5addf5c..4adb598 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"), + ACCOUNT_MONEY(""), + SEARCH_IN_SHOP(""), + GET_SHOP_CARDS(""), + SUCCESSFULL_SELL("you bought\n") ; private String message; diff --git a/src/view/fxmls/shop.fxml b/src/view/fxmls/shop.fxml index a1b9b29..31cedcf 100644 --- a/src/view/fxmls/shop.fxml +++ b/src/view/fxmls/shop.fxml @@ -9,7 +9,7 @@ - + diff --git a/src/view/fxmls/wrapperClasses/CardContainer.java b/src/view/fxmls/wrapperClasses/CardContainer.java index 75d167f..1866a95 100644 --- a/src/view/fxmls/wrapperClasses/CardContainer.java +++ b/src/view/fxmls/wrapperClasses/CardContainer.java @@ -1,12 +1,15 @@ package view.fxmls.wrapperClasses; +import client.RequestSender; import javafx.scene.control.CheckBox; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.AnchorPane; import models.*; - +import server.Environment; +import server.Request; +import server.RequestType; public class CardContainer { private AnchorPane anchorPane = new AnchorPane(); @@ -29,7 +32,39 @@ public class CardContainer { private CardContainer(String name) { - Placeable c = Shop.getInstance().getCard(name); + Request request = new Request(Environment.COLLECTION); + request.setRequestType(RequestType.GET_CARD); + request.setCardToBuy(name); + RequestSender.getInstance().sendRequest(request); + } + + public CardContainer(Card card) { + this(card.getName()); + this.card = card; + } + + public CardContainer(Placeable placeable) { + this(placeable.getName()); + this.card = placeable; + } + + public AnchorPane getAnchorPane() { + return anchorPane; + } + + public CheckBox getCheckBox() { + return checkBox; + } + + public Label getNameLabel() { + return nameLabel; + } + + public Placeable getCard() { + return card; + } + + public void createContainer(Placeable c) { manaLabel = new Label(Integer.toString(c.getNeededMana())); manaLabel.setStyle("-fx-font-size: 24px;-fx-text-fill: black;-fx-text-alignment: center;-fx-text-overrun: ELLIPSIS;-fx-alignment: center;" + "-fx-pref-width: 51;-fx-pref-height: 39;-fx-font-weight: bold"); @@ -54,7 +89,7 @@ private CardContainer(String name) { healthPointLabel.setLayoutX(200); healthPointLabel.setLayoutY(214); - nameLabel = new Label(name+"\n"+c.getClass().getSimpleName()); + nameLabel = new Label(c.getName() + "\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); @@ -80,38 +115,10 @@ 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); } - - } - - public CardContainer(Card card) { - this(card.getName()); - this.card = card; - } - - public CardContainer(Placeable placeable) { - this(placeable.getName()); - this.card = placeable; - } - - public AnchorPane getAnchorPane() { - return anchorPane; - } - - public CheckBox getCheckBox() { - return checkBox; - } - - public Label getNameLabel() { - return nameLabel; } - public Placeable getCard() { - return card; - } - - }