From 97ed99750f62f07f79b88202696d3795f09837da Mon Sep 17 00:00:00 2001 From: Laurent Date: Wed, 3 Jul 2019 15:52:00 +0200 Subject: [PATCH] Updated iOS PA import --- .../android/PokerAnalyticsApplication.kt | 2 +- .../pokeranalytics/android/model/TableSize.kt | 25 +- .../android/model/realm/Import.kt | 2 +- .../android/model/realm/Result.kt | 39 ++- .../android/ui/extensions/UIExtensions.kt | 21 +- .../android/ui/fragment/ImportFragment.kt | 26 +- .../android/util/csv/CSVDescriptor.kt | 10 +- .../android/util/csv/CSVImporter.kt | 5 +- .../android/util/csv/ProductCSVDescriptors.kt | 287 +++++++++--------- .../android/util/csv/SessionCSVDescriptor.kt | 10 +- .../android/util/csv/SessionField.kt | 2 +- .../pokeranalytics/android/BasicUnitTest.kt | 8 + 12 files changed, 258 insertions(+), 179 deletions(-) diff --git a/app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt b/app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt index 99dc6d37..321c458b 100644 --- a/app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt +++ b/app/src/main/java/net/pokeranalytics/android/PokerAnalyticsApplication.kt @@ -60,7 +60,7 @@ class PokerAnalyticsApplication : Application() { if (BuildConfig.DEBUG) { Timber.d("UserPreferences.defaultCurrency: ${UserDefaults.currency.symbol}") - this.createFakeSessions() +// this.createFakeSessions() } Patcher.patchAll(this.applicationContext) diff --git a/app/src/main/java/net/pokeranalytics/android/model/TableSize.kt b/app/src/main/java/net/pokeranalytics/android/model/TableSize.kt index 2b3c69b4..9becbec5 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/TableSize.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/TableSize.kt @@ -4,6 +4,8 @@ import android.content.Context import net.pokeranalytics.android.R import net.pokeranalytics.android.ui.view.RowRepresentable import net.pokeranalytics.android.ui.view.RowViewType +import java.text.NumberFormat +import java.text.ParseException class TableSize(var numberOfPlayer: Int, var rowViewType: Int = RowViewType.TITLE_GRID.ordinal) : RowRepresentable { @@ -15,12 +17,23 @@ class TableSize(var numberOfPlayer: Int, var rowViewType: Int = RowViewType.TITL { index -> TableSize(index + 2) }).toList() } - fun valueForLabel(label: String) : Int? { - return when (label) { - "Full Ring", "Full-Ring" -> 10 - "Short-Handed", "Short Handed" -> 6 - "Heads-Up", "Heads Up" -> 2 - else -> null + /** + * Tries to parse a label into a Table Size Int value, + * using number parsing or label recognition + * A numberFormat can be passed when dealing with lots of volume + */ + fun valueForLabel(label: String, numberFormat: NumberFormat? = null) : Int? { + + return try { + val nf = numberFormat ?: NumberFormat.getInstance() + nf.parse(label).toInt() + } catch (e: ParseException) { + when (label) { + "Full Ring", "Full-Ring" -> 10 + "Short-Handed", "Short Handed" -> 6 + "Heads-Up", "Heads Up" -> 2 + else -> null + } } } } diff --git a/app/src/main/java/net/pokeranalytics/android/model/realm/Import.kt b/app/src/main/java/net/pokeranalytics/android/model/realm/Import.kt index 14449e64..88452cd6 100644 --- a/app/src/main/java/net/pokeranalytics/android/model/realm/Import.kt +++ b/app/src/main/java/net/pokeranalytics/android/model/realm/Import.kt @@ -4,7 +4,7 @@ import io.realm.RealmList import io.realm.RealmObject import java.util.* -class Import : RealmObject() { +open class Import : RealmObject() { var date: Date = Date() 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 19394fcd..5825013a 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 @@ -9,6 +9,7 @@ import io.realm.annotations.RealmClass import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.model.filter.Filterable import net.pokeranalytics.android.model.filter.QueryCondition +import timber.log.Timber @RealmClass open class Result : RealmObject(), Filterable { @@ -51,18 +52,28 @@ open class Result : RealmObject(), Filterable { var netResult: Double? = null set(value) { + var errorMessage: String? = null this.session?.bankroll?.let { bankroll -> if (bankroll.live) { - throw PAIllegalStateException("Can't set net result on a live bankroll") + errorMessage = "Can't set net result on a live bankroll" } } ?: run { - throw PAIllegalStateException("Session doesn't have any bankroll") + errorMessage = "Session doesn't have any bankroll" + } + + errorMessage?.let { + if (isManaged) { + throw PAIllegalStateException(it) + } else { + Timber.w(it) + } } field = value + this.computeNet() if (value != null) { - this.session.end() + this.session?.end() } } @@ -107,16 +118,24 @@ open class Result : RealmObject(), Filterable { val transactionsSum = transactions.sumByDouble { it.amount } - val isLive = this.session?.isLive ?: true - if (isLive) { + 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 - } else { - val netResult = this.netResult ?: 0.0 - this.net = netResult + transactionsSum + val cashedOut = this.cashout ?: 0.0 + this.net = cashedOut - buyin + transactionsSum } +// val isLive = this.session?.isLive ?: true +// if (isLive) { +// val buyin = this.buyin ?: 0.0 +// val cashOut = this.cashout ?: 0.0 +// this.net = cashOut - buyin + transactionsSum +// } else { +// val netResult = this.netResult ?: 0.0 +// this.net = netResult + transactionsSum +// } + // Precompute results this.session?.computeStats() this.session?.sessionSet?.computeStats() diff --git a/app/src/main/java/net/pokeranalytics/android/ui/extensions/UIExtensions.kt b/app/src/main/java/net/pokeranalytics/android/ui/extensions/UIExtensions.kt index 525741b6..22259e2a 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/extensions/UIExtensions.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/extensions/UIExtensions.kt @@ -16,6 +16,7 @@ import androidx.core.content.FileProvider import androidx.core.view.isVisible import net.pokeranalytics.android.BuildConfig import net.pokeranalytics.android.R +import net.pokeranalytics.android.exceptions.PAIllegalStateException import net.pokeranalytics.android.ui.activity.components.PokerAnalyticsActivity import net.pokeranalytics.android.ui.fragment.components.PokerAnalyticsFragment import net.pokeranalytics.android.util.DeviceUtils @@ -104,9 +105,13 @@ fun PokerAnalyticsActivity.showAlertDialog(title: Int? = null, message: Int? = n showAlertDialog(this, title, message) } -fun PokerAnalyticsFragment.showAlertDialog(title: Int? = null, message: Int? = null) { +fun PokerAnalyticsFragment.showAlertDialog(title: Int? = null, message: Int? = null, messageString: String? = null, + cancelButtonTitle: Int? = null, showCancelButton: Boolean = false, + positiveAction: (() -> Unit)? = null, negativeAction: (() -> Unit)? = null) { context?.let { - showAlertDialog(it, title, message) + showAlertDialog(it, title, message, messageString, cancelButtonTitle, showCancelButton, positiveAction, negativeAction) + } ?: run { + throw PAIllegalStateException("Fragment has no context") } } @@ -114,9 +119,10 @@ fun PokerAnalyticsFragment.showAlertDialog(title: Int? = null, message: Int? = n * Create and show an alert dialog */ fun showAlertDialog( - context: Context, title: Int? = null, message: Int? = null, cancelButtonTitle: Int? = null, showCancelButton: Boolean = false, - positiveAction: (() -> Unit)? = null, negativeAction: (() -> Unit)? = null -) { + context: Context, title: Int? = null, message: Int? = null, messageString: String? = null, + cancelButtonTitle: Int? = null, showCancelButton: Boolean = false, + positiveAction: (() -> Unit)? = null, negativeAction: (() -> Unit)? = null) { + val builder = AlertDialog.Builder(context) title?.let { builder.setTitle(title) @@ -124,7 +130,10 @@ fun showAlertDialog( message?.let { builder.setMessage(message) } - builder.setPositiveButton(net.pokeranalytics.android.R.string.ok) { _, _ -> + messageString?.let { + builder.setMessage(it) + } + builder.setPositiveButton(R.string.ok) { _, _ -> positiveAction?.invoke() } diff --git a/app/src/main/java/net/pokeranalytics/android/ui/fragment/ImportFragment.kt b/app/src/main/java/net/pokeranalytics/android/ui/fragment/ImportFragment.kt index 7232cc85..d75fba93 100644 --- a/app/src/main/java/net/pokeranalytics/android/ui/fragment/ImportFragment.kt +++ b/app/src/main/java/net/pokeranalytics/android/ui/fragment/ImportFragment.kt @@ -10,6 +10,7 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.async import kotlinx.coroutines.launch import net.pokeranalytics.android.R +import net.pokeranalytics.android.ui.extensions.showAlertDialog import net.pokeranalytics.android.ui.fragment.components.RealmFragment import net.pokeranalytics.android.util.csv.CSVImporter import net.pokeranalytics.android.util.csv.ImportDelegate @@ -69,7 +70,7 @@ class ImportFragment : RealmFragment(), ImportDelegate { private fun startImport() { -// var shouldDismissActivity = false + var errorMessage: String? = null this.importer = CSVImporter(inputStream) this.importer.delegate = this @@ -83,7 +84,7 @@ class ImportFragment : RealmFragment(), ImportDelegate { try { importer.start() } catch (e: ImportException) { -// shouldDismissActivity = true + errorMessage = e.message } val e = Date() val duration = (e.time - s.time) / 1000.0 @@ -92,16 +93,17 @@ class ImportFragment : RealmFragment(), ImportDelegate { } test.await() -// if (shouldDismissActivity) { -// -// activity?.let { -// it.setResult(ResultCode.IMPORT_UNRECOGNIZED_FORMAT.value) -// it.finish() -// } -// -// } else { -// } - importDidFinish() + if (errorMessage != null) { + + showAlertDialog( + messageString = errorMessage, + positiveAction = { + activity?.finish() + }) + + } else { + importDidFinish() + } } diff --git a/app/src/main/java/net/pokeranalytics/android/util/csv/CSVDescriptor.kt b/app/src/main/java/net/pokeranalytics/android/util/csv/CSVDescriptor.kt index 6079cfcd..f4aef785 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/csv/CSVDescriptor.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/csv/CSVDescriptor.kt @@ -9,6 +9,7 @@ import net.pokeranalytics.android.model.realm.Session import net.pokeranalytics.android.util.extensions.findById import org.apache.commons.csv.CSVRecord import timber.log.Timber +import java.text.NumberFormat /** * The various sources of CSV @@ -127,9 +128,9 @@ abstract class CSVDescriptor(var source: DataSource, vararg elements: CSVField) count++ } } - val mandatoryfields = this.fields.filter { it.optional == false } + val mandatoryFields = this.fields.filter { it.optional == false } Timber.d("source= ${this.source.name} > total fields = ${this.fields.size}, identified = $count") - return count >= mandatoryfields.size + return count >= mandatoryFields.size } fun mapCustomField(record: CSVRecord, realm: Realm) { @@ -142,8 +143,9 @@ abstract class CSVDescriptor(var source: DataSource, vararg elements: CSVField) val splitter = "|" if (header.contains(splitter)) { val info = header.split(splitter) - val typeIdentifier = header.last().toInt() - val type = CustomField.Type.valueByIdentifier(typeIdentifier) + val typeIdentifier = NumberFormat.getInstance().parse(header.last().toString()) + Timber.d("header = $header, info = $info, id = $typeIdentifier") + val type = CustomField.Type.valueByIdentifier(typeIdentifier.toInt()) CustomField.getOrCreate(realm, info.first(), type) } } diff --git a/app/src/main/java/net/pokeranalytics/android/util/csv/CSVImporter.kt b/app/src/main/java/net/pokeranalytics/android/util/csv/CSVImporter.kt index ba04cfcc..c5c3c2c8 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/csv/CSVImporter.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/csv/CSVImporter.kt @@ -178,7 +178,10 @@ open class CSVImporter(istream: InputStream) { */ private fun findDescriptor(record: CSVRecord, realm: Realm): CSVDescriptor? { - ProductCSVDescriptors.all.forEach { descriptor -> + val allCSVDescriptors = ProductCSVDescriptors.all + Timber.d(allCSVDescriptors.toString()) + allCSVDescriptors.forEach { descriptor -> + Timber.d("descriptor = $descriptor, record = $record") if (descriptor.matches(record)) { descriptor.mapCustomField(record, realm) this.currentDescriptor = descriptor diff --git a/app/src/main/java/net/pokeranalytics/android/util/csv/ProductCSVDescriptors.kt b/app/src/main/java/net/pokeranalytics/android/util/csv/ProductCSVDescriptors.kt index baf3f3a5..dd78a0bf 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/csv/ProductCSVDescriptors.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/csv/ProductCSVDescriptors.kt @@ -9,149 +9,164 @@ class ProductCSVDescriptors { */ val all: List = listOf( - ProductCSVDescriptors.pokerIncomeCash, - ProductCSVDescriptors.pokerBankrollTracker, - ProductCSVDescriptors.runGoodCashGames, - ProductCSVDescriptors.runGoodTournaments, - ProductCSVDescriptors.iOSPokerAnalytics - + pokerIncomeCash, + pokerBankrollTracker, + runGoodCashGames, + runGoodTournaments, + iOSPokerAnalytics ) - val pokerIncomeCash: CSVDescriptor = SessionCSVDescriptor( - DataSource.POKER_INCOME, - false, - SessionField.Start("Start Time"), - SessionField.End("End Time"), - SessionField.Buyin("Buy In"), - SessionField.CashedOut("Cashed Out"), - SessionField.Break("Break Minutes"), - SessionField.LimitType("Limit Type"), - SessionField.Game("Game"), - SessionField.Bankroll("Bankroll"), - SessionField.Location("Location"), - SessionField.Location("Location Type"), - SessionField.Comment("Note"), - SessionField.Tips("Tips"), - SessionField.Blind("Stake") - ) + private val pokerIncomeCash: CSVDescriptor + get() { + return SessionCSVDescriptor( + DataSource.POKER_INCOME, + false, + SessionField.Start("Start Time"), + SessionField.End("End Time"), + SessionField.Buyin("Buy In"), + SessionField.CashedOut("Cashed Out"), + SessionField.Break("Break Minutes"), + SessionField.LimitType("Limit Type"), + SessionField.Game("Game"), + SessionField.Bankroll("Bankroll"), + SessionField.Location("Location"), + SessionField.Location("Location Type"), + SessionField.Comment("Note"), + SessionField.Tips("Tips"), + SessionField.Blind("Stake") + ) + } - val pokerBankrollTracker: CSVDescriptor = SessionCSVDescriptor( - DataSource.POKER_BANKROLL_TRACKER, - true, - SessionField.Start("starttime", dateFormat = "MM/dd/yy HH:mm"), - SessionField.End("endtime", dateFormat = "MM/dd/yy HH:mm"), - SessionField.SessionType("variant"), - SessionField.Buyin("buyin"), - SessionField.CashedOut("cashout"), - SessionField.Rebuy("rebuycosts"), - SessionField.Addon("addoncosts"), - SessionField.Break("breakminutes"), - SessionField.LimitType("limit"), - SessionField.Game("game"), - SessionField.Bankroll("currency"), // same as currency code - SessionField.Location("type"), - SessionField.Comment("comment", true), - SessionField.Tips("expensesfromstack"), - SessionField.SmallBlind("smallblind"), - SessionField.BigBlind("bigblind"), - SessionField.TournamentNumberOfPlayers("player"), - SessionField.TournamentPosition("place"), - SessionField.TournamentName("mttname"), - SessionField.CurrencyCode("currency"), - SessionField.StackingIn("sharesincomings"), - SessionField.StackingOut("sharesoutgoings"), - SessionField.CurrencyRate("exchangerate"), - SessionField.TableSize("tablesize") - ) + private val pokerBankrollTracker: CSVDescriptor + get() { + return SessionCSVDescriptor( + DataSource.POKER_BANKROLL_TRACKER, + true, + SessionField.Start("starttime", dateFormat = "MM/dd/yy HH:mm"), + SessionField.End("endtime", dateFormat = "MM/dd/yy HH:mm"), + SessionField.SessionType("variant"), + SessionField.Buyin("buyin"), + SessionField.CashedOut("cashout"), + SessionField.Rebuy("rebuycosts"), + SessionField.Addon("addoncosts"), + SessionField.Break("breakminutes"), + SessionField.LimitType("limit"), + SessionField.Game("game"), + SessionField.Bankroll("currency"), // same as currency code + SessionField.Location("type"), + SessionField.Comment("comment", true), + SessionField.Tips("expensesfromstack"), + SessionField.SmallBlind("smallblind"), + SessionField.BigBlind("bigblind"), + SessionField.TournamentNumberOfPlayers("player"), + SessionField.TournamentPosition("place"), + SessionField.TournamentName("mttname"), + SessionField.CurrencyCode("currency"), + SessionField.StackingIn("sharesincomings"), + SessionField.StackingOut("sharesoutgoings"), + SessionField.CurrencyRate("exchangerate"), + SessionField.TableSize("tablesize") + ) + } - val iOSPokerAnalytics: CSVDescriptor = SessionCSVDescriptor( - DataSource.POKER_ANALYTICS, - true, - SessionField.Start("Start date", dateFormat = "MM/dd/yy HH:mm:ss"), - SessionField.End("End date", dateFormat = "MM/dd/yy HH:mm:ss"), - SessionField.Break("Break", callback = { string -> - val number = NumberCSVField.defaultParse(string) - return@Break number?.times(1000.0) - }), - SessionField.SessionType("Type"), - SessionField.Live("Live"), - SessionField.Buyin("Buy-in"), - SessionField.CashedOut("Cashed Out"), - SessionField.NetResult("Net Result"), - SessionField.Tips("Tips"), - SessionField.LimitType("Limit"), - SessionField.Game("Game"), - SessionField.TableSize("Table size"), - SessionField.Location("Location"), - SessionField.NumberOfTables("Tables"), - SessionField.Bankroll("Bankroll"), - SessionField.CurrencyCode("Currency Code"), - SessionField.SmallBlind("Small Blind"), - SessionField.BigBlind("Big Blind"), - SessionField.TournamentType("Tournament Type"), - SessionField.TournamentEntryFee("Entry fee"), - SessionField.TournamentNumberOfPlayers("Number of players"), - SessionField.TournamentPrizePool("Prize Pool"), - SessionField.TournamentPosition("Position"), - SessionField.Comment("Comment") - ) - val runGoodTournaments: CSVDescriptor = SessionCSVDescriptor( - DataSource.RUNGOOD, - true, - SessionField.Start("Start Date", dateFormat = "dd/MM/yyyy"), - SessionField.StartTime("Start Time"), - SessionField.End("End Date"), - SessionField.EndTime("End Time"), - SessionField.Buyin("Total Buy-In"), - SessionField.CashedOut("Winnings"), - SessionField.NetResult("Profit"), - SessionField.Break("Break"), - SessionField.LimitType("Limit Type"), - SessionField.Game("Game"), - SessionField.Bankroll("Bankroll"), - SessionField.TableSize("Table Type"), - SessionField.Location("Location"), - SessionField.LocationType("Location Type"), - SessionField.Comment("Notes"), - SessionField.CurrencyCode("Currency"), - SessionField.TournamentName("Event Name"), - SessionField.TournamentNumberOfPlayers("Total Players"), - SessionField.TournamentPosition("Finished Place"), - SessionField.TournamentType("Single-Table/Multi-Table") + private val iOSPokerAnalytics: CSVDescriptor + get() { + return SessionCSVDescriptor( + DataSource.POKER_ANALYTICS, + true, + SessionField.Start("Start Date", dateFormat = "MM/dd/yy HH:mm:ss"), + SessionField.End("End Date", dateFormat = "MM/dd/yy HH:mm:ss"), + SessionField.Break("Break", callback = { string -> + val number = NumberCSVField.defaultParse(string) + return@Break number?.times(1000.0) + }), + SessionField.SessionType("Type"), + SessionField.Live("Live"), + SessionField.NumberOfTables("Tables"), + SessionField.Buyin("Buyin"), + SessionField.CashedOut("Cashed Out"), + SessionField.NetResult("Online Net"), + SessionField.Tips("Tips"), + SessionField.LimitType("Limit"), + SessionField.Game("Game"), + SessionField.TableSize("Table Size"), + SessionField.Location("Location"), + SessionField.Bankroll("Bankroll"), + SessionField.CurrencyCode("Currency Code"), + SessionField.CurrencyRate("Currency Rate"), + SessionField.SmallBlind("Small Blind"), + SessionField.BigBlind("Big Blind"), + SessionField.TournamentType("Tournament Type"), + SessionField.TournamentEntryFee("Entry fee"), + SessionField.TournamentNumberOfPlayers("Number of players"), + SessionField.TournamentPrizePool("Prize Pool"), + SessionField.TournamentPosition("Position"), + SessionField.Comment("Comment") + ) + } - ) + private val runGoodTournaments: CSVDescriptor + get() { + return SessionCSVDescriptor( + DataSource.RUNGOOD, + true, + SessionField.Start("Start Date", dateFormat = "dd/MM/yyyy"), + SessionField.StartTime("Start Time"), + SessionField.End("End Date"), + SessionField.EndTime("End Time"), + SessionField.Buyin("Total Buy-In"), + SessionField.CashedOut("Winnings"), + SessionField.NetResult("Profit"), + SessionField.Break("Break"), + SessionField.LimitType("Limit Type"), + SessionField.Game("Game"), + SessionField.Bankroll("Bankroll"), + SessionField.TableSize("Table Type"), + SessionField.Location("Location"), + SessionField.LocationType("Location Type"), + SessionField.Comment("Notes"), + SessionField.CurrencyCode("Currency"), + SessionField.TournamentName("Event Name"), + SessionField.TournamentNumberOfPlayers("Total Players"), + SessionField.TournamentPosition("Finished Place"), + SessionField.TournamentType("Single-Table/Multi-Table") + ) + } - val runGoodCashGames: CSVDescriptor = SessionCSVDescriptor( - DataSource.RUNGOOD, - false, - SessionField.Start("Start Date", dateFormat = "dd/MM/yyyy"), - SessionField.StartTime("Start Time", dateFormat = "HH:mm"), - SessionField.End("End Date", dateFormat = "dd/MM/yyyy"), - SessionField.EndTime("End Time", dateFormat = "HH:mm"), - SessionField.Buyin("Total Buy-In"), - SessionField.CashedOut("Cashed Out"), - SessionField.NetResult("Profit"), - SessionField.Break("Break"), - SessionField.LimitType("Limit Type"), - SessionField.Game("Game"), - SessionField.Bankroll("Bankroll"), - SessionField.TableSize("Table Type"), - SessionField.Location("Location"), - SessionField.LocationType("Location Type"), - SessionField.Comment("Notes"), - SessionField.CurrencyCode("Currency"), - SessionField.Blind("Stakes", callback = { value -> - // $10/20 - value.drop(1) - val blinds = value.split("/") - if (blinds.size == 2) { - return@Blind Pair(blinds.first().toDouble(), blinds.last().toDouble()) - } else { - return@Blind null - } - }) - ) + private val runGoodCashGames: CSVDescriptor + get() { + return SessionCSVDescriptor( + DataSource.RUNGOOD, + false, + SessionField.Start("Start Date", dateFormat = "dd/MM/yyyy"), + SessionField.StartTime("Start Time", dateFormat = "HH:mm"), + SessionField.End("End Date", dateFormat = "dd/MM/yyyy"), + SessionField.EndTime("End Time", dateFormat = "HH:mm"), + SessionField.Buyin("Total Buy-In"), + SessionField.CashedOut("Cashed Out"), + SessionField.NetResult("Profit"), + SessionField.Break("Break"), + SessionField.LimitType("Limit Type"), + SessionField.Game("Game"), + SessionField.Bankroll("Bankroll"), + SessionField.TableSize("Table Type"), + SessionField.Location("Location"), + SessionField.LocationType("Location Type"), + SessionField.Comment("Notes"), + SessionField.CurrencyCode("Currency"), + SessionField.Blind("Stakes", callback = { value -> + // $10/20 + value.drop(1) + val blinds = value.split("/") + if (blinds.size == 2) { + return@Blind Pair(blinds.first().toDouble(), blinds.last().toDouble()) + } else { + return@Blind null + } + }) + ) + } } diff --git a/app/src/main/java/net/pokeranalytics/android/util/csv/SessionCSVDescriptor.kt b/app/src/main/java/net/pokeranalytics/android/util/csv/SessionCSVDescriptor.kt index f5e5f4a0..95814083 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/csv/SessionCSVDescriptor.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/csv/SessionCSVDescriptor.kt @@ -14,6 +14,7 @@ import net.pokeranalytics.android.util.extensions.getOrCreate import net.pokeranalytics.android.util.extensions.setHourMinutes import org.apache.commons.csv.CSVRecord import timber.log.Timber +import java.text.NumberFormat import java.util.* /** @@ -124,12 +125,14 @@ class SessionCSVDescriptor(source: DataSource, private var isTournament: Boolean private fun parseSession(realm: Realm, record: CSVRecord): Session? { val session = Session.newInstance(realm, this.isTournament, managed = false) + val intFormatter = NumberFormat.getInstance() var startDate: Date? = null var endDate: Date? = null var isLive = true var bankrollName = "" + var netResult: Double? = null var currencyCode: String? = null var currencyRate: Double? = null var additionalBuyins = 0.0 // rebuy + addon @@ -167,6 +170,7 @@ class SessionCSVDescriptor(source: DataSource, private var isTournament: Boolean } } is SessionField.CashedOut -> session.result?.cashout = field.parse(value) + is SessionField.NetResult -> netResult = field.parse(value) is SessionField.SessionType -> { Session.Type.getValueFromString(value)?.let { type -> session.type = type.ordinal @@ -202,7 +206,7 @@ class SessionCSVDescriptor(source: DataSource, private var isTournament: Boolean } is SessionField.SmallBlind -> session.cgSmallBlind = field.parse(value) is SessionField.BigBlind -> session.cgBigBlind = field.parse(value) - is SessionField.TableSize -> session.tableSize = TableSize.valueForLabel(value) + is SessionField.TableSize -> session.tableSize = TableSize.valueForLabel(value, intFormatter) is SessionField.TournamentPosition -> session.result?.tournamentFinalPosition = field.parse(value)?.toInt() is SessionField.TournamentName -> { @@ -248,6 +252,10 @@ class SessionCSVDescriptor(source: DataSource, private var isTournament: Boolean val bankroll = Bankroll.getOrCreate(realm, bankrollName, isLive, currencyCode, currencyRate) session.bankroll = bankroll + netResult?.let { + session.result?.netResult = it // need to be set after BR + } + session.result?.buyin?.let { session.result?.buyin = it + additionalBuyins } diff --git a/app/src/main/java/net/pokeranalytics/android/util/csv/SessionField.kt b/app/src/main/java/net/pokeranalytics/android/util/csv/SessionField.kt index c4353635..7e4792c5 100644 --- a/app/src/main/java/net/pokeranalytics/android/util/csv/SessionField.kt +++ b/app/src/main/java/net/pokeranalytics/android/util/csv/SessionField.kt @@ -132,7 +132,7 @@ sealed class SessionField { data class Blind(override var header: String, override var callback: ((String) -> Pair?)? = null) : BlindCSVField - data class Stakes(override var header: String) : CSVField +// data class Stakes(override var header: String) : CSVField data class Game(override var header: String) : CSVField data class Location(override var header: String) : CSVField data class LocationType(override var header: String) : CSVField diff --git a/app/src/test/java/net/pokeranalytics/android/BasicUnitTest.kt b/app/src/test/java/net/pokeranalytics/android/BasicUnitTest.kt index 3ab9079b..e09fb8d7 100644 --- a/app/src/test/java/net/pokeranalytics/android/BasicUnitTest.kt +++ b/app/src/test/java/net/pokeranalytics/android/BasicUnitTest.kt @@ -1,5 +1,6 @@ package net.pokeranalytics.android +import net.pokeranalytics.android.model.TableSize import net.pokeranalytics.android.util.extensions.kmbFormatted import org.junit.Assert import org.junit.Test @@ -33,4 +34,11 @@ class BasicUnitTest : RealmUnitTest() { } + @Test + fun testTableSizeParsing() { + Assert.assertEquals(TableSize.valueForLabel("1"), 1) + Assert.assertEquals(TableSize.valueForLabel("Full Ring"), 10) + Assert.assertNotEquals(TableSize.valueForLabel("pouet1"), 1) + } + }