Skip to content

Commit

Permalink
save to json added
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmadZafarani committed Jun 24, 2019
1 parent 527fb17 commit 253c499
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 66 deletions.
50 changes: 42 additions & 8 deletions src/controller/AccountController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/models/Placeable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package models;

public abstract class Placeable implements Comparable<Placeable> {
public class Placeable implements Comparable<Placeable> {
private Cell cell;
private int neededMana;
private int ID;
Expand Down
1 change: 1 addition & 0 deletions src/models/accountSaves.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
15 changes: 9 additions & 6 deletions src/view/CollectionRequest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package view;

import models.Enums.ErrorType;

public class CollectionRequest extends Request {
private View view = View.getInstance();

@Override
public RequestType getType() {
Expand Down Expand Up @@ -38,6 +35,9 @@ else if (command.matches("show"))
}
}

/**
* @return null when ...
*/
public String getDeckName() {
switch (getType()) {
case CREATE_DECK:
Expand All @@ -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:
Expand All @@ -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 ... */
}

}
43 changes: 0 additions & 43 deletions src/view/GameRequest.java

This file was deleted.

3 changes: 0 additions & 3 deletions src/view/RequestType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

public enum RequestType {

LOGIN,
CREATE_ACCOUNT,
SHOW_LEADERBOARD,
SAVE,
LOGOUT,
HELP,
Expand Down
12 changes: 7 additions & 5 deletions src/view/ShopRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ else if (command.matches("show"))
}
}

/**
* @return null when ...
*/
public String getCardName() {
switch (getType()) {
case SEARCH_CARD_IN_COLLECTION:
Expand All @@ -33,22 +36,21 @@ public String getCardName() {
return command.split(" ")[1];
default:
return null;
/** return null when ... */
}
}

/**
* @return null when ...
*/
public int getCardID() {
if (getType() == RequestType.SELL) {
try {
return Integer.parseInt(command.split(" ")[1]);
}catch (NumberFormatException ex){
return -1;
}

}
return -1;
/** return -1 when ... */

}

}
}

0 comments on commit 253c499

Please sign in to comment.