From b942c12d123c25b3a3d53e1e8584344db0fc1414 Mon Sep 17 00:00:00 2001 From: Laurent Date: Thu, 9 Jan 2020 12:17:11 +0100 Subject: [PATCH] Adds algorithm to determine the user available actions --- .../android/model/handhistory/Builder.kt | 57 ++++++++++++++++++- .../model/handhistory/ComputedAction.kt | 1 + .../android/model/realm/handhistory/Action.kt | 24 +++++++- .../model/realm/handhistory/HandHistory.kt | 4 +- 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/model/handhistory/Builder.kt b/app/src/main/java/net/pokeranalytics/android/model/handhistory/Builder.kt index 2d5fc8c0..ca2f941e 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/handhistory/Builder.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/handhistory/Builder.kt @@ -1,5 +1,7 @@ package net.pokeranalytics.android.model.handhistory +import net.pokeranalytics.android.exceptions.PAIllegalStateException +import net.pokeranalytics.android.model.realm.handhistory.Action import net.pokeranalytics.android.model.realm.handhistory.HandHistory class Builder { @@ -13,7 +15,11 @@ class Builder { load() } - val actionsPerStreet = hashMapOf>() + private val actionsPerStreet = hashMapOf>() + private val sortedActions: List + get() { + return actionsPerStreet.flatMap { it.value } + } constructor(handSetup: HandSetup) { val handHistory = HandHistory() @@ -42,12 +48,59 @@ class Builder { val filteredActions = sortedActions.filter { it.street == street.ordinal } val computedActions = filteredActions.map { action -> totalPotSize += action.effectiveAmount - ComputedAction(action, potSize, action.positionRemainingStack) + ComputedAction(action, potSize, totalPotSize, action.positionRemainingStack) } this.actionsPerStreet[street] = computedActions potSize = totalPotSize } + } + + private fun availableActions(index: Int) : Set { + + val lastSignificantAction: ComputedAction? = getLastSignificantAction(index) + val lastUserAction: ComputedAction? = getLastUserAction(index) + + return if (lastSignificantAction != null) { + + when (lastSignificantAction.action.type) { + Action.Type.POST_BB, Action.Type.STRADDLE -> setOf(Action.Type.STRADDLE, Action.Type.FOLD, Action.Type.CALL, Action.Type.BET, Action.Type.BET_ALLIN) + Action.Type.BET, Action.Type.RAISE, Action.Type.RAISE_ALLIN, Action.Type.BET_ALLIN -> { + + val remainingStack = lastUserAction?.playerRemainingStack + val actionAmount = lastSignificantAction.action.amount + + if (remainingStack != null && actionAmount != null && remainingStack <= actionAmount) { + setOf(Action.Type.FOLD, Action.Type.CALL_ALLIN) + } else { + setOf(Action.Type.FOLD, Action.Type.CALL, Action.Type.RAISE, Action.Type.RAISE_ALLIN) + } + } + else -> { + throw PAIllegalStateException("We should not handle this action: ${lastSignificantAction.action.type}") + } + } + + } else { + setOf(Action.Type.CHECK, Action.Type.BET, Action.Type.BET_ALLIN) + } + + } + + private fun selectAction(index: Int, actionType: Action.Type) { + // remove everything after + } + + private fun getLastUserAction(index: Int): ComputedAction? { + val computedAction = this.sortedActions.first { it.action.index == index } + val action = computedAction.action + + val previousActions = this.sortedActions.drop(index) + return previousActions.lastOrNull { it.action.position == action.position } + } + private fun getLastSignificantAction(index: Int): ComputedAction? { + val previousActions = this.sortedActions.drop(index) + return previousActions.lastOrNull { it.action.isActionSignificant } } private fun save() { 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 9c8cdb69..4620e052 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 class ComputedAction(var action: Action, var potSize: Double = 0.0, + var totalPotSize: Double = 0.0, var playerRemainingStack: Double? = null) { /*** 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 9e91cead..5f9d4ebc 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 @@ -7,13 +7,23 @@ open class Action : RealmObject() { enum class Type { POST_SB, POST_BB, + STRADDLE, + FOLD, CHECK, CALL, BET, RAISE, CALL_ALLIN, BET_ALLIN, - RAISE_ALLIN + RAISE_ALLIN; + + val isSignificant: Boolean + get() { + return when (this) { + POST_BB, STRADDLE, BET, RAISE, BET_ALLIN, RAISE_ALLIN -> true + else -> false + } + } } /*** @@ -34,7 +44,12 @@ open class Action : RealmObject() { /*** * The type of action: check, fold, raise... */ - var type: Int? = null + var typeIdentifier: Int? = null + + val type: Type? + get() { + return typeIdentifier?.let { Type.values()[it] } + } /*** * The amount linked for a bet, raise... @@ -46,4 +61,9 @@ open class Action : RealmObject() { var positionRemainingStack: Double? = null + val isActionSignificant: Boolean + get() { + return this.type?.isSignificant ?: false + } + } \ No newline at end of file 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 fe060d2e..c1a79d74 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 @@ -85,11 +85,11 @@ open class HandHistory : RealmObject() { action.position = i when (i) { 0 -> { - action.type = Action.Type.POST_SB.ordinal + action.typeIdentifier = Action.Type.POST_SB.ordinal action.amount = this.smallBlind } 1 -> { - action.type = Action.Type.POST_BB.ordinal + action.typeIdentifier = Action.Type.POST_BB.ordinal action.amount = this.bigBlind } else -> {