From e7782599e103b69efe8e48485739b1968403e733 Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 21 Jan 2020 12:29:24 +0100 Subject: [PATCH] Fixing todos in the HHBuilder --- .../model/handhistory/ComputedAction.kt | 32 ++++++++- .../android/model/handhistory/HHBuilder.kt | 65 ++++++++++++++++--- .../android/model/realm/handhistory/Action.kt | 47 +++++++++++++- 3 files changed, 133 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/model/handhistory/ComputedAction.kt b/app/src/main/java/net/pokeranalytics/android/model/handhistory/ComputedAction.kt index 53b53df6..200bf607 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/handhistory/ComputedAction.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/handhistory/ComputedAction.kt @@ -4,6 +4,7 @@ import net.pokeranalytics.android.model.realm.handhistory.Action import net.pokeranalytics.android.ui.adapter.HandRowType import net.pokeranalytics.android.ui.view.RowRepresentable import timber.log.Timber +import kotlin.math.min interface HandHistoryRow : RowRepresentable { @@ -36,9 +37,18 @@ class ComputedAction(var action: Action, * Also calculates the player remaining stack if possible */ fun setEffectiveAmount(amount: Double) { + val oldEffective = this.action.effectiveAmount + this.action.effectiveAmount = amount this.playerRemainingStack?.let { - this.playerRemainingStack = it - amount + + val oldPlayerRemainingStack = it + oldEffective + val revisedAmount = min(amount, oldPlayerRemainingStack) + val remainingStack = it - revisedAmount + oldEffective + this.playerRemainingStack = remainingStack + + this.action.toggleType(remainingStack) + } } @@ -57,6 +67,26 @@ class ComputedAction(var action: Action, } } + fun setAmount(amount: Double) { + + val oldAmount = this.action.amount + val remainingStack = this.playerRemainingStack + + if (oldAmount != null && remainingStack != null) { + + val oldPlayerRemainingStack = remainingStack + oldAmount + val revisedAmount = min(amount, oldPlayerRemainingStack) + val revisedRemainingStack = remainingStack - revisedAmount + oldAmount + this.playerRemainingStack = revisedRemainingStack + + this.action.toggleType(remainingStack) + + } else { + this.action.amount = amount + } + + } + /*** * Returns whether the action type can be edited * SB / BB cannot have their action type edited diff --git a/app/src/main/java/net/pokeranalytics/android/model/handhistory/HHBuilder.kt b/app/src/main/java/net/pokeranalytics/android/model/handhistory/HHBuilder.kt index e145f16c..46b1e6ff 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/handhistory/HHBuilder.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/handhistory/HHBuilder.kt @@ -10,7 +10,6 @@ import net.pokeranalytics.android.ui.view.handhistory.StreetCardHeader import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable import timber.log.Timber import java.util.* -import kotlin.math.min enum class HHKeyboard { ACTION, @@ -160,7 +159,16 @@ class HHBuilder { dropNextActionsIfNecessary(index) - // TODO si on set une action et que les actions precedentes ne sont pas renseignees, on fold PF ou check apres + // Automatically sets action for the previous empty actions + getPreviousEmptyActions(index).forEach { + + val lastSignificant = getLastSignificantAction(index) + if (lastSignificant != null) { + it.action.type = Action.Type.FOLD + } else { + it.action.type = Action.Type.CHECK + } + } when (actionType) { Action.Type.CALL -> { @@ -183,8 +191,13 @@ class HHBuilder { return computedAction.requiresAmount } + private fun getPreviousEmptyActions(index: Int) : List { + val street = this.actionForIndex(index).action.street + return this.sortedActions.take(index).filter { it.action.street == street && it.action.type == null } + } + /*** - * Removes the actions, if necessary, following an action change + * Removes the actions, if necessary (those not automatically created), following an action change * We want drop all non-auto added rows after the index */ private fun dropNextActionsIfNecessary(index: Int) { @@ -192,6 +205,10 @@ class HHBuilder { // this.sortedActions = this.sortedActions.take(dropIndex) } + private fun remainingActivePlayerCountAtStreetStart(index: Int) : Int { + return 0 + } + /*** * Sets the amount for the action at the provided [index] * In the case of an UNDEFINED_ALLIN, define if it's a RAISE_ALLIN or a CALL_ALLIN @@ -201,12 +218,16 @@ class HHBuilder { val computedAction = this.actionForIndex(index) Timber.d(">>> Sets $amount at index: $index, for action ${computedAction.action.type}") - val revisedAmount = computedAction.playerRemainingStack?.let { min(it, amount) } ?: amount - computedAction.action.amount = revisedAmount - - // TODO si on change un montant de mise, on change les calls suivants + computedAction.setAmount(amount) when (computedAction.action.type) { + Action.Type.BET, Action.Type.RAISE, Action.Type.BET_ALLIN, Action.Type.RAISE_ALLIN -> { + + getNextCalls(index).forEach { + it.setEffectiveAmount(amount) + } + + } Action.Type.UNDEFINED_ALLIN -> { getLastSignificantAction(index)?.action?.amount?.let { significantActionAmount -> @@ -269,6 +290,22 @@ class HHBuilder { return null } + private fun getNextCalls(index: Int) : List { + val nextSignificantIndex = getNextSignificantAction(index)?.action?.index ?: lastIndexOfStreet(index) + return this.sortedActions.filter { + it.action.index in (index + 1) until nextSignificantIndex && + (it.action.type == Action.Type.CALL || it.action.type == Action.Type.CALL_ALLIN) + } + } + + /*** + * Returns the last action index of the street, for the action at [index] + */ + private fun lastIndexOfStreet(index: Int) : Int { + val street = this.actionForIndex(index).action.street + return this.sortedActions.last { it.action.street == street }.action.index + } + /*** * Returns the last user action, if any, for the action at the provided [index] */ @@ -284,12 +321,22 @@ class HHBuilder { * Returns the last significant player action, if any, for the action at the provided [index] */ private fun getLastSignificantAction(index: Int): ComputedAction? { + val street = this.actionForIndex(index).action.street Timber.d("**** this.sortedActions.size = ${this.sortedActions.size}") - val previousActions = this.sortedActions.take(index) + val previousActions = this.sortedActions.take(index).filter { it.action.street == street } Timber.d("**** this.sortedActions.size = ${this.sortedActions.size}") return previousActions.lastOrNull { it.action.isActionSignificant } } + /*** + * Returns the next significant player action in the street, if any, for the action at the provided [index] + */ + private fun getNextSignificantAction(index: Int): ComputedAction? { + val street = this.actionForIndex(index).action.street + val nextActions = this.sortedActions.drop(index + 1).filter { it.action.street == street } + return nextActions.firstOrNull() { it.action.isActionSignificant } + } + fun setNumberOfPlayers(playerCount: Int) { this.handHistory.numberOfPlayers = playerCount this.positions = Position.positionsPerPlayers(playerCount) @@ -312,7 +359,7 @@ class HHBuilder { var potSize = 0.0 Street.values().forEach { street -> - val actions = this.sortedActions.filter { it.action.street == street.ordinal } + val actions = this.sortedActions.filter { it.action.street == street } if (actions.isNotEmpty()) { // Name of the street + pot size if not preflop 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 e9954ee1..301efc51 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 @@ -3,6 +3,7 @@ package net.pokeranalytics.android.model.realm.handhistory import io.realm.RealmObject import net.pokeranalytics.android.R import net.pokeranalytics.android.exceptions.PAIllegalStateException +import net.pokeranalytics.android.model.handhistory.Street import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowViewType import net.pokeranalytics.android.util.extensions.formatted @@ -47,7 +48,14 @@ open class Action : RealmObject() { /*** * The street of the action */ - var street: Int = 0 + private var streetIdentifier: Int = 0 + var street: Street + set(value) { + this.streetIdentifier = value.ordinal + } + get() { + return streetIdentifier.let { Street.values()[it] } + } /*** * The index of the action @@ -95,4 +103,41 @@ open class Action : RealmObject() { return amount?.formatted() } + fun toggleType(remainingStack: Double) { + + when (this.type) { + Type.BET -> { + if (remainingStack == 0.0) { + this.type = Type.BET_ALLIN + } + } + Type.BET_ALLIN -> { + if (remainingStack > 0.0) { + this.type = Type.BET + } + } + Type.RAISE -> { + if (remainingStack == 0.0) { + this.type = Type.RAISE_ALLIN + } + } + Type.RAISE_ALLIN -> { + if (remainingStack > 0.0) { + this.type = Type.RAISE + } + } + Type.CALL -> { + if (remainingStack == 0.0) { + this.type = Type.CALL_ALLIN + } + } + Type.CALL_ALLIN -> { + if (remainingStack > 0.0) { + this.type = Type.CALL + } + } + else -> {} + } + } + } \ No newline at end of file