|
|
|
|
@ -2,6 +2,7 @@ package net.pokeranalytics.android.calculus |
|
|
|
|
|
|
|
|
|
import android.content.Context |
|
|
|
|
import io.realm.Realm |
|
|
|
|
import io.realm.RealmQuery |
|
|
|
|
import io.realm.RealmResults |
|
|
|
|
import kotlinx.coroutines.CoroutineScope |
|
|
|
|
import kotlinx.coroutines.Dispatchers |
|
|
|
|
@ -30,6 +31,8 @@ class ReportWhistleBlower(var context: Context) { |
|
|
|
|
|
|
|
|
|
private val listeners: MutableList<NewPerformanceListener> = mutableListOf() |
|
|
|
|
|
|
|
|
|
private var paused: Boolean = false |
|
|
|
|
|
|
|
|
|
init { |
|
|
|
|
|
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
@ -43,15 +46,25 @@ class ReportWhistleBlower(var context: Context) { |
|
|
|
|
this.results?.addChangeListener { _ -> |
|
|
|
|
launchReportTask() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
realm.close() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun addListener(newPerformanceListener: NewPerformanceListener) { |
|
|
|
|
this.listeners.add(newPerformanceListener) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun removeListener(listener: NewPerformanceListener) { |
|
|
|
|
this.listeners.remove(listener) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun launchReportTask() { |
|
|
|
|
Timber.d(">>> Launch report") |
|
|
|
|
|
|
|
|
|
if (paused) { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
synchronized(this) { |
|
|
|
|
this.currentTask?.cancel() |
|
|
|
|
val reportTask = ReportTask(this, this.context) |
|
|
|
|
@ -61,6 +74,15 @@ class ReportWhistleBlower(var context: Context) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun pause() { |
|
|
|
|
this.paused = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun resume() { |
|
|
|
|
this.paused = false |
|
|
|
|
this.launchReportTask() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun has(performanceId: String): Boolean { |
|
|
|
|
return this.currentNotifications.contains(performanceId) |
|
|
|
|
} |
|
|
|
|
@ -144,43 +166,46 @@ class ReportTask(private var whistleBlower: ReportWhistleBlower, var context: Co |
|
|
|
|
private fun launchOptimalDuration(realm: Realm, report: StaticReport) { |
|
|
|
|
LiveOnline.values().forEach { key -> |
|
|
|
|
val duration = CashGameOptimalDurationCalculator.start(key.isLive) |
|
|
|
|
duration?.let { |
|
|
|
|
analyseOptimalDuration(realm, report, key, it) |
|
|
|
|
} |
|
|
|
|
analyseOptimalDuration(realm, report, key, duration) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun analyseDefaultReport(realm: Realm, staticReport: StaticReport, result: Report) { |
|
|
|
|
|
|
|
|
|
val nameSeparator = " " |
|
|
|
|
|
|
|
|
|
for (stat in result.options.stats) { |
|
|
|
|
|
|
|
|
|
Timber.d("analyse stat: $stat for report: $staticReport") |
|
|
|
|
|
|
|
|
|
result.max(stat)?.let { computedResults -> |
|
|
|
|
// Get current performance |
|
|
|
|
var query = performancesQuery(realm, staticReport, stat) |
|
|
|
|
|
|
|
|
|
val customField: CustomField? = |
|
|
|
|
(staticReport as? StaticReport.CustomFieldList)?.customField |
|
|
|
|
var query = realm.where(Performance::class.java) |
|
|
|
|
.equalTo("key", stat.uniqueIdentifier) |
|
|
|
|
.equalTo("reportId", staticReport.uniqueIdentifier) |
|
|
|
|
val customField: CustomField? = |
|
|
|
|
(staticReport as? StaticReport.CustomFieldList)?.customField |
|
|
|
|
customField?.let { |
|
|
|
|
query = query.equalTo("customFieldId", it.id) |
|
|
|
|
} |
|
|
|
|
val currentPerf = query.findFirst() |
|
|
|
|
|
|
|
|
|
customField?.let { |
|
|
|
|
query = query.equalTo("customFieldId", it.id) |
|
|
|
|
} |
|
|
|
|
val currentPerf = query.findFirst() |
|
|
|
|
// Store if necessary, delete if necessary |
|
|
|
|
val bestComputedResults = result.max(stat) |
|
|
|
|
bestComputedResults?.let { computedResults -> |
|
|
|
|
|
|
|
|
|
val performanceQuery = computedResults.group.query |
|
|
|
|
val performanceName = performanceQuery.getName(this.context, " ") |
|
|
|
|
val performanceName = performanceQuery.getName(this.context, nameSeparator) |
|
|
|
|
|
|
|
|
|
Timber.d("Best computed = $performanceName, ${computedResults.computedStat(Stat.NET_RESULT)?.value}") |
|
|
|
|
|
|
|
|
|
var storePerf = true |
|
|
|
|
currentPerf?.let { |
|
|
|
|
currentPerf.name?.let { |
|
|
|
|
if (computedResults.group.query.defaultName == it) { |
|
|
|
|
currentPerf.name?.let { name -> |
|
|
|
|
if (computedResults.group.query.getName(this.context, nameSeparator) == name) { |
|
|
|
|
storePerf = false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
currentPerf.objectId?.let { |
|
|
|
|
if (computedResults.group.query.objectId == it) { |
|
|
|
|
currentPerf.objectId?.let { objectId -> |
|
|
|
|
if (computedResults.group.query.objectId == objectId) { |
|
|
|
|
storePerf = false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -209,41 +234,61 @@ class ReportTask(private var whistleBlower: ReportWhistleBlower, var context: Co |
|
|
|
|
this.whistleBlower.notify(performance) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} ?: run { // if there is no max but a now irrelevant Performance, we delete it |
|
|
|
|
Timber.d("NO best computed value, current perf = $currentPerf ") |
|
|
|
|
currentPerf?.let { perf -> |
|
|
|
|
realm.executeTransaction { |
|
|
|
|
Timber.d("Delete perf: stat = ${perf.stat}, report = ${perf.reportId}") |
|
|
|
|
perf.deleteFromRealm() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun analyseOptimalDuration(realm: Realm, staticReport: StaticReport, key: PerformanceKey, duration: Double) { |
|
|
|
|
private fun analyseOptimalDuration(realm: Realm, staticReport: StaticReport, key: PerformanceKey, duration: Double?) { |
|
|
|
|
|
|
|
|
|
var storePerf = true |
|
|
|
|
val performance = performancesQuery(realm, staticReport, key).findFirst() |
|
|
|
|
|
|
|
|
|
val performance = realm.where(Performance::class.java) |
|
|
|
|
.equalTo("reportId", staticReport.uniqueIdentifier) |
|
|
|
|
.equalTo("key", key.value) |
|
|
|
|
.findFirst() |
|
|
|
|
duration?.let { |
|
|
|
|
var storePerf = true |
|
|
|
|
|
|
|
|
|
val formattedDuration = (duration / 3600 / 1000).formattedHourlyDuration() |
|
|
|
|
performance?.let { perf -> |
|
|
|
|
if (perf.value == duration) { |
|
|
|
|
storePerf = false |
|
|
|
|
val formattedDuration = (duration / 3600 / 1000).formattedHourlyDuration() |
|
|
|
|
performance?.let { perf -> |
|
|
|
|
if (perf.value == duration) { |
|
|
|
|
storePerf = false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (storePerf) { |
|
|
|
|
realm.executeTransaction { |
|
|
|
|
perf.name = formattedDuration |
|
|
|
|
perf.value = duration |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (storePerf) { |
|
|
|
|
val perf = Performance(staticReport, key, name = formattedDuration, value = duration) |
|
|
|
|
realm.executeTransaction { it.copyToRealm(perf) } |
|
|
|
|
this.whistleBlower.notify(perf) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} ?: run { // no duration |
|
|
|
|
performance?.let { perf -> |
|
|
|
|
realm.executeTransaction { |
|
|
|
|
perf.name = formattedDuration |
|
|
|
|
perf.value = duration |
|
|
|
|
perf.deleteFromRealm() // delete if the perf exists |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (storePerf) { |
|
|
|
|
val perf = Performance(staticReport, key, name = formattedDuration, value = duration) |
|
|
|
|
realm.executeTransaction { it.copyToRealm(perf) } |
|
|
|
|
this.whistleBlower.notify(perf) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun performancesQuery(realm: Realm, staticReport: StaticReport, key: PerformanceKey): RealmQuery<Performance> { |
|
|
|
|
return realm.where(Performance::class.java) |
|
|
|
|
.equalTo("reportId", staticReport.uniqueIdentifier) |
|
|
|
|
.equalTo("key", key.value) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |