From b24a9370f7065170f9e59601566436029a86a3af Mon Sep 17 00:00:00 2001 From: wakingrufus Date: Thu, 6 Sep 2018 14:42:53 -0500 Subject: [PATCH] Add winning pct to league results view (#25) * add ability to open data from file and save to file add ability to open data from url Signed-off-by: John Burns * fix #14 Show win percentage in league results view add new calculator for most improved over a period of time Signed-off-by: John Burns --- .../eloleague/results/MostImprovedView.kt | 57 +++++++++++++++++++ .../eloleague/results/ResultsView.kt | 24 ++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/main/kotlin/com/github/wakingrufus/eloleague/results/MostImprovedView.kt diff --git a/src/main/kotlin/com/github/wakingrufus/eloleague/results/MostImprovedView.kt b/src/main/kotlin/com/github/wakingrufus/eloleague/results/MostImprovedView.kt new file mode 100644 index 0000000..38e2358 --- /dev/null +++ b/src/main/kotlin/com/github/wakingrufus/eloleague/results/MostImprovedView.kt @@ -0,0 +1,57 @@ +package com.github.wakingrufus.eloleague.results + +import com.github.wakingrufus.eloleague.player.PlayerItem +import javafx.beans.property.SimpleObjectProperty +import javafx.beans.property.SimpleStringProperty +import tornadofx.* +import java.time.LocalDate +import java.time.ZoneOffset +import java.time.ZonedDateTime + +class MostImprovedView : Fragment() { + val leagueResultItem: LeagueResultItem by param() + var startDate = SimpleObjectProperty(LocalDate.now()) + var endDate = SimpleObjectProperty(LocalDate.now()) + var winner = SimpleStringProperty() + override val root = borderpane { + center { + form { + fieldset { + field { + label("Start Date") + datepicker { + bind(startDate) + } + } + field { + label("End Date") + datepicker { + bind(endDate) + } + } + } + textfield { + isEditable = false + bind(winner) + } + } + + } + bottom { + buttonbar { + button("Calculate") { + action { + winner.value = leagueResultItem.games.asSequence() + .filter { ZonedDateTime.parse(it.entryDate).isAfter(startDate.value.atStartOfDay().atOffset(ZoneOffset.UTC).toZonedDateTime()) } + .filter { ZonedDateTime.parse(it.entryDate).isBefore(endDate.value.plusDays(1).atStartOfDay().atOffset(ZoneOffset.UTC).toZonedDateTime()) } + .fold(mapOf()) { map, game -> + map + (game.player to (map[game.player] ?: 0) + game.ratingAdjustment) + }.maxBy { it.value }?.let { + "${it.key.name} (${it.value})" + } + } + } + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/wakingrufus/eloleague/results/ResultsView.kt b/src/main/kotlin/com/github/wakingrufus/eloleague/results/ResultsView.kt index 0354c34..3eeb320 100644 --- a/src/main/kotlin/com/github/wakingrufus/eloleague/results/ResultsView.kt +++ b/src/main/kotlin/com/github/wakingrufus/eloleague/results/ResultsView.kt @@ -1,11 +1,14 @@ package com.github.wakingrufus.eloleague.results import javafx.beans.binding.Bindings +import javafx.beans.property.ReadOnlyStringWrapper import javafx.collections.FXCollections import javafx.collections.ObservableList import javafx.stage.StageStyle import mu.KLogging import tornadofx.* +import java.math.BigDecimal +import java.math.MathContext class ResultsView : Fragment() { companion object : KLogging() @@ -26,6 +29,17 @@ class ResultsView : Fragment() { id = "results-tableview" column("Name", PlayerResultItem::nameProperty) column("Rating", PlayerResultItem::currentRatingProperty) + column("Win %") { + if (it.value.gamesPlayedProperty.value > 0) { + ReadOnlyStringWrapper((BigDecimal(it.value.winsProperty.value) + .divide(BigDecimal(it.value.gamesPlayedProperty.value), MathContext.DECIMAL32)) + .movePointRight(2) + .toString()) + } else { + ReadOnlyStringWrapper("N/A") + } + + } column("Games", PlayerResultItem::gamesPlayedProperty) column("Wins", PlayerResultItem::winsProperty) column("Losses", PlayerResultItem::lossesProperty) @@ -66,6 +80,16 @@ class ResultsView : Fragment() { playerList.predicate = { it.gamesPlayedProperty >= trialPeriod } } } + button("Most Improved") { + action { + find(mapOf("leagueResultItem" to leagueResultItem)).apply { + openModal( + stageStyle = StageStyle.UTILITY, + block = true + ) + } + } + } } } }