-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(export): add character script exporter
- Loading branch information
Patrick Hoefer
committed
Dec 18, 2020
1 parent
5f0342c
commit 98968d6
Showing
8 changed files
with
230 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/github/xetra11/ck3workbench/app/exporter/ScriptExporter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...tlin/com/github/xetra11/ck3workbench/module/character/exporter/CharacterScriptExporter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 " | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
.../com/github/xetra11/ck3workbench/module/character/exporter/CharacterScriptExporterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
}) | ||
|
56 changes: 56 additions & 0 deletions
56
src/test/resources/fixtures/character/export/expectedCharacterExport.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|