Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
alireza1082 committed Jun 25, 2019
2 parents 8c45eae + 253c499 commit 1065077
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 145 deletions.
57 changes: 47 additions & 10 deletions src/controller/AccountController.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package controller;

import models.Account;
import models.ArtificialIntelligence;
import models.Battle;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.stream.JsonReader;
import javafx.scene.Node;
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 @@ -173,28 +178,60 @@ 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());
}

public void enterShop() {
ShopController.getInstance().main(account);
ShopController.getInstance().getShop().setAccount(account);
}

public void setAccount(Account account) {
this.account = account;
}

public void logout() {
public void logout(Node node) {
account = null;
Game.getInstance().loadPage(node, "/view/fxmls/loginPage.fxml");
}

public Account getAccount() {
return this.account;
}

}
}
132 changes: 65 additions & 67 deletions src/controller/ShopController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package controller;

import models.Account;
import models.Enums.ErrorType;
import models.Shop;
import view.ShopRequest;
Expand All @@ -9,61 +8,58 @@
import static models.Enums.ErrorType.CARD_NOT_FOUND_IN_SHOP;
import static models.Enums.ErrorType.NO_ERROR;

class ShopController {
public class ShopController {
private static ShopController shopController = new ShopController();
private Shop shop = Shop.getInstance();
private View view = View.getInstance();

private ShopController() {
}

static ShopController getInstance() {
public static ShopController getInstance() {
return shopController;
}

void main(Account account) {
shop.setAccount(account);
boolean isFinish = false;
do {
ShopRequest request = new ShopRequest();
request.getNewCommand();
switch (request.getType()) {
case EXIT:
isFinish = true;
break;
case HELP:
help();
break;
case SHOW_SHOP_CARDS:
showShopCards();
break;
case SELL:
sellCard(request);
break;
case BUY:
buyCard(request);
break;
case SEARCH_CARD_IN_COLLECTION:
searchInCollection(request);
break;
case SEARCH_SHOP:
searchInShop(request);
break;
case SHOW_COLLECTION_ITEMS_AND_CARDS:
showCollectionCards();
break;
}
}
while (!isFinish);
}

public void help() {
view.printShopMenuHelp(shop.toString());
}

private void showShopCards() {
view.printShopCards(shop.getCards());
}
// void main(Account account) {
// shop.setAccount(account);
// boolean isFinish = false;
// do {
// ShopRequest request = new ShopRequest();
// request.getNewCommand();
// switch (request.getType()) {
// case EXIT:
// isFinish = true;
// break;
// case HELP:
// help();
// break;
// case SHOW_SHOP_CARDS:
// showShopCards();
// break;
// case SELL:
// sellCard(request);
// break;
// case BUY:
// buyCard(request);
// break;
// case SEARCH_CARD_IN_COLLECTION:
// searchInCollection(request);
// break;
// case SEARCH_SHOP:
// searchInShop(request);
// break;
// case SHOW_COLLECTION_ITEMS_AND_CARDS:
// showCollectionCards();
// break;
// }
// }
// while (!isFinish);
// }
// public void help() {
// view.printShopMenuHelp(shop.toString());
// }
// private void showShopCards() {
// view.printShopCards(shop.getCards());
// }

private void sellCard(ShopRequest request) {
int cardID = request.getCardID();
Expand All @@ -88,28 +84,30 @@ private void buyCard(ShopRequest request) {
view.successfulBuyMessage();
}
}

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 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);
}

}
public Shop getShop() {
return shop;
}

}
5 changes: 2 additions & 3 deletions src/controller/fxmlControllers/MainMenuController.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ private void goToCollection() {
}

private void goToShop() {
// accountController.enterShop();
// Main.getStage().getScene().setRoot(Main.getShopPage());
accountController.enterShop();
Game.getInstance().loadPage(exitButton, "/view/fxmls/shop.fxml");
}

Expand All @@ -62,7 +61,7 @@ private void goToBattle() {
}

private void logout() {
accountController.logout();
accountController.logout(exitButton);
}

private void showMatchHistories() {
Expand Down
4 changes: 3 additions & 1 deletion src/controller/fxmlControllers/ShopFxmlController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package controller.fxmlControllers;

import controller.ShopController;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
Expand All @@ -17,6 +18,7 @@ public class ShopFxmlController implements Initializable {

public Button back;
public FlowPane pane;
private ShopController shopController = ShopController.getInstance();

@Override
public void initialize(URL location, ResourceBundle resources) {
Expand Down Expand Up @@ -50,7 +52,7 @@ private void specifyImageAndText(Card c, AnchorPane loader) {
}

private void getImageView(ImageView view, String url) {
Image image = new Image("view/images/cardGifs/boss_decepticleprime_breathing.gif");
Image image = new Image("view/images/cardGifs/boss_grym_breathing.gif");
view.setImage(image);
view.setScaleX(2);
view.setScaleY(2);
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 ... */
}

}
Loading

0 comments on commit 1065077

Please sign in to comment.