From 0a603bab1ab21fe9a8a8d8a0cc0e6414cc7c271d Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 13 Feb 2020 12:57:11 +0100 Subject: [PATCH] Fixes various position issues --- .../android/model/realm/handhistory/Action.kt | 16 +++-- .../model/realm/handhistory/HandHistory.kt | 11 +-- .../modules/handhistory/HandHistoryAdapter.kt | 14 ++-- .../modules/handhistory/model/ActionList.kt | 24 ++++++- .../ui/modules/handhistory/model/CardsRow.kt | 39 +++------- .../handhistory/model/ComputedAction.kt | 9 ++- .../handhistory/model/HandHistoryViewModel.kt | 71 ++++++++++++------- .../handhistory/model/PlayerCardsRow.kt | 50 +++++++++++++ .../handhistory/model/PlayerSetupRow.kt | 25 ++++--- 9 files changed, 173 insertions(+), 86 deletions(-) create mode 100644 app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerCardsRow.kt diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Action.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Action.kt index 986c25a7..7199b3ac 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Action.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Action.kt @@ -69,14 +69,22 @@ open class Action : RealmObject() { } } - val requiresOpponentDecision: Boolean + val isAllin: Boolean get() { - return when(this) { - CALL, CALL_ALLIN, FOLD -> false - else -> true + return when (this) { + UNDEFINED_ALLIN, BET_ALLIN, RAISE_ALLIN, CALL_ALLIN -> true + else -> false } } +// val requiresOpponentDecision: Boolean +// get() { +// return when(this) { +// CALL, CALL_ALLIN, FOLD -> false +// else -> true +// } +// } + override val viewType: Int = RowViewType.TITLE_GRID.ordinal companion object { diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt index 4cd9ef59..54270486 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt @@ -7,7 +7,6 @@ import io.realm.annotations.PrimaryKey import net.pokeranalytics.android.model.filter.Filterable import net.pokeranalytics.android.model.handhistory.HandSetup import net.pokeranalytics.android.model.handhistory.Position -import net.pokeranalytics.android.model.handhistory.Street import net.pokeranalytics.android.model.interfaces.Identifiable import net.pokeranalytics.android.model.interfaces.TimeFilterable import net.pokeranalytics.android.model.realm.Session @@ -137,9 +136,9 @@ open class HandHistory : RealmObject(), RowRepresentable, Identifiable, Filterab this.actions.add(action) } - fun cardsForStreet(street: Street): MutableList { - return this.board.sortedBy { it.index }.take(street.totalBoardCards).toMutableList() - } +// fun cardsForStreet(street: Street): MutableList { +// return this.board.sortedBy { it.index }.take(street.totalBoardCards).toMutableList() +// } // // fun playerSetupForPosition(position: Int) : PlayerSetup { // this.playerSetups.firstOrNull { it.position == position }?.let { @@ -149,6 +148,10 @@ open class HandHistory : RealmObject(), RowRepresentable, Identifiable, Filterab // val ps = PlayerSetup() // ps.position = position // return ps +// } + +// fun getOrCreatePlayerSetup(position: Int): PlayerSetup { +// return this.playerSetupForPosition(position) ?: createPlayerSetup(position) // } fun playerSetupForPosition(position: Int): PlayerSetup? { diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt index c54688e5..1ae8d1df 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt @@ -586,21 +586,19 @@ class HandHistoryAdapter( } return@setOnTouchListener true } - } - } override fun onBind(position: Int, row: RowRepresentable, adapter: RecyclerAdapter) { super.onBind(position, row, adapter) - val setup = row as PlayerSetupRow - val state = setup.state + val setupRow = row as PlayerSetupRow + val state = setupRow.state this.delegate = adapter.delegate // Title - itemView.title.text = setup.title(itemView.context) + itemView.title.text = setupRow.title(itemView.context) // Position recycler val visibility = if (state == PlayerSetupRow.State.SETUP_ONLY) View.GONE else View.VISIBLE @@ -608,14 +606,14 @@ class HandHistoryAdapter( this.positionAdapter.positions = adapter.dataSource.contentForRow(row, itemView.context, Position::class) this.positionAdapter.setOnClickListener { pos -> - setup.closePosition() + setupRow.closePosition() this.delegate?.onRowValueChanged(pos, row) } // Position Button - itemView.posButton.text = setup.position?.value + itemView.posButton.text = adapter.dataSource.charSequenceForRow(row, itemView.context, PlayerSetupRow.Tag.POSITION.ordinal) itemView.posButton.setOnClickListener { - setup.showPosition() + setupRow.showPosition() itemView.positionRecyclerView.visibility = View.VISIBLE itemView.posButton.backgroundTintList = ColorStateList.valueOf(color(true)) } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ActionList.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ActionList.kt index 7c55b074..6874be45 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ActionList.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ActionList.kt @@ -14,9 +14,10 @@ interface ActionManager { fun getStreetNextCalls(index: Int): List fun getPlayerNextStreetActions(index: Int): List fun dropNextActions(index: Int) + fun allinAmountSet(position: Int) } -interface ActionListListener { +interface ActionListListener : PlayerSetupCreationListener { fun actionCountChanged() } @@ -25,6 +26,8 @@ interface ActionListListener { */ class ActionList(var listener: ActionListListener) : ArrayList(), ActionManager { + private lateinit var handHistory: HandHistory + /*** * Indicates whether the list size has changed */ @@ -35,6 +38,7 @@ class ActionList(var listener: ActionListListener) : ArrayList() */ fun load(handHistory: HandHistory) { + this.handHistory = handHistory this.positions = Position.positionsPerPlayers(handHistory.numberOfPlayers) this.clear() @@ -302,7 +306,7 @@ class ActionList(var listener: ActionListListener) : ArrayList() val action = Action() action.index = this.size - action.position = position.ordinal + action.position = this.positions.indexOf(position) action.street = street val computedAction = ComputedAction( @@ -436,6 +440,22 @@ class ActionList(var listener: ActionListListener) : ArrayList() fireListener() } + override fun allinAmountSet(positionIndex: Int) { + + var created = false + var playerSetup = this.handHistory.playerSetupForPosition(positionIndex) + if (playerSetup == null) { + playerSetup = this.handHistory.createPlayerSetup(positionIndex) + created = true + } + + playerSetup.stack = this.filter { it.action.position == positionIndex }.sumByDouble { it.action.effectiveAmount } + + if (created) { + this.listener.playerSetupCreated() + } + } + /*** * Returns the list of the player next street actions */ diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/CardsRow.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/CardsRow.kt index 94dde54a..5051f5c7 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/CardsRow.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/CardsRow.kt @@ -4,11 +4,9 @@ import io.realm.Realm import io.realm.RealmList import io.realm.RealmModel import net.pokeranalytics.android.exceptions.PAIllegalStateException -import net.pokeranalytics.android.model.handhistory.Position import net.pokeranalytics.android.model.handhistory.Street import net.pokeranalytics.android.model.realm.handhistory.Card import net.pokeranalytics.android.model.realm.handhistory.HandHistory -import net.pokeranalytics.android.model.realm.handhistory.PlayerSetup import net.pokeranalytics.android.ui.modules.handhistory.HandRowType interface CardHolder : RealmModel { @@ -84,8 +82,10 @@ abstract class CardsRow : HandHistoryRow { return } + var holderCreated = false if (this.cardHolder == null) { this.createHolder() + holderCreated = true } this.cardHolder?.cards?.lastOrNull()?.let { @@ -101,8 +101,14 @@ abstract class CardsRow : HandHistoryRow { card.index = this.cardCount this.cardHolder?.cards?.add(card) + + if (holderCreated) { + this.holderCreated() + } } + protected open fun holderCreated() { } + protected open fun canAddMoreCards(): Boolean { this.cardLimit()?.let { limit -> return !(this.cardCount >= limit && lastCard()?.suit != null) @@ -194,32 +200,3 @@ class StreetCardsRow(var street: Street, var handHistory: HandHistory) : CardsRo } } - -open class PlayerCardsRow(var position: Position?, - var positionIndex: Int?, - var handHistory: HandHistory, - var playerSetup: PlayerSetup?, - var maxCards: Int? = null) : CardsRow() { - - enum class Tag { - CARDS - } - - override val cardHolder: CardHolder? - get() { return this.playerSetup } - - override val viewType: Int = HandRowType.PLAYER_SUMMARY.ordinal - - override fun createHolder() { - this.playerSetup = this.handHistory.createPlayerSetup(this.positionIndex - ?: throw PAIllegalStateException("Can't create a PlayerSetup without a position")) - } - - override fun cardLimit() : Int? { - return this.maxCards - } - - override val realmInstance: Realm - get() { return this.handHistory.realm } - -} \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ComputedAction.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ComputedAction.kt index 6ce9960c..c272db14 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ComputedAction.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ComputedAction.kt @@ -114,9 +114,8 @@ class ComputedAction(var manager: ActionManager, getStreetNextCalls().forEach { it.updateEffectiveAmount() -// it.setEffectiveAmount(amount) } - getPlayerNextActions().forEach { + getPlayerNextStreetActions().forEach { it.updateEffectiveAmount() } @@ -137,6 +136,10 @@ class ComputedAction(var manager: ActionManager, else -> {} } + if (this.action.type?.isAllin == true) { + this.manager.allinAmountSet(this.action.position) + } + } /*** @@ -224,7 +227,7 @@ class ComputedAction(var manager: ActionManager, return this.manager.getStreetNextCalls(this.action.index) } - private fun getPlayerNextActions(): List { + private fun getPlayerNextStreetActions(): List { return this.manager.getPlayerNextStreetActions(this.action.index) } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt index 194abf39..86d7e3f9 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt @@ -27,6 +27,10 @@ enum class HHKeyboard { class HHSelection(var index: Int, var tag: Int) +interface PlayerSetupCreationListener { + fun playerSetupCreated() +} + class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentralizer, ActionListListener { /*** @@ -197,11 +201,14 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra addStreetHeader(rows, street) val positions = this.sortedActions.activePositions(lastActionIndex, true) - positions.forEach { - val positionIndex = this.sortedActions.positions.indexOf(it) - val playerCardsRow = PlayerCardsRow(it, positionIndex, this.handHistory, - this.handHistory.playerSetupForPosition(positionIndex), this.playerHandMaxCards) -// val playerCardsRow = PlayerCardsRow.newInstance(it, this.handHistory, positionIndex, this.playerHandMaxCards) + positions.forEach { position -> + val positionIndex = this.sortedActions.positions.indexOf(position) + val playerCardsRow = PlayerCardsRow(this, + position, + positionIndex, + this.handHistory, + this.handHistory.playerSetupForPosition(positionIndex), + this.playerHandMaxCards) rows.add(playerCardsRow) } } @@ -272,7 +279,7 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra } /*** - * Returns a list of positions that needs to act + * Returns a list of positions that needs to act after the current selected index */ fun positionsToAct(): List { return this.sortedActions.positionsToActAfterIndex(this.actionIndexForSelection) @@ -436,16 +443,16 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra /*** * Called when the board has changed */ - fun boardChanged(cards: List) { - this.rowRepresentables.filterIsInstance().forEach { - // it.cards = cards - } - } +// fun boardChanged(cards: List) { +// this.rowRepresentables.filterIsInstance().forEach { +// // it.cards = cards +// } +// } /*** * Saves the current hand state in the database */ - private fun save() { + fun save() { } @@ -493,6 +500,10 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra this.createRowRepresentation() } + override fun playerSetupCreated() { + this.createRowRepresentation() + } + fun amountChanged(amount: String?) { this.currentAmount = amount } @@ -561,6 +572,7 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra is StreetCardsRow -> row.cardsForTag(tag)?.formatted(context) is PlayerSetupRow -> { when (tag) { + PlayerSetupRow.Tag.POSITION.ordinal -> row.position?.value PlayerSetupRow.Tag.CARDS.ordinal -> row.cardHolder?.cards?.formatted(context) PlayerSetupRow.Tag.STACK.ordinal -> row.playerSetup?.stack?.formatted() else -> throw PAIllegalStateException("Unmanaged case with $row, tag = $tag") @@ -612,7 +624,8 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra val hh = this.handHistory val arrangedSetups = hh.playerSetups.sortedBy { it.position }.map { - createPlayerSetupRow(false, it) + val position = this.positionForIndex(it.position) + createPlayerSetupRow(false, position, it) }.toMutableList() val heroSetup = arrangedSetups.firstOrNull { it.playerSetup?.position == hh.heroIndex } @@ -621,7 +634,7 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra arrangedSetups.remove(it) arrangedSetups.add(0, it) } ?: run { - arrangedSetups.add(createPlayerSetupRow(true)) + arrangedSetups.add(0, createPlayerSetupRow(true)) } if (arrangedSetups.size < hh.numberOfPlayers) { @@ -629,42 +642,48 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra } return arrangedSetups - } /*** * Convenience method to create a PlayerSetupRow */ - private fun createPlayerSetupRow(hero: Boolean, playerSetup: PlayerSetup? = null): PlayerSetupRow { - return PlayerSetupRow(hero, null, null, this.handHistory, playerSetup, this.playerHandMaxCards) + private fun createPlayerSetupRow(hero: Boolean, position: Position? = null, playerSetup: PlayerSetup? = null): PlayerSetupRow { + return PlayerSetupRow(hero, position, playerSetup?.position, this.handHistory, playerSetup, this.playerHandMaxCards) } /*** - * Sets the both [positionIndex] and position for a PlayerSetup inside a [psRowRepresentable] + * Sets the both [positionIndex] and position for a PlayerSetup inside a [playerSetupRow] */ - fun setPlayerSetupPosition(psRowRepresentable: PlayerSetupRow, positionIndex: Int) { + fun setPlayerSetupPosition(playerSetupRow: PlayerSetupRow, positionIndex: Int) { var setupCreated = false - psRowRepresentable.playerSetup?.let { setup -> - setup.position = positionIndex + playerSetupRow.playerSetup?.let { playerSetup -> + playerSetup.position = positionIndex } ?: run { - psRowRepresentable.playerSetup = this.handHistory.createPlayerSetup(positionIndex) + playerSetupRow.playerSetup = this.handHistory.createPlayerSetup(positionIndex) setupCreated = true } - val position = this.sortedActions.positions.elementAt(positionIndex) - psRowRepresentable.position = position + val position = this.positionForIndex(positionIndex) + playerSetupRow.position = position - if (!psRowRepresentable.hero && setupCreated) { + if (!playerSetupRow.hero && setupCreated) { if (this.handHistory.undefinedPositions().isNotEmpty()) { val newPlayerRow = createPlayerSetupRow(false) - val index = this.indexOfRowRepresentable(psRowRepresentable) + val index = this.indexOfRowRepresentable(playerSetupRow) this.rowsLiveData.value?.add(index + 1, newPlayerRow) } + } + if (playerSetupRow.hero) { + this.handHistory.heroIndex = positionIndex } } + private fun positionForIndex(positionIndex: Int): Position { + return this.sortedActions.positions.elementAt(positionIndex) + } + } \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerCardsRow.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerCardsRow.kt new file mode 100644 index 00000000..fdcbcb76 --- /dev/null +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerCardsRow.kt @@ -0,0 +1,50 @@ +package net.pokeranalytics.android.ui.modules.handhistory.model + +import io.realm.Realm +import net.pokeranalytics.android.exceptions.PAIllegalStateException +import net.pokeranalytics.android.model.handhistory.Position +import net.pokeranalytics.android.model.realm.handhistory.HandHistory +import net.pokeranalytics.android.model.realm.handhistory.PlayerSetup +import net.pokeranalytics.android.ui.modules.handhistory.HandRowType + +open class PlayerCardsRow(private var playerSetupCreationListener: PlayerSetupCreationListener?, + var position: Position?, + private var positionIndex: Int?, + private var handHistory: HandHistory, + var playerSetup: PlayerSetup?, + var maxCards: Int? = null) : CardsRow() { + + constructor(position: Position?, + positionIndex: Int?, + handHistory: HandHistory, + playerSetup: PlayerSetup?, + maxCards: Int? = null) : this(null, position, positionIndex, handHistory, playerSetup, maxCards) + + enum class Tag { + CARDS + } + + override val cardHolder: CardHolder? + get() { return this.playerSetup } + + override val viewType: Int = HandRowType.PLAYER_SUMMARY.ordinal + + override fun createHolder() { + val position = this.positionIndex + ?: throw PAIllegalStateException("Can't create a PlayerSetup without a position") + + this.playerSetup = this.handHistory.createPlayerSetup(position) + } + + override fun cardLimit() : Int? { + return this.maxCards + } + + override val realmInstance: Realm + get() { return this.handHistory.realm } + + override fun holderCreated() { + this.playerSetupCreationListener?.playerSetupCreated() + } + +} \ No newline at end of file diff --git a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerSetupRow.kt b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerSetupRow.kt index c742fde0..9a74f60b 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerSetupRow.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerSetupRow.kt @@ -28,7 +28,23 @@ class PlayerSetupRow(var hero: Boolean = false, STACK } - var state: State = State.POSITIONS_ONLY + private var _state: State? = null + + val state: State + get() { + this._state?.let { + return it + } + return if (this.playerSetup != null) State.SETUP_ONLY else State.POSITIONS_ONLY + } + + fun showPosition() { + this._state = State.BOTH + } + + fun closePosition() { + this._state = null + } fun title(context: Context): String { return if (this.hero) { @@ -46,13 +62,6 @@ class PlayerSetupRow(var hero: Boolean = false, } } - fun showPosition() { - this.state = State.BOTH - } - - fun closePosition() { - this.state = State.SETUP_ONLY - } override val viewType: Int = HandRowType.PLAYER_SETUP.ordinal