From 225371fccba7d5abf065f49fdfc2f890d2362e2f Mon Sep 17 00:00:00 2001 From: Fiereu Date: Sun, 9 Aug 2026 11:59:37 +0200 Subject: [PATCH] feat(game): add a /story command that jumps to a Kanto story checkpoint (#138) * feat(game): add a /story command that jumps to a kanto story checkpoint * feat(game): add a /story reset that starts the region over * fix(game): stop a story jump running over a battle, a dialog or the pc * chore(game): cut the story command comments back * test(game): drop the seed starter expectation the sql seed no longer meets --- .../de/fiereu/openmmo/common/enums/Region.kt | 2 + docs/src/content/docs/concepts/database.md | 14 +- .../de/fiereu/openmmo/server/game/Main.kt | 1 + .../server/game/di/ChatCommandModule.kt | 3 + .../server/game/di/GameServerComponent.kt | 3 + .../game/services/command/KantoCheckpoints.kt | 151 +++++++++++++++++ .../game/services/command/StoryCheckpoint.kt | 24 +++ .../game/services/command/StoryCommand.kt | 138 +++++++++++++++ .../server/game/storage/CharacterStore.kt | 3 +- .../server/game/storage/DevCharacterSeeder.kt | 41 +++++ .../db/dev/R__seed_dev_characters.sql | 29 +--- .../services/command/KantoCheckpointsTest.kt | 61 +++++++ .../game/services/command/StoryResetTest.kt | 157 ++++++++++++++++++ .../game/storage/CharacterRepositoryIT.kt | 6 +- .../game/storage/DevCharacterSeederTest.kt | 50 ++++++ 15 files changed, 647 insertions(+), 36 deletions(-) create mode 100644 server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/command/KantoCheckpoints.kt create mode 100644 server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/command/StoryCheckpoint.kt create mode 100644 server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/command/StoryCommand.kt create mode 100644 server.game/src/main/kotlin/de/fiereu/openmmo/server/game/storage/DevCharacterSeeder.kt create mode 100644 server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/command/KantoCheckpointsTest.kt create mode 100644 server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/command/StoryResetTest.kt create mode 100644 server.game/src/test/kotlin/de/fiereu/openmmo/server/game/storage/DevCharacterSeederTest.kt diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Region.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Region.kt index 1e1cdbf..46c3659 100644 --- a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Region.kt +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/Region.kt @@ -4,6 +4,8 @@ enum class Region(val wireValue: Byte) { KANTO(0), HOENN(1); + val displayName: String = name.lowercase().replaceFirstChar { it.uppercase() } + companion object { fun byId(id: Int): Region? = entries.find { it.wireValue.toInt() == id } diff --git a/docs/src/content/docs/concepts/database.md b/docs/src/content/docs/concepts/database.md index 7fba46c..6ef8bae 100644 --- a/docs/src/content/docs/concepts/database.md +++ b/docs/src/content/docs/concepts/database.md @@ -43,9 +43,10 @@ seconds at most, and only connected players are cached. ## Dev data seeding -Dev seed data (the `admin`/`test` accounts and the `Test`/`Test2` characters) -does NOT belong in `db/migration`. Anything there runs in every environment, -including production. Seeds live in a separate Flyway location: +Dev seed data (the `admin`/`test` accounts, and the developer bit on the +characters those accounts make) does NOT belong in `db/migration`. Anything +there runs in every environment, including production. Seeds live in a separate +Flyway location: - Files go into `src/main/resources/db/dev/`. - They are repeatable migrations (`R__seed_something.sql`), not versioned @@ -64,3 +65,10 @@ variables. To add more dev data, extend an existing `R__` file or add a new one, keep it idempotent, and restart the server. Flyway reapplies the changed file on the next start. + +Dev characters are the exception, because a character built by SQL drifts from +what a real new character gets. `DevCharacterSeeder` makes one per region on +startup, named after the region, through the same `CharacterStore.createCharacter` +the client uses, so their start state comes from `NewGameStarts`. It also skips +regions that already have their character, so it never overwrites one you have +played. diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/Main.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/Main.kt index 3ad283c..0276a51 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/Main.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/Main.kt @@ -17,6 +17,7 @@ fun main() { component.databaseBootstrap().migrate() val characterStore = component.characterStore() characterStore.startPeriodicFlush() + runBlocking { component.devCharacterSeeder().seed() } Runtime.getRuntime() .addShutdownHook( Thread { diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/di/ChatCommandModule.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/di/ChatCommandModule.kt index 6a0a742..451b4b4 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/di/ChatCommandModule.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/di/ChatCommandModule.kt @@ -7,6 +7,7 @@ import de.fiereu.openmmo.server.game.services.command.CatchCommand import de.fiereu.openmmo.server.game.services.command.ChatCommand import de.fiereu.openmmo.server.game.services.command.HelpCommand import de.fiereu.openmmo.server.game.services.command.PosCommand +import de.fiereu.openmmo.server.game.services.command.StoryCommand import de.fiereu.openmmo.server.game.services.command.TestBattleCommand /** @@ -22,4 +23,6 @@ interface ChatCommandModule { @Binds @IntoSet fun testBattleCommand(command: TestBattleCommand): ChatCommand @Binds @IntoSet fun catchCommand(command: CatchCommand): ChatCommand + + @Binds @IntoSet fun storyCommand(command: StoryCommand): ChatCommand } diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/di/GameServerComponent.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/di/GameServerComponent.kt index e440031..d747785 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/di/GameServerComponent.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/di/GameServerComponent.kt @@ -8,6 +8,7 @@ import de.fiereu.openmmo.server.game.db.DatabaseBootstrap import de.fiereu.openmmo.server.game.handler.GameAppHandler import de.fiereu.openmmo.server.game.session.SessionRegistry import de.fiereu.openmmo.server.game.storage.CharacterStore +import de.fiereu.openmmo.server.game.storage.DevCharacterSeeder import javax.inject.Provider import javax.inject.Singleton @@ -25,6 +26,8 @@ interface GameServerComponent { fun characterStore(): CharacterStore + fun devCharacterSeeder(): DevCharacterSeeder + @Component.Factory fun interface Factory { fun create(@BindsInstance config: GameServerConfig): GameServerComponent diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/command/KantoCheckpoints.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/command/KantoCheckpoints.kt new file mode 100644 index 0000000..b137a5d --- /dev/null +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/command/KantoCheckpoints.kt @@ -0,0 +1,151 @@ +package de.fiereu.openmmo.server.game.services.command + +import de.fiereu.openmmo.common.enums.Direction +import de.fiereu.openmmo.common.enums.Region +import de.fiereu.openmmo.items.generated.Items +import de.fiereu.openmmo.story.generated.kanto.KantoFlags +import de.fiereu.openmmo.story.generated.kanto.KantoVars + +private const val PALLET_TOWN_BANK = 3 +private const val PALLET_TOWN_MAP = 0 +private const val INDOOR_PALLET_BANK = 4 +private const val PLAYERS_HOUSE_2F_MAP = 1 +private const val OAKS_LAB_MAP = 3 +private const val VIRIDIAN_MART_BANK = 5 +private const val VIRIDIAN_MART_MAP = 3 + +private val BULBASAUR = CheckpointMon(dexId = 1, level = 5, moveIds = listOf(33, 45)) + +// Clearing the hide flag is what makes NpcService spawn Oak in his lab. +private val AFTER_GRASS_SCENE = KantoFlags.initiallySet - KantoFlags.FLAG_HIDE_OAK_IN_HIS_LAB + +// The scripts despawn these without persisting a flag, so a skipped scene sets them by hand. +private val TAKEN_BALLS = + setOf(KantoFlags.FLAG_HIDE_BULBASAUR_BALL, KantoFlags.FLAG_HIDE_CHARMANDER_BALL) + +private val AFTER_STARTER = + AFTER_GRASS_SCENE + + setOf( + KantoFlags.FLAG_WORLD_MAP_PALLET_TOWN, + KantoFlags.FLAG_VISITED_OAKS_LAB, + KantoFlags.FLAG_SYS_POKEMON_GET, + KantoFlags.FLAG_PALLET_LADY_NOT_BLOCKING_SIGN, + ) + +private val AFTER_RIVAL = + AFTER_STARTER + + TAKEN_BALLS + + setOf( + KantoFlags.FLAG_BEAT_RIVAL_IN_OAKS_LAB, + KantoFlags.FLAG_HIDE_RIVAL_IN_LAB, + KantoFlags.FLAG_OPENED_START_MENU, + KantoFlags.FLAG_WORLD_MAP_VIRIDIAN_CITY, + KantoFlags.FLAG_GOT_POTION_ON_ROUTE_1, + ) + +val KANTO_CHECKPOINTS: List = + listOf( + StoryCheckpoint( + name = "new-game", + description = "the bedroom, exactly as a fresh character starts", + region = Region.KANTO, + bankId = INDOOR_PALLET_BANK, + mapId = PLAYERS_HOUSE_2F_MAP, + x = 6, + y = 6, + facing = Direction.DOWN, + storyFlags = KantoFlags.initiallySet, + ), + StoryCheckpoint( + name = "oak-grass", + description = "on the trigger where Oak stops you leaving Pallet Town", + region = Region.KANTO, + bankId = PALLET_TOWN_BANK, + mapId = PALLET_TOWN_MAP, + x = 12, + y = 1, + facing = Direction.UP, + storyFlags = KantoFlags.initiallySet + KantoFlags.FLAG_WORLD_MAP_PALLET_TOWN, + ), + StoryCheckpoint( + name = "starter-choice", + description = "inside Oak's lab, the starter scene about to play", + region = Region.KANTO, + bankId = INDOOR_PALLET_BANK, + mapId = OAKS_LAB_MAP, + x = 6, + y = 12, + facing = Direction.UP, + storyFlags = + AFTER_GRASS_SCENE + + setOf( + KantoFlags.FLAG_WORLD_MAP_PALLET_TOWN, + KantoFlags.FLAG_VISITED_OAKS_LAB, + KantoFlags.FLAG_DONT_TRANSITION_MUSIC, + ), + storyVars = + mapOf( + KantoVars.VAR_MAP_SCENE_PALLET_TOWN_OAK to 1, + KantoVars.VAR_MAP_SCENE_PALLET_TOWN_PROFESSOR_OAKS_LAB to 1, + ), + ), + StoryCheckpoint( + name = "rival-battle", + description = "on the lab exit tile that starts the rival battle", + region = Region.KANTO, + bankId = INDOOR_PALLET_BANK, + mapId = OAKS_LAB_MAP, + x = 6, + y = 8, + facing = Direction.DOWN, + storyFlags = AFTER_STARTER + TAKEN_BALLS, + storyVars = + mapOf( + KantoVars.VAR_MAP_SCENE_PALLET_TOWN_OAK to 1, + KantoVars.VAR_MAP_SCENE_PALLET_TOWN_PROFESSOR_OAKS_LAB to 3, + // Picks the rival's monster, so it has to match the party below. + KantoVars.VAR_STARTER_MON to 0, + ), + party = listOf(BULBASAUR), + ), + StoryCheckpoint( + name = "oaks-parcel", + description = "walking into the Viridian mart for Oak's parcel", + region = Region.KANTO, + bankId = VIRIDIAN_MART_BANK, + mapId = VIRIDIAN_MART_MAP, + x = 4, + y = 7, + facing = Direction.UP, + storyFlags = AFTER_RIVAL, + storyVars = + mapOf( + KantoVars.VAR_MAP_SCENE_PALLET_TOWN_OAK to 1, + KantoVars.VAR_MAP_SCENE_PALLET_TOWN_PROFESSOR_OAKS_LAB to 4, + KantoVars.VAR_MAP_SCENE_PALLET_TOWN_SIGN_LADY to 2, + KantoVars.VAR_STARTER_MON to 0, + ), + party = listOf(BULBASAUR), + ), + StoryCheckpoint( + name = "pokedex", + description = "in front of Oak with the parcel, ready for the Pokedex", + region = Region.KANTO, + bankId = INDOOR_PALLET_BANK, + mapId = OAKS_LAB_MAP, + x = 6, + y = 4, + facing = Direction.UP, + storyFlags = AFTER_RIVAL, + storyVars = + mapOf( + KantoVars.VAR_MAP_SCENE_PALLET_TOWN_OAK to 1, + KantoVars.VAR_MAP_SCENE_PALLET_TOWN_PROFESSOR_OAKS_LAB to 5, + KantoVars.VAR_MAP_SCENE_VIRIDIAN_CITY_MART to 1, + KantoVars.VAR_MAP_SCENE_PALLET_TOWN_SIGN_LADY to 2, + KantoVars.VAR_STARTER_MON to 0, + ), + party = listOf(BULBASAUR), + items = mapOf(Items.OAKS_PARCEL to 1), + ), + ) diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/command/StoryCheckpoint.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/command/StoryCheckpoint.kt new file mode 100644 index 0000000..3dd08c6 --- /dev/null +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/command/StoryCheckpoint.kt @@ -0,0 +1,24 @@ +package de.fiereu.openmmo.server.game.services.command + +import de.fiereu.openmmo.common.enums.Direction +import de.fiereu.openmmo.common.enums.Region + +data class CheckpointMon(val dexId: Int, val level: Int, val moveIds: List) + +/** + * [storyFlags] and [storyVars] replace the character's whole story state rather than merge into it. + */ +data class StoryCheckpoint( + val name: String, + val description: String, + val region: Region, + val bankId: Int, + val mapId: Int, + val x: Int, + val y: Int, + val facing: Direction, + val storyFlags: Set, + val storyVars: Map = emptyMap(), + val party: List = emptyList(), + val items: Map = emptyMap(), +) diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/command/StoryCommand.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/command/StoryCommand.kt new file mode 100644 index 0000000..2ffcd69 --- /dev/null +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/command/StoryCommand.kt @@ -0,0 +1,138 @@ +package de.fiereu.openmmo.server.game.services.command + +import de.fiereu.openmmo.common.CharacterPermissions +import de.fiereu.openmmo.common.enums.CharacterGender +import de.fiereu.openmmo.common.enums.Direction +import de.fiereu.openmmo.common.enums.Region +import de.fiereu.openmmo.maps.WarpTile +import de.fiereu.openmmo.server.game.services.BattleService +import de.fiereu.openmmo.server.game.services.StoryPlayerService +import de.fiereu.openmmo.server.game.services.WarpService +import de.fiereu.openmmo.server.game.services.WorldStateService +import de.fiereu.openmmo.server.game.storage.CharacterStore +import de.fiereu.openmmo.server.game.storage.NewGameStarts +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class StoryCommand +@Inject +constructor( + private val characterStore: CharacterStore, + private val worldStateService: WorldStateService, + private val storyPlayerService: StoryPlayerService, + private val warpService: WarpService, + private val battleService: BattleService, +) : ChatCommand { + override val name = "story" + override val usage = "/story [checkpoint|reset]" + override val description = "jumps to a story scene, or lists the scenes with no argument" + override val permission = CharacterPermissions.DEVELOPER + + override fun run(ctx: CommandContext) { + val wanted = ctx.args.firstOrNull() + if (wanted == null) { + KANTO_CHECKPOINTS.forEach { ctx.reply("${it.name} - ${it.description}") } + ctx.reply("reset - starts the region's story over, keeping money and the counters") + return + } + // A jump warps out from under a parked script or a running battle, and neither recovers. + if (ctx.state.inDialog) { + ctx.reply("Finish what you are talking to first.") + return + } + if (battleService.inBattle(ctx.characterId)) { + ctx.reply("Finish the battle first.") + return + } + if (wanted.equals("reset", ignoreCase = true)) { + reset(ctx) + return + } + val checkpoint = KANTO_CHECKPOINTS.find { it.name.equals(wanted, ignoreCase = true) } + if (checkpoint == null) { + ctx.reply("No checkpoint called $wanted. Run /story for the list.") + return + } + apply(ctx, checkpoint) + } + + private fun reset(ctx: CommandContext) { + val charId = ctx.characterId + val stored = characterStore.getCharacter(charId) ?: return + val region = Region.byWireValue(stored.info.positionRegionId) + if (region == null) { + ctx.reply("That region has no known start, so there is nothing to reset to.") + return + } + val female = stored.info.rivalSex == CharacterGender.FEMALE.wireValue + val start = NewGameStarts.forRegion(region, female) + + characterStore.replaceProgress( + characterId = charId, + party = emptyList(), + items = emptyMap(), + storyFlags = start.storyFlags, + storyVars = start.storyVars, + ) + // Hoenn's opening reads the dynamic warp on its way out of the truck. + characterStore.setDynamicWarp(charId, start.dynamicWarp) + + val refreshed = characterStore.getCharacter(charId) ?: return + worldStateService.send(ctx.session, refreshed, fullVars = true) + warpService.executeWarp( + ctx.session, + charId, + WarpTile( + x = 0, + y = 0, + targetRegionId = region.wireValue, + targetBankId = start.bankId, + targetMapId = start.mapId, + targetX = start.x.toInt(), + targetY = start.y.toInt(), + exitFacing = Direction.DOWN, + ), + ) + characterStore.flushCharacterAsync(charId) + ctx.reply("Reset to the ${region.displayName} start. Your party, PC and bag are empty.") + } + + private fun apply(ctx: CommandContext, checkpoint: StoryCheckpoint) { + val charId = ctx.characterId + characterStore.replaceProgress( + characterId = charId, + party = emptyList(), + items = checkpoint.items, + storyFlags = checkpoint.storyFlags, + storyVars = checkpoint.storyVars, + ) + checkpoint.party.forEach { + storyPlayerService.givePokemon(ctx.session, ctx.state, it.dexId, it.level, it.moveIds) + } + + // There is no packet for a single var, so the whole block goes again, and its ids resolve + // against the region on the character, which the warp has not moved yet. + val stored = characterStore.getCharacter(charId) ?: return + val aimed = stored.copy(info = stored.info.copy(positionRegionId = checkpoint.region.wireValue)) + worldStateService.send(ctx.session, aimed, fullVars = true) + + // A warp rather than a move, so the destination runs its entry scripts and the scene fires. + warpService.executeWarp( + ctx.session, + charId, + WarpTile( + x = 0, + y = 0, + targetRegionId = checkpoint.region.wireValue, + targetBankId = checkpoint.bankId.toByte(), + targetMapId = checkpoint.mapId.toByte(), + targetX = checkpoint.x, + targetY = checkpoint.y, + exitFacing = checkpoint.facing, + ), + ) + characterStore.flushCharacterAsync(charId) + ctx.reply("Jumped to ${checkpoint.name}.") + } +} diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/storage/CharacterStore.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/storage/CharacterStore.kt index cf3296e..3be1c16 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/storage/CharacterStore.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/storage/CharacterStore.kt @@ -245,7 +245,7 @@ constructor( markDirty(characterId) } - /** Replaces the whole party, bag and story state at once, for a dev jump to a story point. */ + /** Replaces every monster, the party and the pc alike, along with the bag and story state. */ fun replaceProgress( characterId: Long, party: List, @@ -257,6 +257,7 @@ constructor( characters[characterId] = stored.copy( pokemon = party.toMutableList(), + pcStorage = mutableListOf(), items = items.toMutableMap(), storyFlags = storyFlags.toMutableSet(), // A var of 0 is the default, so it is stored as absent everywhere else too. diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/storage/DevCharacterSeeder.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/storage/DevCharacterSeeder.kt new file mode 100644 index 0000000..31d4868 --- /dev/null +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/storage/DevCharacterSeeder.kt @@ -0,0 +1,41 @@ +package de.fiereu.openmmo.server.game.storage + +import de.fiereu.openmmo.common.CharacterPermissions +import de.fiereu.openmmo.common.enums.CharacterGender +import de.fiereu.openmmo.common.enums.Region +import de.fiereu.openmmo.server.game.config.GameServerConfig +import io.github.oshai.kotlinlogging.KotlinLogging +import javax.inject.Inject +import javax.inject.Singleton + +private val log = KotlinLogging.logger {} + +private const val DEV_USER_ID = 1 + +/** + * One character per region for local testing, built through [CharacterStore.createCharacter] so + * their start state cannot drift from what a real new character gets. + */ +@Singleton +class DevCharacterSeeder +@Inject +constructor( + private val characterStore: CharacterStore, + private val config: GameServerConfig, +) { + + suspend fun seed() { + if (!config.db.seedDev) return + val existing = characterStore.getCharactersByUser(DEV_USER_ID) + for (region in Region.entries) { + val name = region.name.lowercase().replaceFirstChar { it.uppercase() } + if (existing.any { it.info.name == name }) continue + val created = characterStore.createCharacter(DEV_USER_ID, name, CharacterGender.MALE, region) + characterStore.updateCharacter( + created.info.copy( + permissions = created.info.permissions or CharacterPermissions.DEVELOPER)) + characterStore.flushCharacterAsync(created.info.id) + log.info { "Seeded dev character '$name' for $region" } + } + } +} diff --git a/server.game/src/main/resources/db/dev/R__seed_dev_characters.sql b/server.game/src/main/resources/db/dev/R__seed_dev_characters.sql index a2881df..ab06cd2 100644 --- a/server.game/src/main/resources/db/dev/R__seed_dev_characters.sql +++ b/server.game/src/main/resources/db/dev/R__seed_dev_characters.sql @@ -1,32 +1,5 @@ --- Only applied when db.seedDev is enabled, never in production. --- Fixed low ids, so they never collide with EntityIdService ids, which carry a timestamp head. --- Party dex ids must stay in Gen 1 to 3, the only range the species registry covers. --- The delete cascades, so re-seeding drops whatever these characters caught, carried or unlocked. - +-- Drops the two characters the SQL seed used to create, which DevCharacterSeeder now replaces. DELETE FROM characters WHERE id IN (102400, 364544); -INSERT INTO characters ( - id, user_id, name, name_prefix, rival_sex, last_login, created_at, money, permissions, - remaining_safari_steps, remaining_safari_balls, pc_extra_slots, battle_box_extra_slots, - template_amount, position_region_id, position_bank_id, position_map_id, position_x, - position_y, repel_left, repel_item_id, lure_left, lure_item_id -) --- 264 is the 8 these characters already carried plus CharacterPermissions.DEVELOPER, 0x100. -VALUES - (102400, 1, 'Test', '', 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 30000, 264, - 0, 0, 0, 0, 0, 1, 51, 3, 4, 4, 0, 0, 0, 0), - (364544, 2, 'Test2', '', 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 30000, 264, - 0, 0, 0, 0, 0, 1, 51, 3, 4, 4, 0, 0, 0, 0); - -INSERT INTO pokemon ( - id, owner_id, container, container_slot, dex_id, seed, ot, pokemon_level, hp, xp, - move1_id, move1_pp, move2_id, move2_pp, caught_at -) -VALUES - (180224, 102400, 'PARTY', 0, 1, 0, 'Test', 5, 19, 135, 33, 35, 45, 40, CURRENT_TIMESTAMP), - (311296, 102400, 'PARTY', 1, 19, 0, 'Test', 3, 14, 27, 33, 35, 39, 30, CURRENT_TIMESTAMP), - (442368, 364544, 'PARTY', 0, 1, 0, 'Test2', 5, 19, 135, 33, 35, 45, 40, CURRENT_TIMESTAMP), - (507904, 364544, 'PARTY', 1, 19, 0, 'Test2', 3, 14, 27, 33, 35, 39, 30, CURRENT_TIMESTAMP); - -- Gives the developer bit to characters made through the client, on the dev accounts only. UPDATE characters SET permissions = permissions | 256 WHERE user_id IN (1, 2); diff --git a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/command/KantoCheckpointsTest.kt b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/command/KantoCheckpointsTest.kt new file mode 100644 index 0000000..d2ed394 --- /dev/null +++ b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/command/KantoCheckpointsTest.kt @@ -0,0 +1,61 @@ +package de.fiereu.openmmo.server.game.services.command + +import de.fiereu.openmmo.maps.MapManager +import de.fiereu.openmmo.story.generated.kanto.KantoFlags +import de.fiereu.openmmo.story.generated.kanto.KantoVars +import io.kotest.assertions.withClue +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe +import io.kotest.matchers.shouldNotBe + +private val LAB_CHECKPOINTS = setOf("starter-choice", "rival-battle", "pokedex") + +class KantoCheckpointsTest : + FunSpec({ + test("every checkpoint stands on a walkable tile of a map we have") { + val maps = MapManager() + + KANTO_CHECKPOINTS.forEach { checkpoint -> + val map = + maps.getMap( + checkpoint.region.wireValue, + checkpoint.bankId.toByte(), + checkpoint.mapId.toByte(), + ) + withClue(checkpoint.name) { + map shouldNotBe null + val tile = map!!.tileAt(checkpoint.x, checkpoint.y) + tile shouldNotBe null + tile!!.blocksMovement() shouldBe false + } + } + } + + test("names are unique and lower case, so /story can match on them") { + KANTO_CHECKPOINTS.map { it.name } shouldBe KANTO_CHECKPOINTS.map { it.name }.distinct() + KANTO_CHECKPOINTS.all { it.name == it.name.lowercase() } shouldBe true + } + + test("lab checkpoints clear the flag that would hide Oak") { + KANTO_CHECKPOINTS.filter { it.name in LAB_CHECKPOINTS } + .forEach { + withClue(it.name) { + it.storyFlags.contains(KantoFlags.FLAG_HIDE_OAK_IN_HIS_LAB) shouldBe false + } + } + } + + test("a checkpoint the rival battle depends on carries a party matching its starter var") { + val rivalBattle = KANTO_CHECKPOINTS.single { it.name == "rival-battle" } + + rivalBattle.storyVars[KantoVars.VAR_STARTER_MON] shouldBe 0 + rivalBattle.party.single().dexId shouldBe 1 + } + + test("every checkpoint past the starter carries the party its scenes need") { + val needParty = setOf("rival-battle", "oaks-parcel", "pokedex") + + KANTO_CHECKPOINTS.filter { it.name in needParty } + .forEach { withClue(it.name) { it.party.isEmpty() shouldBe false } } + } + }) diff --git a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/command/StoryResetTest.kt b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/command/StoryResetTest.kt new file mode 100644 index 0000000..d85705d --- /dev/null +++ b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/command/StoryResetTest.kt @@ -0,0 +1,157 @@ +package de.fiereu.openmmo.server.game.services.command + +import de.fiereu.openmmo.common.CharacterPermissions +import de.fiereu.openmmo.common.enums.CharacterGender +import de.fiereu.openmmo.common.enums.PokemonContainer +import de.fiereu.openmmo.common.enums.Region +import de.fiereu.openmmo.maps.MapManager +import de.fiereu.openmmo.moves.MoveRegistry +import de.fiereu.openmmo.net.game.packets.ChatMessagePacket +import de.fiereu.openmmo.pokemon.LearnsetRegistry +import de.fiereu.openmmo.pokemon.SpeciesRegistry +import de.fiereu.openmmo.server.game.battle.BattlePacketEmitter +import de.fiereu.openmmo.server.game.battle.BattleRegistry +import de.fiereu.openmmo.server.game.battle.BattleRewards +import de.fiereu.openmmo.server.game.battle.BattleRng +import de.fiereu.openmmo.server.game.battle.MoveLearner +import de.fiereu.openmmo.server.game.battle.TurnEngine +import de.fiereu.openmmo.server.game.battle.WildMonFactory +import de.fiereu.openmmo.server.game.services.BattleService +import de.fiereu.openmmo.server.game.services.MapLoadService +import de.fiereu.openmmo.server.game.services.PresenceService +import de.fiereu.openmmo.server.game.services.StoryPlayerService +import de.fiereu.openmmo.server.game.services.WarpService +import de.fiereu.openmmo.server.game.services.WorldStateService +import de.fiereu.openmmo.server.game.storage.CharacterStore +import de.fiereu.openmmo.server.game.storage.EntityIdService +import de.fiereu.openmmo.server.game.storage.NewGameStarts +import de.fiereu.openmmo.server.game.testsupport.FakeCharacterRepository +import de.fiereu.openmmo.server.game.testsupport.FakeSession +import de.fiereu.openmmo.server.game.world.interest.InterestManager +import de.fiereu.openmmo.server.game.world.interest.PassThroughInterestPolicy +import de.fiereu.openmmo.story.generated.kanto.KantoFlags +import de.fiereu.openmmo.story.generated.kanto.KantoVars +import de.fiereu.openmmo.trainer.TrainerRegistry +import de.fiereu.openmmo.typechart.TypeChart +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.collections.shouldContain +import io.kotest.matchers.shouldBe +import io.kotest.matchers.string.shouldContain +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest + +@OptIn(ExperimentalCoroutinesApi::class) +class StoryResetTest : + FunSpec({ + fun storyCommand(store: CharacterStore): StoryCommand { + val mapManager = MapManager() + val mapLoad = MapLoadService(mapManager) + val interest = InterestManager() + val species = SpeciesRegistry() + val moves = MoveRegistry() + return StoryCommand( + characterStore = store, + worldStateService = WorldStateService(), + storyPlayerService = + StoryPlayerService( + store, + WildMonFactory(species, moves, LearnsetRegistry(), EntityIdService()), + species, + moves, + ), + warpService = + WarpService( + mapLoad, + mapManager, + store, + PresenceService(interest, PassThroughInterestPolicy(), mapLoad, store), + ), + battleService = + BattleService( + characterStore = store, + battles = BattleRegistry(), + engine = TurnEngine(moves, TypeChart()), + wildMons = + WildMonFactory(species, moves, LearnsetRegistry(), EntityIdService()), + emitter = BattlePacketEmitter(interest), + rewards = BattleRewards(), + moveLearner = MoveLearner(LearnsetRegistry(), moves), + interestManager = interest, + speciesRegistry = species, + moveRegistry = moves, + trainers = TrainerRegistry(), + ), + ) + } + + suspend fun developer(store: CharacterStore): Long { + val created = store.createCharacter(1, "Red", CharacterGender.MALE, Region.KANTO) + store.updateCharacter( + created.info.copy( + permissions = created.info.permissions or CharacterPermissions.DEVELOPER)) + return created.info.id + } + + test("reset puts the character back on its region's new game story state") { + runTest { + val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope) + val charId = developer(store) + store.setStoryFlag(charId, KantoFlags.FLAG_SYS_POKEMON_GET) + store.setStoryVar(charId, KantoVars.VAR_STARTER_MON, 2) + store.addItem(charId, itemId = 4, amount = 5) + val session = FakeSession(characterId = charId) + val service = ChatCommandService(store, setOf(storyCommand(store))) + + service.tryHandle(session, "/story reset") shouldBe true + + val after = store.getCharacter(charId)!! + after.storyFlags shouldBe NewGameStarts.forRegion(Region.KANTO, female = false).storyFlags + after.storyVars shouldBe emptyMap() + after.items shouldBe emptyMap() + session.sent.filterIsInstance().last().message shouldContain "Reset" + } + } + + test("reset empties the party and the pc") { + runTest { + val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope) + val charId = developer(store) + val species = SpeciesRegistry() + val factory = + WildMonFactory(species, MoveRegistry(), LearnsetRegistry(), EntityIdService()) + store.addPokemon(charId, factory.create(1, 5, BattleRng(seed = 1))!!) + // A loaded character keeps boxed monsters here, so addPokemon would test the wrong shape. + store + .getCharacter(charId)!! + .pcStorage + .add( + factory + .create(4, 5, BattleRng(seed = 2))!! + .copy(container = PokemonContainer.PC, containerSlot = 0)) + val session = FakeSession(characterId = charId) + val service = ChatCommandService(store, setOf(storyCommand(store))) + + service.tryHandle(session, "/story reset") shouldBe true + + val after = store.getCharacter(charId)!! + after.pokemon shouldBe emptyList() + after.pcStorage shouldBe emptyList() + } + } + + test("reset is refused while a dialog is open") { + runTest { + val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope) + val charId = developer(store) + store.setStoryFlag(charId, KantoFlags.FLAG_SYS_POKEMON_GET) + val session = FakeSession(characterId = charId) + session.state().inDialog = true + val service = ChatCommandService(store, setOf(storyCommand(store))) + + service.tryHandle(session, "/story reset") shouldBe true + + store.getCharacter(charId)!!.storyFlags shouldContain KantoFlags.FLAG_SYS_POKEMON_GET + session.sent.filterIsInstance().last().message shouldContain "Finish" + } + } + }) diff --git a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/storage/CharacterRepositoryIT.kt b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/storage/CharacterRepositoryIT.kt index 62eb07d..be82921 100644 --- a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/storage/CharacterRepositoryIT.kt +++ b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/storage/CharacterRepositoryIT.kt @@ -248,9 +248,7 @@ class CharacterRepositoryIT : shouldThrowAny { repository.insertAggregate(stored) } } - test("the seed migration provides the dev characters with their starters") { - val seeded = repository.loadByUser(1) - seeded.map { it.info.name } shouldBe listOf("Test") - seeded.single().pokemon.map { it.dexId } shouldBe listOf(1, 19) + test("the seed migration leaves the dev user without characters for the seeder to make") { + repository.loadByUser(1).shouldBeEmpty() } }) diff --git a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/storage/DevCharacterSeederTest.kt b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/storage/DevCharacterSeederTest.kt new file mode 100644 index 0000000..e5c2100 --- /dev/null +++ b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/storage/DevCharacterSeederTest.kt @@ -0,0 +1,50 @@ +package de.fiereu.openmmo.server.game.storage + +import de.fiereu.openmmo.common.CharacterPermissions +import de.fiereu.openmmo.common.enums.Region +import de.fiereu.openmmo.common.hasPermission +import de.fiereu.openmmo.server.game.config.ConfigLoader +import de.fiereu.openmmo.server.game.testsupport.FakeCharacterRepository +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest + +@OptIn(ExperimentalCoroutinesApi::class) +class DevCharacterSeederTest : + FunSpec({ + val config = ConfigLoader.load() + + test("seeds one developer character per region on its new game start") { + runTest { + val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope) + + DevCharacterSeeder(store, config).seed() + + val seeded = store.getCharactersByUser(1) + seeded.size shouldBe Region.entries.size + Region.entries.forEach { region -> + val start = NewGameStarts.forRegion(region, female = false) + val character = seeded.single { it.info.positionRegionId == region.wireValue } + character.info.positionBankId shouldBe start.bankId + character.info.positionMapId shouldBe start.mapId + character.info.positionX shouldBe start.x + character.info.positionY shouldBe start.y + character.storyFlags shouldBe start.storyFlags + character.info.hasPermission(CharacterPermissions.DEVELOPER) shouldBe true + } + } + } + + test("running twice does not add a second set") { + runTest { + val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope) + val seeder = DevCharacterSeeder(store, config) + + seeder.seed() + seeder.seed() + + store.getCharactersByUser(1).size shouldBe Region.entries.size + } + } + })