feat(game): open the mart shop and key items by the ids the client reads (#151)
This commit is contained in:
1 parent
a69fd0d07f
commit
499d20fdb3
46 files changed
+7863
-269
No files matched your search
@@ -15,6 +15,7 @@ import de.fiereu.openmmo.net.game.packets.CreateCharacterPacket
|
||||
import de.fiereu.openmmo.net.game.packets.DeleteCharacterPacket
|
||||
import de.fiereu.openmmo.net.game.packets.DialogChoicePacket
|
||||
import de.fiereu.openmmo.net.game.packets.EntityInteractPacket
|
||||
import de.fiereu.openmmo.net.game.packets.ExchangeItemRequestPacket
|
||||
import de.fiereu.openmmo.net.game.packets.FaceDirectionPacket
|
||||
import de.fiereu.openmmo.net.game.packets.JoinPacket
|
||||
import de.fiereu.openmmo.net.game.packets.KeepAlivePacket
|
||||
@@ -26,6 +27,7 @@ import de.fiereu.openmmo.net.game.packets.RequestCharactersPacket
|
||||
import de.fiereu.openmmo.net.game.packets.RequestPlayerPacket
|
||||
import de.fiereu.openmmo.net.game.packets.RequestSocialProfilePacket
|
||||
import de.fiereu.openmmo.net.game.packets.SelectCharacterPacket
|
||||
import de.fiereu.openmmo.net.game.packets.ShopSellRequestPacket
|
||||
import de.fiereu.openmmo.net.game.packets.TileInteractPacket
|
||||
import de.fiereu.openmmo.net.game.packets.UnblockPlayerPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleActionPacket
|
||||
@@ -69,6 +71,7 @@ import de.fiereu.openmmo.server.game.services.LoginService
|
||||
import de.fiereu.openmmo.server.game.services.MovementService
|
||||
import de.fiereu.openmmo.server.game.services.MultiplayerService
|
||||
import de.fiereu.openmmo.server.game.services.PresenceService
|
||||
import de.fiereu.openmmo.server.game.services.ShopService
|
||||
import de.fiereu.openmmo.server.game.services.SocialService
|
||||
import de.fiereu.openmmo.server.game.services.command.ChatCommandService
|
||||
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
||||
@@ -95,6 +98,7 @@ constructor(
|
||||
private val guildService: GuildService,
|
||||
private val battleService: BattleService,
|
||||
private val chatCommandService: ChatCommandService,
|
||||
private val shopService: ShopService,
|
||||
private val scriptRunner: ScriptRunner,
|
||||
private val sessionRegistry: SessionRegistry,
|
||||
private val characterStore: CharacterStore,
|
||||
@@ -116,6 +120,8 @@ constructor(
|
||||
onSuspend<TileInteractPacket> { event -> interactionService.onTileInteract(event) }
|
||||
onSuspend<DialogActionResponsePacket> { event -> dialogService.onInteractive(event) }
|
||||
onSuspend<DialogChoicePacket> { event -> dialogService.onDialogChoice(event) }
|
||||
onSuspend<ExchangeItemRequestPacket> { event -> shopService.onBuy(event) }
|
||||
onSuspend<ShopSellRequestPacket> { event -> shopService.onSell(event) }
|
||||
|
||||
on<AddFriendPacket> { event -> socialService.onAddFriend(event) }
|
||||
on<RemoveFriendPacket> { event -> socialService.onRemoveFriend(event) }
|
||||
|
||||
@@ -5,6 +5,7 @@ import de.fiereu.openmmo.common.DynamicWarp
|
||||
import de.fiereu.openmmo.common.dialog.DialogLine
|
||||
import de.fiereu.openmmo.common.enums.Direction
|
||||
import de.fiereu.openmmo.common.enums.Region
|
||||
import de.fiereu.openmmo.items.ItemDef
|
||||
import de.fiereu.openmmo.maps.MapManager
|
||||
import de.fiereu.openmmo.net.game.packets.dialog.TextPokemonSpeciesArg
|
||||
import de.fiereu.openmmo.server.game.battle.BattleResult
|
||||
@@ -14,6 +15,7 @@ import de.fiereu.openmmo.server.game.services.DialogService
|
||||
import de.fiereu.openmmo.server.game.services.MapEntryScripts
|
||||
import de.fiereu.openmmo.server.game.services.ScriptMovementService
|
||||
import de.fiereu.openmmo.server.game.services.ScriptWarpService
|
||||
import de.fiereu.openmmo.server.game.services.ShopService
|
||||
import de.fiereu.openmmo.server.game.services.StoryClientState
|
||||
import de.fiereu.openmmo.server.game.services.StoryPlayerService
|
||||
import de.fiereu.openmmo.server.game.services.StoryService
|
||||
@@ -36,6 +38,7 @@ internal constructor(
|
||||
internal val characters: CharacterStore? = null,
|
||||
private val maps: MapManager? = null,
|
||||
private val entryScripts: MapEntryScripts? = null,
|
||||
private val shops: ShopService? = null,
|
||||
) {
|
||||
private val characterId: Long?
|
||||
get() = state.characterId
|
||||
@@ -135,11 +138,18 @@ internal constructor(
|
||||
|
||||
fun healParty() = checkNotNull(player) { STORY_PLAYER_UNAVAILABLE }.healParty(session, state)
|
||||
|
||||
suspend fun giveItem(itemId: Int, quantity: Int = 1): Boolean =
|
||||
checkNotNull(player) { STORY_PLAYER_UNAVAILABLE }.giveItem(session, state, itemId, quantity)
|
||||
suspend fun giveItem(item: ItemDef, quantity: Int = 1): Boolean =
|
||||
checkNotNull(player) { STORY_PLAYER_UNAVAILABLE }.giveItem(session, state, item, quantity)
|
||||
|
||||
/** Take an item back out of the bag, the decomp removeitem. False when the bag lacks it. */
|
||||
suspend fun takeItem(itemId: Int, quantity: Int = 1): Boolean = giveItem(itemId, -quantity)
|
||||
suspend fun takeItem(item: ItemDef, quantity: Int = 1): Boolean = giveItem(item, -quantity)
|
||||
|
||||
/**
|
||||
* Opens the mart window on [items], the decomp pokemart. It does not wait: the player shops while
|
||||
* the script ends, because nothing in the capture tells the server when the window closed.
|
||||
*/
|
||||
fun pokemart(vararg items: ItemDef) =
|
||||
checkNotNull(shops) { "Shop service is unavailable" }.open(session, entityId, items.toList())
|
||||
|
||||
/** Run a non-catchable, non-escapable story battle and wait for its result. */
|
||||
suspend fun battle(dexId: Int, level: Int, vararg moveIds: Int): BattleResult =
|
||||
|
||||
@@ -7,6 +7,7 @@ import de.fiereu.openmmo.server.game.services.DialogService
|
||||
import de.fiereu.openmmo.server.game.services.MapEntryScripts
|
||||
import de.fiereu.openmmo.server.game.services.ScriptMovementService
|
||||
import de.fiereu.openmmo.server.game.services.ScriptWarpService
|
||||
import de.fiereu.openmmo.server.game.services.ShopService
|
||||
import de.fiereu.openmmo.server.game.services.StoryPlayerService
|
||||
import de.fiereu.openmmo.server.game.services.StoryService
|
||||
import de.fiereu.openmmo.server.game.session.PlayerState
|
||||
@@ -44,6 +45,7 @@ constructor(
|
||||
private val characterStore: CharacterStore,
|
||||
private val mapManager: MapManager,
|
||||
private val entryScripts: MapEntryScripts,
|
||||
private val shopService: ShopService,
|
||||
) {
|
||||
fun run(session: SessionContext, state: PlayerState, script: Script, entityId: Long) =
|
||||
runAll(session, state, listOf(script), entityId)
|
||||
@@ -94,6 +96,7 @@ constructor(
|
||||
characterStore,
|
||||
mapManager,
|
||||
entryScripts,
|
||||
shopService,
|
||||
)
|
||||
scope.launch {
|
||||
var finished = false
|
||||
|
||||
+2
-2
@@ -1,6 +1,7 @@
|
||||
package de.fiereu.openmmo.server.game.script.generated.hoenn
|
||||
|
||||
import de.fiereu.openmmo.dialog.generated.hoenn.RustboroCity_Gym
|
||||
import de.fiereu.openmmo.items.generated.Items
|
||||
import de.fiereu.openmmo.server.game.battle.BattleResult
|
||||
import de.fiereu.openmmo.server.game.script.Script
|
||||
import de.fiereu.openmmo.server.game.script.ScriptContext
|
||||
@@ -9,7 +10,6 @@ import de.fiereu.openmmo.story.generated.hoenn.HoennVars
|
||||
|
||||
private const val GEODUDE = 74
|
||||
private const val NOSEPASS = 299
|
||||
private const val TM39_ROCK_TOMB_ITEM = 5327
|
||||
|
||||
/**
|
||||
* Not ported yet. Decomp body:
|
||||
@@ -52,7 +52,7 @@ internal object RustboroCity_Gym_EventScript_Roxanne : Script {
|
||||
}
|
||||
|
||||
private suspend fun giveRockTomb(ctx: ScriptContext) {
|
||||
if (ctx.giveItem(TM39_ROCK_TOMB_ITEM)) {
|
||||
if (ctx.giveItem(Items.TM39)) {
|
||||
ctx.setFlag(HoennFlags.FLAG_RECEIVED_TM_ROCK_TOMB)
|
||||
ctx.say(RustboroCity_Gym.ExplainRockTomb)
|
||||
}
|
||||
|
||||
+1
-1
@@ -265,7 +265,7 @@ internal object PalletTown_ProfessorOaksLab_EventScript_ProfOak : Script {
|
||||
private suspend fun receiveDexScene(ctx: ScriptContext) {
|
||||
ctx.say(PalletTown_ProfessorOaksLab.OakHaveSomethingForMe)
|
||||
ctx.sign(PalletTown_ProfessorOaksLab.DeliveredOaksParcel)
|
||||
ctx.takeItem(Items.OAKS_PARCEL)
|
||||
ctx.takeItem(Items.PARCEL)
|
||||
ctx.say(PalletTown_ProfessorOaksLab.OakCustomBallIOrdered)
|
||||
|
||||
ctx.sign(PalletTown_ProfessorOaksLab.RivalGramps)
|
||||
|
||||
+3
-3
@@ -60,16 +60,16 @@ internal object ViridianCity_EventScript_TutorialTriggerRight : Script {
|
||||
}
|
||||
}
|
||||
|
||||
/** The old man's catching demonstration, which ends with him handing over the Teachy TV. */
|
||||
private suspend fun tutorialBattle(ctx: ScriptContext) {
|
||||
ctx.sayNpc(LOCALID_TUTORIAL_MAN, ViridianCity.ShowYouHowToCatchMons)
|
||||
// TODO Play the catching tutorial battle
|
||||
// The decomp runs StartOldManTutorialBattle, a scripted battle the old man fights for the
|
||||
// player. Scripted battles always use the player's own party today, so the demonstration is
|
||||
// skipped and only its reward is given.
|
||||
// skipped.
|
||||
ctx.sayNpc(LOCALID_TUTORIAL_MAN, ViridianCity.ThatWasEducationalTakeThis)
|
||||
ctx.setVar(KantoVars.VAR_MAP_SCENE_VIRIDIAN_CITY_OLD_MAN, 2)
|
||||
ctx.giveItem(Items.TEACHY_TV)
|
||||
// The decomp hands over a Teachy TV here. PokeMMO has no such item, so the reward is dropped
|
||||
// rather than substituted, and the lines around it are all the player gets.
|
||||
ctx.sayNpc(LOCALID_TUTORIAL_MAN, ViridianCity.WatchThatToLearnBasics)
|
||||
}
|
||||
|
||||
|
||||
+4
-3
@@ -1,5 +1,6 @@
|
||||
package de.fiereu.openmmo.server.game.script.generated.kanto
|
||||
|
||||
import de.fiereu.openmmo.dialog.generated.kanto.Misc
|
||||
import de.fiereu.openmmo.dialog.generated.kanto.ViridianCity_Mart
|
||||
import de.fiereu.openmmo.items.generated.Items
|
||||
import de.fiereu.openmmo.server.game.script.MovementStep.FACE_DOWN
|
||||
@@ -23,7 +24,7 @@ internal object ViridianCity_Mart_EventScript_ParcelScene : Script {
|
||||
)
|
||||
ctx.sayNpc(LOCALID_MART_CLERK, ViridianCity_Mart.TakeThisToProfOak)
|
||||
ctx.setVar(KantoVars.VAR_MAP_SCENE_VIRIDIAN_CITY_MART, 1)
|
||||
ctx.giveItem(Items.OAKS_PARCEL)
|
||||
ctx.giveItem(Items.PARCEL)
|
||||
ctx.sign(ViridianCity_Mart.ReceivedOaksParcelFromClerk)
|
||||
ctx.setVar(KantoVars.VAR_MAP_SCENE_PALLET_TOWN_PROFESSOR_OAKS_LAB, 5)
|
||||
}
|
||||
@@ -34,8 +35,8 @@ internal object ViridianCity_Mart_EventScript_Clerk : Script {
|
||||
if (ctx.getVar(KantoVars.VAR_MAP_SCENE_VIRIDIAN_CITY_MART) == 1) {
|
||||
return ctx.say(ViridianCity_Mart.SayHiToOakForMe)
|
||||
}
|
||||
// TODO Open the mart shop
|
||||
// The decomp runs pokemart with the shop's item list. Buying and selling needs a shop verb.
|
||||
ctx.say(Misc.Text_MayIHelpYou)
|
||||
ctx.pokemart(Items.POKE_BALL, Items.POTION, Items.ANTIDOTE, Items.PARLYZ_HEAL)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import de.fiereu.openmmo.common.enums.BattleAction
|
||||
import de.fiereu.openmmo.common.enums.IVs
|
||||
import de.fiereu.openmmo.common.enums.PokemonContainer
|
||||
import de.fiereu.openmmo.common.enums.Region
|
||||
import de.fiereu.openmmo.items.ItemRegistry
|
||||
import de.fiereu.openmmo.items.generated.Items
|
||||
import de.fiereu.openmmo.moves.MoveRegistry
|
||||
import de.fiereu.openmmo.net.game.packets.MapLoadedAckPacket
|
||||
@@ -43,8 +44,6 @@ import javax.inject.Singleton
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
private val POKE_BALL_ITEM: Short = Items.POKE_BALL.toShort()
|
||||
|
||||
/**
|
||||
* A prompt waiting for its answer, kept after the battle ends. A trainer battle can raise one
|
||||
* monster past a level more than once, so these are held per monster rather than per player.
|
||||
@@ -75,8 +74,11 @@ constructor(
|
||||
private val speciesRegistry: SpeciesRegistry,
|
||||
private val moveRegistry: MoveRegistry,
|
||||
private val trainers: TrainerRegistry,
|
||||
private val items: ItemRegistry,
|
||||
) {
|
||||
|
||||
private val pokeBallItemId: Short by lazy { items.idOf(Items.POKE_BALL).toShort() }
|
||||
|
||||
private val pendingLearns = ConcurrentHashMap<Long, PendingMoveLearn>()
|
||||
|
||||
fun onBattlePacket(event: PacketEvent<*>) {
|
||||
@@ -425,7 +427,7 @@ constructor(
|
||||
battle.session.send(
|
||||
BattleListEventPacket(
|
||||
kind = 0,
|
||||
value = POKE_BALL_ITEM,
|
||||
value = pokeBallItemId,
|
||||
subKind = 4,
|
||||
detail = BattleListEventDetail(listType = 1, value = 1),
|
||||
),
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.network.PacketEvent
|
||||
import de.fiereu.network.SessionContext
|
||||
import de.fiereu.openmmo.items.ItemDef
|
||||
import de.fiereu.openmmo.items.ItemRegistry
|
||||
import de.fiereu.openmmo.net.game.packets.ExchangeItemRequestPacket
|
||||
import de.fiereu.openmmo.net.game.packets.LocalCharacterDeltaPacket
|
||||
import de.fiereu.openmmo.net.game.packets.ShopCatalogPacket
|
||||
import de.fiereu.openmmo.net.game.packets.ShopItem
|
||||
import de.fiereu.openmmo.net.game.packets.ShopSellRequestPacket
|
||||
import de.fiereu.openmmo.server.game.session.OPEN_SHOP
|
||||
import de.fiereu.openmmo.server.game.session.OpenShop
|
||||
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
||||
import de.fiereu.openmmo.server.game.storage.CharacterStore
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
// From the captures: every mart carries the same shop id, and a shelf the player cannot empty.
|
||||
private const val MART_SHOP_ID: Short = 103
|
||||
private const val ENDLESS_SHELF: Short = 0x7FFF
|
||||
// A bag stack id is the item id shifted up, with the item entity tag in the low bits.
|
||||
private const val ITEM_ENTITY_SHIFT = 16
|
||||
|
||||
@Singleton
|
||||
class ShopService
|
||||
@Inject
|
||||
constructor(
|
||||
private val characterStore: CharacterStore,
|
||||
private val items: ItemRegistry,
|
||||
) {
|
||||
|
||||
fun open(session: SessionContext, npcEntityId: Long, shelf: List<ItemDef>) {
|
||||
// Deliberately the item table's price, until we know where the real one comes from. The
|
||||
// mart_catalog capture charges 200 for a Potion where the table says 300.
|
||||
val offered =
|
||||
shelf.mapNotNull { item ->
|
||||
val id = items.idOrNull(item)
|
||||
if (id == null) {
|
||||
log.warn { "Shop offered ${item.name}, which has no id in this build" }
|
||||
null
|
||||
} else {
|
||||
item to ShopItem(id.toShort(), 1, ENDLESS_SHELF, item.price)
|
||||
}
|
||||
}
|
||||
val state = session.attributes[PLAYER_STATE] ?: return
|
||||
session.attributes[OPEN_SHOP] =
|
||||
OpenShop(state.regionId, state.bankId, state.mapId, offered.toMap())
|
||||
session.send(ShopCatalogPacket.open(MART_SHOP_ID, npcEntityId, offered.map { it.second }))
|
||||
}
|
||||
|
||||
/** The shelf, if one is open and the player is still standing where they opened it. */
|
||||
private fun openShelf(session: SessionContext): Map<ItemDef, ShopItem>? {
|
||||
val open = session.attributes[OPEN_SHOP] ?: return null
|
||||
val state = session.attributes[PLAYER_STATE] ?: return null
|
||||
if (open.regionId != state.regionId ||
|
||||
open.bankId != state.bankId ||
|
||||
open.mapId != state.mapId) {
|
||||
session.attributes.remove(OPEN_SHOP)
|
||||
return null
|
||||
}
|
||||
return open.shelf
|
||||
}
|
||||
|
||||
suspend fun onBuy(event: PacketEvent<ExchangeItemRequestPacket>) {
|
||||
val session = event.session
|
||||
val shelf = openShelf(session) ?: return
|
||||
val charId = session.attributes[PLAYER_STATE]?.characterId ?: return
|
||||
val quantity = event.packet.quantity.toInt()
|
||||
val item = items.get(event.packet.itemEntityRef.toInt())
|
||||
val offer = item?.let { shelf[it] }
|
||||
if (offer == null || quantity <= 0) {
|
||||
log.warn {
|
||||
"char=$charId asked for $quantity of ${event.packet.itemEntityRef}, not on this shelf"
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
val itemId = offer.itemId.toInt()
|
||||
// A quantity is a signed Short, so a dear enough item would wrap the product negative.
|
||||
val cost = offer.price.toLong() * quantity
|
||||
val stored = characterStore.getCharacter(charId) ?: return
|
||||
if (stored.info.money < cost) {
|
||||
log.info { "char=$charId cannot afford $quantity of ${item.name} at $cost" }
|
||||
return
|
||||
}
|
||||
|
||||
// The item goes in first, so a bag that refuses it costs the player nothing.
|
||||
if (!characterStore.addItem(charId, itemId, quantity)) return
|
||||
if (!characterStore.addMoney(charId, -cost.toInt())) {
|
||||
if (!characterStore.addItem(charId, itemId, -quantity)) {
|
||||
log.error { "char=$charId keeps $quantity ${item.name} unpaid, the refund did not persist" }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
log.info { "char=$charId bought $quantity ${item.name} for $cost" }
|
||||
sendBagAndMoney(session, charId, itemId)
|
||||
}
|
||||
|
||||
suspend fun onSell(event: PacketEvent<ShopSellRequestPacket>) {
|
||||
val session = event.session
|
||||
if (openShelf(session) == null) return
|
||||
val charId = session.attributes[PLAYER_STATE]?.characterId ?: return
|
||||
val quantity = event.packet.quantity.toInt()
|
||||
val stored = characterStore.getCharacter(charId) ?: return
|
||||
// The client sells the bag stack by the id the bag packet gave it, not by item id.
|
||||
val itemId = (event.packet.itemEntityId shr ITEM_ENTITY_SHIFT).toInt()
|
||||
val held = stored.items[itemId] ?: 0
|
||||
if (quantity <= 0 || held < quantity) {
|
||||
log.warn { "char=$charId offered $quantity of stack $itemId, holding $held" }
|
||||
return
|
||||
}
|
||||
|
||||
val item = items.get(itemId)
|
||||
// Anything halving to nothing, a key item above all, would be deleted and paid nothing for.
|
||||
if (item == null || item.price < 2) {
|
||||
log.warn { "char=$charId offered stack $itemId, which a mart does not buy" }
|
||||
return
|
||||
}
|
||||
|
||||
val paid = item.price / 2 * quantity
|
||||
if (!characterStore.addItem(charId, itemId, -quantity)) return
|
||||
if (!characterStore.addMoney(charId, paid)) {
|
||||
if (!characterStore.addItem(charId, itemId, quantity)) {
|
||||
log.error { "char=$charId lost $quantity of stack $itemId, the refund did not persist" }
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
log.info { "char=$charId sold $quantity of ${item.name} for $paid" }
|
||||
sendBagAndMoney(session, charId, itemId)
|
||||
}
|
||||
|
||||
private fun sendBagAndMoney(session: SessionContext, charId: Long, itemId: Int) {
|
||||
val after = characterStore.getCharacter(charId) ?: return
|
||||
session.send(LocalCharacterDeltaPacket(money = after.info.money))
|
||||
session.send(itemStackUpdatePacket(itemId, after.items[itemId] ?: 0))
|
||||
}
|
||||
}
|
||||
+28
-4
@@ -5,9 +5,13 @@ import de.fiereu.openmmo.common.MAX_PARTY_SIZE
|
||||
import de.fiereu.openmmo.common.Pokemon
|
||||
import de.fiereu.openmmo.common.PokemonMove
|
||||
import de.fiereu.openmmo.common.enums.PokemonContainer
|
||||
import de.fiereu.openmmo.items.ItemDef
|
||||
import de.fiereu.openmmo.items.ItemRegistry
|
||||
import de.fiereu.openmmo.moves.MoveRegistry
|
||||
import de.fiereu.openmmo.net.game.packets.PokemonContainerPacket
|
||||
import de.fiereu.openmmo.net.game.packets.SocialListEntryAddPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleAddPokemon
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleSideAddPokemonPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.ItemStack
|
||||
import de.fiereu.openmmo.net.game.packets.battle.itemStacksPacket
|
||||
import de.fiereu.openmmo.pokemon.SpeciesRegistry
|
||||
@@ -29,6 +33,7 @@ constructor(
|
||||
private val pokemonFactory: WildMonFactory,
|
||||
private val species: SpeciesRegistry,
|
||||
private val moves: MoveRegistry,
|
||||
private val items: ItemRegistry,
|
||||
) {
|
||||
|
||||
/** Gives a story Pokemon and syncs it. */
|
||||
@@ -94,13 +99,13 @@ constructor(
|
||||
suspend fun giveItem(
|
||||
session: SessionContext,
|
||||
state: PlayerState,
|
||||
itemId: Int,
|
||||
item: ItemDef,
|
||||
quantity: Int
|
||||
): Boolean {
|
||||
val characterId = state.characterId ?: return false
|
||||
if (!characters.addItem(characterId, itemId, quantity)) return false
|
||||
val items = characters.getCharacter(characterId)?.items ?: return false
|
||||
session.send(storyItemStacksPacket(items))
|
||||
if (!characters.addItem(characterId, items.idOf(item), quantity)) return false
|
||||
val bag: Map<Int, Int> = characters.getCharacter(characterId)?.items ?: return false
|
||||
session.send(storyItemStacksPacket(bag))
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -127,4 +132,23 @@ fun storyItemStacksPacket(items: Map<Int, Int>) =
|
||||
)
|
||||
})
|
||||
|
||||
/**
|
||||
* A bag stack, not a monster. The open shop window only refreshes its count when the update arrives
|
||||
* as this single stack rather than as a whole new bag.
|
||||
*/
|
||||
fun itemStackUpdatePacket(itemId: Int, quantity: Int) =
|
||||
BattleSideAddPokemonPacket(
|
||||
side = 1,
|
||||
pokemon =
|
||||
BattleAddPokemon(
|
||||
entityId = (itemId.toLong() shl 16) or ITEM_ENTITY_TAG,
|
||||
frontSpriteId = itemId.toShort(),
|
||||
backSpriteId = quantity.toShort(),
|
||||
side = 1,
|
||||
slot = 0,
|
||||
partyIndex = -1,
|
||||
statusEffect = null,
|
||||
),
|
||||
)
|
||||
|
||||
private const val ITEM_ENTITY_TAG = 0x5000L
|
||||
+1
-1
@@ -146,6 +146,6 @@ val KANTO_CHECKPOINTS: List<StoryCheckpoint> =
|
||||
KantoVars.VAR_STARTER_MON to 0,
|
||||
),
|
||||
party = listOf(BULBASAUR),
|
||||
items = mapOf(Items.OAKS_PARCEL to 1),
|
||||
items = mapOf(Items.PARCEL to 1),
|
||||
),
|
||||
)
|
||||
+2
-1
@@ -2,6 +2,7 @@ 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.ItemDef
|
||||
|
||||
data class CheckpointMon(val dexId: Int, val level: Int, val moveIds: List<Int>)
|
||||
|
||||
@@ -20,5 +21,5 @@ data class StoryCheckpoint(
|
||||
val storyFlags: Set<String>,
|
||||
val storyVars: Map<String, Int> = emptyMap(),
|
||||
val party: List<CheckpointMon> = emptyList(),
|
||||
val items: Map<Int, Int> = emptyMap(),
|
||||
val items: Map<ItemDef, Int> = emptyMap(),
|
||||
)
|
||||
+3
-1
@@ -4,6 +4,7 @@ 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.items.ItemRegistry
|
||||
import de.fiereu.openmmo.maps.WarpTile
|
||||
import de.fiereu.openmmo.server.game.services.BattleService
|
||||
import de.fiereu.openmmo.server.game.services.StoryPlayerService
|
||||
@@ -23,6 +24,7 @@ constructor(
|
||||
private val storyPlayerService: StoryPlayerService,
|
||||
private val warpService: WarpService,
|
||||
private val battleService: BattleService,
|
||||
private val items: ItemRegistry,
|
||||
) : ChatCommand {
|
||||
override val name = "story"
|
||||
override val usage = "/story [checkpoint|reset]"
|
||||
@@ -103,7 +105,7 @@ constructor(
|
||||
characterStore.replaceProgress(
|
||||
characterId = charId,
|
||||
party = emptyList(),
|
||||
items = checkpoint.items,
|
||||
items = checkpoint.items.mapKeys { items.idOf(it.key) },
|
||||
storyFlags = checkpoint.storyFlags,
|
||||
storyVars = checkpoint.storyVars,
|
||||
)
|
||||
|
||||
+15
@@ -1,6 +1,8 @@
|
||||
package de.fiereu.openmmo.server.game.session
|
||||
|
||||
import de.fiereu.network.SessionAttribute
|
||||
import de.fiereu.openmmo.items.ItemDef
|
||||
import de.fiereu.openmmo.net.game.packets.ShopItem
|
||||
import de.fiereu.openmmo.net.game.packets.dialog.DialogActionResponsePacket
|
||||
import de.fiereu.openmmo.server.game.storage.StoredCharacter
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
@@ -25,3 +27,16 @@ val SCRIPT_SCOPE = SessionAttribute.of<CoroutineScope>("scriptScope")
|
||||
* The character as it was before the running script started, so an unfinished one can be undone.
|
||||
*/
|
||||
val SCRIPT_SNAPSHOT = SessionAttribute.of<StoredCharacter>("scriptSnapshot")
|
||||
|
||||
/**
|
||||
* Nothing tells the server the mart window closed, so the map it opened on is recorded and a shelf
|
||||
* left behind stops applying once the player walks off that map.
|
||||
*/
|
||||
data class OpenShop(
|
||||
val regionId: Int,
|
||||
val bankId: Int,
|
||||
val mapId: Int,
|
||||
val shelf: Map<ItemDef, ShopItem>,
|
||||
)
|
||||
|
||||
val OPEN_SHOP = SessionAttribute.of<OpenShop>("openShop")
|
||||
+18
@@ -9,6 +9,7 @@ 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.Region
|
||||
import de.fiereu.openmmo.items.ItemRegistry
|
||||
import de.fiereu.openmmo.moves.MoveRegistry
|
||||
import de.fiereu.openmmo.net.game.packets.EntityMovePpPacket
|
||||
import de.fiereu.openmmo.net.game.packets.EntityPresencePacket
|
||||
@@ -17,6 +18,7 @@ import de.fiereu.openmmo.net.game.packets.battle.BattleActionSelectPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleBulkStatePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleEntityDeltaPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleFieldStatePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleListEventPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleQueuedEventPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleSidePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleSwitchInPacket
|
||||
@@ -103,6 +105,7 @@ private class Fixture(scope: CoroutineScope) {
|
||||
speciesRegistry = SpeciesRegistry(),
|
||||
moveRegistry = MoveRegistry(),
|
||||
trainers = TrainerRegistry(),
|
||||
items = ItemRegistry(),
|
||||
)
|
||||
|
||||
suspend fun playerWithParty(level: Byte = 50, hp: Short = 999): Pair<FakeSession, Long> {
|
||||
@@ -303,6 +306,21 @@ class BattleServiceTest :
|
||||
}
|
||||
}
|
||||
|
||||
test("the ball throw names the Poke Ball by the id the client knows") {
|
||||
runTest {
|
||||
val fx = Fixture(this)
|
||||
val (session, _) = fx.playerWithParty()
|
||||
session.startBattle(fx.service)
|
||||
|
||||
session.act(fx.service, BattleAction.ITEM)
|
||||
|
||||
session.sent
|
||||
.filterIsInstance<BattleListEventPacket>()
|
||||
.single { it.subKind == 4.toByte() }
|
||||
.value shouldBe 5004.toShort()
|
||||
}
|
||||
}
|
||||
|
||||
test("a disconnect persists and removes the battle") {
|
||||
runTest {
|
||||
val fx = Fixture(this)
|
||||
|
||||
+230
@@ -0,0 +1,230 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.network.PacketEvent
|
||||
import de.fiereu.openmmo.common.enums.CharacterGender
|
||||
import de.fiereu.openmmo.common.enums.Region
|
||||
import de.fiereu.openmmo.items.ItemDef
|
||||
import de.fiereu.openmmo.items.ItemRegistry
|
||||
import de.fiereu.openmmo.items.generated.Items
|
||||
import de.fiereu.openmmo.net.game.packets.ExchangeItemRequestPacket
|
||||
import de.fiereu.openmmo.net.game.packets.LocalCharacterDeltaPacket
|
||||
import de.fiereu.openmmo.net.game.packets.ShopCatalogPacket
|
||||
import de.fiereu.openmmo.net.game.packets.ShopSellRequestPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleSideAddPokemonPacket
|
||||
import de.fiereu.openmmo.server.game.session.OPEN_SHOP
|
||||
import de.fiereu.openmmo.server.game.storage.CharacterStore
|
||||
import de.fiereu.openmmo.server.game.storage.EntityIdService
|
||||
import de.fiereu.openmmo.server.game.testsupport.FakeCharacterRepository
|
||||
import de.fiereu.openmmo.server.game.testsupport.FakeSession
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.collections.shouldBeEmpty
|
||||
import io.kotest.matchers.collections.shouldContainExactly
|
||||
import io.kotest.matchers.nulls.shouldBeNull
|
||||
import io.kotest.matchers.shouldBe
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.runTest
|
||||
|
||||
private val SHELF = listOf(Items.POKE_BALL, Items.POTION, Items.ANTIDOTE, Items.PARLYZ_HEAL)
|
||||
private const val CLERK_ENTITY = 4711L
|
||||
|
||||
private class ShopFixture(scope: CoroutineScope) {
|
||||
val items = ItemRegistry()
|
||||
val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), scope)
|
||||
val service = ShopService(store, items)
|
||||
|
||||
suspend fun shopper(): Pair<FakeSession, Long> {
|
||||
val created = store.createCharacter(1, "Ash", CharacterGender.MALE, Region.HOENN)
|
||||
val session = FakeSession(created.info.id)
|
||||
service.open(session, CLERK_ENTITY, SHELF)
|
||||
session.sent.clear()
|
||||
return session to created.info.id
|
||||
}
|
||||
|
||||
suspend fun buy(session: FakeSession, id: Int, quantity: Int) {
|
||||
service.onBuy(
|
||||
PacketEvent(ExchangeItemRequestPacket(id.toShort(), quantity.toShort(), 0), session))
|
||||
}
|
||||
|
||||
suspend fun sell(session: FakeSession, id: Int, quantity: Int) {
|
||||
service.onSell(
|
||||
PacketEvent(
|
||||
ShopSellRequestPacket((id.toLong() shl 16) or 0x5000, quantity.toShort()), session))
|
||||
}
|
||||
|
||||
suspend fun money(charId: Long): Int = store.getCharacter(charId)!!.info.money
|
||||
|
||||
suspend fun held(charId: Long, id: Int): Int = store.getCharacter(charId)!!.items[id] ?: 0
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class ShopServiceTest :
|
||||
FunSpec({
|
||||
test("the shelf carries the ids and prices the client expects") {
|
||||
runTest {
|
||||
val fx = ShopFixture(backgroundScope)
|
||||
val created = fx.store.createCharacter(1, "Ash", CharacterGender.MALE, Region.HOENN)
|
||||
val session = FakeSession(created.info.id)
|
||||
|
||||
fx.service.open(session, CLERK_ENTITY, SHELF)
|
||||
|
||||
val catalog = session.sent.filterIsInstance<ShopCatalogPacket>().single()
|
||||
catalog.npcEntityId shouldBe CLERK_ENTITY
|
||||
catalog.items.map { it.itemId.toInt() to it.price } shouldContainExactly
|
||||
listOf(5004 to 200, 5017 to 300, 5018 to 100, 5022 to 200)
|
||||
}
|
||||
}
|
||||
|
||||
test("an item with no id is left off the shelf rather than failing the open") {
|
||||
runTest {
|
||||
val fx = ShopFixture(backgroundScope)
|
||||
val created = fx.store.createCharacter(1, "Ash", CharacterGender.MALE, Region.HOENN)
|
||||
val session = FakeSession(created.info.id)
|
||||
|
||||
fx.service.open(session, CLERK_ENTITY, listOf(Items.POTION, ItemDef("Teachy TV", 100)))
|
||||
|
||||
session.sent.filterIsInstance<ShopCatalogPacket>().single().items.map {
|
||||
it.itemId.toInt()
|
||||
} shouldContainExactly listOf(5017)
|
||||
}
|
||||
}
|
||||
|
||||
test("buying resolves the id the client sends") {
|
||||
runTest {
|
||||
val fx = ShopFixture(backgroundScope)
|
||||
val (session, charId) = fx.shopper()
|
||||
|
||||
fx.buy(session, 5017, 3)
|
||||
|
||||
fx.held(charId, 5017) shouldBe 3
|
||||
fx.money(charId) shouldBe 30000 - 900
|
||||
session.sent.filterIsInstance<LocalCharacterDeltaPacket>().single().money shouldBe 29100
|
||||
}
|
||||
}
|
||||
|
||||
test("the bought stack is announced on its own, so the open window can recount it") {
|
||||
runTest {
|
||||
val fx = ShopFixture(backgroundScope)
|
||||
val (session, _) = fx.shopper()
|
||||
|
||||
fx.buy(session, 5004, 5)
|
||||
|
||||
val update = session.sent.filterIsInstance<BattleSideAddPokemonPacket>().single()
|
||||
update.pokemon.frontSpriteId shouldBe 5004.toShort()
|
||||
update.pokemon.backSpriteId shouldBe 5.toShort()
|
||||
}
|
||||
}
|
||||
|
||||
test("refuses an item this clerk does not sell") {
|
||||
runTest {
|
||||
val fx = ShopFixture(backgroundScope)
|
||||
val (session, charId) = fx.shopper()
|
||||
|
||||
fx.buy(session, fx.items.idOf(Items.MASTER_BALL), 1)
|
||||
|
||||
fx.money(charId) shouldBe 30000
|
||||
session.sent.shouldBeEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
test("refuses a buy the player cannot pay for") {
|
||||
runTest {
|
||||
val fx = ShopFixture(backgroundScope)
|
||||
val (session, charId) = fx.shopper()
|
||||
|
||||
fx.buy(session, 5017, 200)
|
||||
|
||||
fx.held(charId, 5017) shouldBe 0
|
||||
fx.money(charId) shouldBe 30000
|
||||
}
|
||||
}
|
||||
|
||||
test("refuses a non-positive quantity") {
|
||||
runTest {
|
||||
val fx = ShopFixture(backgroundScope)
|
||||
val (session, charId) = fx.shopper()
|
||||
|
||||
fx.buy(session, 5017, 0)
|
||||
fx.buy(session, 5017, -2)
|
||||
|
||||
fx.money(charId) shouldBe 30000
|
||||
}
|
||||
}
|
||||
|
||||
test("selling pays half and reads the item out of the bag stack id") {
|
||||
runTest {
|
||||
val fx = ShopFixture(backgroundScope)
|
||||
val (session, charId) = fx.shopper()
|
||||
fx.buy(session, 5017, 4)
|
||||
session.sent.clear()
|
||||
|
||||
fx.sell(session, 5017, 2)
|
||||
|
||||
fx.held(charId, 5017) shouldBe 2
|
||||
fx.money(charId) shouldBe 30000 - 1200 + 300
|
||||
session.sent
|
||||
.filterIsInstance<BattleSideAddPokemonPacket>()
|
||||
.single()
|
||||
.pokemon
|
||||
.backSpriteId shouldBe 2.toShort()
|
||||
}
|
||||
}
|
||||
|
||||
test("refuses to sell more than the bag holds") {
|
||||
runTest {
|
||||
val fx = ShopFixture(backgroundScope)
|
||||
val (session, charId) = fx.shopper()
|
||||
fx.buy(session, 5017, 1)
|
||||
|
||||
fx.sell(session, 5017, 2)
|
||||
|
||||
fx.held(charId, 5017) shouldBe 1
|
||||
fx.money(charId) shouldBe 30000 - 300
|
||||
}
|
||||
}
|
||||
|
||||
test("ignores a sell with no shop open") {
|
||||
runTest {
|
||||
val fx = ShopFixture(backgroundScope)
|
||||
val (session, charId) = fx.shopper()
|
||||
fx.buy(session, 5017, 1)
|
||||
session.attributes.remove(OPEN_SHOP)
|
||||
session.sent.clear()
|
||||
|
||||
fx.sell(session, 5017, 1)
|
||||
|
||||
fx.held(charId, 5017) shouldBe 1
|
||||
session.sent.shouldBeEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
test("refuses to buy from a shelf the player has walked away from") {
|
||||
runTest {
|
||||
val fx = ShopFixture(backgroundScope)
|
||||
val (session, charId) = fx.shopper()
|
||||
session.state().mapId += 1
|
||||
|
||||
fx.buy(session, 5017, 1)
|
||||
|
||||
fx.held(charId, 5017) shouldBe 0
|
||||
fx.money(charId) shouldBe 30000
|
||||
session.attributes[OPEN_SHOP].shouldBeNull()
|
||||
}
|
||||
}
|
||||
|
||||
test("refuses to sell a key item, rather than deleting it for nothing") {
|
||||
runTest {
|
||||
val fx = ShopFixture(backgroundScope)
|
||||
val (session, charId) = fx.shopper()
|
||||
val townMap = fx.items.idOf(Items.TOWN_MAP)
|
||||
fx.store.addItem(charId, townMap, 1)
|
||||
session.sent.clear()
|
||||
|
||||
fx.sell(session, townMap, 1)
|
||||
|
||||
fx.held(charId, townMap) shouldBe 1
|
||||
fx.money(charId) shouldBe 30000
|
||||
session.sent.shouldBeEmpty()
|
||||
}
|
||||
}
|
||||
})
|
||||
+5
@@ -4,6 +4,7 @@ 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.items.ItemRegistry
|
||||
import de.fiereu.openmmo.maps.MapManager
|
||||
import de.fiereu.openmmo.moves.MoveRegistry
|
||||
import de.fiereu.openmmo.net.game.packets.ChatMessagePacket
|
||||
@@ -49,6 +50,7 @@ class StoryResetTest :
|
||||
val interest = InterestManager()
|
||||
val species = SpeciesRegistry()
|
||||
val moves = MoveRegistry()
|
||||
val items = ItemRegistry()
|
||||
return StoryCommand(
|
||||
characterStore = store,
|
||||
worldStateService = WorldStateService(),
|
||||
@@ -58,6 +60,7 @@ class StoryResetTest :
|
||||
WildMonFactory(species, moves, LearnsetRegistry(), EntityIdService()),
|
||||
species,
|
||||
moves,
|
||||
items,
|
||||
),
|
||||
warpService =
|
||||
WarpService(
|
||||
@@ -80,7 +83,9 @@ class StoryResetTest :
|
||||
speciesRegistry = species,
|
||||
moveRegistry = moves,
|
||||
trainers = TrainerRegistry(),
|
||||
items = items,
|
||||
),
|
||||
items = items,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.fiereu.openmmo.server.game.testsupport
|
||||
|
||||
import de.fiereu.openmmo.items.ItemRegistry
|
||||
import de.fiereu.openmmo.maps.MapManager
|
||||
import de.fiereu.openmmo.moves.MoveRegistry
|
||||
import de.fiereu.openmmo.pokemon.LearnsetRegistry
|
||||
@@ -23,6 +24,7 @@ import de.fiereu.openmmo.server.game.services.NpcService
|
||||
import de.fiereu.openmmo.server.game.services.PresenceService
|
||||
import de.fiereu.openmmo.server.game.services.ScriptMovementService
|
||||
import de.fiereu.openmmo.server.game.services.ScriptWarpService
|
||||
import de.fiereu.openmmo.server.game.services.ShopService
|
||||
import de.fiereu.openmmo.server.game.services.StoryPlayerService
|
||||
import de.fiereu.openmmo.server.game.services.StoryService
|
||||
import de.fiereu.openmmo.server.game.services.WarpService
|
||||
@@ -77,16 +79,18 @@ fun scriptRunner(
|
||||
val species = SpeciesRegistry()
|
||||
val moves = MoveRegistry()
|
||||
val wildMons = WildMonFactory(species, moves, LearnsetRegistry(), EntityIdService())
|
||||
val items = ItemRegistry()
|
||||
return ScriptRunner(
|
||||
DialogService(),
|
||||
story,
|
||||
ScriptMovementService(mapManager, npcs, store),
|
||||
ScriptWarpService(mapManager, mapLoad, store, presence),
|
||||
StoryPlayerService(store, wildMons, species, moves),
|
||||
StoryPlayerService(store, wildMons, species, moves, items),
|
||||
battles,
|
||||
store,
|
||||
mapManager,
|
||||
MapEntryScripts(ScriptRegistry(emptyMap()), story),
|
||||
ShopService(store, items),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -105,5 +109,6 @@ private fun battleService(store: CharacterStore, interest: InterestManager): Bat
|
||||
speciesRegistry = species,
|
||||
moveRegistry = moves,
|
||||
trainers = TrainerRegistry(),
|
||||
items = ItemRegistry(),
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user