Skip to content

Commit

Permalink
add ability to open data from file and save to file (#24)
Browse files Browse the repository at this point in the history
add ability to open data from url

Signed-off-by: John Burns <[email protected]>
  • Loading branch information
wakingrufus authored Sep 6, 2018
1 parent bef01cb commit 04cdf70
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 33 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ idea {
}

task wrapper(type: Wrapper) {
gradleVersion = '4.7'
gradleVersion = '4.8.1'
}

jacocoTestReport {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package com.github.wakingrufus.eloleague.data
package com.github.wakingrufus.eloleague.dao

import com.github.wakingrufus.eloleague.data.ConfigData


interface DataHandler {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.github.wakingrufus.eloleague.data
package com.github.wakingrufus.eloleague.dao

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule
import com.github.wakingrufus.eloleague.data.ConfigData
import mu.KLogging
import java.io.File
import java.io.IOException

class FileDataHandler(val file: File = File(File(System.getProperty("user.home")), ".elo.json"))
class JacksonFileDataHandler(val file: File = File(File(System.getProperty("user.home")), ".elo.json"))
: DataHandler {
companion object : KLogging() {
val objectMapper = ObjectMapper()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.wakingrufus.eloleague.dao

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule
import com.github.wakingrufus.eloleague.data.ConfigData
import mu.KLogging
import java.io.IOException
import java.net.URL

class JacksonUrlDataHandler(val url: URL) : DataHandler {
companion object : KLogging() {
val objectMapper = ObjectMapper()
.registerModule(ParameterNamesModule())
.registerModule(KotlinModule())
}

override fun readFileConfig(): ConfigData {
return try {
objectMapper.readValue(url, ConfigData::class.java)
} catch (e: IOException) {
logger.error("Error reading data file: " + e.localizedMessage, e)
ConfigData()
}
}

override fun saveConfig(configData: ConfigData) {
System.out.println("save url not implemented")
}

}
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
package com.github.wakingrufus.eloleague.league

import com.github.wakingrufus.eloleague.data.ConfigData
import com.github.wakingrufus.eloleague.data.DataHandler
import com.github.wakingrufus.eloleague.data.FileDataHandler
import javafx.collections.FXCollections
import tornadofx.Controller
import java.io.File
import tornadofx.*
import java.util.*

class LeagueListController : Controller() {

val leagues = FXCollections.observableArrayList<LeagueItem>(
LeagueItem(id = UUID.randomUUID().toString())
)
val configHandler: DataHandler =
FileDataHandler(File(File(System.getProperty("user.home")), ".elo.json"))

init {
val configData = configHandler.readFileConfig()
if (configData.leagues.isNotEmpty()) {
leagues.setAll(configData.leagues.map { fromData(it) })
}
}

fun save() {
log.info("saving data")
configHandler.saveConfig(
ConfigData(leagues = leagues
.filterNotNull()
.map { toData(it) }
.toSet()))
}

fun newLeague(): LeagueItem {
return LeagueItem(id = UUID.randomUUID().toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ class LeagueListView : View("League List") {
controller.leagues.remove(model.item)
}
}
button(text = "Save All").setOnAction {
controller.save()
}
}
}
}
54 changes: 52 additions & 2 deletions src/main/kotlin/com/github/wakingrufus/eloleague/ui/MainView.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,65 @@
package com.github.wakingrufus.eloleague.ui

import com.github.wakingrufus.eloleague.league.LeagueListView
import com.github.wakingrufus.eloleague.league.LeagueView
import com.github.wakingrufus.eloleague.dao.JacksonFileDataHandler
import com.github.wakingrufus.eloleague.dao.JacksonUrlDataHandler
import com.github.wakingrufus.eloleague.data.ConfigData
import com.github.wakingrufus.eloleague.league.*
import javafx.stage.FileChooser
import javafx.stage.StageStyle
import mu.KLogging
import tornadofx.*

class MainView : View("ELO League") {
companion object : KLogging()

val leagueListController: LeagueListController by inject()

override val root = borderpane {
minHeight = 100.percent.value
top {
menubar {
menu("File") {
item("Open File...").action {
FileChooser().apply {
extensionFilters.add(FileChooser.ExtensionFilter("JSON File", "*.json"))
}.run {
showOpenDialog(null)
}?.run {
JacksonFileDataHandler(this).readFileConfig().leagues.run {
if (isNotEmpty()) {
leagueListController.leagues.setAll(this.map { fromData(it) })
}
}
}
}
item("Open Url...").action {
find<UrlDialog>().apply {
openModal(stageStyle = StageStyle.UTILITY, block = true)
}.url?.run {
JacksonUrlDataHandler(this).readFileConfig().leagues.run {
if (isNotEmpty()) {
leagueListController.leagues.setAll(this.map { fromData(it) })
}
}
}
}
separator()
item("Save File...").action {
FileChooser().apply {
extensionFilters.add(FileChooser.ExtensionFilter("JSON File", "*.json"))
}.run {
showSaveDialog(null)
}?.run {
JacksonFileDataHandler(this).saveConfig(
ConfigData(leagues = leagueListController.leagues
.filterNotNull()
.map { toData(it) }
.toSet()))
}
}
}
}
}
left {
this += LeagueListView::class
}
Expand Down
52 changes: 52 additions & 0 deletions src/main/kotlin/com/github/wakingrufus/eloleague/ui/UrlDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.github.wakingrufus.eloleague.ui

import javafx.beans.property.SimpleStringProperty
import tornadofx.*
import java.net.URI
import java.net.URL


class UrlPickerItem(url: String) {
val urlProperty = SimpleStringProperty(this, "", url)
var url by urlProperty
}

class UrlPickerModel : ItemViewModel<UrlPickerItem>() {
var url = bind(UrlPickerItem::urlProperty)
}

class UrlDialog : Fragment() {
var url: URL? = null
private val urlProperty = UrlPickerModel()
override val root = borderpane {
center {
form {
fieldset {
field {
label("URL")
textfield(urlProperty.url).validator {
if (URI.create(it.toString()) != null) null else error("error")
}
}
}
}
}
bottom {
buttonbar {
button("OK") {
enableWhen(urlProperty.dirty)
action {
url = URI.create(urlProperty.url.value).toURL()
close()
}
}
button("Cancel") {
action {
url = null
close()
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.wakingrufus.eloleague

import com.github.wakingrufus.eloleague.data.ConfigData
import com.github.wakingrufus.eloleague.data.DataHandler
import com.github.wakingrufus.eloleague.dao.DataHandler
import com.nhaarman.mockito_kotlin.doReturn
import com.nhaarman.mockito_kotlin.mock
import javafx.stage.Stage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.github.wakingrufus.eloleague.dao

import org.junit.Test
import kotlin.test.assertEquals

class JacksonUrlDataHandlerTest {
@Test
fun test() {
val actual = JacksonUrlDataHandler(javaClass.classLoader.getResource("elo-test.json")).readFileConfig()
assertEquals(expected = 1, actual = actual.leagues.size)
}
}
1 change: 1 addition & 0 deletions src/test/resources/elo-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"leagues":[{"id":"8ee58b3b-183b-455b-8d54-db9972c26f33","name":"Test League","players":[{"id":"0ef83839-474e-4204-8c27-877acfb6f1a8","name":"Dave"},{"id":"56c5d390-f15c-45c5-a2f8-11076bada6be","name":"Alice"},{"id":"c143defc-27b6-4682-bfd4-b718c6774d26","name":"Bob"},{"id":"f4ed42ec-45e2-43c4-b976-0c79e9a0aa20","name":"Harold"},{"id":"f7110f75-fbb2-4291-a5db-cbfff784f296","name":"Frank"},{"id":"7fbf8032-5fed-4d11-8030-8099fdb6fc7f","name":"Gary"},{"id":"ba6778db-ead2-4265-8940-d7d14b5fe03b","name":"Carol"},{"id":"6c84336a-6caa-4369-8224-89611c7e003b","name":"Erin"},{"id":"59be1e67-1993-47dc-9727-0964b71f034c","name":"Ian"}],"games":[{"id":"331299ee-6ea1-4b4a-a970-f01c77174fbb","team1Score":1,"team2Score":0,"timestamp":1516922304148,"team1PlayerIds":["56c5d390-f15c-45c5-a2f8-11076bada6be","c143defc-27b6-4682-bfd4-b718c6774d26"],"team2PlayerIds":["0ef83839-474e-4204-8c27-877acfb6f1a8","ba6778db-ead2-4265-8940-d7d14b5fe03b"]},{"id":"f9c1b4b5-c52b-44bd-9b9a-287da1eed88e","team1Score":4,"team2Score":0,"timestamp":1516922316670,"team1PlayerIds":["c143defc-27b6-4682-bfd4-b718c6774d26","ba6778db-ead2-4265-8940-d7d14b5fe03b"],"team2PlayerIds":["56c5d390-f15c-45c5-a2f8-11076bada6be","0ef83839-474e-4204-8c27-877acfb6f1a8"]},{"id":"454902e3-5f62-4a1b-9429-271196de3489","team1Score":0,"team2Score":9,"timestamp":1516975661716,"team1PlayerIds":["c143defc-27b6-4682-bfd4-b718c6774d26","56c5d390-f15c-45c5-a2f8-11076bada6be"],"team2PlayerIds":["ba6778db-ead2-4265-8940-d7d14b5fe03b","0ef83839-474e-4204-8c27-877acfb6f1a8"]},{"id":"a807b30b-43d3-421f-bea0-2f338ee979aa","team1Score":10,"team2Score":0,"timestamp":1516975894382,"team1PlayerIds":["c143defc-27b6-4682-bfd4-b718c6774d26","56c5d390-f15c-45c5-a2f8-11076bada6be"],"team2PlayerIds":["ba6778db-ead2-4265-8940-d7d14b5fe03b","0ef83839-474e-4204-8c27-877acfb6f1a8"]},{"id":"dca1ef21-27e5-4a8a-a9ae-78aa639b7cae","team1Score":5,"team2Score":0,"timestamp":1517319555925,"team1PlayerIds":["f4ed42ec-45e2-43c4-b976-0c79e9a0aa20","7fbf8032-5fed-4d11-8030-8099fdb6fc7f"],"team2PlayerIds":["6c84336a-6caa-4369-8224-89611c7e003b","f7110f75-fbb2-4291-a5db-cbfff784f296"]},{"id":"d8553781-2eb1-4bd8-aad3-808044b976d5","team1Score":0,"team2Score":8,"timestamp":1517319572836,"team1PlayerIds":["7fbf8032-5fed-4d11-8030-8099fdb6fc7f","f4ed42ec-45e2-43c4-b976-0c79e9a0aa20"],"team2PlayerIds":["56c5d390-f15c-45c5-a2f8-11076bada6be","c143defc-27b6-4682-bfd4-b718c6774d26"]},{"id":"02e4e605-0c1e-4db9-a47a-c998e8664c56","team1Score":2,"team2Score":3,"timestamp":1517319590081,"team1PlayerIds":["f4ed42ec-45e2-43c4-b976-0c79e9a0aa20","7fbf8032-5fed-4d11-8030-8099fdb6fc7f"],"team2PlayerIds":["ba6778db-ead2-4265-8940-d7d14b5fe03b","0ef83839-474e-4204-8c27-877acfb6f1a8"]},{"id":"a8f2fe9b-c0df-457a-b3fd-dfd997515df1","team1Score":0,"team2Score":2,"timestamp":1517319609428,"team1PlayerIds":["6c84336a-6caa-4369-8224-89611c7e003b","f7110f75-fbb2-4291-a5db-cbfff784f296"],"team2PlayerIds":["56c5d390-f15c-45c5-a2f8-11076bada6be","c143defc-27b6-4682-bfd4-b718c6774d26"]},{"id":"a7097e4d-5ea8-4f62-ba1e-ac9fe786229d","team1Score":3,"team2Score":6,"timestamp":1517319621723,"team1PlayerIds":["6c84336a-6caa-4369-8224-89611c7e003b","f7110f75-fbb2-4291-a5db-cbfff784f296"],"team2PlayerIds":["ba6778db-ead2-4265-8940-d7d14b5fe03b","0ef83839-474e-4204-8c27-877acfb6f1a8"]},{"id":"d5a29f13-fde9-4458-bd9a-26a52a128623","team1Score":1,"team2Score":3,"timestamp":1518217771143,"team1PlayerIds":["0ef83839-474e-4204-8c27-877acfb6f1a8"],"team2PlayerIds":["c143defc-27b6-4682-bfd4-b718c6774d26"]},{"id":"c9bd9644-bd99-4687-b546-490ef13a332c","team1Score":2,"team2Score":0,"timestamp":1518217787941,"team1PlayerIds":["0ef83839-474e-4204-8c27-877acfb6f1a8"],"team2PlayerIds":["c143defc-27b6-4682-bfd4-b718c6774d26"]}],"startingRating":1500,"xi":1000,"trialPeriod":10,"trialKFactorMultiplier":2,"tournamentData":[{"id":"fd172f6d-9294-4295-b35c-01d127ec961d","startTime":1516752150053,"name":"Tournament 1","teams":[{"id":"bbd8b1ab-4f4c-4374-8a46-9102b89b1d81","name":"Team 2","playerIds":["0ef83839-474e-4204-8c27-877acfb6f1a8","ba6778db-ead2-4265-8940-d7d14b5fe03b"]},{"id":"7c01d205-8f11-43ca-9175-b52c484705a1","name":"Team 1","playerIds":["56c5d390-f15c-45c5-a2f8-11076bada6be","c143defc-27b6-4682-bfd4-b718c6774d26"]}],"rounds":[{"roundNumber":1,"pairings":[{"id":"4a917b01-1468-42a1-b31d-36301952442a","teamIds":["bbd8b1ab-4f4c-4374-8a46-9102b89b1d81","7c01d205-8f11-43ca-9175-b52c484705a1"],"games":[{"id":"989a5474-1d5f-425b-ab23-617b13449ae0","gameId":"331299ee-6ea1-4b4a-a970-f01c77174fbb","winningTeamId":"7c01d205-8f11-43ca-9175-b52c484705a1"},{"id":"6b14fd59-b43f-478e-9b5e-0380dc1552c2","gameId":"a807b30b-43d3-421f-bea0-2f338ee979aa","winningTeamId":"7c01d205-8f11-43ca-9175-b52c484705a1"},{"id":"f5e1fbd0-91a9-4886-b94a-a55fb3745842","gameId":"454902e3-5f62-4a1b-9429-271196de3489","winningTeamId":"bbd8b1ab-4f4c-4374-8a46-9102b89b1d81"}],"drops":[]}]}]},{"id":"54f93ead-ad7a-4cbb-b035-2c33c9e0c773","startTime":1516752284200,"name":"tournament2","teams":[{"id":"68eda989-77e5-488c-b467-0ec52ac3b3da","name":"team 4","playerIds":["f4ed42ec-45e2-43c4-b976-0c79e9a0aa20","7fbf8032-5fed-4d11-8030-8099fdb6fc7f"]},{"id":"5f193445-3f27-44f5-90f7-c1a728580976","name":"team 3","playerIds":["6c84336a-6caa-4369-8224-89611c7e003b","f7110f75-fbb2-4291-a5db-cbfff784f296"]},{"id":"8b01dc11-4544-4319-a71e-6ba2e65f3960","name":"team 2","playerIds":["0ef83839-474e-4204-8c27-877acfb6f1a8","ba6778db-ead2-4265-8940-d7d14b5fe03b"]},{"id":"18d316fe-cf8a-4bf2-814d-5ea8872522f2","name":"team 1","playerIds":["56c5d390-f15c-45c5-a2f8-11076bada6be","c143defc-27b6-4682-bfd4-b718c6774d26"]}],"rounds":[{"roundNumber":1,"pairings":[{"id":"3f4788fa-6d01-4ec6-8cd3-0b359ac80e96","teamIds":["68eda989-77e5-488c-b467-0ec52ac3b3da","8b01dc11-4544-4319-a71e-6ba2e65f3960"],"games":[{"id":"95b17c8b-6357-4e0a-b062-0dfdd24c5bed","gameId":"02e4e605-0c1e-4db9-a47a-c998e8664c56","winningTeamId":"8b01dc11-4544-4319-a71e-6ba2e65f3960"}],"drops":[]},{"id":"6150ef4d-0232-450c-b458-47dc9a0a9d99","teamIds":["5f193445-3f27-44f5-90f7-c1a728580976","18d316fe-cf8a-4bf2-814d-5ea8872522f2"],"games":[{"id":"02fcba8a-f75f-4a9e-85d2-c6c2f9c8f4ba","gameId":"a8f2fe9b-c0df-457a-b3fd-dfd997515df1","winningTeamId":"18d316fe-cf8a-4bf2-814d-5ea8872522f2"}],"drops":[]}]}]}],"kfactorBase":32}]}

0 comments on commit 04cdf70

Please sign in to comment.