|
|
|
|
@ -4,118 +4,102 @@ import io.realm.Realm |
|
|
|
|
import io.realm.Sort |
|
|
|
|
import io.realm.kotlin.where |
|
|
|
|
import net.pokeranalytics.android.exceptions.PokerAnalyticsException |
|
|
|
|
import net.pokeranalytics.android.model.Criteria.Bankrolls.comparison |
|
|
|
|
import net.pokeranalytics.android.model.Criteria.Blinds.comparison |
|
|
|
|
import net.pokeranalytics.android.model.Criteria.Games.comparison |
|
|
|
|
import net.pokeranalytics.android.model.Criteria.Limits.comparison |
|
|
|
|
import net.pokeranalytics.android.model.Criteria.Locations.comparison |
|
|
|
|
import net.pokeranalytics.android.model.Criteria.TableSizes.comparison |
|
|
|
|
import net.pokeranalytics.android.model.Criteria.TournamentFeatures.comparison |
|
|
|
|
import net.pokeranalytics.android.model.Criteria.TournamentFees.comparison |
|
|
|
|
import net.pokeranalytics.android.model.Criteria.TournamentNames.comparison |
|
|
|
|
import net.pokeranalytics.android.model.Criteria.TournamentTypes.comparison |
|
|
|
|
import net.pokeranalytics.android.model.filter.Query |
|
|
|
|
import net.pokeranalytics.android.model.filter.QueryCondition |
|
|
|
|
import net.pokeranalytics.android.model.interfaces.NameManageable |
|
|
|
|
import net.pokeranalytics.android.model.realm.* |
|
|
|
|
import java.util.* |
|
|
|
|
import kotlin.collections.ArrayList |
|
|
|
|
|
|
|
|
|
fun List<Criteria>.combined(): List<List<QueryCondition>> { |
|
|
|
|
val comparatorList = ArrayList<List<QueryCondition>>() |
|
|
|
|
this.forEach { |
|
|
|
|
comparatorList.add(it.queryConditions) |
|
|
|
|
fun List<Criteria>.combined(): List<Query> { |
|
|
|
|
val comparatorList = ArrayList<List<Query>>() |
|
|
|
|
this.forEach { criteria -> |
|
|
|
|
comparatorList.add(criteria.queries) |
|
|
|
|
} |
|
|
|
|
return getCombinations(comparatorList) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun List<List<QueryCondition>>.upToNow(): List<List<QueryCondition>> { |
|
|
|
|
val calendar = Calendar.getInstance() |
|
|
|
|
calendar.time = Date() |
|
|
|
|
val currentYear = calendar.get(Calendar.YEAR) |
|
|
|
|
val currentMonth = calendar.get(Calendar.MONTH) |
|
|
|
|
fun getCombinations(queries: List<List<Query>>): List<Query> { |
|
|
|
|
|
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
val firstSession = realm.where<Session>().sort("year", Sort.ASCENDING).findFirst() |
|
|
|
|
realm.close() |
|
|
|
|
if (queries.size == 0) { return listOf() } |
|
|
|
|
|
|
|
|
|
val firstYear = firstSession?.year ?: currentYear |
|
|
|
|
val firstMonth = firstSession?.month ?: currentMonth |
|
|
|
|
val mutableQueries = queries.toMutableList() |
|
|
|
|
var combinations = mutableQueries.removeAt(0) |
|
|
|
|
|
|
|
|
|
val toRemove = this.filter { list -> |
|
|
|
|
list.any { it is QueryCondition.AnyYear && it.listOfValues.first() == currentYear } |
|
|
|
|
}.filter { list -> |
|
|
|
|
list.any { |
|
|
|
|
it is QueryCondition.AnyMonthOfYear && it.listOfValues.first() > currentMonth |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val toRemoveBefore = this.filter { list -> |
|
|
|
|
list.any { it is QueryCondition.AnyYear && it.listOfValues.first() == firstYear } |
|
|
|
|
}.filter { list -> |
|
|
|
|
list.any { |
|
|
|
|
it is QueryCondition.AnyMonthOfYear && it.listOfValues.first() < firstMonth |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
for (queryList in mutableQueries) { |
|
|
|
|
|
|
|
|
|
return this.filter{ list -> |
|
|
|
|
var keep = true |
|
|
|
|
toRemove.forEach { |
|
|
|
|
if (list.containsAll(it)) { |
|
|
|
|
keep = false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
firstSession?.let { |
|
|
|
|
toRemoveBefore.forEach { |
|
|
|
|
if (list.containsAll(it)) { |
|
|
|
|
keep = false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
keep |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun <T> getCombinations(lists: List<List<T>>): List<List<T>> { |
|
|
|
|
var combinations: LinkedHashSet<List<T>> = LinkedHashSet() |
|
|
|
|
var newCombinations: LinkedHashSet<List<T>> |
|
|
|
|
|
|
|
|
|
var index = 0 |
|
|
|
|
|
|
|
|
|
// extract each of the integers in the first list |
|
|
|
|
// and add each to ints as a new list |
|
|
|
|
if (lists.isNotEmpty()) { |
|
|
|
|
for (i in lists[0]) { |
|
|
|
|
val newList = ArrayList<T>() |
|
|
|
|
newList.add(i) |
|
|
|
|
combinations.add(newList) |
|
|
|
|
} |
|
|
|
|
index++ |
|
|
|
|
} |
|
|
|
|
while (index < lists.size) { |
|
|
|
|
val nextList = lists[index] |
|
|
|
|
newCombinations = LinkedHashSet() |
|
|
|
|
for (first in combinations) { |
|
|
|
|
for (second in nextList) { |
|
|
|
|
val newList = ArrayList<T>() |
|
|
|
|
newList.addAll(first) |
|
|
|
|
newList.add(second) |
|
|
|
|
newCombinations.add(newList) |
|
|
|
|
val newCombinations = mutableListOf<Query>() |
|
|
|
|
combinations.forEach { combinedQuery -> |
|
|
|
|
queryList.forEach { queryToAdd -> |
|
|
|
|
val nq = Query().merge(combinedQuery).merge(queryToAdd) |
|
|
|
|
newCombinations.add(nq) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
combinations = newCombinations |
|
|
|
|
|
|
|
|
|
index++ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return combinations.toList() |
|
|
|
|
return combinations |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//fun getCombinations(lists: List<Query>): List<Query> { |
|
|
|
|
// var combinations: LinkedHashSet<Query> = LinkedHashSet() |
|
|
|
|
// var newCombinations: LinkedHashSet<Query> |
|
|
|
|
// |
|
|
|
|
// var index = 0 |
|
|
|
|
// |
|
|
|
|
// // extract each of the integers in the first list |
|
|
|
|
// // and add each to ints as a new list |
|
|
|
|
// if (lists.isNotEmpty()) { |
|
|
|
|
// for (i in lists[0]) { |
|
|
|
|
// val newList = ArrayList<T>() |
|
|
|
|
// newList.add(i) |
|
|
|
|
// combinations.add(newList) |
|
|
|
|
// } |
|
|
|
|
// index++ |
|
|
|
|
// } |
|
|
|
|
// while (index < lists.size) { |
|
|
|
|
// val nextList = lists[index] |
|
|
|
|
// newCombinations = LinkedHashSet() |
|
|
|
|
// for (first in combinations) { |
|
|
|
|
// for (second in nextList) { |
|
|
|
|
// val newList = ArrayList<T>() |
|
|
|
|
// newList.addAll(first) |
|
|
|
|
// newList.add(second) |
|
|
|
|
// newCombinations.add(newList) |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
// combinations = newCombinations |
|
|
|
|
// |
|
|
|
|
// index++ |
|
|
|
|
// } |
|
|
|
|
// |
|
|
|
|
// return combinations.toList() |
|
|
|
|
//} |
|
|
|
|
|
|
|
|
|
sealed class Criteria { |
|
|
|
|
abstract class RealmCriteria : Criteria() { |
|
|
|
|
inline fun <reified T: NameManageable> comparison(): List<QueryCondition> { |
|
|
|
|
inline fun <reified T: NameManageable> comparison(): List<Query> { |
|
|
|
|
return compare<QueryCondition.QueryDataCondition<NameManageable>, T>() |
|
|
|
|
.sorted() |
|
|
|
|
// .sorted() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
abstract class SimpleCriteria(private val conditions:List<QueryCondition>): Criteria() { |
|
|
|
|
fun comparison(): List<QueryCondition> { |
|
|
|
|
return conditions |
|
|
|
|
fun comparison(): List<Query> { |
|
|
|
|
return conditions.map { Query(it) } |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
abstract class ListCriteria : Criteria() { |
|
|
|
|
inline fun <reified T:QueryCondition.ListOfValues<S>, reified S:Comparable<S>> comparison(): List<QueryCondition> { |
|
|
|
|
inline fun <reified T : QueryCondition.ListOfValues<S>, reified S : Comparable<S>> comparison(): List<Query> { |
|
|
|
|
QueryCondition.distinct<Session, T, S>()?.let { |
|
|
|
|
val values = it.mapNotNull { session -> |
|
|
|
|
when (this) { |
|
|
|
|
@ -127,9 +111,9 @@ sealed class Criteria { |
|
|
|
|
else -> null |
|
|
|
|
} |
|
|
|
|
}.distinct() |
|
|
|
|
return compareList<T, S>(values = values).sorted() |
|
|
|
|
return compareList<T, S>(values = values)//.sorted() |
|
|
|
|
} |
|
|
|
|
return listOf<T>() |
|
|
|
|
return listOf() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -154,7 +138,7 @@ sealed class Criteria { |
|
|
|
|
object Cash: SimpleCriteria(listOf(QueryCondition.IsCash)) |
|
|
|
|
object Tournament: SimpleCriteria(listOf(QueryCondition.IsTournament)) |
|
|
|
|
|
|
|
|
|
val predicates: List<List<QueryCondition>> |
|
|
|
|
val queries: List<Query> |
|
|
|
|
get() { |
|
|
|
|
return when (this) { |
|
|
|
|
is AllMonthsUpToNow -> { |
|
|
|
|
@ -163,7 +147,7 @@ sealed class Criteria { |
|
|
|
|
val lastSession = realm.where<Session>().sort("startDate", Sort.DESCENDING).findFirst() |
|
|
|
|
realm.close() |
|
|
|
|
|
|
|
|
|
val years: ArrayList<List<QueryCondition>> = arrayListOf() |
|
|
|
|
val years: ArrayList<Query> = arrayListOf() |
|
|
|
|
|
|
|
|
|
val firstYear = firstSession?.year ?: return years |
|
|
|
|
val firstMonth = firstSession.month ?: return years |
|
|
|
|
@ -182,17 +166,19 @@ sealed class Criteria { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val currentMonth = QueryCondition.AnyMonthOfYear(month) |
|
|
|
|
val currentList = listOf<QueryCondition>(currentYear, currentMonth) |
|
|
|
|
years.add(currentList) |
|
|
|
|
val query = Query(currentYear, currentMonth) |
|
|
|
|
years.add(query) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
years |
|
|
|
|
} |
|
|
|
|
else -> listOf(listOf()) |
|
|
|
|
else -> { |
|
|
|
|
return this.queryConditions |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
val queryConditions: List<QueryCondition> |
|
|
|
|
val queryConditions: List<Query> |
|
|
|
|
get() { |
|
|
|
|
return when (this) { |
|
|
|
|
is Bankrolls -> comparison<Bankroll>() |
|
|
|
|
@ -206,20 +192,21 @@ sealed class Criteria { |
|
|
|
|
is TableSizes -> comparison<QueryCondition.AnyTableSize, Int>() |
|
|
|
|
is TournamentFees -> comparison<QueryCondition.TournamentFee, Double >() |
|
|
|
|
is Years -> { |
|
|
|
|
val years = arrayListOf<QueryCondition.AnyYear>() |
|
|
|
|
val years = arrayListOf<Query>() |
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
val lastSession = realm.where<Session>().sort("startDate", Sort.DESCENDING).findFirst() |
|
|
|
|
val yearNow = lastSession?.year ?: return years |
|
|
|
|
|
|
|
|
|
realm.where<Session>().sort("year", Sort.ASCENDING).findFirst()?.year?.let { |
|
|
|
|
for (index in 0..(yearNow - it)) { |
|
|
|
|
years.add(QueryCondition.AnyYear().apply { |
|
|
|
|
val yearCondition = QueryCondition.AnyYear().apply { |
|
|
|
|
listOfValues = arrayListOf(yearNow - index) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
years.add(Query(yearCondition)) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
realm.close() |
|
|
|
|
years.sorted() |
|
|
|
|
years//.sorted() |
|
|
|
|
} |
|
|
|
|
is Blinds -> comparison<QueryCondition.AnyBlind, String >() |
|
|
|
|
else -> throw PokerAnalyticsException.QueryTypeUnhandled |
|
|
|
|
@ -227,24 +214,27 @@ sealed class Criteria { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
companion object { |
|
|
|
|
inline fun < reified S : QueryCondition.QueryDataCondition<NameManageable >, reified T : NameManageable > compare(): List<S> { |
|
|
|
|
val objects = arrayListOf<S>() |
|
|
|
|
inline fun <reified S : QueryCondition.QueryDataCondition<NameManageable >, reified T : NameManageable> compare(): List<Query> { |
|
|
|
|
val objects = mutableListOf<Query>() |
|
|
|
|
val realm = Realm.getDefaultInstance() |
|
|
|
|
realm.where<T>().findAll().forEach { |
|
|
|
|
objects.add((QueryCondition.getInstance<T>() as S).apply { |
|
|
|
|
val condition = (QueryCondition.getInstance<T>() as S).apply { |
|
|
|
|
setObject(it) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
val query = Query(condition) |
|
|
|
|
objects.add(query) |
|
|
|
|
} |
|
|
|
|
realm.close() |
|
|
|
|
return objects |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
inline fun < reified S : QueryCondition.ListOfValues<T>, T:Any > compareList(values:List<T>): List<S> { |
|
|
|
|
val objects = arrayListOf<S>() |
|
|
|
|
inline fun < reified S : QueryCondition.ListOfValues<T>, T:Any > compareList(values:List<T>): List<Query> { |
|
|
|
|
val objects = mutableListOf<Query>() |
|
|
|
|
values.forEach { |
|
|
|
|
objects.add((S::class.java.newInstance()).apply { |
|
|
|
|
val condition =(S::class.java.newInstance()).apply { |
|
|
|
|
listOfValues = arrayListOf(it) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
objects.add(Query(condition)) |
|
|
|
|
} |
|
|
|
|
return objects |
|
|
|
|
} |
|
|
|
|
|