Skip to content

Commit

Permalink
feat(export): add character script exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Hoefer committed Dec 18, 2020
1 parent 5f0342c commit 98968d6
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 6 deletions.
56 changes: 56 additions & 0 deletions characters.txt
Original file line number Diff line number Diff line change
@@ -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
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.xetra11.ck3workbench.app.exporter

interface ScriptExporter {
fun export()
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() }) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Skill, Short>.toSkillAttributes(): String {
return this.map { (skill, value) ->
"""${skill.name.toLowerCase()} = $value"""
}.joinToString(separator = "") { it + NEWLINE }
}

private fun List<String>.toTraitAttributes(): String {
return this.map { trait ->
"""trait = $trait"""
}.joinToString(separator = "") { it + NEWLINE }
}

companion object {
private const val NEWLINE = "\n "
}
}
8 changes: 7 additions & 1 deletion src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ fun main() = invokeLater {
val characterScriptImporter = CharacterScriptImporter()
characterScriptImporter.importCharactersScript(file)
}
)
),
MenuItem(
"Export Character Scripts",
onClick = {

}
),
),
Menu("Dynasties")
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -67,7 +68,6 @@ class SessionManagerTest : ShouldSpec({
val projectFile = File("test.wbp")

sessionManager.initialize()

StateManager.characters.addAll(
listOf(
CharacterTemplate.DEFAULT_CHARACTER,
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
})

Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 98968d6

Please sign in to comment.