feat(server.game): create new characters with an empty party (#57)

This commit is contained in:
Fiereu authored and GitHub committed 2026-07-25 16:45:50 +02:00
1 parent 1e19440743
commit 214663531a
4 files changed
+72 -84

No files matched your search

@@ -2,10 +2,6 @@ package de.fiereu.openmmo.server.game.storage
import de.fiereu.openmmo.common.CharacterInfo
import de.fiereu.openmmo.common.Pokemon
import de.fiereu.openmmo.common.PokemonMove
import de.fiereu.openmmo.common.enums.EVs
import de.fiereu.openmmo.common.enums.IVs
import de.fiereu.openmmo.common.enums.PokemonContainer
import io.github.oshai.kotlinlogging.KotlinLogging
import java.time.LocalDateTime
import java.util.concurrent.ConcurrentHashMap
@@ -23,10 +19,6 @@ import kotlinx.coroutines.launch
private val log = KotlinLogging.logger {}
// The starter party every new character gets.
const val SNIVY_DEX_ID = 495
const val PATRAT_DEX_ID = 504
private val FLUSH_TICK = 5.seconds
private val FLUSH_DEBOUNCE = 10.seconds
@@ -61,7 +53,7 @@ constructor(
private val dirtySince = ConcurrentHashMap<Long, Long>()
private val pendingUnload = ConcurrentHashMap.newKeySet<Long>()
/** Create a character with its own entity id and a starter party with its own monster uids. */
/** Create a character with its own entity id and an empty party. */
suspend fun createCharacter(
userId: Int,
name: String,
@@ -95,8 +87,6 @@ constructor(
lureItemId = 0,
)
val stored = StoredCharacter(info, mutableListOf(), mutableListOf(), mutableMapOf())
stored.pokemon.add(starterSnivy(id, name))
stored.pokemon.add(starterPatrat(id, name))
repository.insertAggregate(stored)
characters[id] = stored
charactersByUser.computeIfAbsent(userId) { CopyOnWriteArrayList() }.add(id)
@@ -160,6 +150,14 @@ constructor(
markDirty(characterId)
}
/** Replace one party monster by id, for example after a battle changed hp, xp, or level. */
fun updatePokemon(characterId: Long, updated: Pokemon) {
val stored = characters[characterId] ?: return
val party = stored.pokemon.map { if (it.id == updated.id) updated else it }
characters[characterId] = stored.copy(pokemon = party.toMutableList())
markDirty(characterId)
}
fun addMoney(characterId: Long, amount: Int) {
val stored = characters[characterId] ?: return
val newInfo = stored.info.copy(money = stored.info.money + amount)
@@ -243,66 +241,4 @@ constructor(
val stored = characters.remove(id) ?: return
charactersByUser.remove(stored.info.userId)
}
private fun starterSnivy(ownerId: Long, ot: String): Pokemon =
Pokemon(
id = entityIds.newMonsterId(),
ownerId = ownerId,
container = PokemonContainer.PARTY,
containerSlot = 0,
dexId = SNIVY_DEX_ID,
seed = 0,
ot = ot,
nickname = "",
level = 5,
hp = 20,
xp = 165,
eVs = EVs(),
iVs = IVs(),
moves =
listOf(
PokemonMove(33, 35),
PokemonMove(43, 30),
PokemonMove(0, 0),
PokemonMove(0, 0),
),
isShiny = false,
hasHiddenAbility = false,
isAlpha = false,
isSecret = false,
isFatefulEncounter = false,
isRaidEncounter = false,
caughtAt = LocalDateTime.now(),
)
private fun starterPatrat(ownerId: Long, ot: String): Pokemon =
Pokemon(
id = entityIds.newMonsterId(),
ownerId = ownerId,
container = PokemonContainer.PARTY,
containerSlot = 1,
dexId = PATRAT_DEX_ID,
seed = 0,
ot = ot,
nickname = "",
level = 3,
hp = 12,
xp = 27,
eVs = EVs(),
iVs = IVs(),
moves =
listOf(
PokemonMove(33, 35),
PokemonMove(0, 0),
PokemonMove(0, 0),
PokemonMove(0, 0),
),
isShiny = false,
hasHiddenAbility = false,
isAlpha = false,
isSecret = false,
isFatefulEncounter = false,
isRaidEncounter = false,
caughtAt = LocalDateTime.now(),
)
}
@@ -1,4 +1,5 @@
-- One character per dev account, each with the Snivy and Patrat starter party.
-- One character per dev account, each with a Bulbasaur and Rattata party. The species
-- registry only covers Gen 1 to 3, so the party must stay inside that range.
-- Only applied when db.seedDev is enabled, never in production.
-- Ids are fixed low values with the kind tag in the low 16 bits. Runtime ids from
-- EntityIdService carry a timestamp head, so they can never collide with these.
@@ -20,8 +21,8 @@ INSERT INTO pokemon (
move1_id, move1_pp, move2_id, move2_pp, caught_at
)
VALUES
(180224, 102400, 'PARTY', 0, 495, 0, 'Test', 5, 20, 165, 33, 35, 43, 30, CURRENT_TIMESTAMP),
(311296, 102400, 'PARTY', 1, 504, 0, 'Test', 3, 12, 27, 33, 35, 0, 0, CURRENT_TIMESTAMP),
(442368, 364544, 'PARTY', 0, 495, 0, 'Test2', 5, 20, 165, 33, 35, 43, 30, CURRENT_TIMESTAMP),
(507904, 364544, 'PARTY', 1, 504, 0, 'Test2', 3, 12, 27, 33, 35, 0, 0, CURRENT_TIMESTAMP)
(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)
ON CONFLICT (id) DO NOTHING;
@@ -172,6 +172,6 @@ class CharacterRepositoryIT :
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(495, 504)
seeded.single().pokemon.map { it.dexId } shouldBe listOf(1, 19)
}
})
@@ -1,13 +1,43 @@
package de.fiereu.openmmo.server.game.storage
import de.fiereu.openmmo.common.Pokemon
import de.fiereu.openmmo.common.enums.EVs
import de.fiereu.openmmo.common.enums.IVs
import de.fiereu.openmmo.common.enums.PokemonContainer
import de.fiereu.openmmo.server.game.testsupport.FakeCharacterRepository
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import java.time.LocalDateTime
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
private fun testPokemon(ownerId: Long): Pokemon =
Pokemon(
id = EntityIdService().newMonsterId(),
ownerId = ownerId,
container = PokemonContainer.PARTY,
containerSlot = 0,
dexId = 19,
seed = 0,
ot = "Ash",
nickname = "",
level = 3,
hp = 14,
xp = 27,
eVs = EVs(),
iVs = IVs(),
moves = listOf(),
isShiny = false,
hasHiddenAbility = false,
isAlpha = false,
isSecret = false,
isFatefulEncounter = false,
isRaidEncounter = false,
caughtAt = LocalDateTime.now(),
)
@OptIn(ExperimentalCoroutinesApi::class)
class CharacterStoreCacheTest :
FunSpec({
@@ -17,7 +47,7 @@ class CharacterStoreCacheTest :
val store = CharacterStore(repo, EntityIdService(), backgroundScope)
val created = store.createCharacter(1, "Ash")
repo.saved[created.info.id].shouldNotBeNull()
repo.saved[created.info.id]!!.pokemon.size shouldBe 2
repo.saved[created.info.id]!!.pokemon.size shouldBe 0
}
}
@@ -55,11 +85,32 @@ class CharacterStoreCacheTest :
val created = store.createCharacter(1, "Ash")
val before = store.getCharacter(created.info.id)!!.pokemon
val extra = before.first().copy(id = EntityIdService().newMonsterId(), containerSlot = 2)
store.addPokemon(created.info.id, extra)
store.addPokemon(created.info.id, testPokemon(created.info.id))
before.size shouldBe 2
store.getCharacter(created.info.id)!!.pokemon.size shouldBe 3
before.size shouldBe 0
store.getCharacter(created.info.id)!!.pokemon.size shouldBe 1
}
}
test("updatePokemon replaces the party entry by id and marks dirty") {
runTest {
val repo = FakeCharacterRepository()
val store = CharacterStore(repo, EntityIdService(), backgroundScope)
val created = store.createCharacter(1, "Ash")
val mon = testPokemon(created.info.id)
store.addPokemon(created.info.id, mon)
store.flushAll()
val before = store.getCharacter(created.info.id)!!.pokemon
store.updatePokemon(created.info.id, mon.copy(level = 4, hp = 11))
before.single().level shouldBe 3.toByte()
val after = store.getCharacter(created.info.id)!!.pokemon.single()
after.level shouldBe 4.toByte()
after.hp shouldBe 11.toShort()
store.flushAll()
repo.saved[created.info.id]!!.pokemon.single().level shouldBe 4.toByte()
}
}