Skip to content

Commit 0f92796

Browse files
committed
collection has allot of bugs.
1 parent 459122c commit 0f92796

38 files changed

+1711
-206
lines changed

src/controller/Main.java

+11-21
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,11 @@
33
import controller.window.LoadWindow;
44
import javafx.application.Application;
55
import javafx.application.Platform;
6-
import javafx.fxml.FXMLLoader;
76
import javafx.scene.input.KeyCombination;
87
import javafx.stage.Stage;
9-
import model.Account;
10-
import model.Deck;
11-
import model.Game;
12-
import model.Level;
138
import view.WindowChanger;
14-
import view.fxmlControllers.ArenaController;
15-
import view.fxmlControllers.GamePreviewButtonController;
16-
import view.fxmlControllers.LoadingGamePreviewScenes;
179
import view.fxmls.LoadedScenes;
1810

19-
import java.io.IOException;
20-
2111
public class Main extends Application {
2212
public static Stage mainStage;
2313

@@ -37,17 +27,17 @@ public void start(Stage primaryStage) {
3727

3828
new LoadWindow().main();
3929

40-
// WindowChanger.instance.setNewScene(LoadedScenes.registerMenu);
41-
42-
{//arena
43-
Account account = new Account("test", "test");
44-
account.getCollection().setMainDeck(Deck.getAllDecks().get("level1"));
45-
Game game = Level.getAvailableLevels().get("1").getLevelGame(account);
46-
ArenaController.ac.init(game);
47-
game.initialiseGameFields();
48-
49-
WindowChanger.instance.setNewScene(LoadedScenes.arena);
50-
}
30+
WindowChanger.instance.setNewScene(LoadedScenes.registerMenu);
31+
32+
// {//arena
33+
// Account account = new Account("test", "test");
34+
// account.getCollection().setMainDeck(Deck.getAllDecks().get("level1"));
35+
// Game game = Level.getAvailableLevels().get("1").getLevelGame(account);
36+
// ArenaController.ac.init(game);
37+
// game.initialiseGameFields();
38+
//
39+
// WindowChanger.instance.setNewScene(LoadedScenes.arena);
40+
// }
5141

5242
mainStage.show();
5343
}

src/model/Collection.java

+92-61
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import model.cards.Hero;
55
import model.cards.Spell;
66
import model.cards.Warrior;
7-
import view.Utility;
7+
import view.fxmlControllers.AlertController;
88
import view.fxmlControllers.DeckController;
99

1010
import java.io.Serializable;
@@ -18,33 +18,28 @@ public class Collection implements Serializable {
1818
private HashMap<String, Integer> howManyCard = new HashMap<>();
1919
private Deck mainDeck;
2020

21-
public void createDeck(String deckName) {
22-
for (String template : getDecks()) {
23-
if (template.toLowerCase().equals(deckName.toLowerCase())) {
24-
Utility.showMessage("There is already a deck with this name");
25-
return;
26-
}
21+
public boolean createDeck(String deckName) {
22+
if (decks.contains(deckName)) {
23+
AlertController.setAndShowAndGetResultByAnAlertController("There is already a deck with this name", false);
24+
return false;
2725
}
2826
Deck deck = new Deck();
2927
deck.setName(deckName);
3028
getDecks().add(deckName);
3129
getAllDecks().put(deckName, deck);
3230
Deck.getAllDecks().put(deckName, deck);
3331
DeckController.putADeckToList(deckName);
34-
Utility.showMessage("Deck created :)");
32+
AlertController.setAndShowAndGetResultByAnAlertController("Deck created", false);
33+
return true;
3534
}
3635

37-
public void deleteDeck(String deckName) {
38-
boolean isDeckNameValid = false;
39-
for (String template : getDecks()) {
40-
if (template.equals(deckName)) {
41-
isDeckNameValid = true;
42-
}
43-
}
44-
if (!isDeckNameValid) {
45-
Utility.showMessage("There is no deck with this name :(");
46-
return;
36+
public boolean deleteDeck(String deckName) {
37+
//todo
38+
if (!getDecks().contains(deckName)) {
39+
AlertController.setAndShowAndGetResultByAnAlertController("There is no deck with this name", false);
40+
return false;
4741
}
42+
//todo
4843
Deck deck = Account.getActiveAccount().getCollection().getAllDecks().get(deckName);
4944
if (this.getMainDeck() != null && this.getMainDeck().equals(deck)) {
5045
this.setMainDeck(null);
@@ -53,95 +48,104 @@ public void deleteDeck(String deckName) {
5348
getAllDecks().remove(deckName);
5449
Deck.getAllDecks().remove(deckName);
5550
DeckController.removeADeckFromList(deckName);
56-
Utility.showMessage("Deck deleted :)");
51+
AlertController.setAndShowAndGetResultByAnAlertController("Deck deleted", false);
52+
return true;
5753
}
5854

59-
public void addCardToDeck(String cardName, String deckName) {
55+
public boolean addCardToDeck(String cardName, String deckName) {
56+
//todo
6057
if (!Account.getActiveAccount().getCollection().getAllDecks().containsKey(deckName)) {
61-
Utility.showMessage("There is no deck with this name :(");
62-
return;
58+
AlertController.setAndShowAndGetResultByAnAlertController("There is no deck with this name", false);
59+
return false;
6360
}
61+
//todo
6462
Deck deck = Account.getActiveAccount().getCollection().getAllDecks().get(deckName);
6563
int cardID = getIDByName(cardName);
64+
//todo
6665
if (!this.getCardIDs().contains(cardID)) {
67-
Utility.showMessage("There is no card with this name in collection cards :(");
68-
return;
66+
AlertController.setAndShowAndGetResultByAnAlertController("There is no card with this name in collection cards", false);
67+
return false;
6968
}
69+
//todo
7070
Card card = Card.getAllCards().get(cardID);
7171
if (card instanceof Hero) {
7272
if (deck.getHero() != null) {
73-
Utility.showMessage("There is already a hero in this deck. You can't add any other");
74-
return;
73+
AlertController.setAndShowAndGetResultByAnAlertController("There is already a hero in this deck. You can't add any other", false);
74+
return false;
7575
} else {
7676
deck.setHero((Hero) card);
77-
Utility.showMessage("Card added to deck successfully :)");
78-
return;
77+
AlertController.setAndShowAndGetResultByAnAlertController("Card added to deck successfully", false);
78+
return true;
7979
}
8080
}
8181
if (Spell.checkIsItem(card)) {
8282
if (deck.getItem() != null) {
83-
Utility.showMessage("There is an item in this deck");
84-
return;
83+
AlertController.setAndShowAndGetResultByAnAlertController("There is an item in this deck", false);
84+
return false;
8585
} else {
8686
deck.setItem((Spell) card);
87-
Utility.showMessage("Card added to deck successfully :)");
88-
return;
87+
AlertController.setAndShowAndGetResultByAnAlertController("Card added to deck successfully", false);
88+
return true;
8989
}
9090
}
9191
if (deck.getCardIDs().size() == 20) {
92-
Utility.showMessage("You have 20 cards in your deck. You couldn't put any other card");
93-
return;
92+
AlertController.setAndShowAndGetResultByAnAlertController("You have 20 cards in your deck. You couldn't put any other card", false);
93+
return false;
9494
}
95+
//todo
9596
int numberOfCard = 0;
9697
for (int ID : deck.getCardIDs()) {
9798
if (ID == cardID) {
9899
numberOfCard++;
99100
}
100101
}
101102
if (numberOfCard >= Collection.getCollection().howManyCard.get(cardName)) {
102-
Utility.showMessage("You can't add this card to your deck. You haven't enough number of it in your collection");
103-
return;
103+
AlertController.setAndShowAndGetResultByAnAlertController("You can't add this card to your deck. You haven't enough number of it in your collection", false);
104+
return false;
104105
}
106+
//todo
105107
deck.getCardIDs().add(cardID);
106108
if (card instanceof Warrior) {
107109
deck.minions.add(card);
108110
} else {
109111
deck.spells.add(card);
110112
}
111-
Utility.showMessage("Card added to deck successfully :)");
113+
AlertController.setAndShowAndGetResultByAnAlertController("Card added to deck successfully", false);
114+
return true;
112115
}
113116

114-
public void removeCardFromDeck(String cardName, String deckName) {
117+
public boolean removeCardFromDeck(String cardName, String deckName) {
115118
int cardID = getIDByName(cardName);
119+
//todo
116120
if (!Account.getActiveAccount().getCollection().getAllDecks().containsKey(deckName)) {
117-
Utility.showMessage("There is no deck with this name :(");
118-
return;
121+
AlertController.setAndShowAndGetResultByAnAlertController("There is no deck with this name", false);
122+
return false;
119123
}
120124
if (!Card.getAllCards().containsKey(cardID)) {
121-
Utility.showMessage("There is no card with this name in this deck :(");
122-
return;
125+
AlertController.setAndShowAndGetResultByAnAlertController("There is no card with this name in this deck", false);
126+
return false;
123127
}
128+
//todo
124129
Deck deck = Account.getActiveAccount().getCollection().getAllDecks().get(deckName);
125130
Card card = Card.getAllCards().get(cardID);
126-
127131
if (card instanceof Hero) {
128132
if (deck.getHero().equals((Hero) card)) {
129133
deck.setHero(null);
130-
Utility.showMessage("Card removed from deck successfully :)");
131-
return;
134+
AlertController.setAndShowAndGetResultByAnAlertController("Card removed from deck successfully", false);
135+
return true;
132136
} else {
133-
Utility.showMessage("There is no card with this name in this deck :(");
134-
return;
137+
AlertController.setAndShowAndGetResultByAnAlertController("There is no card with this name in this deck", false);
138+
return false;
135139
}
136140
}
137141
if (Spell.checkIsItem(card)) {
138142
if (deck.getItem().equals((Spell) card)) {
139143
deck.setItem(null);
140-
Utility.showMessage("Card removed from deck successfully :)");
141-
return;
144+
AlertController.setAndShowAndGetResultByAnAlertController("Card removed from deck successfully", false);
145+
return true;
142146
} else {
143-
Utility.showMessage("There is no card with this name in this deck :(");
144-
return;
147+
AlertController.setAndShowAndGetResultByAnAlertController("There is no card with this name in this deck", false);
148+
return false;
145149
}
146150
}
147151
if (deck.getCardIDs().contains(cardID)) {
@@ -151,45 +155,72 @@ public void removeCardFromDeck(String cardName, String deckName) {
151155
} else {
152156
deck.spells.remove(card);
153157
}
154-
Utility.showMessage("Card removed from deck successfully :)");
158+
AlertController.setAndShowAndGetResultByAnAlertController("Card removed from deck successfully", false);
159+
return true;
155160
} else {
156-
Utility.showMessage("There is no card with this name in this deck :(");
161+
AlertController.setAndShowAndGetResultByAnAlertController("There is no card with this name in this deck", false);
162+
return false;
157163
}
158164
}
159165

160166
public boolean validateDeck(String deckName, boolean showMessage) {
167+
//todo
161168
if (!Account.getActiveAccount().getCollection().getAllDecks().containsKey(deckName)) {
162169
if (showMessage) {
163-
Utility.showMessage("There is no deck with this name :(");
170+
AlertController.setAndShowAndGetResultByAnAlertController("There is no deck with this name", false);
164171
}
165172
return false;
166173
}
174+
//todo
167175
Deck deck = Account.getActiveAccount().getCollection().getAllDecks().get(deckName);
168176
if (deck.getCardIDs().size() != 20 || deck.getHero() == null) {
169177
if (showMessage) {
170-
Utility.showMessage("This deck is not valid :(");
178+
AlertController.setAndShowAndGetResultByAnAlertController("This deck is not valid", false);
171179
}
172180
return false;
173181
}
174182
if (showMessage) {
175-
Utility.showMessage("This deck is valid :)");
183+
AlertController.setAndShowAndGetResultByAnAlertController("This deck is valid", false);
176184
}
177185
return true;
178186
}
179187

180-
public void selectMainDeck(String deckName) {
188+
public boolean selectMainDeck(String deckName) {
189+
//todo
181190
if (!Account.getActiveAccount().getCollection().getAllDecks().containsKey(deckName)) {
182-
Utility.showMessage("There is no deck with this name :(");
183-
return;
191+
AlertController.setAndShowAndGetResultByAnAlertController("There is no deck with this name", false);
192+
return false;
184193
}
194+
//todo
185195
if (validateDeck(deckName, false)) {
186196
setMainDeck(Account.getActiveAccount().getCollection().getAllDecks().get(deckName));
187-
Utility.showMessage("This deck selected as main successfully :)");
197+
AlertController.setAndShowAndGetResultByAnAlertController("This deck selected as main successfully", false);
198+
return true;
188199
} else {
189-
Utility.showMessage("This deck is not valid :(");
200+
AlertController.setAndShowAndGetResultByAnAlertController("This deck is not valid", false);
201+
return false;
190202
}
191203
}
192204

205+
public boolean renameDeck(String deckName, String newName) {
206+
//todo
207+
if (!Account.getActiveAccount().getCollection().getAllDecks().containsKey(deckName)) {
208+
AlertController.setAndShowAndGetResultByAnAlertController("There is no deck with this name", false);
209+
return false;
210+
}
211+
//todo
212+
if (Account.getActiveAccount().getCollection().getAllDecks().containsKey(newName)) {
213+
AlertController.setAndShowAndGetResultByAnAlertController("You have an another deck with this name", false);
214+
return false;
215+
}
216+
Deck deck = Account.getActiveAccount().getCollection().getAllDecks().get(deckName);
217+
deck.setName(newName);
218+
allDecks.remove(deckName);
219+
decks.remove(deckName);
220+
allDecks.put(newName, deck);
221+
return true;
222+
}
223+
193224
private int getIDByName(String cardName) {
194225
for (int ID : Shop.getShop().getCardIDs()) {
195226
if (Card.getAllCards().get(ID).getName().equals(cardName)) {

src/model/Deck.java

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.Serializable;
99
import java.util.ArrayList;
1010
import java.util.HashMap;
11+
import java.util.Objects;
1112

1213
public class Deck implements Serializable {
1314
private static HashMap<String, Deck> allDecks = new HashMap<>();
@@ -116,4 +117,17 @@ public Hero getHero() {
116117
public static HashMap<String, Deck> getAllDecks() {
117118
return allDecks;
118119
}
120+
121+
@Override
122+
public boolean equals(Object o) {
123+
if (this == o) return true;
124+
if (o == null || getClass() != o.getClass()) return false;
125+
Deck deck = (Deck) o;
126+
return Objects.equals(name, deck.name);
127+
}
128+
129+
@Override
130+
public int hashCode() {
131+
return Objects.hash(name);
132+
}
119133
}

src/model/cards/Card.java

+5
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,9 @@ public int getPrice() {
7474
public int hashCode() {
7575
return Objects.hash(name);
7676
}
77+
78+
public static Card getCardByItsName(String cardName) {
79+
return Card.getAllCards().values().stream()
80+
.filter(card -> card.getName().equals(cardName)).findFirst().orElse(null);
81+
}
7782
}

src/view/Message.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static void showMainMenuHelp() {
3838
System.out.println(" 0- Exit");
3939
}
4040

41-
// in shop
41+
// in shopAndCollection
4242
static void showShopHelp() {
4343
System.out.println("___--Shop Menu--___");
4444
System.out.println(" 1- Show Cards In Shop");
@@ -86,7 +86,7 @@ static void InterCardName() {
8686
}
8787

8888
static void thereIsNoCardWithThisNameInShop() {
89-
System.out.println("There is no card with this name in shop cards :(");
89+
System.out.println("There is no card with this name in shopAndCollection cards :(");
9090
}
9191

9292
static void thereIsNoCardWithThisNameInCollection() {
@@ -118,7 +118,7 @@ static void haveXNumberOfCardIDInYourCollection(int numberOfFoundIDs) {
118118
}
119119

120120
static void existACardWithThisNameInShop() {
121-
System.out.println("There is a card with this name in shop :)");
121+
System.out.println("There is a card with this name in shopAndCollection :)");
122122
}
123123

124124
// in account:

src/view/fxmlControllers/AlertController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void resetButton(MouseEvent mouseEvent) {
5454
}
5555
}
5656

57-
public static AlertController setAndShowAndWaitToGetResult (String text, boolean haveAcceptButton) {
57+
public static AlertController setAndShowAndGetResultByAnAlertController(String text, boolean haveAcceptButton) {
5858
FXMLLoader fxmlLoader = new FXMLLoader(AlertController.class.getResource("../fxmls/alert.fxml"));
5959
AnchorPane alertPane = null;
6060
try {

0 commit comments

Comments
 (0)