diff --git a/app/src/main/java/net/pokeranalytics/android/calculus/Computable.kt b/app/src/main/java/net/pokeranalytics/android/calculus/Computable.kt index 94ce69bc..0452cc90 100644 --- a/app/src/main/java/net/pokeranalytics/android/calculus/Computable.kt +++ b/app/src/main/java/net/pokeranalytics/android/calculus/Computable.kt @@ -13,7 +13,7 @@ interface Summable { * An interface describing some class that can be computed */ interface SessionInterface : Summable { - var serie: SessionSet + var sessionSet: SessionSet? var estimatedHands: Double var bbNetResult: Double var bigBlindSessionCount: Int // 0 or 1 diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/Result.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/Result.kt index 548f27b0..d2a92ecd 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/Result.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/Result.kt @@ -37,6 +37,7 @@ open class Result : RealmObject() { // The net (readonly) var net: Double = 0.0 + private set // The transactions associated with the Result, impacting the result var transactions: RealmList = RealmList() @@ -50,7 +51,16 @@ open class Result : RealmObject() { // Computes the Net fun computeNet() { -// this.net = ... + + val transactionsSum = transactions.sumByDouble { it.amount } + this.netResult?.let { + this.net = it + transactionsSum + } ?: run { + val buyin = this.buyin ?: 0.0 + val cashOut = this.cashout ?: 0.0 + this.net = cashOut - buyin + transactionsSum + } + } // @todo tips? diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt index a9071093..f5f4d843 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/Session.kt @@ -3,6 +3,7 @@ package net.pokeranalytics.android.model.realm import io.realm.* import io.realm.annotations.Ignore import io.realm.annotations.PrimaryKey +import net.pokeranalytics.android.calculus.SessionInterface import net.pokeranalytics.android.ui.adapter.components.DisplayableDataSource import net.pokeranalytics.android.ui.adapter.components.DynamicRowDelegate import net.pokeranalytics.android.ui.adapter.components.DynamicRowInterface @@ -12,7 +13,7 @@ import java.util.* import kotlin.collections.ArrayList -open class Session(comment: String = "") : RealmObject(), DynamicRowDelegate, DisplayableDataSource { +open class Session : RealmObject(), SessionInterface, DynamicRowDelegate, DisplayableDataSource { @PrimaryKey var id = UUID.randomUUID().toString() @@ -24,7 +25,7 @@ open class Session(comment: String = "") : RealmObject(), DynamicRowDelegate, Di var timeFrame: TimeFrame? = null // The time frame sessionGroup, which can contain multiple sessions - var sessionSet: SessionSet? = null + override var sessionSet: SessionSet? = null // the date of creation of the app var creationDate: Date = Date() @@ -83,20 +84,32 @@ open class Session(comment: String = "") : RealmObject(), DynamicRowDelegate, Di companion object { - fun newInstance() : Session { + fun newInstance(): Session { var session: Session = Session() session.result = Result() session.timeFrame = TimeFrame() return session } + fun testInstance(netResult: Double): Session { + var session: Session = Session.newInstance() + session.result?.netResult = netResult + return session + } + } + @Ignore // SessionInterface value + override var value: Double = 0.0 + get() { + return this.result?.net ?: 0.0 + } + @Ignore - var estimatedHands: Double = 0.0 + override var estimatedHands: Double = 0.0 @Ignore - var bbNetResult: Double = 0.0 + override var bbNetResult: Double = 0.0 get() { this.cgBigBlind?.let { bb -> this.result?.let { result -> @@ -107,10 +120,10 @@ open class Session(comment: String = "") : RealmObject(), DynamicRowDelegate, Di } @Ignore - var bigBlindSessionCount: Int = if (this.cgBigBlind != null) 1 else 0 + override var bigBlindSessionCount: Int = if (this.cgBigBlind != null) 1 else 0 @Ignore - var buyin: Double = 0.0 + override var buyin: Double = 0.0 get() { this.result?.let { it.buyin?.let { diff --git a/app/src/test/java/net/pokeranalytics/android/ExampleUnitTest.kt b/app/src/test/java/net/pokeranalytics/android/ExampleUnitTest.kt index e4b9978a..6666d633 100644 --- a/app/src/test/java/net/pokeranalytics/android/ExampleUnitTest.kt +++ b/app/src/test/java/net/pokeranalytics/android/ExampleUnitTest.kt @@ -1,6 +1,7 @@ package net.pokeranalytics.android import net.pokeranalytics.android.calculus.* +import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.model.realm.SessionSet import org.junit.Assert.fail import org.junit.Test @@ -14,10 +15,10 @@ import org.junit.Test class ExampleUnitTest { class Grade(someValue: Double) : SessionInterface { -// override var serie: Serie = Serie(TimeFrame()) + override var value: Double = someValue - override var serie: SessionSet = SessionSet() + override var sessionSet: SessionSet? = SessionSet() override var estimatedHands: Double = 0.0 override var bbNetResult: Double = 0.0 override var bigBlindSessionCount: Int = 0 // 0 or 1 @@ -49,4 +50,29 @@ class ExampleUnitTest { } + @Test + fun testSessionStats() { + + val sessions: List = listOf(Session.testInstance(-10.0), Session.testInstance(20.0)) + val group = SessionGroup(name = "test", sessions = sessions) + + val results: ComputedResults = Calculator.compute(group, Calculator.Options()) + + val sum = results.computedStat(Stat.NETRESULT) + if (sum != null) { + assert(sum.value == 10.0) { "sum is ${sum.value}" } + } else { + fail("No Net result stat") + } + + val average = results.computedStat(Stat.AVERAGE) + if (average != null) { + assert(average.value == 5.0) { "average is ${average.value}" } + } else { + fail("No AVERAGE stat") + } + + } + + }