feat(game): keep the appearance the client picks at character creation (#145)
This commit is contained in:
1 parent
0dab4b16fa
commit
0200858be9
14 files changed
+234
-18
No files matched your search
@@ -128,7 +128,15 @@ constructor(
|
||||
ctx.send(buildCharacterList(state.userId))
|
||||
return
|
||||
}
|
||||
characterStore.createCharacter(state.userId, name, gender, startingRegion)
|
||||
val appearance = event.packet.appearance
|
||||
characterStore.createCharacter(
|
||||
state.userId,
|
||||
name,
|
||||
gender,
|
||||
startingRegion,
|
||||
skins = appearance.toMap(),
|
||||
skinRegionSelectionIndex = appearance.regionSelectionIndex,
|
||||
)
|
||||
ctx.send(buildCharacterList(state.userId))
|
||||
}
|
||||
|
||||
@@ -167,7 +175,7 @@ constructor(
|
||||
characters.map { stored ->
|
||||
CharacterEntry(
|
||||
characterInfo = stored.info,
|
||||
skinSet = SkinSet(),
|
||||
skinSet = SkinSet(stored.info.skinRegionSelectionIndex, stored.skins),
|
||||
guildId = null,
|
||||
// Character selection shows the complete party.
|
||||
pokemon = stored.pokemon,
|
||||
@@ -295,7 +303,8 @@ constructor(
|
||||
log.info { "Sending LoadEntity for character '${info.name}'" }
|
||||
val facing = state.facingDirection
|
||||
val loadEntity =
|
||||
mapLoadService.createLoadEntity(info, facing, state.elevation, party = stored.pokemon)
|
||||
mapLoadService.createLoadEntity(
|
||||
info, facing, state.elevation, party = stored.pokemon, skins = stored.skins)
|
||||
ctx.send(loadEntity)
|
||||
|
||||
npcService.spawnNpcsForMap(
|
||||
|
||||
+4
-1
@@ -3,8 +3,10 @@ package de.fiereu.openmmo.server.game.services
|
||||
import de.fiereu.network.SessionContext
|
||||
import de.fiereu.openmmo.common.CharacterInfo
|
||||
import de.fiereu.openmmo.common.Pokemon
|
||||
import de.fiereu.openmmo.common.Skin
|
||||
import de.fiereu.openmmo.common.enums.Direction
|
||||
import de.fiereu.openmmo.common.enums.EntityStatus
|
||||
import de.fiereu.openmmo.common.enums.SkinSlot
|
||||
import de.fiereu.openmmo.maps.MapDef
|
||||
import de.fiereu.openmmo.maps.MapManager
|
||||
import de.fiereu.openmmo.net.game.codecs.SkinSet
|
||||
@@ -27,10 +29,11 @@ constructor(
|
||||
facing: Direction = Direction.DOWN,
|
||||
z: Int = 0,
|
||||
party: List<Pokemon> = emptyList(),
|
||||
skins: Map<SkinSlot, Skin> = emptyMap(),
|
||||
): LoadEntityPacket {
|
||||
return LoadEntityPacket(
|
||||
entityId = info.id,
|
||||
skin = SkinSet(),
|
||||
skin = SkinSet(info.skinRegionSelectionIndex, skins),
|
||||
name = info.name,
|
||||
regionId = info.positionRegionId.toInt(),
|
||||
bankId = info.positionBankId.toInt(),
|
||||
|
||||
+1
-1
@@ -92,6 +92,6 @@ constructor(
|
||||
val charId = state.characterId ?: return null
|
||||
val stored = characterStore.getCharacter(charId) ?: return null
|
||||
return mapLoadService.createLoadEntity(
|
||||
stored.info, state.facingDirection, party = stored.pokemon)
|
||||
stored.info, state.facingDirection, party = stored.pokemon, skins = stored.skins)
|
||||
}
|
||||
}
|
||||
+38
@@ -4,18 +4,22 @@ import de.fiereu.openmmo.common.CharacterInfo
|
||||
import de.fiereu.openmmo.common.DynamicWarp
|
||||
import de.fiereu.openmmo.common.Pokemon
|
||||
import de.fiereu.openmmo.common.PokemonMove
|
||||
import de.fiereu.openmmo.common.Skin
|
||||
import de.fiereu.openmmo.common.enums.Direction
|
||||
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.common.enums.SkinSlot
|
||||
import de.fiereu.openmmo.db.game.tables.records.CharacterFlagsRecord
|
||||
import de.fiereu.openmmo.db.game.tables.records.CharacterItemsRecord
|
||||
import de.fiereu.openmmo.db.game.tables.records.CharacterSkinsRecord
|
||||
import de.fiereu.openmmo.db.game.tables.records.CharacterVarsRecord
|
||||
import de.fiereu.openmmo.db.game.tables.records.CharactersRecord
|
||||
import de.fiereu.openmmo.db.game.tables.records.PokemonRecord
|
||||
import de.fiereu.openmmo.db.game.tables.references.CHARACTERS
|
||||
import de.fiereu.openmmo.db.game.tables.references.CHARACTER_FLAGS
|
||||
import de.fiereu.openmmo.db.game.tables.references.CHARACTER_ITEMS
|
||||
import de.fiereu.openmmo.db.game.tables.references.CHARACTER_SKINS
|
||||
import de.fiereu.openmmo.db.game.tables.references.CHARACTER_VARS
|
||||
import de.fiereu.openmmo.db.game.tables.references.POKEMON
|
||||
import javax.inject.Inject
|
||||
@@ -110,6 +114,12 @@ constructor(
|
||||
{ CHARACTER_VARS.CHARACTER_ID.eq(id).and(CHARACTER_VARS.VAR_KEY.eq(it)) },
|
||||
{ key, value -> CharacterVarsRecord(id, key, value) },
|
||||
)
|
||||
tx.writeDelta(
|
||||
CHARACTER_SKINS,
|
||||
rowDelta(previous?.skins.orEmpty(), current.skins),
|
||||
{ CHARACTER_SKINS.CHARACTER_ID.eq(id).and(CHARACTER_SKINS.SLOT.eq(it.name)) },
|
||||
{ slot, skin -> skin.toRecord(id, slot) },
|
||||
)
|
||||
}
|
||||
|
||||
private fun StoredCharacter?.monstersById(): Map<Long, Pokemon> =
|
||||
@@ -141,6 +151,10 @@ constructor(
|
||||
.set(CHARACTER_VARS.VAR_VALUE, value)
|
||||
.execute()
|
||||
}
|
||||
if (stored.skins.isNotEmpty()) {
|
||||
tx.batchInsert(stored.skins.map { (slot, skin) -> skin.toRecord(stored.info.id, slot) })
|
||||
.execute()
|
||||
}
|
||||
}
|
||||
|
||||
/** Loads pokemon and items for all rows in one query each instead of per character. */
|
||||
@@ -172,6 +186,12 @@ constructor(
|
||||
.fetch()
|
||||
.groupBy({ it.characterId }, { it.varKey to it.varValue })
|
||||
.mapValues { (_, pairs) -> pairs.toMap() }
|
||||
val skinsByOwner: Map<Long, Map<SkinSlot, Skin>> =
|
||||
dsl.selectFrom(CHARACTER_SKINS)
|
||||
.where(CHARACTER_SKINS.CHARACTER_ID.`in`(ids))
|
||||
.fetch()
|
||||
.groupBy({ it.characterId }, { it.toSkin() })
|
||||
.mapValues { (_, skins) -> skins.associateBy { it.slot } }
|
||||
return rows.map { row ->
|
||||
val monsters = monstersByOwner[row.id].orEmpty()
|
||||
val (party, pc) = monsters.partition { it.container == PokemonContainer.PARTY }
|
||||
@@ -182,6 +202,7 @@ constructor(
|
||||
items = itemsByOwner[row.id].orEmpty().toMutableMap(),
|
||||
storyFlags = flagsByOwner[row.id].orEmpty().toMutableSet(),
|
||||
storyVars = varsByOwner[row.id].orEmpty().toMutableMap(),
|
||||
skins = skinsByOwner[row.id].orEmpty(),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -193,6 +214,7 @@ constructor(
|
||||
name = name,
|
||||
namePrefix = namePrefix,
|
||||
rivalSex = rivalSex.toShort(),
|
||||
skinRegionSelectionIndex = skinRegionSelectionIndex.toShort(),
|
||||
lastLogin = lastLogin,
|
||||
createdAt = createdAt,
|
||||
money = money,
|
||||
@@ -227,6 +249,7 @@ constructor(
|
||||
namePrefix = namePrefix ?: "",
|
||||
userId = userId,
|
||||
rivalSex = rivalSex.toByte(),
|
||||
skinRegionSelectionIndex = skinRegionSelectionIndex?.toInt() ?: 0,
|
||||
lastLogin = lastLogin,
|
||||
createdAt = createdAt,
|
||||
money = money,
|
||||
@@ -267,6 +290,21 @@ constructor(
|
||||
)
|
||||
}
|
||||
|
||||
private fun Skin.toRecord(characterId: Long, slot: SkinSlot): CharacterSkinsRecord =
|
||||
CharacterSkinsRecord(
|
||||
characterId = characterId,
|
||||
slot = slot.name,
|
||||
skinType = type?.toShort(),
|
||||
skinColor = color?.toShort(),
|
||||
)
|
||||
|
||||
private fun CharacterSkinsRecord.toSkin(): Skin =
|
||||
Skin(
|
||||
slot = SkinSlot.valueOf(slot),
|
||||
type = skinType?.toUShort(),
|
||||
color = skinColor?.toUByte(),
|
||||
)
|
||||
|
||||
private fun Pokemon.toRecord(): PokemonRecord =
|
||||
PokemonRecord(
|
||||
id = id,
|
||||
|
||||
@@ -3,9 +3,11 @@ package de.fiereu.openmmo.server.game.storage
|
||||
import de.fiereu.openmmo.common.CharacterInfo
|
||||
import de.fiereu.openmmo.common.DynamicWarp
|
||||
import de.fiereu.openmmo.common.Pokemon
|
||||
import de.fiereu.openmmo.common.Skin
|
||||
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.common.enums.SkinSlot
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import java.time.LocalDateTime
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
@@ -38,6 +40,7 @@ data class StoredCharacter(
|
||||
// Keys are opaque strings supplied by the content layer, so the store stays game agnostic.
|
||||
val storyFlags: MutableSet<String> = mutableSetOf(),
|
||||
val storyVars: MutableMap<String, Int> = mutableMapOf(),
|
||||
val skins: Map<SkinSlot, Skin> = emptyMap(),
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -73,6 +76,8 @@ constructor(
|
||||
name: String,
|
||||
gender: CharacterGender,
|
||||
startingRegion: Region,
|
||||
skins: Map<SkinSlot, Skin> = emptyMap(),
|
||||
skinRegionSelectionIndex: Int = 0,
|
||||
): StoredCharacter {
|
||||
val female = gender == CharacterGender.FEMALE
|
||||
val start = NewGameStarts.forRegion(startingRegion, female)
|
||||
@@ -86,6 +91,7 @@ constructor(
|
||||
userId = userId,
|
||||
// This historical field stores the player's gender.
|
||||
rivalSex = gender.wireValue,
|
||||
skinRegionSelectionIndex = skinRegionSelectionIndex,
|
||||
lastLogin = now,
|
||||
createdAt = now,
|
||||
money = 30000,
|
||||
@@ -114,6 +120,7 @@ constructor(
|
||||
mutableMapOf(),
|
||||
storyFlags = start.storyFlags.toMutableSet(),
|
||||
storyVars = start.storyVars.toMutableMap(),
|
||||
skins = skins.toMap(),
|
||||
)
|
||||
repository.insertAggregate(stored)
|
||||
characters[id] = stored
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
-- The appearance picked in the character creator. One row per occupied slot, keyed by the SkinSlot
|
||||
-- enum name. Renaming an enum entry breaks stored data. Type and color are the packed 10 and 6 bit
|
||||
-- fields the client sends.
|
||||
|
||||
CREATE TABLE character_skins (
|
||||
character_id BIGINT NOT NULL REFERENCES characters (id) ON DELETE CASCADE,
|
||||
slot VARCHAR(16) NOT NULL,
|
||||
skin_type SMALLINT,
|
||||
skin_color SMALLINT,
|
||||
PRIMARY KEY (character_id, slot)
|
||||
);
|
||||
|
||||
ALTER TABLE characters
|
||||
ADD COLUMN skin_region_selection_index SMALLINT NOT NULL DEFAULT 0;
|
||||
+40
@@ -4,10 +4,12 @@ import de.fiereu.openmmo.common.CharacterInfo
|
||||
import de.fiereu.openmmo.common.DynamicWarp
|
||||
import de.fiereu.openmmo.common.Pokemon
|
||||
import de.fiereu.openmmo.common.PokemonMove
|
||||
import de.fiereu.openmmo.common.Skin
|
||||
import de.fiereu.openmmo.common.enums.Direction
|
||||
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.common.enums.SkinSlot
|
||||
import de.fiereu.openmmo.common.test.DockerAvailable
|
||||
import de.fiereu.openmmo.db.game.tables.references.CHARACTER_ITEMS
|
||||
import de.fiereu.openmmo.db.game.tables.references.POKEMON
|
||||
@@ -240,6 +242,44 @@ class CharacterRepositoryIT :
|
||||
loaded.items shouldBe stored.items
|
||||
}
|
||||
|
||||
test("insert and load round-trips the appearance") {
|
||||
val stored =
|
||||
aggregate(userId = 97).let {
|
||||
it.copy(
|
||||
info = it.info.copy(skinRegionSelectionIndex = 3),
|
||||
skins =
|
||||
mapOf(
|
||||
SkinSlot.HAIR to Skin(SkinSlot.HAIR, 4u, 24u),
|
||||
SkinSlot.EYES to Skin(SkinSlot.EYES, 15u, 0u),
|
||||
),
|
||||
)
|
||||
}
|
||||
repository.insertAggregate(stored)
|
||||
|
||||
val loaded = repository.loadById(stored.info.id).shouldNotBeNull()
|
||||
loaded.info.skinRegionSelectionIndex shouldBe 3
|
||||
loaded.skins shouldBe stored.skins
|
||||
}
|
||||
|
||||
test("saveChanges rewrites a changed slot and drops a removed one") {
|
||||
val stored =
|
||||
aggregate(userId = 98).let {
|
||||
it.copy(
|
||||
skins =
|
||||
mapOf(
|
||||
SkinSlot.HAIR to Skin(SkinSlot.HAIR, 4u, 24u),
|
||||
SkinSlot.HAT to Skin(SkinSlot.HAT, 2u, 7u),
|
||||
))
|
||||
}
|
||||
repository.insertAggregate(stored)
|
||||
|
||||
val current = stored.copy(skins = mapOf(SkinSlot.HAIR to Skin(SkinSlot.HAIR, 9u, 24u)))
|
||||
repository.saveChanges(stored, current)
|
||||
|
||||
val loaded = repository.loadById(stored.info.id).shouldNotBeNull()
|
||||
loaded.skins shouldBe mapOf(SkinSlot.HAIR to Skin(SkinSlot.HAIR, 9u, 24u))
|
||||
}
|
||||
|
||||
test("loadById returns null for an unknown id") {
|
||||
repository.loadById(entityIds.newCharacterId()).shouldBeNull()
|
||||
}
|
||||
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package de.fiereu.openmmo.server.game.storage
|
||||
|
||||
import de.fiereu.openmmo.common.Skin
|
||||
import de.fiereu.openmmo.common.enums.CharacterGender
|
||||
import de.fiereu.openmmo.common.enums.Region
|
||||
import de.fiereu.openmmo.common.enums.SkinSlot
|
||||
import de.fiereu.openmmo.server.game.testsupport.FakeCharacterRepository
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.maps.shouldBeEmpty
|
||||
import io.kotest.matchers.shouldBe
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
|
||||
private val CREATOR_SKINS =
|
||||
mapOf(
|
||||
SkinSlot.HAIR to Skin(SkinSlot.HAIR, 4u, 24u),
|
||||
SkinSlot.EYES to Skin(SkinSlot.EYES, 15u, 0u),
|
||||
SkinSlot.TOP to Skin(SkinSlot.TOP, 3u, 2u),
|
||||
)
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class CharacterSkinsTest :
|
||||
FunSpec({
|
||||
test("creation keeps the appearance the client picked") {
|
||||
runTest {
|
||||
val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope)
|
||||
|
||||
val stored =
|
||||
store.createCharacter(
|
||||
1,
|
||||
"Red",
|
||||
CharacterGender.MALE,
|
||||
Region.KANTO,
|
||||
skins = CREATOR_SKINS,
|
||||
skinRegionSelectionIndex = 3,
|
||||
)
|
||||
|
||||
val cached = store.getCharacter(stored.info.id)!!
|
||||
cached.skins shouldBe CREATOR_SKINS
|
||||
cached.info.skinRegionSelectionIndex shouldBe 3
|
||||
}
|
||||
}
|
||||
|
||||
test("creation writes the appearance out with the rest of the aggregate") {
|
||||
runTest {
|
||||
val repository = FakeCharacterRepository()
|
||||
val store = CharacterStore(repository, EntityIdService(), backgroundScope)
|
||||
|
||||
val id =
|
||||
store
|
||||
.createCharacter(
|
||||
1, "Red", CharacterGender.MALE, Region.KANTO, skins = CREATOR_SKINS)
|
||||
.info
|
||||
.id
|
||||
|
||||
repository.saved[id]!!.skins shouldBe CREATOR_SKINS
|
||||
}
|
||||
}
|
||||
|
||||
test("a character created without an appearance has no slots") {
|
||||
runTest {
|
||||
val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope)
|
||||
|
||||
val stored = store.createCharacter(1, "Red", CharacterGender.MALE, Region.KANTO)
|
||||
|
||||
stored.skins.shouldBeEmpty()
|
||||
stored.info.skinRegionSelectionIndex shouldBe 0
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user