From 253c4994d8788fba16db81f9923937ebdc26c82d Mon Sep 17 00:00:00 2001 From: AhmadZafarani Date: Tue, 25 Jun 2019 00:38:09 +0430 Subject: [PATCH] save to json added --- src/controller/AccountController.java | 50 ++++++++++++++++++++++----- src/models/Placeable.java | 2 +- src/models/accountSaves.json | 1 + src/view/CollectionRequest.java | 15 ++++---- src/view/GameRequest.java | 43 ----------------------- src/view/RequestType.java | 3 -- src/view/ShopRequest.java | 12 ++++--- 7 files changed, 60 insertions(+), 66 deletions(-) create mode 100644 src/models/accountSaves.json delete mode 100644 src/view/GameRequest.java diff --git a/src/controller/AccountController.java b/src/controller/AccountController.java index d12f3e9..116d129 100644 --- a/src/controller/AccountController.java +++ b/src/controller/AccountController.java @@ -1,17 +1,21 @@ package controller; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.stream.JsonReader; import javafx.scene.Node; -import models.Account; -import models.ArtificialIntelligence; -import models.Battle; +import models.*; import models.Enums.BattleKind; import models.Enums.BattleMode; import models.Enums.ErrorType; -import models.Game; import view.AccountRequest; import view.RequestType; import view.View; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + public class AccountController extends Thread { private static AccountController accountController = new AccountController(); private Account account; @@ -174,10 +178,40 @@ private int selectStoryModeLevel(AccountRequest request) { //----------------------------------------------------------------- public void save() { + Gson gson = new Gson(); + JsonReader reader; + try { + reader = new JsonReader(new FileReader("src\\models\\accountSaves.json")); + JsonArray array = gson.fromJson(reader, JsonArray.class); + Account[] accounts = gson.fromJson(array, Account[].class); + write(gson, accounts); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void write(Gson gson, Account[] accounts) throws IOException { + FileWriter writer; + Account[] accounts2; + writer = new FileWriter("src/models/accountSaves.json"); + accounts2 = getAccounts(accounts); + writer.write(gson.toJson(accounts2)); + writer.flush(); + writer.close(); + } + + private Account[] getAccounts(Account[] accounts) { + Account[] accounts2; + if (accounts != null) { + accounts2 = new Account[accounts.length + 1]; + System.arraycopy(accounts, 0, accounts2, 0, accounts.length); + accounts2[accounts2.length - 1] = account; + } else { + accounts2 = new Account[1]; + accounts2[0] = account; + } + return accounts2; } -// private void help() { -// view.printAccountMenuHelp(account.toString()); -// } public void enterCollection() { CollectionController.getInstance().main(this.account.getCollection()); @@ -193,7 +227,7 @@ public void setAccount(Account account) { public void logout(Node node) { account = null; - Game.getInstance().loadPage(node,"/view/fxmls/loginPage.fxml"); + Game.getInstance().loadPage(node, "/view/fxmls/loginPage.fxml"); } public Account getAccount() { diff --git a/src/models/Placeable.java b/src/models/Placeable.java index 385bc3f..1d3c8ca 100644 --- a/src/models/Placeable.java +++ b/src/models/Placeable.java @@ -1,6 +1,6 @@ package models; -public abstract class Placeable implements Comparable { +public class Placeable implements Comparable { private Cell cell; private int neededMana; private int ID; diff --git a/src/models/accountSaves.json b/src/models/accountSaves.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/src/models/accountSaves.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/src/view/CollectionRequest.java b/src/view/CollectionRequest.java index 2b39235..3d43ceb 100644 --- a/src/view/CollectionRequest.java +++ b/src/view/CollectionRequest.java @@ -1,9 +1,6 @@ package view; -import models.Enums.ErrorType; - public class CollectionRequest extends Request { - private View view = View.getInstance(); @Override public RequestType getType() { @@ -38,6 +35,9 @@ else if (command.matches("show")) } } + /** + * @return null when ... + */ public String getDeckName() { switch (getType()) { case CREATE_DECK: @@ -56,10 +56,12 @@ public String getDeckName() { return command.split(" ")[2]; default: return null; - /** return null when ... */ } } + /** + * @return -1 when ... + */ public int getCardID() { switch (getType()) { case ADD_CARD_TO_DECK: @@ -68,16 +70,17 @@ public int getCardID() { return Integer.parseInt(command.split(" ")[1]); default: return -1; - /** return -1 when ... */ } } + /** + * @return null when ... + */ public String getCardName() { if (getType() == RequestType.SEARCH_CARD_IN_COLLECTION) { return command.split(" ")[1]; } return null; - /** return null when ... */ } } \ No newline at end of file diff --git a/src/view/GameRequest.java b/src/view/GameRequest.java deleted file mode 100644 index fa66c16..0000000 --- a/src/view/GameRequest.java +++ /dev/null @@ -1,43 +0,0 @@ -package view; - -public class GameRequest extends Request { - private static final String SIGN_IN = "login \\w+"; - private static final String SIGN_UP = "create account \\w+"; - private static final String SHOW_LEADERBOARD = "show leaderboard"; - - @Override - public RequestType getType() { - if (command.matches(SIGN_IN)) { - return RequestType.LOGIN; - } else if (command.matches(SIGN_UP)) { - return RequestType.CREATE_ACCOUNT; - } else if (command.matches(SHOW_LEADERBOARD)) { - return RequestType.SHOW_LEADERBOARD; - } else if (command.equals(HELP)) { - return RequestType.HELP; - } else if (command.equals(EXIT)) { - return RequestType.EXIT; - } else { - /* INVALID_COMMAND can be converted to null*/ - return RequestType.INVALID_COMMAND; - } - } - - public String getUserName() { - switch (getType()) { - case LOGIN: - return command.split("\\s")[1]; - case CREATE_ACCOUNT: - return command.split("\\s")[2]; - default: - // TODO: 15/04/2019 what to do? - return null; - } - } - - public String getPassword(View view) { - view.printGetPasswordCommand(); - getNewCommand(); - return command.trim(); - } -} diff --git a/src/view/RequestType.java b/src/view/RequestType.java index 2618a69..9d5ba6e 100644 --- a/src/view/RequestType.java +++ b/src/view/RequestType.java @@ -2,9 +2,6 @@ public enum RequestType { - LOGIN, - CREATE_ACCOUNT, - SHOW_LEADERBOARD, SAVE, LOGOUT, HELP, diff --git a/src/view/ShopRequest.java b/src/view/ShopRequest.java index 0ece797..f28a604 100644 --- a/src/view/ShopRequest.java +++ b/src/view/ShopRequest.java @@ -23,6 +23,9 @@ else if (command.matches("show")) } } + /** + * @return null when ... + */ public String getCardName() { switch (getType()) { case SEARCH_CARD_IN_COLLECTION: @@ -33,10 +36,12 @@ public String getCardName() { return command.split(" ")[1]; default: return null; - /** return null when ... */ } } + /** + * @return null when ... + */ public int getCardID() { if (getType() == RequestType.SELL) { try { @@ -44,11 +49,8 @@ public int getCardID() { }catch (NumberFormatException ex){ return -1; } - } return -1; - /** return -1 when ... */ - } -} \ No newline at end of file +}