Skip to content

Commit

Permalink
- optimized initial deck creation
Browse files Browse the repository at this point in the history
- (breaking change!) changed Decks to Map
  • Loading branch information
NotAWiz4rd committed Jun 2, 2019
1 parent 017998b commit 38fde05
Show file tree
Hide file tree
Showing 6 changed files with 966 additions and 988 deletions.
6 changes: 6 additions & 0 deletions app/src/main/java/com/afms/cahgame/data/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public Card(String text, Colour colour) {
this.text = text;
}

public Card(Colour colour, String text) {
this.id = 0;
this.colour = colour;
this.text = text;
}

public Card(int id, Colour colour, String text) {
this.id = id;
this.colour = colour;
Expand Down
10 changes: 4 additions & 6 deletions app/src/main/java/com/afms/cahgame/game/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ public class Card extends com.afms.cahgame.data.Card implements Serializable {
public Card() {
}

public Card(int id, Colour colour, String text) {
super(id, colour, text);
public Card(Colour colour, String text) {
super(colour, text);
}

public Card(Colour colour, String text) {
super();
setColour(colour);
setText(text);
public Card(com.afms.cahgame.data.Card dataCard) {
super(dataCard.getColour(), dataCard.getText());
}

public Player getOwner() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void onItemClick(String result) {
List<Card> gameCards = new ArrayList<>();
gameCards.addAll(selectedDeck.getBlackCards());
gameCards.addAll(selectedDeck.getWhiteCards());
List<Integer> dataCardIds = gameCards.stream().map(e -> Database.createNewCard(e.getText(), e.getColour()).getId()).collect(Collectors.toList());
List<Integer> dataCardIds = gameCards.stream().map(e -> Database.createNewCard(e.getText(), e.getColour())).collect(Collectors.toList());
gameCards.clear();
com.afms.cahgame.data.Deck deck = new com.afms.cahgame.data.Deck();
deck.setName(label_explore_decks_name.getText().toString());
Expand Down Expand Up @@ -201,7 +201,7 @@ public void clearReference() {
deckSelectorDialog.setResultListener(new ResultListener() {
@Override
public void onItemClick(String result) {
selectedDeck = Util.convertDataDeckToPlayDeck(Util.getDataDeckFromName(result));
selectedDeck = Database.getDeck(result);
updateCardList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat

label_dialog_deckselector_title.setText(Objects.requireNonNull(getArguments()).getString("title"));
DeckListAdapter deckListAdapter = new DeckListAdapter(Objects.requireNonNull(getContext()), new ArrayList<>());
deckListAdapter.addAll(Database.getDecks().stream().map(Util::convertDataDeckToPlayDeck).collect(Collectors.toList()));
deckListAdapter.addAll(Database.getDecks().values().stream().map(Util::convertDataDeckToPlayDeck).collect(Collectors.toList()));
list_dialog_deckselector.setAdapter(deckListAdapter);
list_dialog_deckselector.setOnItemClickListener((parent, view1, position, id) -> {
String deckName = ((TextView) view1.findViewById(R.id.item_deckselector_name)).getText().toString();
Expand Down
61 changes: 27 additions & 34 deletions app/src/main/java/com/afms/cahgame/util/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import java.util.Objects;

public class Database {
private static List<Deck> decks = new ArrayList<>();
private static List<Card> cards = new ArrayList<>();
private static Map<String, Deck> decks = new HashMap<>();
private static ArrayList<Card> cards = new ArrayList<>();
private static Map<String, Lobby> lobbies = new HashMap<>();

private static FirebaseDatabase database = FirebaseDatabase.getInstance();
Expand All @@ -49,13 +49,15 @@ private static void initializeDecksDatabaseConnection() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
// this interesting construct is here to suffice Firebases need for type safety
GenericTypeIndicator<ArrayList<Deck>> genericTypeIndicator = new GenericTypeIndicator<ArrayList<Deck>>() {
GenericTypeIndicator<Map<String, Deck>> genericTypeIndicator = new GenericTypeIndicator<Map<String, Deck>>() {
};
decks = dataSnapshot.getValue(genericTypeIndicator);
if (decks == null) {
decks = new ArrayList<>();
} else if (decks.size() == 0) {
Util.createAllCardsDeck();
decks = new HashMap<>();
} else if (decks.size() < 3) {
Util.createStandardDeck();
Util.createGerman1();
Util.createGerman2();
}
}

Expand Down Expand Up @@ -101,9 +103,6 @@ public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
cards = dataSnapshot.getValue(genericTypeIndicator);
if (cards == null) {
cards = new ArrayList<>();
} else if (cards.size() < 400) {
Util.createStandardCards();
Util.createAllCardsDeck();
}
}

Expand Down Expand Up @@ -212,10 +211,9 @@ public static void sendMessageInLobby(String lobbyId, String playername, String
* @param deckName Name of the deck.
*/
public static void addCardToDeck(Integer cardId, String deckName) {
Deck deck = Util.getDataDeckFromName(deckName);
Deck deck = decks.get(deckName);
if (deck != null) {
int deckIndex = Database.getDecks().indexOf(deck);
decks.get(deckIndex).addCard(cardId);
deck.addCard(cardId);
decksReference.setValue(decks);
}
}
Expand Down Expand Up @@ -285,16 +283,12 @@ public static void deleteCard(Integer cardId) {
* @param text Text of the card.
* @param colour Colour of the card.
*/
public static Card createNewCard(String text, Colour colour) {
boolean cardExists = cards.stream().anyMatch(card -> card.getText().equals(text));
if (cardExists) {
return cards.stream().filter(card -> card.getText().equals(text)).findAny().get();
}
public static int createNewCard(String text, Colour colour) {
int id = cards.size() == 0 ? 0 : cards.get(cards.size() - 1).getId() + 1;
Card card = new Card(id, colour, text);
cards.add(card);
cardsReference.setValue(cards);
return card;
return id;
}

/**
Expand All @@ -303,15 +297,18 @@ public static Card createNewCard(String text, Colour colour) {
*
* @param newCards A list of cards without proper IDs.
*/
public static void addNewCards(List<Card> newCards) {
public static ArrayList<Integer> addNewCards(List<Card> newCards) {
int id = cards.size() == 0 ? 0 : cards.get(cards.size() - 1).getId() + 1;
ArrayList<Integer> ids = new ArrayList<>();
for (Card card : newCards) {
card.setId(id);
ids.add(id);
id++;
}

cards.addAll(newCards);
cardsReference.setValue(cards);
return ids;
}

/**
Expand All @@ -321,10 +318,9 @@ public static void addNewCards(List<Card> newCards) {
* @param cardId CardId of the card which is to be removed.
*/
public static void removeCardFromDeck(String deckName, Integer cardId) {
Deck deck = Util.getDataDeckFromName(deckName);
Deck deck = decks.get(deckName);
if (deck != null) {
int deckIndex = decks.indexOf(deck);
decks.get(deckIndex).removeCard(cardId);
deck.removeCard(cardId);
decksReference.setValue(decks);
}
}
Expand All @@ -335,11 +331,8 @@ public static void removeCardFromDeck(String deckName, Integer cardId) {
* @param deckName The name of the deck.
*/
public static void removeDeck(String deckName) {
Deck deck = Util.getDataDeckFromName(deckName);
if (deck != null) {
decks.remove(deck);
decksReference.setValue(decks);
}
decks.remove(deckName);
decksReference.setValue(decks);
}

/**
Expand All @@ -348,7 +341,7 @@ public static void removeDeck(String deckName) {
* @param deckName The name of the new deck.
*/
public static void addDeck(String deckName) {
decks.add(new Deck(deckName));
decks.put(deckName, new Deck(deckName));
decksReference.setValue(decks);
}

Expand All @@ -358,7 +351,7 @@ public static void addDeck(String deckName) {
* @param deck The new deck.
*/
public static void addDeck(Deck deck) {
decks.add(deck);
decks.put(deck.getName(), deck);
decksReference.setValue(decks);
}

Expand All @@ -369,7 +362,7 @@ public static void addDeck(Deck deck) {
* @return GameDeck of the given deckname.
*/
public static com.afms.cahgame.game.Deck getDeck(String deckname) {
return Util.convertDataDeckToPlayDeck(Util.getDataDeckFromName(deckname));
return Util.convertDataDeckToPlayDeck(decks.get(deckname));
}

//....................................Getters and Setters......................................
Expand All @@ -379,7 +372,7 @@ public static com.afms.cahgame.game.Deck getDeck(String deckname) {
*
* @return Current list of all decks.
*/
public static List<Deck> getDecks() {
public static Map<String, Deck> getDecks() {
if (decks == null) {
initializeDecksDatabaseConnection();
}
Expand All @@ -391,7 +384,7 @@ public static List<Deck> getDecks() {
*
* @return Current list of all cards.
*/
public static List<Card> getCards() {
public static ArrayList<Card> getCards() {
if (cards == null) {
initializeCardsDatabaseConnection();
}
Expand All @@ -415,7 +408,7 @@ public static Map<String, Lobby> getLobbies() {
*
* @param decks Decks.
*/
public static void setDecks(List<Deck> decks) {
public static void setDecks(Map<String, Deck> decks) {
Database.decks = decks;
decksReference.setValue(Database.decks);
}
Expand All @@ -425,7 +418,7 @@ public static void setDecks(List<Deck> decks) {
*
* @param cards Cards.
*/
public static void setCards(List<Card> cards) {
public static void setCards(ArrayList<Card> cards) {
Database.cards = cards;
cardsReference.setValue(Database.cards);
}
Expand Down
Loading

0 comments on commit 38fde05

Please sign in to comment.