parent
d054ab04c3
commit
6c29b8e493
@ -1,80 +0,0 @@ |
||||
package net.pokeranalytics.android.model.handhistory |
||||
|
||||
import net.pokeranalytics.android.exceptions.PAIllegalStateException |
||||
import net.pokeranalytics.android.model.realm.handhistory.Card |
||||
|
||||
/*** |
||||
* An interface used for board changes notifications |
||||
*/ |
||||
interface BoardChangedListener { |
||||
fun boardChanged() |
||||
} |
||||
|
||||
/*** |
||||
* The BoardManager purpose is to manage the cards from a hand history board |
||||
* and notify its listener when a change occurs |
||||
*/ |
||||
class BoardManager(cards: List<Card>, var listener: BoardChangedListener) { |
||||
|
||||
/*** |
||||
* The sorted list of cards |
||||
*/ |
||||
private var sortedBoardCards: MutableList<Card> = mutableListOf() |
||||
|
||||
/*** |
||||
* All cards |
||||
*/ |
||||
val allCards: List<Card> |
||||
get() { |
||||
return this.sortedBoardCards |
||||
} |
||||
|
||||
init { |
||||
this.sortedBoardCards = cards.sortedBy { it.index }.toMutableList() |
||||
} |
||||
|
||||
/*** |
||||
* Adds a card to the board, notifies the listener |
||||
*/ |
||||
fun add(card: Card) { |
||||
|
||||
this.sortedBoardCards.lastOrNull()?.let { |
||||
if (it.suit == null) { |
||||
it.suit = Card.Suit.UNDEFINED |
||||
} |
||||
} |
||||
|
||||
if (this.sortedBoardCards.size == 5) { |
||||
throw PAIllegalStateException("Can't add anymore cards") |
||||
} |
||||
|
||||
card.index = this.sortedBoardCards.size |
||||
|
||||
this.sortedBoardCards.add(card) |
||||
this.listener.boardChanged() |
||||
} |
||||
|
||||
/*** |
||||
* Clears the street's cards, notifies the listener |
||||
*/ |
||||
fun clearStreet(street: Street) { |
||||
this.sortedBoardCards.removeAll { it.street == street } |
||||
this.listener.boardChanged() |
||||
} |
||||
|
||||
/*** |
||||
* Returns the last card of a given [street] |
||||
*/ |
||||
fun lastCard(street: Street) : Card? { |
||||
return this.sortedBoardCards.lastOrNull { it.street == street } |
||||
} |
||||
|
||||
/*** |
||||
* Remove the given [card], notifies the listener |
||||
*/ |
||||
fun remove(card: Card) { |
||||
this.sortedBoardCards.remove(card) |
||||
this.listener.boardChanged() |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue