diff --git a/docs/mkdocs/docs/non-playable-character.md b/docs/mkdocs/docs/non-playable-character.md index a5dbaa6..1cf468a 100644 --- a/docs/mkdocs/docs/non-playable-character.md +++ b/docs/mkdocs/docs/non-playable-character.md @@ -47,13 +47,13 @@ goblin.conversation = Conversation( Paragraph("This is a the first line."), Paragraph("This is a question.).also { it.responses = listOf( - Response("This is the first response., 1), - Response("This is the second response.", 2), - Response("This is the third response.", 3) + Response("This is the first response., Jump(1)), + Response("This is the second response.", Jump(2)), + Response("This is the third response.", Jump(3)) ) }, - Paragraph("You picked first response, return to start of conversation.", -2), - Paragraph("You picked second response, return to start of conversation., -2), + Paragraph("You picked first response, return to start of conversation.", GoTo(1)), + Paragraph("You picked second response, return to start of conversation., GoTo(1)), Paragraph("This is the third response.") { it.player.kill() } diff --git a/ktaf-example/src/main/kotlin/com/github/benpollarduk/ktaf/example/hub/npc/Parrot.kt b/ktaf-example/src/main/kotlin/com/github/benpollarduk/ktaf/example/hub/npc/Parrot.kt index 14aee2b..fbf9c57 100644 --- a/ktaf-example/src/main/kotlin/com/github/benpollarduk/ktaf/example/hub/npc/Parrot.kt +++ b/ktaf-example/src/main/kotlin/com/github/benpollarduk/ktaf/example/hub/npc/Parrot.kt @@ -4,6 +4,8 @@ import com.github.benpollarduk.ktaf.assets.characters.NonPlayableCharacter import com.github.benpollarduk.ktaf.conversations.Conversation import com.github.benpollarduk.ktaf.conversations.Paragraph import com.github.benpollarduk.ktaf.conversations.Response +import com.github.benpollarduk.ktaf.conversations.instructions.GoTo +import com.github.benpollarduk.ktaf.conversations.instructions.Jump import com.github.benpollarduk.ktaf.utilities.templates.AssetTemplate internal class Parrot : AssetTemplate { @@ -17,14 +19,14 @@ internal class Parrot : AssetTemplate { Paragraph("Squarrrkkk"), Paragraph("Will you feed me?").also { it.responses = listOf( - Response("I don't have any food to feed you with!", 1), - Response("Sure here's some food!", 2), - Response("I'll try bring you some.", 3) + Response("I don't have any food to feed you with!", Jump(1)), + Response("Sure here's some food!", Jump(2)), + Response("I'll try bring you some.", Jump(3)) ) }, - Paragraph("Bring some next time then!", -1), - Paragraph("You don't have any food! Errkggg!", -2), - Paragraph("Thanks a lot! Erckgah!", -3) + Paragraph("Bring some next time then!", GoTo(1)), + Paragraph("You don't have any food! Errkggg!", GoTo(1)), + Paragraph("Thanks a lot! Erckgah!", GoTo(1)) ) ) } diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/Conversation.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/Conversation.kt index 68426bf..d06dc8f 100644 --- a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/Conversation.kt +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/Conversation.kt @@ -2,6 +2,7 @@ package com.github.benpollarduk.ktaf.conversations import com.github.benpollarduk.ktaf.assets.interaction.Reaction import com.github.benpollarduk.ktaf.assets.interaction.ReactionResult +import com.github.benpollarduk.ktaf.conversations.instructions.EndOfParagraphInstruction import com.github.benpollarduk.ktaf.extensions.ensureFinishedSentence import com.github.benpollarduk.ktaf.extensions.toSpeech import com.github.benpollarduk.ktaf.logic.Game @@ -13,8 +14,7 @@ import com.github.benpollarduk.ktaf.logic.Game public class Conversation( private val paragraphs: List = emptyList() ) { - private var paragraphIndex: Int = 0 - private var selectedResponseDelta: Int? = null + private var selectedResponseInstruction: EndOfParagraphInstruction? = null private val mutableLog: MutableList = mutableListOf() /** @@ -30,19 +30,26 @@ public class Conversation( get() = mutableLog.toList() /** - * Shift this [Conversation] to a new [Paragraph]. The [delta] specifies the next [Paragraph], which is retrieved - * from the [paragraphs] array. If the [delta] shifts the conversation out of bounds then null is returned. + * Shift this [Conversation] to a new [Paragraph]. The [index] specifies the next [Paragraph], which is retrieved + * from the [paragraphs] array. If the [index] shifts the conversation out of bounds then the current paragraph is + * returned. */ - private fun shift(delta: Int): Paragraph? { - paragraphIndex += delta - - return if (paragraphIndex >= 0 && paragraphIndex < paragraphs.count()) { - paragraphs[paragraphIndex] + private fun shift(index: Int): Paragraph? { + return if (index >= 0 && index < paragraphs.count()) { + paragraphs[index] } else { currentParagraph } } + /** + * Update the [currentParagraph] based on an [instruction]. + */ + private fun updateCurrentParagraph(instruction: EndOfParagraphInstruction) { + val current = currentParagraph ?: return + currentParagraph = shift(instruction.getIndexOfNext(current, paragraphs)) + } + /** * Trigger the next line in this [Conversation] to obtain a [Reaction]. */ @@ -58,13 +65,13 @@ public class Conversation( currentParagraph = paragraphs.firstOrNull() } current.canRespond -> { - selectedResponseDelta?.let { delta -> - currentParagraph = shift(delta) - selectedResponseDelta = null + selectedResponseInstruction?.let { + updateCurrentParagraph(it) + selectedResponseInstruction = null } ?: return Reaction(ReactionResult.INTERNAL, "Awaiting response.") } else -> { - currentParagraph = shift(current.delta) + updateCurrentParagraph(current.instruction) } } @@ -86,7 +93,7 @@ public class Conversation( return@respond Reaction(ReactionResult.ERROR, "Invalid response.") } mutableLog.add(LogItem(Participant.PLAYER, response.line.ensureFinishedSentence())) - selectedResponseDelta = response.delta + selectedResponseInstruction = response.instruction return@respond next(game) } ?: return Reaction(ReactionResult.ERROR, "No paragraph.") } diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/Paragraph.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/Paragraph.kt index 9e42960..a3378e0 100644 --- a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/Paragraph.kt +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/Paragraph.kt @@ -1,13 +1,17 @@ package com.github.benpollarduk.ktaf.conversations +import com.github.benpollarduk.ktaf.conversations.instructions.EndOfParagraphInstruction +import com.github.benpollarduk.ktaf.conversations.instructions.Next + /** * A paragraph within a [Conversation]. Must contain a [line], but can also have an optional [action] that is invoked - * as a response to this paragraph being triggered. The [delta] is a relative pointer to the next element within the - * [Conversation]. + * as a response to this paragraph being triggered. The [instruction] is applied to direct the conversation after this + * paragraph. */ public class Paragraph( public val line: String, - public val delta: Int = 1, + public val instruction: EndOfParagraphInstruction = Next(), + public val name: String = "", public val action: ConversationAction = { } ) { /** diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/Response.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/Response.kt index 536e3ef..1c1a6a3 100644 --- a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/Response.kt +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/Response.kt @@ -1,7 +1,13 @@ package com.github.benpollarduk.ktaf.conversations +import com.github.benpollarduk.ktaf.conversations.instructions.EndOfParagraphInstruction +import com.github.benpollarduk.ktaf.conversations.instructions.Next + /** - * A response which forms part of a [Conversation]. The [line] forms the body of the response and the [delta] is a - * relative pointer to the next element within the [Conversation]. + * A response which forms part of a [Conversation]. The [line] forms the body of the response and the [instruction] is + * applied to direct the conversation after this paragraph. */ -public data class Response(public val line: String, public val delta: Int = 1) +public data class Response( + public val line: String, + public val instruction: EndOfParagraphInstruction = Next() +) diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/EndOfParagraphInstruction.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/EndOfParagraphInstruction.kt new file mode 100644 index 0000000..4b14227 --- /dev/null +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/EndOfParagraphInstruction.kt @@ -0,0 +1,13 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph + +/** + * Provides a contract for providing instructions for ends of paragraphs. + */ +public fun interface EndOfParagraphInstruction { + /** + * Get the index of the next paragraph from a [current] paragraph and an array of [paragraphs]. + */ + public fun getIndexOfNext(current: Paragraph, paragraphs: List): Int +} diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/First.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/First.kt new file mode 100644 index 0000000..e95192b --- /dev/null +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/First.kt @@ -0,0 +1,12 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph + +/** + * An end of paragraph instruction that shifts paragraphs to the start. + */ +public class First : EndOfParagraphInstruction { + override fun getIndexOfNext(current: Paragraph, paragraphs: List): Int { + return 0 + } +} diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/GoTo.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/GoTo.kt new file mode 100644 index 0000000..c50c57b --- /dev/null +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/GoTo.kt @@ -0,0 +1,18 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph + +/** + * An end of paragraph instruction that shifts paragraphs based on an absolute [index]. + */ +public class GoTo(public val index: Int) : EndOfParagraphInstruction { + override fun getIndexOfNext(current: Paragraph, paragraphs: List): Int { + return if (index < 0) { + 0 + } else if (index >= paragraphs.size) { + paragraphs.size - 1 + } else { + index + } + } +} diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Jump.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Jump.kt new file mode 100644 index 0000000..c6b7f2f --- /dev/null +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Jump.kt @@ -0,0 +1,20 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph + +/** + * An end of paragraph instruction that shifts paragraphs based on a delta. + */ +public class Jump(public val delta: Int) : EndOfParagraphInstruction { + override fun getIndexOfNext(current: Paragraph, paragraphs: List): Int { + val index = paragraphs.indexOf(current) + val offset = index + delta + return if (offset < 0) { + 0 + } else if (offset >= paragraphs.size) { + paragraphs.size - 1 + } else { + offset + } + } +} diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Last.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Last.kt new file mode 100644 index 0000000..854a4ae --- /dev/null +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Last.kt @@ -0,0 +1,12 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph + +/** + * An end of paragraph instruction that shifts paragraphs to the end. + */ +public class Last : EndOfParagraphInstruction { + override fun getIndexOfNext(current: Paragraph, paragraphs: List): Int { + return paragraphs.size - 1 + } +} diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Next.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Next.kt new file mode 100644 index 0000000..ff4d1d6 --- /dev/null +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Next.kt @@ -0,0 +1,19 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph + +/** + * An end of paragraph instruction that shifts paragraphs to the next paragraph. + */ +public class Next : EndOfParagraphInstruction { + override fun getIndexOfNext(current: Paragraph, paragraphs: List): Int { + val index = paragraphs.indexOf(current) + return if (index == -1) { + 0 + } else if (index < paragraphs.size - 1) { + index + 1 + } else { + paragraphs.size - 1 + } + } +} diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Previous.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Previous.kt new file mode 100644 index 0000000..9031f5c --- /dev/null +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Previous.kt @@ -0,0 +1,17 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph + +/** + * An end of paragraph instruction that shifts paragraphs to the previous paragraph. + */ +public class Previous : EndOfParagraphInstruction { + override fun getIndexOfNext(current: Paragraph, paragraphs: List): Int { + val index = paragraphs.indexOf(current) + return if (index > 0) { + index - 1 + } else { + 0 + } + } +} diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Repeat.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Repeat.kt new file mode 100644 index 0000000..f97ea97 --- /dev/null +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/Repeat.kt @@ -0,0 +1,17 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph + +/** + * An end of paragraph instruction that repeats. + */ +public class Repeat : EndOfParagraphInstruction { + override fun getIndexOfNext(current: Paragraph, paragraphs: List): Int { + val index = paragraphs.indexOf(current) + return if (index < 0) { + 0 + } else { + index + } + } +} diff --git a/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/ToName.kt b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/ToName.kt new file mode 100644 index 0000000..5a552ee --- /dev/null +++ b/ktaf/src/main/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/ToName.kt @@ -0,0 +1,20 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph + +/** + * An end of paragraph instruction that shifts paragraphs based on a name. + */ +public class ToName(public val name: String) : EndOfParagraphInstruction { + override fun getIndexOfNext(current: Paragraph, paragraphs: List): Int { + val target = paragraphs.firstOrNull { + it.name.equals(name, true) + } + + return if (target != null) { + paragraphs.indexOf(target) + } else { + 0 + } + } +} diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/TestGame.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/TestGame.kt index fa6e183..75e9361 100644 --- a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/TestGame.kt +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/TestGame.kt @@ -9,6 +9,7 @@ import com.github.benpollarduk.ktaf.assets.locations.Room import com.github.benpollarduk.ktaf.conversations.Conversation import com.github.benpollarduk.ktaf.conversations.Paragraph import com.github.benpollarduk.ktaf.conversations.Response +import com.github.benpollarduk.ktaf.conversations.instructions.Previous import com.github.benpollarduk.ktaf.io.IOConfiguration import com.github.benpollarduk.ktaf.logic.Game import com.github.benpollarduk.ktaf.logic.GameInformation @@ -46,7 +47,7 @@ internal object TestGame : GameTemplate() { Paragraph("Here is a question?").also { paragraph -> paragraph.responses = listOf( Response("Continue"), - Response("Repeat", -1) + Response("Repeat", Previous()) ) } ) diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/command/conversation/RespondTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/command/conversation/RespondTest.kt index b9fa9c1..b4f567a 100644 --- a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/command/conversation/RespondTest.kt +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/command/conversation/RespondTest.kt @@ -4,6 +4,7 @@ import com.github.benpollarduk.ktaf.assets.characters.NonPlayableCharacter import com.github.benpollarduk.ktaf.assets.interaction.ReactionResult import com.github.benpollarduk.ktaf.commands.conversation.Respond import com.github.benpollarduk.ktaf.conversations.Response +import com.github.benpollarduk.ktaf.conversations.instructions.Next import com.github.benpollarduk.ktaf.logic.GameTestHelper import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test @@ -12,7 +13,7 @@ class RespondTest { @Test fun `given no converser when invoke then return error`() { // Given - val command = Respond(Response("", 1)) + val command = Respond(Response("", Next())) // When val result = command.invoke(GameTestHelper.getBlankGame()) @@ -26,7 +27,7 @@ class RespondTest { // Given val game = GameTestHelper.getBlankGame() val npc = NonPlayableCharacter("", "") - val command = Respond(Response("", 1)) + val command = Respond(Response("", Next())) game.startConversation(npc) // When @@ -40,7 +41,7 @@ class RespondTest { fun `given converser when invoke with valid response then return internal`() { // Given val game = GameTestHelper.getBlankGame() - val response = Response("", 1) + val response = Response("", Next()) val npc = NonPlayableCharacter("", "") val command = Respond(response) game.startConversation(npc) diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/FirstTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/FirstTest.kt new file mode 100644 index 0000000..c537425 --- /dev/null +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/FirstTest.kt @@ -0,0 +1,40 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class FirstTest { + @Test + fun `given two paragraphs when get index of next then return 0`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = First() + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(0, result) + } + + @Test + fun `given three paragraphs when get index of next then return 0`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph(""), + Paragraph("") + ) + val instruction = First() + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(0, result) + } +} diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/GoToTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/GoToTest.kt new file mode 100644 index 0000000..dcedff9 --- /dev/null +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/GoToTest.kt @@ -0,0 +1,71 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class GoToTest { + @Test + fun `given 0 two paragraphs when get index of next then return 0`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = GoTo(0) + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(0, result) + } + + @Test + fun `given minus 1 when get index of next then return 0`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = GoTo(-1) + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(0, result) + } + + @Test + fun `given 1 two paragraphs when get index of next then return 1`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = GoTo(1) + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(1, result) + } + + @Test + fun `given 2 two paragraphs when get index of next then return 1`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = GoTo(2) + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(1, result) + } +} diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/JumpTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/JumpTest.kt new file mode 100644 index 0000000..2f5ebe8 --- /dev/null +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/JumpTest.kt @@ -0,0 +1,71 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class JumpTest { + @Test + fun `given 1 two paragraphs on first when get index of next then return 1`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = Jump(1) + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(1, result) + } + + @Test + fun `given minus 1 two paragraphs on second when get index of next then return 0`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = Jump(-1) + + // When + val result = instruction.getIndexOfNext(paragraphs[1], paragraphs) + + // Then + Assertions.assertEquals(0, result) + } + + @Test + fun `given minus 1 two paragraphs on first when get index of next then return 0`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = Jump(-1) + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(0, result) + } + + @Test + fun `given 8 two paragraphs on first when get index of next then return 1`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = Jump(8) + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(1, result) + } +} diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/LastTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/LastTest.kt new file mode 100644 index 0000000..8d0b14c --- /dev/null +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/LastTest.kt @@ -0,0 +1,40 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class LastTest { + @Test + fun `given two paragraphs when get index of next then return 1`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = Last() + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(1, result) + } + + @Test + fun `given three paragraphs when get index of next then return 2`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph(""), + Paragraph("") + ) + val instruction = Last() + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(2, result) + } +} diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/NextTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/NextTest.kt new file mode 100644 index 0000000..4d22706 --- /dev/null +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/NextTest.kt @@ -0,0 +1,57 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class NextTest { + @Test + fun `given currently on first when get index of next then return 1`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = Next() + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(1, result) + } + + @Test + fun `given currently on second when get index of next then return 2`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph(""), + Paragraph("") + ) + val instruction = Next() + + // When + val result = instruction.getIndexOfNext(paragraphs[1], paragraphs) + + // Then + Assertions.assertEquals(2, result) + } + + @Test + fun `given currently on third and only three when get index of next then return 2`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph(""), + Paragraph("") + ) + val instruction = Next() + + // When + val result = instruction.getIndexOfNext(paragraphs[2], paragraphs) + + // Then + Assertions.assertEquals(2, result) + } +} diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/PreviousTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/PreviousTest.kt new file mode 100644 index 0000000..5fad971 --- /dev/null +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/PreviousTest.kt @@ -0,0 +1,57 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class PreviousTest { + @Test + fun `given currently on first when get index of next then return 0`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = Previous() + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(0, result) + } + + @Test + fun `given currently on second when get index of next then return 0`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph(""), + Paragraph("") + ) + val instruction = Previous() + + // When + val result = instruction.getIndexOfNext(paragraphs[1], paragraphs) + + // Then + Assertions.assertEquals(0, result) + } + + @Test + fun `given currently on third when get index of next then return 1`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph(""), + Paragraph("") + ) + val instruction = Previous() + + // When + val result = instruction.getIndexOfNext(paragraphs[2], paragraphs) + + // Then + Assertions.assertEquals(1, result) + } +} diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/RepeatTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/RepeatTest.kt new file mode 100644 index 0000000..79c9c3a --- /dev/null +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/RepeatTest.kt @@ -0,0 +1,40 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class RepeatTest { + @Test + fun `given first when get index of next then return 0`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("") + ) + val instruction = Repeat() + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(0, result) + } + + @Test + fun `given second when get index of next then return 1`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph(""), + Paragraph("") + ) + val instruction = Repeat() + + // When + val result = instruction.getIndexOfNext(paragraphs[1], paragraphs) + + // Then + Assertions.assertEquals(1, result) + } +} diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/ToNameTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/ToNameTest.kt new file mode 100644 index 0000000..fad17e4 --- /dev/null +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/conversations/instructions/ToNameTest.kt @@ -0,0 +1,57 @@ +package com.github.benpollarduk.ktaf.conversations.instructions + +import com.github.benpollarduk.ktaf.conversations.Paragraph +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test + +class ToNameTest { + @Test + fun `given name of first when get index of next then return 0`() { + // Given + val paragraphs = listOf( + Paragraph("", name = "Test"), + Paragraph("") + ) + val instruction = ToName("Test") + + // When + val result = instruction.getIndexOfNext(paragraphs[0], paragraphs) + + // Then + Assertions.assertEquals(0, result) + } + + @Test + fun `given name of second when get index of next then return 1`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("", name = "Test"), + Paragraph("") + ) + val instruction = ToName("Test") + + // When + val result = instruction.getIndexOfNext(paragraphs[1], paragraphs) + + // Then + Assertions.assertEquals(1, result) + } + + @Test + fun `given invalid name when get index of next then return 0`() { + // Given + val paragraphs = listOf( + Paragraph(""), + Paragraph("", name = "Test"), + Paragraph("") + ) + val instruction = ToName("1234") + + // When + val result = instruction.getIndexOfNext(paragraphs[1], paragraphs) + + // Then + Assertions.assertEquals(0, result) + } +} diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/rendering/frames/ansi/AnsiConversationFrameBuilderTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/rendering/frames/ansi/AnsiConversationFrameBuilderTest.kt index c989ebe..191e45d 100644 --- a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/rendering/frames/ansi/AnsiConversationFrameBuilderTest.kt +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/rendering/frames/ansi/AnsiConversationFrameBuilderTest.kt @@ -5,6 +5,7 @@ import com.github.benpollarduk.ktaf.assets.characters.NonPlayableCharacter import com.github.benpollarduk.ktaf.conversations.Conversation import com.github.benpollarduk.ktaf.conversations.Paragraph import com.github.benpollarduk.ktaf.conversations.Response +import com.github.benpollarduk.ktaf.conversations.instructions.Next import com.github.benpollarduk.ktaf.interpretation.ConversationCommandInterpreter import com.github.benpollarduk.ktaf.logic.GameTestHelper import org.junit.jupiter.api.Assertions @@ -21,9 +22,9 @@ class AnsiConversationFrameBuilderTest { val paragraph2 = Paragraph("Then I said a load more.") val paragraph3 = Paragraph("A test paragraph, this is something that is said.").also { it.responses = listOf( - Response("Response a.", 1), - Response("Response b.", 1), - Response("Response c.", 1) + Response("Response a.", Next()), + Response("Response b.", Next()), + Response("Response c.", Next()) ) } character.conversation = Conversation(listOf(paragraph1, paragraph2, paragraph3)) diff --git a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/rendering/frames/html/HtmlConversationFrameBuilderTest.kt b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/rendering/frames/html/HtmlConversationFrameBuilderTest.kt index 39ecccb..6329192 100644 --- a/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/rendering/frames/html/HtmlConversationFrameBuilderTest.kt +++ b/ktaf/src/test/kotlin/com/github/benpollarduk/ktaf/rendering/frames/html/HtmlConversationFrameBuilderTest.kt @@ -4,6 +4,7 @@ import com.github.benpollarduk.ktaf.assets.characters.NonPlayableCharacter import com.github.benpollarduk.ktaf.conversations.Conversation import com.github.benpollarduk.ktaf.conversations.Paragraph import com.github.benpollarduk.ktaf.conversations.Response +import com.github.benpollarduk.ktaf.conversations.instructions.Next import com.github.benpollarduk.ktaf.interpretation.ConversationCommandInterpreter import com.github.benpollarduk.ktaf.logic.GameTestHelper import org.junit.jupiter.api.Assertions @@ -20,9 +21,9 @@ class HtmlConversationFrameBuilderTest { val paragraph2 = Paragraph("Then I said a load more.") val paragraph3 = Paragraph("A test paragraph, this is something that is said.").also { it.responses = listOf( - Response("Response a.", 1), - Response("Response b.", 1), - Response("Response c.", 1) + Response("Response a.", Next()), + Response("Response b.", Next()), + Response("Response c.", Next()) ) } character.conversation = Conversation(listOf(paragraph1, paragraph2, paragraph3)) diff --git a/ktaf/src/test/resources/test.jar b/ktaf/src/test/resources/test.jar index acf0f6a..a96644f 100644 Binary files a/ktaf/src/test/resources/test.jar and b/ktaf/src/test/resources/test.jar differ