diff --git a/src/controller/AccountController.java b/src/controller/AccountController.java index 116d129..92fa6fd 100644 --- a/src/controller/AccountController.java +++ b/src/controller/AccountController.java @@ -28,38 +28,6 @@ public static AccountController getInstance() { accountController.setDaemon(true); return accountController; } -// void main(Account account) { -// AccountRequest request; -// this.account = account; -// view.showMainMenu(); -// request = new AccountRequest(); -// request.getNewCommand(); -// switch (request.getType()) { -// case ENTER_COLLECTION: -// enterCollection(); -// break; -// case ENTER_SHOP: -// enterShop(); -// break; -// case ENTER_BATTLE: -// enterBattle(request); -// break; -// case EXIT: -// System.exit(0); -// break; -// case HELP: -// help(); -// break; -// case LOGOUT: -// -// break; -// case SAVE: -// save(); -// case SHOW_MATCH_HISTORY: -// account.showHistory(); -// } -// -// } //------------------------------------------------------------Battle public void enterBattle(AccountRequest request) { @@ -214,7 +182,7 @@ private Account[] getAccounts(Account[] accounts) { } public void enterCollection() { - CollectionController.getInstance().main(this.account.getCollection()); + CollectionController.getInstance().setCollection(this.account.getCollection()); } public void enterShop() { diff --git a/src/controller/CollectionController.java b/src/controller/CollectionController.java index 510755e..4c2a24c 100644 --- a/src/controller/CollectionController.java +++ b/src/controller/CollectionController.java @@ -1,11 +1,15 @@ package controller; +import com.google.gson.Gson; import models.*; import models.Enums.ErrorType; import view.CollectionRequest; import view.View; -class CollectionController { +import java.io.FileWriter; +import java.io.IOException; + +public class CollectionController { private static CollectionController collectionController = new CollectionController(); private Collection collection; private View view = View.getInstance(); @@ -13,62 +17,10 @@ class CollectionController { private CollectionController() { } - static CollectionController getInstance() { + public static CollectionController getInstance() { return collectionController; } - void main(Collection collection) { - CollectionRequest request; - this.collection = collection; - boolean isFinish = false; - do { - request = new CollectionRequest(); - request.getNewCommand(); - switch (request.getType()) { - case EXIT: - isFinish = true; - break; - case SHOW_COLLECTION_ITEMS_AND_CARDS: - showCollectionItemsAndCards(); - break; - case SEARCH_CARD_IN_COLLECTION: - search(request); - break; - case SAVE: - save(); - break; - case CREATE_DECK: - createDeck(request); - break; - case DELETE_DECK: - deleteDeck(request); - break; - case ADD_CARD_TO_DECK: - addCardToDeck(request); - break; - case REMOVE_CARD_FROM_DECK: - removeCardFromDeck(request); - break; - case VALIDATE: - validateDeck(request); - break; - case SELECT_DECK: - selectMainDeck(request); - break; - case SHOW_ALL_DECKS: - showAllDecks(); - break; - case SHOW_DECK: - showDeck(request); - break; - case HELP: - help(); - break; - } - } - while (!isFinish); - } - private void showCollectionItemsAndCards() { view.printCollectionItems(collection.getCollectionCards(), false); } @@ -175,11 +127,24 @@ private void showDeck(CollectionRequest request) { } - public void help() { - view.printCollectionMenuHelp(collection.toString()); + private void save() { + } + + public void setCollection(Collection collection) { + this.collection = collection; } - private void save() { + public void exportDeck(String deckName) { + Deck deck = collection.getDeck(deckName); + Gson gson = new Gson(); + try { + FileWriter fileWriter = new FileWriter("deck.json"); + fileWriter.write(gson.toJson(deck)); + fileWriter.flush(); + fileWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } } -} \ No newline at end of file +} diff --git a/src/controller/GameController.java b/src/controller/GameController.java index 488d33f..4bae339 100644 --- a/src/controller/GameController.java +++ b/src/controller/GameController.java @@ -18,32 +18,6 @@ private GameController() { public static GameController getInstance() { return gameController; } -// public void main() { -// GameRequest request; -// boolean isFinish = false; -// do { -// view.printStartMenu(); /*show menu*/ -// request = new GameRequest(); -// request.getNewCommand(); -// switch (request.getType()) { -// case LOGIN: -// login(request); -// break; -// case CREATE_ACCOUNT: -// createAccount(request); -// break; -// case HELP: -// help(); -// break; -// case SHOW_LEADERBOARD: -// showLeaderboard(); -// break; -// case EXIT: -// isFinish = true; -// } -// } -// while (!isFinish); -// } private boolean login() { Account account = Game.getAccount(username); @@ -79,16 +53,7 @@ private void createAccount() { private void showMessage(ErrorType errorType) { labelText = errorType.getMessage(); - System.out.println(errorType.getMessage()); } -// -// private void showLeaderboard() { -// view.printLeaderboard(game.getSortedAccounts()); -// } -// -// private void help() { -// view.printGameMenuHelp(game.toString()); -// } @Override public void run() { @@ -122,4 +87,4 @@ public String getLabelText() { return labelText; } -} \ No newline at end of file +} diff --git a/src/controller/ShopController.java b/src/controller/ShopController.java index fa50787..66cfa78 100644 --- a/src/controller/ShopController.java +++ b/src/controller/ShopController.java @@ -1,17 +1,18 @@ package controller; +import controller.fxmlControllers.ShopFxmlController; import models.Enums.ErrorType; import models.Shop; import view.ShopRequest; import view.View; -import static models.Enums.ErrorType.CARD_NOT_FOUND_IN_SHOP; import static models.Enums.ErrorType.NO_ERROR; public class ShopController { private static ShopController shopController = new ShopController(); private Shop shop = Shop.getInstance(); private View view = View.getInstance(); + private ShopFxmlController shopFxmlController; private ShopController() { } @@ -61,6 +62,10 @@ public static ShopController getInstance() { // view.printShopCards(shop.getCards()); // } + public void setShopFxmlController(ShopFxmlController shopFxmlController) { + this.shopFxmlController = shopFxmlController; + } + private void sellCard(ShopRequest request) { int cardID = request.getCardID(); boolean isDone = shop.sell(cardID); @@ -70,44 +75,44 @@ private void sellCard(ShopRequest request) { view.unSuccessfulSellMessage(); } - private void buyCard(ShopRequest request) { - ErrorType error; - String cardName = request.getCardName(); - if (shop.getCard(cardName) != null) { - error = shop.buy(cardName); - } else { - error = CARD_NOT_FOUND_IN_SHOP; - } + public void buyCard(String cardName) { + cardName = cardName.split("\n")[0]; + ErrorType error = shop.buy(cardName); if (error != NO_ERROR) { - view.printError(error); + shopFxmlController.message.setText(error.getMessage()); } else { - view.successfulBuyMessage(); + shopFxmlController.message.setText("you bought \n" + cardName); } + shopFxmlController.money.setText(String.valueOf(shop.getAccount().getMoney())); } -// private void searchInShop(ShopRequest request) { -// String cardName = request.getCardName(); -// if (shop.searchInShop(cardName)) { -// view.printCardWasFound(); -// } else { -// view.printError(CARD_NOT_FOUND_IN_SHOP); -// } +//// private void searchInShop(ShopRequest request) { +//// String cardName = request.getCardName(); +//// if (shop.searchInShop(cardName)) { +//// view.printCardWasFound(); +//// } else { +//// view.printError(CARD_NOT_FOUND_IN_SHOP); +//// } +//// } +//// private void searchInCollection(ShopRequest request) { +//// String cardName = request.getCardName(); +//// int state = shop.searchInCollection(cardName); +//// if (state != -1) { +//// view.printCardInCollection(cardName, state); +//// } else { +//// view.printNoCardWithThisName(cardName); +//// } +//// } +// +// private void showCollectionCards() { +// view.printCollectionItems(shop.getCards(), true); // } -// private void searchInCollection(ShopRequest request) { -// String cardName = request.getCardName(); -// int state = shop.searchInCollection(cardName); -// if (state != -1) { -// view.printCardInCollection(cardName, state); -// } else { -// view.printNoCardWithThisName(cardName); -// } -// } - - private void showCollectionCards() { - view.printCollectionItems(shop.getCards(), true); - } public Shop getShop() { return shop; } + public ShopFxmlController getShopFxmlController() { + return shopFxmlController; + } + } diff --git a/src/controller/fxmlControllers/CardInShopController.java b/src/controller/fxmlControllers/CardInShopController.java index 356e354..f6b3e0f 100644 --- a/src/controller/fxmlControllers/CardInShopController.java +++ b/src/controller/fxmlControllers/CardInShopController.java @@ -1,11 +1,12 @@ package controller.fxmlControllers; +import controller.ShopController; import javafx.fxml.FXML; import javafx.fxml.Initializable; -import javafx.scene.control.CheckBox; import javafx.scene.control.Label; import javafx.scene.image.ImageView; import javafx.scene.layout.*; +import models.Shop; import java.net.URL; import java.util.ResourceBundle; @@ -19,11 +20,11 @@ public class CardInShopController implements Initializable { public Label HP; public Label name; public ImageView imageView; - public CheckBox checkBox; @Override public void initialize(URL url, ResourceBundle resourceBundle) { - + ShopController shopController = ShopController.getInstance(); + pane.setOnMouseClicked(mouseEvent -> shopController.buyCard(name.getText())); } } diff --git a/src/controller/fxmlControllers/ShopFxmlController.java b/src/controller/fxmlControllers/ShopFxmlController.java index 4ff051e..d931963 100644 --- a/src/controller/fxmlControllers/ShopFxmlController.java +++ b/src/controller/fxmlControllers/ShopFxmlController.java @@ -18,10 +18,13 @@ public class ShopFxmlController implements Initializable { public Button back; public FlowPane pane; + public Label money; + public Label message; private ShopController shopController = ShopController.getInstance(); @Override public void initialize(URL location, ResourceBundle resources) { + shopController.setShopFxmlController(this); back.setOnAction(actionEvent -> Game.getInstance().loadPage(back, "/view/fxmls/mainMenu.fxml")); craftGraphics(); } diff --git a/src/models/Account.java b/src/models/Account.java index 4680740..176fdc3 100644 --- a/src/models/Account.java +++ b/src/models/Account.java @@ -38,7 +38,7 @@ String getPassword() { return password; } - int getMoney() { + public int getMoney() { return money; } @@ -90,4 +90,5 @@ public ArrayList getHistories() { return histories; } -} \ No newline at end of file + +} diff --git a/src/models/Enums/ErrorType.java b/src/models/Enums/ErrorType.java index e1a014e..3e2b556 100644 --- a/src/models/Enums/ErrorType.java +++ b/src/models/Enums/ErrorType.java @@ -6,18 +6,15 @@ public enum ErrorType { INVALID_PASSWORD("invalid password!!!"), INVALID_COMMAND("invalid command!!!"), ERROR_WITHOUT_MESSAGE("empty command"), - ACCOUNT_NOT_FOUND("account not found! please register first"), CARD_NOT_FOUND_IN_COLLECTION("card not found in collection"), DUPLICATE_DECK("creation failed, this deck already exists"), DECK_NOT_FOUND("deck not found in collection"), - DUPLICATE_CARD("card already exists in deck"), FULL_DECK("deck is full"), HERO_SET_BEFORE("hero has been set before"), ITEM_SET_BEFORE("item has been before"), CARD_NOT_FOUND_IN_DECK("there is no card with this ID in deck !!!"), - CARD_NOT_FOUND_IN_SHOP("there is no card with tish name in shop !!!"), - NOT_ENOUGH_MONEY_TO_BUY_CARD("you don't have enough money to buy this card !!!"), - MAX_ITEMS_IN_COLLECTION_REACHED("you can not buy this card because you have 3 items in your collection !!!"), + NOT_ENOUGH_MONEY_TO_BUY_CARD("you don't \nhave enough\n money to buy\n this card !!!"), + MAX_ITEMS_IN_COLLECTION_REACHED("you can't \nbuy this card\n because you have\n 3 items in your\n collection !!!"), NO_ERROR("NO error"), MAIN_DECK_IS_NOT_VALID("selected deck is invalid"), CARD_NOT_FOUND_IN_BATTLE("there is no card with tish name in game map !!!"), @@ -29,7 +26,6 @@ public enum ErrorType { DEST_IS_UNAVAILABLE_FOR_ATTACK("opponent minion is unavailable for attack"), INVALID_DEST_ID("Invalid card id"), CARD_CANT_MOVE("this card has been moved this turn"), - CARD_CANT_ATTACK("card can't attack"), ACCOUNT_CREATED("account created successfully!"); diff --git a/src/models/Game.java b/src/models/Game.java index 775d3b4..2759bc3 100644 --- a/src/models/Game.java +++ b/src/models/Game.java @@ -1,10 +1,14 @@ package models; +import com.google.gson.Gson; +import com.google.gson.stream.JsonReader; import javafx.fxml.FXMLLoader; import javafx.scene.Node; +import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; public class Game { @@ -13,6 +17,7 @@ public class Game { private static Game game = new Game(); private Game() { + fillAccounts(); } public static Game getInstance() { @@ -68,6 +73,19 @@ public void loadPage(Node node, final String url) { } } + private void fillAccounts() { + Gson gson = new Gson(); + JsonReader reader; + try { + reader = new JsonReader(new FileReader("src\\models\\accountSaves.json")); + Account[] json = gson.fromJson(reader, Account[].class); + if (json != null) + accounts.addAll(Arrays.asList(json)); + } catch (IOException e) { + e.printStackTrace(); + } + } + @Override public String toString() { return "1.login\n" + diff --git a/src/models/Shop.java b/src/models/Shop.java index 9bebcb7..29493ec 100644 --- a/src/models/Shop.java +++ b/src/models/Shop.java @@ -120,4 +120,8 @@ private boolean haveEnoughMoneyToBuyCard(Placeable card, Account account) { return account.getMoney() >= card.getCost(); } -} \ No newline at end of file + public Account getAccount() { + return account; + } + +} diff --git a/src/view/fxmls/cardInShop.fxml b/src/view/fxmls/cardInShop.fxml index 2f9daf0..aa7363b 100644 --- a/src/view/fxmls/cardInShop.fxml +++ b/src/view/fxmls/cardInShop.fxml @@ -1,6 +1,5 @@ - @@ -12,7 +11,7 @@ -