Fixing todos in the HHBuilder

hh
Laurent 6 years ago
parent 39709e3e97
commit e7782599e1
  1. 32
      app/src/main/java/net/pokeranalytics/android/model/handhistory/ComputedAction.kt
  2. 65
      app/src/main/java/net/pokeranalytics/android/model/handhistory/HHBuilder.kt
  3. 47
      app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Action.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.adapter.HandRowType
import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowRepresentable
import timber.log.Timber import timber.log.Timber
import kotlin.math.min
interface HandHistoryRow : RowRepresentable { interface HandHistoryRow : RowRepresentable {
@ -36,9 +37,18 @@ class ComputedAction(var action: Action,
* Also calculates the player remaining stack if possible * Also calculates the player remaining stack if possible
*/ */
fun setEffectiveAmount(amount: Double) { fun setEffectiveAmount(amount: Double) {
val oldEffective = this.action.effectiveAmount
this.action.effectiveAmount = amount this.action.effectiveAmount = amount
this.playerRemainingStack?.let { 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 * Returns whether the action type can be edited
* SB / BB cannot have their action type edited * SB / BB cannot have their action type edited

@ -10,7 +10,6 @@ import net.pokeranalytics.android.ui.view.handhistory.StreetCardHeader
import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable import net.pokeranalytics.android.ui.view.rowrepresentable.CustomizableRowRepresentable
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.*
import kotlin.math.min
enum class HHKeyboard { enum class HHKeyboard {
ACTION, ACTION,
@ -160,7 +159,16 @@ class HHBuilder {
dropNextActionsIfNecessary(index) 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) { when (actionType) {
Action.Type.CALL -> { Action.Type.CALL -> {
@ -183,8 +191,13 @@ class HHBuilder {
return computedAction.requiresAmount return computedAction.requiresAmount
} }
private fun getPreviousEmptyActions(index: Int) : List<ComputedAction> {
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 * We want drop all non-auto added rows after the index
*/ */
private fun dropNextActionsIfNecessary(index: Int) { private fun dropNextActionsIfNecessary(index: Int) {
@ -192,6 +205,10 @@ class HHBuilder {
// this.sortedActions = this.sortedActions.take(dropIndex) // this.sortedActions = this.sortedActions.take(dropIndex)
} }
private fun remainingActivePlayerCountAtStreetStart(index: Int) : Int {
return 0
}
/*** /***
* Sets the amount for the action at the provided [index] * 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 * 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) val computedAction = this.actionForIndex(index)
Timber.d(">>> Sets $amount at index: $index, for action ${computedAction.action.type}") Timber.d(">>> Sets $amount at index: $index, for action ${computedAction.action.type}")
val revisedAmount = computedAction.playerRemainingStack?.let { min(it, amount) } ?: amount computedAction.setAmount(amount)
computedAction.action.amount = revisedAmount
// TODO si on change un montant de mise, on change les calls suivants
when (computedAction.action.type) { 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 -> { Action.Type.UNDEFINED_ALLIN -> {
getLastSignificantAction(index)?.action?.amount?.let { significantActionAmount -> getLastSignificantAction(index)?.action?.amount?.let { significantActionAmount ->
@ -269,6 +290,22 @@ class HHBuilder {
return null return null
} }
private fun getNextCalls(index: Int) : List<ComputedAction> {
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] * 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] * Returns the last significant player action, if any, for the action at the provided [index]
*/ */
private fun getLastSignificantAction(index: Int): ComputedAction? { private fun getLastSignificantAction(index: Int): ComputedAction? {
val street = this.actionForIndex(index).action.street
Timber.d("**** this.sortedActions.size = ${this.sortedActions.size}") 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}") Timber.d("**** this.sortedActions.size = ${this.sortedActions.size}")
return previousActions.lastOrNull { it.action.isActionSignificant } 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) { fun setNumberOfPlayers(playerCount: Int) {
this.handHistory.numberOfPlayers = playerCount this.handHistory.numberOfPlayers = playerCount
this.positions = Position.positionsPerPlayers(playerCount) this.positions = Position.positionsPerPlayers(playerCount)
@ -312,7 +359,7 @@ class HHBuilder {
var potSize = 0.0 var potSize = 0.0
Street.values().forEach { street -> 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()) { if (actions.isNotEmpty()) {
// Name of the street + pot size if not preflop // Name of the street + pot size if not preflop

@ -3,6 +3,7 @@ package net.pokeranalytics.android.model.realm.handhistory
import io.realm.RealmObject import io.realm.RealmObject
import net.pokeranalytics.android.R import net.pokeranalytics.android.R
import net.pokeranalytics.android.exceptions.PAIllegalStateException 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.RowRepresentable
import net.pokeranalytics.android.ui.view.RowViewType import net.pokeranalytics.android.ui.view.RowViewType
import net.pokeranalytics.android.util.extensions.formatted import net.pokeranalytics.android.util.extensions.formatted
@ -47,7 +48,14 @@ open class Action : RealmObject() {
/*** /***
* The street of the action * 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 * The index of the action
@ -95,4 +103,41 @@ open class Action : RealmObject() {
return amount?.formatted() 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 -> {}
}
}
} }
Loading…
Cancel
Save