diff --git a/build.gradle b/build.gradle index 9fb2889..5b3f452 100644 --- a/build.gradle +++ b/build.gradle @@ -70,7 +70,7 @@ idea { } task wrapper(type: Wrapper) { - gradleVersion = '4.7' + gradleVersion = '4.8.1' } jacocoTestReport { diff --git a/src/main/kotlin/com/github/wakingrufus/eloleague/data/DataHandler.kt b/src/main/kotlin/com/github/wakingrufus/eloleague/dao/DataHandler.kt similarity index 51% rename from src/main/kotlin/com/github/wakingrufus/eloleague/data/DataHandler.kt rename to src/main/kotlin/com/github/wakingrufus/eloleague/dao/DataHandler.kt index 72cf288..9a84cf1 100644 --- a/src/main/kotlin/com/github/wakingrufus/eloleague/data/DataHandler.kt +++ b/src/main/kotlin/com/github/wakingrufus/eloleague/dao/DataHandler.kt @@ -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 { diff --git a/src/main/kotlin/com/github/wakingrufus/eloleague/data/FileDataHandler.kt b/src/main/kotlin/com/github/wakingrufus/eloleague/dao/JacksonFileDataHandler.kt similarity index 86% rename from src/main/kotlin/com/github/wakingrufus/eloleague/data/FileDataHandler.kt rename to src/main/kotlin/com/github/wakingrufus/eloleague/dao/JacksonFileDataHandler.kt index 0d38177..80d9397 100644 --- a/src/main/kotlin/com/github/wakingrufus/eloleague/data/FileDataHandler.kt +++ b/src/main/kotlin/com/github/wakingrufus/eloleague/dao/JacksonFileDataHandler.kt @@ -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() diff --git a/src/main/kotlin/com/github/wakingrufus/eloleague/dao/JacksonUrlDataHandler.kt b/src/main/kotlin/com/github/wakingrufus/eloleague/dao/JacksonUrlDataHandler.kt new file mode 100644 index 0000000..7ebd883 --- /dev/null +++ b/src/main/kotlin/com/github/wakingrufus/eloleague/dao/JacksonUrlDataHandler.kt @@ -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") + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/wakingrufus/eloleague/league/LeagueListController.kt b/src/main/kotlin/com/github/wakingrufus/eloleague/league/LeagueListController.kt index 68edbee..44ee63e 100644 --- a/src/main/kotlin/com/github/wakingrufus/eloleague/league/LeagueListController.kt +++ b/src/main/kotlin/com/github/wakingrufus/eloleague/league/LeagueListController.kt @@ -1,11 +1,7 @@ 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() { @@ -13,24 +9,6 @@ class LeagueListController : Controller() { val leagues = FXCollections.observableArrayList( 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()) diff --git a/src/main/kotlin/com/github/wakingrufus/eloleague/league/LeagueListView.kt b/src/main/kotlin/com/github/wakingrufus/eloleague/league/LeagueListView.kt index 7a0bad3..6a6aefe 100644 --- a/src/main/kotlin/com/github/wakingrufus/eloleague/league/LeagueListView.kt +++ b/src/main/kotlin/com/github/wakingrufus/eloleague/league/LeagueListView.kt @@ -31,9 +31,6 @@ class LeagueListView : View("League List") { controller.leagues.remove(model.item) } } - button(text = "Save All").setOnAction { - controller.save() - } } } } diff --git a/src/main/kotlin/com/github/wakingrufus/eloleague/ui/MainView.kt b/src/main/kotlin/com/github/wakingrufus/eloleague/ui/MainView.kt index fbea875..95374d5 100644 --- a/src/main/kotlin/com/github/wakingrufus/eloleague/ui/MainView.kt +++ b/src/main/kotlin/com/github/wakingrufus/eloleague/ui/MainView.kt @@ -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().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 } diff --git a/src/main/kotlin/com/github/wakingrufus/eloleague/ui/UrlDialog.kt b/src/main/kotlin/com/github/wakingrufus/eloleague/ui/UrlDialog.kt new file mode 100644 index 0000000..e64ef2e --- /dev/null +++ b/src/main/kotlin/com/github/wakingrufus/eloleague/ui/UrlDialog.kt @@ -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() { + 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() + } + } + } + } + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/github/wakingrufus/eloleague/IntegrationTest.kt b/src/test/kotlin/com/github/wakingrufus/eloleague/IntegrationTest.kt index dd6bc20..201f07c 100644 --- a/src/test/kotlin/com/github/wakingrufus/eloleague/IntegrationTest.kt +++ b/src/test/kotlin/com/github/wakingrufus/eloleague/IntegrationTest.kt @@ -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 diff --git a/src/test/kotlin/com/github/wakingrufus/eloleague/dao/JacksonUrlDataHandlerTest.kt b/src/test/kotlin/com/github/wakingrufus/eloleague/dao/JacksonUrlDataHandlerTest.kt new file mode 100644 index 0000000..a11f7a5 --- /dev/null +++ b/src/test/kotlin/com/github/wakingrufus/eloleague/dao/JacksonUrlDataHandlerTest.kt @@ -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) + } +} \ No newline at end of file diff --git a/src/test/resources/elo-test.json b/src/test/resources/elo-test.json new file mode 100644 index 0000000..3d3a22c --- /dev/null +++ b/src/test/resources/elo-test.json @@ -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}]} \ No newline at end of file