diff --git a/app/src/main/java/net/pokeranalytics/android/calculus/Calculator.kt b/app/src/main/java/net/pokeranalytics/android/calculus/Calculator.kt index 338ac2db..d1accf5b 100644 --- a/app/src/main/java/net/pokeranalytics/android/calculus/Calculator.kt +++ b/app/src/main/java/net/pokeranalytics/android/calculus/Calculator.kt @@ -82,7 +82,7 @@ class Calculator { return this.displayedStats.contains(DAYS_PLAYED) } - val evolutionForAggregatedValues: Boolean + val shouldManageMultiGroupProgressValues: Boolean get() { if (this.aggregationType != null) { return this.aggregationType == AggregationType.MONTH || this.aggregationType == AggregationType.YEAR @@ -97,12 +97,14 @@ class Calculator { fun computeStatsWithEvolutionByAggregationType( realm: Realm, + stat: Stat, group: ComputableGroup, aggregationType: AggregationType, stats: List? = null ): Report { val options = Options(evolutionValues = Options.EvolutionValues.STANDARD, aggregationType = aggregationType) + options.displayedStats = listOf(stat) if (aggregationType == AggregationType.DURATION) { options.evolutionValues = Options.EvolutionValues.TIMED } @@ -195,7 +197,7 @@ class Calculator { */ fun compute(realm: Realm, computableGroup: ComputableGroup, options: Options): ComputedResults { - val results = ComputedResults(computableGroup) + val results = ComputedResults(computableGroup, options.shouldManageMultiGroupProgressValues) val computables = computableGroup.computables(realm) Timber.d(">>>> Start computing group ${computableGroup.name}, ${computables.size} computables") diff --git a/app/src/main/java/net/pokeranalytics/android/calculus/Report.kt b/app/src/main/java/net/pokeranalytics/android/calculus/Report.kt index ad27c08e..6ffaa8ad 100644 --- a/app/src/main/java/net/pokeranalytics/android/calculus/Report.kt +++ b/app/src/main/java/net/pokeranalytics/android/calculus/Report.kt @@ -16,33 +16,30 @@ import net.pokeranalytics.android.model.realm.SessionSet */ class Report(var options: Calculator.Options) { + /** + * The mutable list of ComputedResults, one for each group of data + */ private var _results: MutableList = mutableListOf() val results: List = this._results - -// private var groups: MutableList = mutableListOf() -// -// -// fun addGroup(group: ComputableGroup) { -// this.groups.add(group) -// } -// -// fun addGroups(groups: Collection) { -// this.groups.addAll(groups) -// } - + /** + * Adds a new result to the list of ComputedResults + */ fun addResults(result: ComputedResults) { this._results.add(result) } + /** + * Returns the list of entries corresponding to the provided [stat] + * One value will be returned by result + */ fun lineEntries(stat: Stat): List { val entries = mutableListOf() this._results.forEachIndexed { index, results -> - val cs = results.computedStat(stat) - cs?.let { computedStat -> - entries.add(Entry(index.toFloat(), computedStat.value.toFloat(), results)) + results.computedStat(stat)?.progressValue?.let { evoValue -> + entries.add(Entry(index.toFloat(), evoValue.toFloat(), results)) } } return entries @@ -165,7 +162,7 @@ class ComputableGroup(name: String, conditions: List = listOf(), } -class ComputedResults(group: ComputableGroup) : StatEntry { +class ComputedResults(group: ComputableGroup, shouldManageMultiGroupProgressValues: Boolean = false) : StatEntry { /** * The session group used to computed the stats @@ -178,6 +175,8 @@ class ComputedResults(group: ComputableGroup) : StatEntry { // The map containing all evolution numericValues for all stats private var _evolutionValues: MutableMap> = mutableMapOf() + private var shouldManageMultiGroupProgressValues = shouldManageMultiGroupProgressValues + fun allStats(): Collection { return this._computedStats.values } @@ -216,31 +215,99 @@ class ComputedResults(group: ComputableGroup) : StatEntry { } } + /** + * Adds a [computedStat] to the list of stats + * Also computes evolution values using the previously computed values + */ private fun addComputedStat(computedStat: ComputedStat) { - this.group.comparedComputedResults?.let { result -> + if (this.shouldManageMultiGroupProgressValues) { + + computedStat.progressValue = computedStat.value // useful for first occurence, otherwise overridden + + // Computes the evolution value for the stat + this.group.comparedComputedResults?.let { result -> - result.computedStat(computedStat.stat)?.let { previousComputedStat -> + result.computedStat(computedStat.stat)?.let { previousComputedStat -> - val previousValue = previousComputedStat.secondValue ?: previousComputedStat.value - when (computedStat.stat) { - Stat.NET_RESULT, Stat.DURATION, Stat.BB_NET_RESULT -> { - computedStat.secondValue = previousValue + computedStat.value + val previousValue = previousComputedStat.progressValue ?: previousComputedStat.value + when (computedStat.stat) { + Stat.NET_RESULT, Stat.DURATION, Stat.BB_NET_RESULT, Stat.BB_SESSION_COUNT, + Stat.WINNING_SESSION_COUNT, Stat.TOTAL_BUYIN, Stat.HANDS_PLAYED -> { + computedStat.progressValue = previousValue + computedStat.value + } + else -> {} } } } } this._computedStats[computedStat.stat] = computedStat + } fun consolidateEvolutionStat() { + val netResult = this.computedStat(Stat.NET_RESULT)?.secondValue + val bbNetResult = this.computedStat(Stat.BB_NET_RESULT)?.secondValue + val duration = this.computedStat(Stat.DURATION)?.secondValue + val numberOfGames = this.computedStat(Stat.NUMBER_OF_GAMES)?.secondValue + val numberOfSets = this.computedStat(Stat.NUMBER_OF_SETS)?.secondValue + val handsPlayed = this.computedStat(Stat.HANDS_PLAYED)?.secondValue + val winningCount = this.computedStat(Stat.WINNING_SESSION_COUNT)?.secondValue + val bbSessionCount = this.computedStat(Stat.BB_SESSION_COUNT)?.secondValue + val totalBuyin = this.computedStat(Stat.TOTAL_BUYIN)?.secondValue + this.allStats().forEach { computedStat -> when (computedStat.stat) { - + Stat.HOURLY_RATE -> { + if (netResult != null && duration != null) { + computedStat.secondValue = netResult / duration + } + } + Stat.AVERAGE -> { + if (netResult != null && numberOfGames != null) { + computedStat.secondValue = netResult / numberOfGames + } + } + Stat.AVERAGE_DURATION -> { + if (duration != null && numberOfSets != null) { + computedStat.secondValue = duration / numberOfSets + } + } + Stat.NET_BB_PER_100_HANDS -> { + if (bbNetResult != null && handsPlayed != null) { + computedStat.secondValue = Stat.netBBPer100Hands(bbNetResult, handsPlayed) + } + } + Stat.HOURLY_RATE_BB -> { + if (bbNetResult != null && duration != null) { + computedStat.secondValue = bbNetResult / duration + } + } + Stat.AVERAGE_NET_BB -> { + if (bbNetResult != null && bbSessionCount != null) { + computedStat.secondValue = bbNetResult / bbSessionCount + } + } + Stat.WIN_RATIO -> { + if (winningCount != null && numberOfGames != null) { + computedStat.secondValue = winningCount / numberOfGames + } + } + Stat.AVERAGE_BUYIN -> { + if (totalBuyin != null && numberOfGames != null) { + computedStat.secondValue = totalBuyin / numberOfGames + } + } + Stat.ROI -> { + if (totalBuyin != null && netResult != null) { + computedStat.secondValue = Stat.returnOnInvestment(netResult, totalBuyin) + } + } } } + } fun computedStat(stat: Stat): ComputedStat? { @@ -273,13 +340,6 @@ class ComputedResults(group: ComputableGroup) : StatEntry { } } - /** - * Returns the number of computed stats - */ - fun numberOfStats(): Int { - return this._computedStats.size - } - // MPAndroidChart fun defaultStatEntries(stat: Stat): List { @@ -328,7 +388,7 @@ class ComputedResults(group: ComputableGroup) : StatEntry { override val entryTitle: String = this.group.name override fun formattedValue(stat: Stat, context: Context): TextFormat { - this.computedStat(stat)?.secondValue?.let { + this.computedStat(stat)?.progressValue?.let { return stat.format(it, context = context) // return it.format(context) } ?: run { diff --git a/app/src/main/java/net/pokeranalytics/android/calculus/Stat.kt b/app/src/main/java/net/pokeranalytics/android/calculus/Stat.kt index 9cf06183..4e1aea9b 100644 --- a/app/src/main/java/net/pokeranalytics/android/calculus/Stat.kt +++ b/app/src/main/java/net/pokeranalytics/android/calculus/Stat.kt @@ -269,6 +269,11 @@ class ComputedStat(var stat: Stat, var value: Double, var secondValue: Double? = } } + /** + * The value used to get evolution dataset + */ + var progressValue: Double? = null + /** * The variation of the stat */ diff --git a/app/src/main/java/net/pokeranalytics/android/ui/fragment/StatisticDetailsFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/fragment/StatisticDetailsFragment.kt index efb19b62..d9e1bc4a 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/fragment/StatisticDetailsFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/fragment/StatisticDetailsFragment.kt @@ -146,9 +146,7 @@ class StatisticDetailsFragment : PokerAnalyticsFragment() { val realm = Realm.getDefaultInstance() - val requiredStats: List = listOf(stat) - - val report = Calculator.computeStatsWithEvolutionByAggregationType(realm, computableGroup, aggregationType, requiredStats) + val report = Calculator.computeStatsWithEvolutionByAggregationType(realm, stat, computableGroup, aggregationType) reports[aggregationType] = report realm.close() @@ -162,9 +160,7 @@ class StatisticDetailsFragment : PokerAnalyticsFragment() { progressBar.hideWithAnimation() graphContainer.showWithAnimation() } - } - } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/fragment/StatsFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/fragment/StatsFragment.kt index 192c35e3..2d28347e 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/fragment/StatsFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/fragment/StatsFragment.kt @@ -246,9 +246,8 @@ class StatsFragment : SessionObserverFragment(), StaticRowRepresentableDataSourc val realm = Realm.getDefaultInstance() - val requiredStats: List = listOf(stat) val aggregationType = stat.aggregationTypes.first() - report = Calculator.computeStatsWithEvolutionByAggregationType(realm, computableGroup, aggregationType, requiredStats) + report = Calculator.computeStatsWithEvolutionByAggregationType(realm, stat, computableGroup, aggregationType) realm.close()