From 2da1f6f410ca6cb8643742a1110f1c9d84d997e4 Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 25 Feb 2020 15:28:55 +0100 Subject: [PATCH] Remaining stack calculations --- .../modules/handhistory/model/ActionList.kt | 44 +++++++++++++++++-- .../handhistory/model/ComputedAction.kt | 2 +- .../handhistory/model/HandHistoryViewModel.kt | 36 +++++---------- 3 files changed, 53 insertions(+), 29 deletions(-) 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 ac5092fb..cac4ac68 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 @@ -15,7 +15,7 @@ interface ActionManager { fun getPlayerNextStreetActions(index: Int): List fun dropNextActions(index: Int) fun allinAmountSet(positionIndex: Int) - fun blindSet(type: Action.Type, amount: Double) + fun blindsUpdated(type: Action.Type, amount: Double) } interface ActionListListener : PlayerSetupCreationListener { @@ -460,10 +460,15 @@ class ActionList(var listener: ActionListListener) : ArrayList() /*** * Sets the blinds value in the hand history */ - override fun blindSet(type: Action.Type, amount: Double) { + override fun blindsUpdated(type: Action.Type, amount: Double) { when (type) { Action.Type.POST_SB -> this.handHistory.smallBlind = amount - Action.Type.POST_BB -> this.handHistory.bigBlind = amount + Action.Type.POST_BB -> { + this.handHistory.bigBlind = amount + if (this.handHistory.bigBlindAnte) { + this.updateRemainingStacksForPositions(listOf()) + } + } else -> throw PAIllegalStateException("Should never happen") } } @@ -570,4 +575,37 @@ class ActionList(var listener: ActionListListener) : ArrayList() } } + /*** + * Updates the remaining stacks for each action of the big blind + */ + fun updateBigBlindRemainingStack() { + val bbIndex = this.positions.indexOf(Position.BB) + this.updateRemainingStacksForPositions(listOf(bbIndex)) + } + + /*** + * Recomputes all remaining stacks for the given [positions] + */ + fun updateRemainingStacksForPositions(positions: List) { + + val ante = if (this.handHistory.bigBlindAnte) this.handHistory.bigBlind ?: 0.0 else this.handHistory.ante + + positions.forEach { position -> + this.handHistory.playerSetupForPosition(position)?.stack?.let { initialStack -> + + var remainingStack = initialStack - ante + val playerActions = this.filter { it.action.position == position } + playerActions.forEach { + remainingStack -= it.action.effectiveAmount + it.playerRemainingStack = remainingStack + if (it.action.type?.isAllin == true) { + it.action.amount = remainingStack + } + } + } + + } + + } + } 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 3a7d1f27..94caa2dd 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 @@ -127,7 +127,7 @@ class ComputedAction(var manager: ActionManager, this.manager.allinAmountSet(this.action.position) } this.action.type?.isBlind == true -> { - this.manager.blindSet(this.action.type!!, amount) + this.manager.blindsUpdated(this.action.type!!, amount) } } 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 9eafe3fb..58907ba7 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 @@ -75,6 +75,9 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra val currentSelection: HHSelection get() { return selectionLiveData.value ?: throw PAIllegalStateException("No selection") } + /*** + * Returns the current selected keyboard + */ val currentKeyboard: HHKeyboard? get() { this.selectionLiveData.value?.let { @@ -821,39 +824,22 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra return this.sortedActions.positions.elementAt(positionIndex) } + /*** + * Sets the ante value + */ fun setAnte(ante: Double) { this.handHistory.ante = ante val positionIndexes = (0..this.handHistory.numberOfPlayers).toList() - updatePlayerRemainingStacks(positionIndexes) + this.sortedActions.updateRemainingStacksForPositions(positionIndexes) } + /*** + * Sets whether the hand uses big blind ante or not + */ fun setBigBlindAnte(bigBlindAnte: Boolean) { this.handHistory.bigBlindAnte = bigBlindAnte - val bbIndex = this.sortedActions.positions.indexOf(Position.BB) - updatePlayerRemainingStacks(listOf(bbIndex)) - } - - private fun updatePlayerRemainingStacks(positions: List) { - - val ante = if (this.handHistory.bigBlindAnte) this.handHistory.bigBlind ?: 0.0 else this.handHistory.ante - - positions.forEach { position -> - this.handHistory.playerSetupForPosition(position)?.stack?.let { initialStack -> - - var remainingStack = initialStack - ante - val playerActions = this.sortedActions.filter { it.action.position == position } - playerActions.forEach { - remainingStack -= it.action.effectiveAmount - it.playerRemainingStack = remainingStack - if (it.action.type?.isAllin == true) { - it.action.amount = remainingStack - } - } - } - - } - + this.sortedActions.updateBigBlindRemainingStack() } } \ No newline at end of file