Fixes various position issues

hh
Laurent 6 years ago
parent 9254c40425
commit 0a603bab1a
  1. 16
      app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/Action.kt
  2. 11
      app/src/main/java/net/pokeranalytics/android/model/realm/handhistory/HandHistory.kt
  3. 14
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/HandHistoryAdapter.kt
  4. 24
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ActionList.kt
  5. 39
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/CardsRow.kt
  6. 9
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/ComputedAction.kt
  7. 71
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/HandHistoryViewModel.kt
  8. 50
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerCardsRow.kt
  9. 25
      app/src/main/java/net/pokeranalytics/android/ui/modules/handhistory/model/PlayerSetupRow.kt

@ -69,14 +69,22 @@ open class Action : RealmObject() {
} }
} }
val requiresOpponentDecision: Boolean val isAllin: Boolean
get() { get() {
return when(this) { return when (this) {
CALL, CALL_ALLIN, FOLD -> false UNDEFINED_ALLIN, BET_ALLIN, RAISE_ALLIN, CALL_ALLIN -> true
else -> 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 override val viewType: Int = RowViewType.TITLE_GRID.ordinal
companion object { companion object {

@ -7,7 +7,6 @@ import io.realm.annotations.PrimaryKey
import net.pokeranalytics.android.model.filter.Filterable import net.pokeranalytics.android.model.filter.Filterable
import net.pokeranalytics.android.model.handhistory.HandSetup import net.pokeranalytics.android.model.handhistory.HandSetup
import net.pokeranalytics.android.model.handhistory.Position 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.Identifiable
import net.pokeranalytics.android.model.interfaces.TimeFilterable import net.pokeranalytics.android.model.interfaces.TimeFilterable
import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.model.realm.Session
@ -137,9 +136,9 @@ open class HandHistory : RealmObject(), RowRepresentable, Identifiable, Filterab
this.actions.add(action) this.actions.add(action)
} }
fun cardsForStreet(street: Street): MutableList<Card> { // fun cardsForStreet(street: Street): MutableList<Card> {
return this.board.sortedBy { it.index }.take(street.totalBoardCards).toMutableList() // return this.board.sortedBy { it.index }.take(street.totalBoardCards).toMutableList()
} // }
// //
// fun playerSetupForPosition(position: Int) : PlayerSetup { // fun playerSetupForPosition(position: Int) : PlayerSetup {
// this.playerSetups.firstOrNull { it.position == position }?.let { // this.playerSetups.firstOrNull { it.position == position }?.let {
@ -149,6 +148,10 @@ open class HandHistory : RealmObject(), RowRepresentable, Identifiable, Filterab
// val ps = PlayerSetup() // val ps = PlayerSetup()
// ps.position = position // ps.position = position
// return ps // return ps
// }
// fun getOrCreatePlayerSetup(position: Int): PlayerSetup {
// return this.playerSetupForPosition(position) ?: createPlayerSetup(position)
// } // }
fun playerSetupForPosition(position: Int): PlayerSetup? { fun playerSetupForPosition(position: Int): PlayerSetup? {

@ -586,21 +586,19 @@ class HandHistoryAdapter(
} }
return@setOnTouchListener true return@setOnTouchListener true
} }
} }
} }
override fun onBind(position: Int, row: RowRepresentable, adapter: RecyclerAdapter) { override fun onBind(position: Int, row: RowRepresentable, adapter: RecyclerAdapter) {
super.onBind(position, row, adapter) super.onBind(position, row, adapter)
val setup = row as PlayerSetupRow val setupRow = row as PlayerSetupRow
val state = setup.state val state = setupRow.state
this.delegate = adapter.delegate this.delegate = adapter.delegate
// Title // Title
itemView.title.text = setup.title(itemView.context) itemView.title.text = setupRow.title(itemView.context)
// Position recycler // Position recycler
val visibility = if (state == PlayerSetupRow.State.SETUP_ONLY) View.GONE else View.VISIBLE 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.positions = adapter.dataSource.contentForRow(row, itemView.context, Position::class)
this.positionAdapter.setOnClickListener { pos -> this.positionAdapter.setOnClickListener { pos ->
setup.closePosition() setupRow.closePosition()
this.delegate?.onRowValueChanged(pos, row) this.delegate?.onRowValueChanged(pos, row)
} }
// Position Button // Position Button
itemView.posButton.text = setup.position?.value itemView.posButton.text = adapter.dataSource.charSequenceForRow(row, itemView.context, PlayerSetupRow.Tag.POSITION.ordinal)
itemView.posButton.setOnClickListener { itemView.posButton.setOnClickListener {
setup.showPosition() setupRow.showPosition()
itemView.positionRecyclerView.visibility = View.VISIBLE itemView.positionRecyclerView.visibility = View.VISIBLE
itemView.posButton.backgroundTintList = ColorStateList.valueOf(color(true)) itemView.posButton.backgroundTintList = ColorStateList.valueOf(color(true))
} }

@ -14,9 +14,10 @@ interface ActionManager {
fun getStreetNextCalls(index: Int): List<ComputedAction> fun getStreetNextCalls(index: Int): List<ComputedAction>
fun getPlayerNextStreetActions(index: Int): List<ComputedAction> fun getPlayerNextStreetActions(index: Int): List<ComputedAction>
fun dropNextActions(index: Int) fun dropNextActions(index: Int)
fun allinAmountSet(position: Int)
} }
interface ActionListListener { interface ActionListListener : PlayerSetupCreationListener {
fun actionCountChanged() fun actionCountChanged()
} }
@ -25,6 +26,8 @@ interface ActionListListener {
*/ */
class ActionList(var listener: ActionListListener) : ArrayList<ComputedAction>(), ActionManager { class ActionList(var listener: ActionListListener) : ArrayList<ComputedAction>(), ActionManager {
private lateinit var handHistory: HandHistory
/*** /***
* Indicates whether the list size has changed * Indicates whether the list size has changed
*/ */
@ -35,6 +38,7 @@ class ActionList(var listener: ActionListListener) : ArrayList<ComputedAction>()
*/ */
fun load(handHistory: HandHistory) { fun load(handHistory: HandHistory) {
this.handHistory = handHistory
this.positions = Position.positionsPerPlayers(handHistory.numberOfPlayers) this.positions = Position.positionsPerPlayers(handHistory.numberOfPlayers)
this.clear() this.clear()
@ -302,7 +306,7 @@ class ActionList(var listener: ActionListListener) : ArrayList<ComputedAction>()
val action = Action() val action = Action()
action.index = this.size action.index = this.size
action.position = position.ordinal action.position = this.positions.indexOf(position)
action.street = street action.street = street
val computedAction = val computedAction =
ComputedAction( ComputedAction(
@ -436,6 +440,22 @@ class ActionList(var listener: ActionListListener) : ArrayList<ComputedAction>()
fireListener() 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 * Returns the list of the player next street actions
*/ */

@ -4,11 +4,9 @@ import io.realm.Realm
import io.realm.RealmList import io.realm.RealmList
import io.realm.RealmModel import io.realm.RealmModel
import net.pokeranalytics.android.exceptions.PAIllegalStateException 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.handhistory.Street
import net.pokeranalytics.android.model.realm.handhistory.Card import net.pokeranalytics.android.model.realm.handhistory.Card
import net.pokeranalytics.android.model.realm.handhistory.HandHistory import net.pokeranalytics.android.model.realm.handhistory.HandHistory
import net.pokeranalytics.android.model.realm.handhistory.PlayerSetup
import net.pokeranalytics.android.ui.modules.handhistory.HandRowType import net.pokeranalytics.android.ui.modules.handhistory.HandRowType
interface CardHolder : RealmModel { interface CardHolder : RealmModel {
@ -84,8 +82,10 @@ abstract class CardsRow : HandHistoryRow {
return return
} }
var holderCreated = false
if (this.cardHolder == null) { if (this.cardHolder == null) {
this.createHolder() this.createHolder()
holderCreated = true
} }
this.cardHolder?.cards?.lastOrNull()?.let { this.cardHolder?.cards?.lastOrNull()?.let {
@ -101,8 +101,14 @@ abstract class CardsRow : HandHistoryRow {
card.index = this.cardCount card.index = this.cardCount
this.cardHolder?.cards?.add(card) this.cardHolder?.cards?.add(card)
if (holderCreated) {
this.holderCreated()
}
} }
protected open fun holderCreated() { }
protected open fun canAddMoreCards(): Boolean { protected open fun canAddMoreCards(): Boolean {
this.cardLimit()?.let { limit -> this.cardLimit()?.let { limit ->
return !(this.cardCount >= limit && lastCard()?.suit != null) 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 }
}

@ -114,9 +114,8 @@ class ComputedAction(var manager: ActionManager,
getStreetNextCalls().forEach { getStreetNextCalls().forEach {
it.updateEffectiveAmount() it.updateEffectiveAmount()
// it.setEffectiveAmount(amount)
} }
getPlayerNextActions().forEach { getPlayerNextStreetActions().forEach {
it.updateEffectiveAmount() it.updateEffectiveAmount()
} }
@ -137,6 +136,10 @@ class ComputedAction(var manager: ActionManager,
else -> {} 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) return this.manager.getStreetNextCalls(this.action.index)
} }
private fun getPlayerNextActions(): List<ComputedAction> { private fun getPlayerNextStreetActions(): List<ComputedAction> {
return this.manager.getPlayerNextStreetActions(this.action.index) return this.manager.getPlayerNextStreetActions(this.action.index)
} }

@ -27,6 +27,10 @@ enum class HHKeyboard {
class HHSelection(var index: Int, var tag: Int) class HHSelection(var index: Int, var tag: Int)
interface PlayerSetupCreationListener {
fun playerSetupCreated()
}
class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentralizer, ActionListListener { class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentralizer, ActionListListener {
/*** /***
@ -197,11 +201,14 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
addStreetHeader(rows, street) addStreetHeader(rows, street)
val positions = this.sortedActions.activePositions(lastActionIndex, true) val positions = this.sortedActions.activePositions(lastActionIndex, true)
positions.forEach { positions.forEach { position ->
val positionIndex = this.sortedActions.positions.indexOf(it) val positionIndex = this.sortedActions.positions.indexOf(position)
val playerCardsRow = PlayerCardsRow(it, positionIndex, this.handHistory, val playerCardsRow = PlayerCardsRow(this,
this.handHistory.playerSetupForPosition(positionIndex), this.playerHandMaxCards) position,
// val playerCardsRow = PlayerCardsRow.newInstance(it, this.handHistory, positionIndex, this.playerHandMaxCards) positionIndex,
this.handHistory,
this.handHistory.playerSetupForPosition(positionIndex),
this.playerHandMaxCards)
rows.add(playerCardsRow) 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<Position> { fun positionsToAct(): List<Position> {
return this.sortedActions.positionsToActAfterIndex(this.actionIndexForSelection) return this.sortedActions.positionsToActAfterIndex(this.actionIndexForSelection)
@ -436,16 +443,16 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
/*** /***
* Called when the board has changed * Called when the board has changed
*/ */
fun boardChanged(cards: List<Card>) { // fun boardChanged(cards: List<Card>) {
this.rowRepresentables.filterIsInstance<StreetCardsRow>().forEach { // this.rowRepresentables.filterIsInstance<StreetCardsRow>().forEach {
// it.cards = cards // // it.cards = cards
} // }
} // }
/*** /***
* Saves the current hand state in the database * Saves the current hand state in the database
*/ */
private fun save() { fun save() {
} }
@ -493,6 +500,10 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
this.createRowRepresentation() this.createRowRepresentation()
} }
override fun playerSetupCreated() {
this.createRowRepresentation()
}
fun amountChanged(amount: String?) { fun amountChanged(amount: String?) {
this.currentAmount = amount this.currentAmount = amount
} }
@ -561,6 +572,7 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
is StreetCardsRow -> row.cardsForTag(tag)?.formatted(context) is StreetCardsRow -> row.cardsForTag(tag)?.formatted(context)
is PlayerSetupRow -> { is PlayerSetupRow -> {
when (tag) { when (tag) {
PlayerSetupRow.Tag.POSITION.ordinal -> row.position?.value
PlayerSetupRow.Tag.CARDS.ordinal -> row.cardHolder?.cards?.formatted(context) PlayerSetupRow.Tag.CARDS.ordinal -> row.cardHolder?.cards?.formatted(context)
PlayerSetupRow.Tag.STACK.ordinal -> row.playerSetup?.stack?.formatted() PlayerSetupRow.Tag.STACK.ordinal -> row.playerSetup?.stack?.formatted()
else -> throw PAIllegalStateException("Unmanaged case with $row, tag = $tag") else -> throw PAIllegalStateException("Unmanaged case with $row, tag = $tag")
@ -612,7 +624,8 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
val hh = this.handHistory val hh = this.handHistory
val arrangedSetups = hh.playerSetups.sortedBy { it.position }.map { val arrangedSetups = hh.playerSetups.sortedBy { it.position }.map {
createPlayerSetupRow(false, it) val position = this.positionForIndex(it.position)
createPlayerSetupRow(false, position, it)
}.toMutableList() }.toMutableList()
val heroSetup = arrangedSetups.firstOrNull { it.playerSetup?.position == hh.heroIndex } val heroSetup = arrangedSetups.firstOrNull { it.playerSetup?.position == hh.heroIndex }
@ -621,7 +634,7 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
arrangedSetups.remove(it) arrangedSetups.remove(it)
arrangedSetups.add(0, it) arrangedSetups.add(0, it)
} ?: run { } ?: run {
arrangedSetups.add(createPlayerSetupRow(true)) arrangedSetups.add(0, createPlayerSetupRow(true))
} }
if (arrangedSetups.size < hh.numberOfPlayers) { if (arrangedSetups.size < hh.numberOfPlayers) {
@ -629,42 +642,48 @@ class HandHistoryViewModel : ViewModel(), RowRepresentableDataSource, CardCentra
} }
return arrangedSetups return arrangedSetups
} }
/*** /***
* Convenience method to create a PlayerSetupRow * Convenience method to create a PlayerSetupRow
*/ */
private fun createPlayerSetupRow(hero: Boolean, playerSetup: PlayerSetup? = null): PlayerSetupRow { private fun createPlayerSetupRow(hero: Boolean, position: Position? = null, playerSetup: PlayerSetup? = null): PlayerSetupRow {
return PlayerSetupRow(hero, null, null, this.handHistory, playerSetup, this.playerHandMaxCards) 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 var setupCreated = false
psRowRepresentable.playerSetup?.let { setup -> playerSetupRow.playerSetup?.let { playerSetup ->
setup.position = positionIndex playerSetup.position = positionIndex
} ?: run { } ?: run {
psRowRepresentable.playerSetup = this.handHistory.createPlayerSetup(positionIndex) playerSetupRow.playerSetup = this.handHistory.createPlayerSetup(positionIndex)
setupCreated = true setupCreated = true
} }
val position = this.sortedActions.positions.elementAt(positionIndex) val position = this.positionForIndex(positionIndex)
psRowRepresentable.position = position playerSetupRow.position = position
if (!psRowRepresentable.hero && setupCreated) { if (!playerSetupRow.hero && setupCreated) {
if (this.handHistory.undefinedPositions().isNotEmpty()) { if (this.handHistory.undefinedPositions().isNotEmpty()) {
val newPlayerRow = createPlayerSetupRow(false) val newPlayerRow = createPlayerSetupRow(false)
val index = this.indexOfRowRepresentable(psRowRepresentable) val index = this.indexOfRowRepresentable(playerSetupRow)
this.rowsLiveData.value?.add(index + 1, newPlayerRow) 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)
}
} }

@ -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()
}
}

@ -28,7 +28,23 @@ class PlayerSetupRow(var hero: Boolean = false,
STACK 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 { fun title(context: Context): String {
return if (this.hero) { 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 override val viewType: Int = HandRowType.PLAYER_SETUP.ordinal

Loading…
Cancel
Save