From 98968d689e2a52de952cbacae4a69eeaf8159243 Mon Sep 17 00:00:00 2001 From: Patrick Hoefer Date: Fri, 18 Dec 2020 19:45:06 +0100 Subject: [PATCH] feat(export): add character script exporter --- characters.txt | 56 +++++++++++++++ .../app/exporter/ScriptExporter.kt | 5 ++ .../ck3workbench/app/view/DialogView.kt | 11 +-- .../exporter/CharacterScriptExporter.kt | 70 +++++++++++++++++++ src/main/kotlin/main.kt | 8 ++- .../ck3workbench/app/SessionManagerTest.kt | 2 +- .../exporter/CharacterScriptExporterTest.kt | 28 ++++++++ .../export/expectedCharacterExport.txt | 56 +++++++++++++++ 8 files changed, 230 insertions(+), 6 deletions(-) create mode 100644 characters.txt create mode 100644 src/main/kotlin/com/github/xetra11/ck3workbench/app/exporter/ScriptExporter.kt create mode 100644 src/main/kotlin/com/github/xetra11/ck3workbench/module/character/exporter/CharacterScriptExporter.kt create mode 100644 src/test/kotlin/com/github/xetra11/ck3workbench/module/character/exporter/CharacterScriptExporterTest.kt create mode 100644 src/test/resources/fixtures/character/export/expectedCharacterExport.txt diff --git a/characters.txt b/characters.txt new file mode 100644 index 0000000..8bb9cc7 --- /dev/null +++ b/characters.txt @@ -0,0 +1,56 @@ +0 = { + name = "Thorak" + dna = thorak_dna + dynasty = my_dynastie + religion = "asatru" + culture = cheruscii + + diplomacy = 5 + martial = 5 + stewardship = 5 + intrigue = 5 + learning = 5 + + trait = ambitious + trait = hunter_1 + trait = wrathful + trait = callous + trait = viking + trait = education_martial_3 + + 763.1.1 = { + birth = yes + } + 800.1.1 = { + death = yes + } +} + +1 = { + name = "Thorak" + dna = thorak_dna + dynasty = my_dynastie + religion = "asatru" + culture = cheruscii + + diplomacy = 5 + martial = 5 + stewardship = 5 + intrigue = 5 + learning = 5 + + trait = ambitious + trait = hunter_1 + trait = wrathful + trait = callous + trait = viking + trait = education_martial_3 + + 763.1.1 = { + birth = yes + } + 800.1.1 = { + death = yes + } +} + diff --git a/src/main/kotlin/com/github/xetra11/ck3workbench/app/exporter/ScriptExporter.kt b/src/main/kotlin/com/github/xetra11/ck3workbench/app/exporter/ScriptExporter.kt new file mode 100644 index 0000000..11e7071 --- /dev/null +++ b/src/main/kotlin/com/github/xetra11/ck3workbench/app/exporter/ScriptExporter.kt @@ -0,0 +1,5 @@ +package com.github.xetra11.ck3workbench.app.exporter + +interface ScriptExporter { + fun export() +} diff --git a/src/main/kotlin/com/github/xetra11/ck3workbench/app/view/DialogView.kt b/src/main/kotlin/com/github/xetra11/ck3workbench/app/view/DialogView.kt index 91effd4..0bdb3b7 100644 --- a/src/main/kotlin/com/github/xetra11/ck3workbench/app/view/DialogView.kt +++ b/src/main/kotlin/com/github/xetra11/ck3workbench/app/view/DialogView.kt @@ -1,13 +1,18 @@ package com.github.xetra11.ck3workbench.app.view +import androidx.compose.desktop.AppManager +import androidx.compose.desktop.AppWindow import androidx.compose.foundation.border import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material.Button +import androidx.compose.material.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.platform.WindowManager import androidx.compose.ui.unit.dp import androidx.compose.ui.window.Dialog import com.github.xetra11.ck3workbench.app.DialogManager @@ -26,11 +31,9 @@ private fun CreateCharacterDialog() { Dialog( onDismissRequest = { DialogManager.closeDialog() } ) { - Column( - Modifier.fillMaxSize().border(2.dp, Color.Blue), + Column(Modifier.fillMaxSize().border(2.dp, Color.Blue ), horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.SpaceEvenly - ) { + verticalArrangement = Arrangement.SpaceEvenly) { CharacterCreateView() /* Button(onClick = { AppManager.focusedWindow?.close() }) { diff --git a/src/main/kotlin/com/github/xetra11/ck3workbench/module/character/exporter/CharacterScriptExporter.kt b/src/main/kotlin/com/github/xetra11/ck3workbench/module/character/exporter/CharacterScriptExporter.kt new file mode 100644 index 0000000..a9bd968 --- /dev/null +++ b/src/main/kotlin/com/github/xetra11/ck3workbench/module/character/exporter/CharacterScriptExporter.kt @@ -0,0 +1,70 @@ +package com.github.xetra11.ck3workbench.module.character.exporter + +import com.github.xetra11.ck3workbench.app.StateManager +import com.github.xetra11.ck3workbench.app.exporter.ScriptExporter +import com.github.xetra11.ck3workbench.module.character.CK3Character.Skill +import java.io.File + +class CharacterScriptExporter( + private val exportFileName: String = "characters" + +) : ScriptExporter { + + override fun export() { + val exportFile = File("$exportFileName.txt") + if (exportFile.exists()) { + exportFile.delete() + } + exportFile.createNewFile() + exportFile.writeText(characterData()) + } + + private fun characterData(): String { + return StateManager.characters.mapIndexed { index, character -> + """ +$index = { + ${character.name.toScriptAttribute("name", true)} + ${character.dna.toScriptAttribute("dna")} + ${character.dynasty.toScriptAttribute("dynasty")} + ${character.religion.toScriptAttribute("religion", true)} + ${character.culture.toScriptAttribute("culture")} + + ${character.skills.toSkillAttributes()} + ${character.traits.toTraitAttributes()} + ${character.birth.toDateAttribute("birth")} + ${character.death.toDateAttribute("death")} +} +""".trimIndent() + }.joinToString(separator = "") { it + "\n\n" } + } + + private fun String.toDateAttribute(id: String): String { + val date = +""" + $this = { + $id = yes + } +""".trim() + return date + } + + private fun String.toScriptAttribute(id: String, withQuotation: Boolean = false): String { + return if (withQuotation) """$id = "$this"""" else "$id = $this" + } + + private fun Map.toSkillAttributes(): String { + return this.map { (skill, value) -> + """${skill.name.toLowerCase()} = $value""" + }.joinToString(separator = "") { it + NEWLINE } + } + + private fun List.toTraitAttributes(): String { + return this.map { trait -> + """trait = $trait""" + }.joinToString(separator = "") { it + NEWLINE } + } + + companion object { + private const val NEWLINE = "\n " + } +} diff --git a/src/main/kotlin/main.kt b/src/main/kotlin/main.kt index a985787..a45ac4c 100644 --- a/src/main/kotlin/main.kt +++ b/src/main/kotlin/main.kt @@ -56,7 +56,13 @@ fun main() = invokeLater { val characterScriptImporter = CharacterScriptImporter() characterScriptImporter.importCharactersScript(file) } - ) + ), + MenuItem( + "Export Character Scripts", + onClick = { + + } + ), ), Menu("Dynasties") ) diff --git a/src/test/kotlin/com/github/xetra11/ck3workbench/app/SessionManagerTest.kt b/src/test/kotlin/com/github/xetra11/ck3workbench/app/SessionManagerTest.kt index e81bb88..e25bd05 100644 --- a/src/test/kotlin/com/github/xetra11/ck3workbench/app/SessionManagerTest.kt +++ b/src/test/kotlin/com/github/xetra11/ck3workbench/app/SessionManagerTest.kt @@ -1,5 +1,6 @@ package com.github.xetra11.ck3workbench.app +import androidx.compose.desktop.AppManager import com.github.xetra11.ck3workbench.module.character.CharacterTemplate import io.kotest.core.spec.style.ShouldSpec import io.kotest.matchers.collections.shouldHaveSize @@ -67,7 +68,6 @@ class SessionManagerTest : ShouldSpec({ val projectFile = File("test.wbp") sessionManager.initialize() - StateManager.characters.addAll( listOf( CharacterTemplate.DEFAULT_CHARACTER, diff --git a/src/test/kotlin/com/github/xetra11/ck3workbench/module/character/exporter/CharacterScriptExporterTest.kt b/src/test/kotlin/com/github/xetra11/ck3workbench/module/character/exporter/CharacterScriptExporterTest.kt new file mode 100644 index 0000000..8e53a3b --- /dev/null +++ b/src/test/kotlin/com/github/xetra11/ck3workbench/module/character/exporter/CharacterScriptExporterTest.kt @@ -0,0 +1,28 @@ +package com.github.xetra11.ck3workbench.module.character.exporter + +import com.github.xetra11.ck3workbench.app.StateManager +import com.github.xetra11.ck3workbench.module.character.CharacterTemplate +import io.kotest.core.spec.style.ShouldSpec +import io.kotest.matchers.shouldBe +import io.kotest.matchers.string.shouldContain +import java.io.File + +class CharacterScriptExporterTest : ShouldSpec({ + val exportFile = File("characters.txt") + val expectedExportFile = File("src/test/resources/fixtures/character/export/expectedCharacterExport.txt") + + val characterScriptExporter = CharacterScriptExporter() + + should("export characters from state manager to script txt file") { + StateManager.characters.addAll( + listOf( + CharacterTemplate.DEFAULT_CHARACTER, + CharacterTemplate.DEFAULT_CHARACTER + ) + ) + characterScriptExporter.export() + + exportFile.readText() shouldBe expectedExportFile.readText() + } +}) + diff --git a/src/test/resources/fixtures/character/export/expectedCharacterExport.txt b/src/test/resources/fixtures/character/export/expectedCharacterExport.txt new file mode 100644 index 0000000..8bb9cc7 --- /dev/null +++ b/src/test/resources/fixtures/character/export/expectedCharacterExport.txt @@ -0,0 +1,56 @@ +0 = { + name = "Thorak" + dna = thorak_dna + dynasty = my_dynastie + religion = "asatru" + culture = cheruscii + + diplomacy = 5 + martial = 5 + stewardship = 5 + intrigue = 5 + learning = 5 + + trait = ambitious + trait = hunter_1 + trait = wrathful + trait = callous + trait = viking + trait = education_martial_3 + + 763.1.1 = { + birth = yes + } + 800.1.1 = { + death = yes + } +} + +1 = { + name = "Thorak" + dna = thorak_dna + dynasty = my_dynastie + religion = "asatru" + culture = cheruscii + + diplomacy = 5 + martial = 5 + stewardship = 5 + intrigue = 5 + learning = 5 + + trait = ambitious + trait = hunter_1 + trait = wrathful + trait = callous + trait = viking + trait = education_martial_3 + + 763.1.1 = { + birth = yes + } + 800.1.1 = { + death = yes + } +} +