Skip to content

Commit

Permalink
shop finished
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmadZafarani committed Jul 8, 2019
1 parent dabf80e commit b5ba5da
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 249 deletions.
115 changes: 74 additions & 41 deletions src/client/ResponseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,32 @@
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();
private JsonStreamParser jsonStreamParser;
private Gson gson = new Gson();
private Response response;
private CollectionFxmlController collectionController;
private ShopFxmlController shopFxmlController;

public static ResponseHandler getInstance() {
return RESPONSE_HANDLER;
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -122,7 +119,6 @@ public void run() {
});
}


private void removeDeck(String deckToRemove) {
Platform.runLater(() -> {
collectionController.decks.getItems().remove(deckToRemove);
Expand All @@ -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);
}
}

Expand All @@ -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);
}

}
22 changes: 14 additions & 8 deletions src/controller/fxmlControllers/CardInShopController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/controller/fxmlControllers/CustomCardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class CustomCardController implements Initializable {
public Button back;
public Label message;
public ComboBox<String> specialPower;
private Shop shop = Shop.getInstance();
private Shop shop = new Shop();

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
Expand Down
17 changes: 0 additions & 17 deletions src/controller/fxmlControllers/MainMenuController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +12,6 @@
import server.Request;
import server.RequestType;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

Expand All @@ -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) {
Expand All @@ -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");
}

Expand Down Expand Up @@ -99,8 +86,4 @@ private void createCustomCard() {
Game.getInstance().loadPage(error, "/view/fxmls/customCard.fxml");
}

public static ShopFxmlController getShopFxmlController() {
return shopFxmlController;
}

}
46 changes: 16 additions & 30 deletions src/controller/fxmlControllers/ShopFxmlController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -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 -> {
Expand All @@ -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<Placeable> 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) {
Expand Down
3 changes: 0 additions & 3 deletions src/controller/logicController/AccountController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit b5ba5da

Please sign in to comment.