refactor(network): rework network module, use new bytecodec module and introduce proper packet events
This commit is contained in:
1 parent
8048827121
commit
d27a54f360
340 files changed
+7462
-62719
No files matched your search
@@ -1 +0,0 @@
|
||||
src/main/resources/game.*.pem
|
||||
@@ -1,61 +0,0 @@
|
||||
# Multi-stage build for OpenMMO Game Server
|
||||
FROM azul/zulu-openjdk:24-latest AS builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy gradle files first for better caching
|
||||
COPY ./gradle gradle/
|
||||
COPY ./gradlew gradlew.bat gradle.properties settings.gradle.kts gradle.env ./
|
||||
COPY ./buildSrc buildSrc/
|
||||
|
||||
# remove all includ("...") from settings.gradle.kts
|
||||
RUN sed -i '/include(/d' settings.gradle.kts
|
||||
# manually include only the required modules
|
||||
RUN echo 'include("common")' >> settings.gradle.kts
|
||||
RUN echo 'include("keys")' >> settings.gradle.kts
|
||||
RUN echo 'include("protocol")' >> settings.gradle.kts
|
||||
RUN echo 'include("protocol.tls")' >> settings.gradle.kts
|
||||
RUN echo 'include("protocol.game")' >> settings.gradle.kts
|
||||
RUN echo 'include("server")' >> settings.gradle.kts
|
||||
RUN echo 'include("server.game")' >> settings.gradle.kts
|
||||
|
||||
# Copy all module build files
|
||||
COPY ./build.gradle.kts ./
|
||||
COPY ./common/build.gradle.kts common/
|
||||
COPY ./keys/build.gradle.kts keys/
|
||||
COPY ./protocol/build.gradle.kts protocol/
|
||||
COPY ./protocol.tls/build.gradle.kts protocol.tls/
|
||||
COPY ./protocol.game/build.gradle.kts protocol.game/
|
||||
COPY ./server/build.gradle.kts server/
|
||||
COPY ./server.game/build.gradle.kts server.game/
|
||||
|
||||
# Download dependencies
|
||||
RUN ./gradlew dependencies --no-daemon
|
||||
|
||||
# Copy source code
|
||||
COPY ./common/src common/src/
|
||||
COPY ./protocol/src protocol/src/
|
||||
COPY ./protocol.tls/src protocol.tls/src/
|
||||
COPY ./protocol.game/src protocol.game/src/
|
||||
COPY ./server/src server/src/
|
||||
COPY ./server.game/src server.game/src/
|
||||
|
||||
# Build the application
|
||||
RUN ./gradlew :server.game:distTar --no-daemon
|
||||
|
||||
# Extract the distribution
|
||||
RUN cd server.game/build/distributions && \
|
||||
tar -xf server.game-*.tar && \
|
||||
mv server.game-*/ server.game/
|
||||
|
||||
# Runtime stage
|
||||
FROM azul/zulu-openjdk:24-jre-latest
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
# Copy the extracted distribution from builder
|
||||
COPY --from=builder /app/server.game/build/distributions/server.game/ .
|
||||
|
||||
EXPOSE 2106
|
||||
|
||||
ENTRYPOINT ["./bin/server.game"]
|
||||
@@ -1,9 +0,0 @@
|
||||
# Server.Game Module
|
||||
|
||||
The server.game module implements the game server for OpenMMO.
|
||||
|
||||
## Development
|
||||
- Run with: `./gradlew :server.game:run`
|
||||
- Database setup: `./gradlew cleanMigrateAndGenerate`
|
||||
- Code generation: `./gradlew jooqCodegen`
|
||||
- Docker build: `docker build -t openmmo-game server.game/`
|
||||
+17
-104
@@ -1,123 +1,36 @@
|
||||
import org.flywaydb.gradle.task.AbstractFlywayTask
|
||||
|
||||
buildscript { dependencies { classpath(libs.flyway.postgresql) } }
|
||||
|
||||
plugins {
|
||||
application
|
||||
distribution
|
||||
id("buildsrc.convention.kotlin-jvm")
|
||||
id("buildsrc.convention.spotless")
|
||||
id("buildsrc.convention.sonarlint")
|
||||
id("buildsrc.common.keys")
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
alias(libs.plugins.jooq)
|
||||
alias(libs.plugins.flyway)
|
||||
alias(libs.plugins.ksp)
|
||||
}
|
||||
|
||||
group = "de.fiereu.openmmo"
|
||||
|
||||
version = "0.1.0"
|
||||
|
||||
application { mainClass.set("de.fiereu.openmmo.server.game.MainKt") }
|
||||
|
||||
dependencies {
|
||||
implementation(project(":server"))
|
||||
implementation(project(":protocol.game"))
|
||||
implementation(libs.logback)
|
||||
api(project(":network"))
|
||||
api(project(":protocols.game"))
|
||||
api(project(":common"))
|
||||
api(project(":maps"))
|
||||
|
||||
implementation(libs.dagger)
|
||||
ksp(libs.dagger.compiler)
|
||||
|
||||
implementation(libs.typesafe.config)
|
||||
implementation(libs.kotlinx.coroutines)
|
||||
implementation(libs.kotlin.logging)
|
||||
implementation(libs.logback)
|
||||
|
||||
testImplementation(libs.bundles.kotest)
|
||||
testImplementation(libs.kotlinx.coroutines.test)
|
||||
testImplementation(libs.mockk)
|
||||
}
|
||||
|
||||
application { mainClass.set("de.fiereu.openmmo.server.game.GameServerKt") }
|
||||
|
||||
fun env(key: String): Any {
|
||||
val value =
|
||||
property(key)
|
||||
?: System.getenv(key)
|
||||
?: throw IllegalArgumentException(
|
||||
"Environment variable or system property '$key' is not set.")
|
||||
fun interpolate(value: String): Any {
|
||||
val PATTERN_WORD = Regex("\\$([a-zA-Z0-9_]+)")
|
||||
val PATTERN_MULTI = Regex("\\$\\{([a-zA-Z0-9_]+)\\}")
|
||||
return (PATTERN_WORD.findAll(value) + PATTERN_MULTI.findAll(value)).distinct().fold(value) {
|
||||
acc,
|
||||
match ->
|
||||
val envName = match.groupValues[1]
|
||||
val envValue = System.getProperty(envName) ?: System.getProperty(envName)
|
||||
if (envValue == null) {
|
||||
throw IllegalArgumentException(
|
||||
"Environment variable or system property '$envName' is not set.")
|
||||
}
|
||||
acc.replace(match.value, envValue)
|
||||
}
|
||||
}
|
||||
return if (value is String && (value.contains('$') || value.contains('{'))) {
|
||||
interpolate(value)
|
||||
} else value
|
||||
listOf("classes", "processResources").forEach { taskName ->
|
||||
tasks.named(taskName) { dependsOn("copyPublicKeyGame", "copyPrivateKeyGame") }
|
||||
}
|
||||
|
||||
val dbPort = env("openmmo.db.game.port") as String
|
||||
val dbName = env("openmmo.db.game.name") as String
|
||||
val dbUser = env("openmmo.db.game.user") as String
|
||||
val dbPassword = env("openmmo.db.game.password") as String
|
||||
|
||||
jooq {
|
||||
configuration {
|
||||
jdbc {
|
||||
driver = "org.postgresql.Driver"
|
||||
url = "jdbc:postgresql://localhost:$dbPort/$dbName"
|
||||
user = dbUser
|
||||
password = dbPassword
|
||||
}
|
||||
generator {
|
||||
database {
|
||||
name = "org.jooq.meta.postgres.PostgresDatabase"
|
||||
includes = ".*"
|
||||
excludes =
|
||||
"""
|
||||
flyway_schema_history |
|
||||
pgp_armor_headers
|
||||
"""
|
||||
inputSchema = "public"
|
||||
}
|
||||
|
||||
generate { name = "org.jooq.codegen.KotlinGenerator" }
|
||||
target {
|
||||
packageName = "de.fiereu.openmmo.server.game.jooq"
|
||||
directory = "src/main/jooq"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
flyway {
|
||||
driver = "org.postgresql.Driver"
|
||||
url = "jdbc:postgresql://localhost:$dbPort/$dbName"
|
||||
user = dbUser
|
||||
password = dbPassword
|
||||
schemas = arrayOf("public")
|
||||
locations = arrayOf("filesystem:src/main/resources/db/migration")
|
||||
cleanDisabled = false
|
||||
}
|
||||
|
||||
// Fuck this shit uhh we are not gonna update our gradle plugin bc all our enterprise customers are
|
||||
// stuck on Java 1.6 and gradle 5 anyway
|
||||
tasks.withType<AbstractFlywayTask>().configureEach {
|
||||
notCompatibleWithConfigurationCache("https://github.com/flyway/flyway/issues/3550")
|
||||
}
|
||||
|
||||
tasks.register("cleanMigrateAndGenerate") {
|
||||
group = "openmmo"
|
||||
description = "Cleans the database, migrates it and generates the jOOQ classes."
|
||||
dependsOn("flywayClean", "flywayMigrate", "jooqCodegen")
|
||||
}
|
||||
|
||||
spotless { kotlin { targetExclude("src/main/jooq/**") } }
|
||||
|
||||
// If someone knows a better way then this please please please help me :(
|
||||
listOf("classes", "processResources", "spotlessKotlin", "spotlessKotlinGradle", "spotlessMisc")
|
||||
.forEach { taskName ->
|
||||
tasks.named(taskName) {
|
||||
listOf("copyPublicKeyGame", "copyPrivateKeyGame").forEach { dependsOn(it) }
|
||||
}
|
||||
}
|
||||
@@ -1,30 +1,78 @@
|
||||
package de.fiereu.openmmo.server.game
|
||||
|
||||
import de.fiereu.openmmo.protocols.game.GameServerProtocol
|
||||
import de.fiereu.openmmo.server.ServerBuilder
|
||||
import de.fiereu.openmmo.server.config.ServerConfig
|
||||
import de.fiereu.openmmo.server.config.TlsConfig
|
||||
import de.fiereu.openmmo.server.game.protocol.game.GameProtocolHandler
|
||||
import de.fiereu.openmmo.server.io.resource
|
||||
import de.fiereu.openmmo.server.keys.KeyLoader
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import de.fiereu.network.PipelineNames
|
||||
import de.fiereu.network.PipelineOptions
|
||||
import de.fiereu.network.SessionIdentity
|
||||
import de.fiereu.network.installPipeline
|
||||
import de.fiereu.network.session
|
||||
import de.fiereu.openmmo.net.game.GameProtocol
|
||||
import de.fiereu.openmmo.server.game.config.GameServerConfig
|
||||
import de.fiereu.openmmo.server.game.handler.GameAppHandler
|
||||
import de.fiereu.openmmo.server.game.session.SessionRegistry
|
||||
import de.fiereu.openmmo.server.game.session.SessionRegistryHandler
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.netty.bootstrap.ServerBootstrap
|
||||
import io.netty.channel.Channel
|
||||
import io.netty.channel.ChannelInitializer
|
||||
import io.netty.channel.ChannelOption
|
||||
import io.netty.channel.EventLoopGroup
|
||||
import io.netty.channel.socket.nio.NioServerSocketChannel
|
||||
import java.security.interfaces.ECPrivateKey
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Named
|
||||
import javax.inject.Provider
|
||||
import javax.inject.Singleton
|
||||
|
||||
fun main() {
|
||||
val serverConfig = ServerConfig(port = 7777)
|
||||
val tlsConfig = TlsConfig(checksumSize = 2)
|
||||
val coroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||
val server =
|
||||
ServerBuilder.create()
|
||||
.withCoroutineScope(coroutineScope)
|
||||
.withConfig(serverConfig)
|
||||
.withTlsConfig(tlsConfig)
|
||||
.withPublicKey(KeyLoader.loadPemECPublicKey(resource("game.public.pem")))
|
||||
.withPrivateKey(KeyLoader.loadPemECPrivateKey(resource("game.private.pem")))
|
||||
.withChannelHandlerProvider {
|
||||
GameProtocolHandler(GameServerProtocol(), serverConfig, coroutineScope)
|
||||
}
|
||||
.build()
|
||||
server.start()
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
@Singleton
|
||||
class GameServer
|
||||
@Inject
|
||||
constructor(
|
||||
private val config: GameServerConfig,
|
||||
@Named("boss") private val bossGroup: EventLoopGroup,
|
||||
@Named("worker") private val workerGroup: EventLoopGroup,
|
||||
private val rootKey: ECPrivateKey,
|
||||
private val handlerProvider: Provider<GameAppHandler>,
|
||||
private val sessionRegistry: SessionRegistry,
|
||||
) {
|
||||
fun start() {
|
||||
val channel =
|
||||
ServerBootstrap()
|
||||
.group(bossGroup, workerGroup)
|
||||
.channel(NioServerSocketChannel::class.java)
|
||||
.option(ChannelOption.SO_BACKLOG, 128)
|
||||
.childOption(ChannelOption.TCP_NODELAY, true)
|
||||
.childHandler(
|
||||
object : ChannelInitializer<Channel>() {
|
||||
override fun initChannel(ch: Channel) {
|
||||
installPipeline(
|
||||
pipeline = ch.pipeline(),
|
||||
side = de.fiereu.network.Side.SERVER,
|
||||
identity = SessionIdentity.ServerRoot(rootKey),
|
||||
applicationProtocol = GameProtocol,
|
||||
applicationHandlerFactory = { handlerProvider.get() },
|
||||
options = PipelineOptions(checksumSize = config.checksumSize),
|
||||
)
|
||||
val session = ch.session() ?: error("session missing after installPipeline")
|
||||
ch.pipeline()
|
||||
.addAfter(
|
||||
PipelineNames.PROTOCOL_HANDLER,
|
||||
"session-registry",
|
||||
SessionRegistryHandler(sessionRegistry, session),
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
.bind(config.host, config.port)
|
||||
.sync()
|
||||
.channel()
|
||||
log.info { "Game server listening on ${channel.localAddress()}" }
|
||||
channel.closeFuture().sync()
|
||||
}
|
||||
|
||||
fun shutdown() {
|
||||
workerGroup.shutdownGracefully()
|
||||
bossGroup.shutdownGracefully()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package de.fiereu.openmmo.server.game
|
||||
|
||||
import de.fiereu.openmmo.server.game.config.ConfigLoader
|
||||
import de.fiereu.openmmo.server.game.di.DaggerGameServerComponent
|
||||
|
||||
fun main() {
|
||||
val config = ConfigLoader.load()
|
||||
val component = DaggerGameServerComponent.factory().create(config)
|
||||
component.server().start()
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package de.fiereu.openmmo.server.game.config
|
||||
|
||||
import com.typesafe.config.ConfigFactory
|
||||
|
||||
object ConfigLoader {
|
||||
fun load(): GameServerConfig {
|
||||
val config = ConfigFactory.load()
|
||||
val secret = config.getString("server.sessionSecret")
|
||||
require(secret.isNotEmpty()) { "server.sessionSecret must not be empty" }
|
||||
return GameServerConfig(
|
||||
host = config.getString("server.host"),
|
||||
port = config.getInt("server.port"),
|
||||
checksumSize = config.getInt("server.checksumSize"),
|
||||
rootKeyResource = config.getString("server.rootKeyResource"),
|
||||
sessionSecret = secret.toByteArray(Charsets.UTF_8),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package de.fiereu.openmmo.server.game.config
|
||||
|
||||
data class GameServerConfig(
|
||||
val host: String,
|
||||
val port: Int,
|
||||
val checksumSize: Int,
|
||||
val rootKeyResource: String,
|
||||
val sessionSecret: ByteArray,
|
||||
) {
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is GameServerConfig &&
|
||||
host == other.host &&
|
||||
port == other.port &&
|
||||
checksumSize == other.checksumSize &&
|
||||
rootKeyResource == other.rootKeyResource &&
|
||||
sessionSecret.contentEquals(other.sessionSecret)
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var h = host.hashCode()
|
||||
h = h * 31 + port
|
||||
h = h * 31 + checksumSize
|
||||
h = h * 31 + rootKeyResource.hashCode()
|
||||
h = h * 31 + sessionSecret.contentHashCode()
|
||||
return h
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package de.fiereu.openmmo.server.game.di
|
||||
|
||||
import dagger.BindsInstance
|
||||
import dagger.Component
|
||||
import de.fiereu.openmmo.server.game.GameServer
|
||||
import de.fiereu.openmmo.server.game.config.GameServerConfig
|
||||
import de.fiereu.openmmo.server.game.handler.GameAppHandler
|
||||
import de.fiereu.openmmo.server.game.session.SessionRegistry
|
||||
import javax.inject.Provider
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
@Component(modules = [GameServerModule::class])
|
||||
interface GameServerComponent {
|
||||
|
||||
fun server(): GameServer
|
||||
|
||||
fun handlerProvider(): Provider<GameAppHandler>
|
||||
|
||||
fun sessionRegistry(): SessionRegistry
|
||||
|
||||
@Component.Factory
|
||||
fun interface Factory {
|
||||
fun create(@BindsInstance config: GameServerConfig): GameServerComponent
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package de.fiereu.openmmo.server.game.di
|
||||
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import de.fiereu.openmmo.common.auth.SessionTokenVerifier
|
||||
import de.fiereu.openmmo.common.io.PemKeyLoader
|
||||
import de.fiereu.openmmo.common.io.resource
|
||||
import de.fiereu.openmmo.server.game.config.GameServerConfig
|
||||
import io.netty.channel.EventLoopGroup
|
||||
import io.netty.channel.MultiThreadIoEventLoopGroup
|
||||
import io.netty.channel.nio.NioIoHandler
|
||||
import java.security.interfaces.ECPrivateKey
|
||||
import javax.inject.Named
|
||||
import javax.inject.Singleton
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
|
||||
@Module
|
||||
object GameServerModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun rootKey(config: GameServerConfig): ECPrivateKey =
|
||||
PemKeyLoader.loadEcPrivate(resource(config.rootKeyResource))
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun tokenVerifier(config: GameServerConfig): SessionTokenVerifier =
|
||||
SessionTokenVerifier(config.sessionSecret)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
@Named("boss")
|
||||
fun bossGroup(): EventLoopGroup = MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory())
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
@Named("worker")
|
||||
fun workerGroup(): EventLoopGroup = MultiThreadIoEventLoopGroup(0, NioIoHandler.newFactory())
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun coroutineScope(): CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package de.fiereu.openmmo.server.game.handler
|
||||
|
||||
import de.fiereu.network.PacketEvent
|
||||
import de.fiereu.network.Side
|
||||
import de.fiereu.network.coroutines.CoroutineProtocolHandler
|
||||
import de.fiereu.openmmo.common.enums.ChatType
|
||||
import de.fiereu.openmmo.common.enums.Language
|
||||
import de.fiereu.openmmo.net.game.GameProtocol
|
||||
import de.fiereu.openmmo.net.game.packets.ChatMessagePacket
|
||||
import de.fiereu.openmmo.net.game.packets.CreateCharacterPacket
|
||||
import de.fiereu.openmmo.net.game.packets.DialogChoicePacket
|
||||
import de.fiereu.openmmo.net.game.packets.EntityInteractPacket
|
||||
import de.fiereu.openmmo.net.game.packets.EntityLeavePacket
|
||||
import de.fiereu.openmmo.net.game.packets.FaceDirectionPacket
|
||||
import de.fiereu.openmmo.net.game.packets.InteractiveResponsePacket
|
||||
import de.fiereu.openmmo.net.game.packets.JoinPacket
|
||||
import de.fiereu.openmmo.net.game.packets.KeepAlivePacket
|
||||
import de.fiereu.openmmo.net.game.packets.MovementPacket
|
||||
import de.fiereu.openmmo.net.game.packets.RequestCharactersPacket
|
||||
import de.fiereu.openmmo.net.game.packets.RequestPlayerPacket
|
||||
import de.fiereu.openmmo.net.game.packets.SelectCharacterPacket
|
||||
import de.fiereu.openmmo.server.game.services.DialogService
|
||||
import de.fiereu.openmmo.server.game.services.InteractionService
|
||||
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.session.PLAYER_STATE
|
||||
import de.fiereu.openmmo.server.game.session.SessionRegistry
|
||||
import de.fiereu.openmmo.server.game.storage.CharacterStore
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import javax.inject.Inject
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
class GameAppHandler
|
||||
@Inject
|
||||
constructor(
|
||||
private val loginService: LoginService,
|
||||
private val movementService: MovementService,
|
||||
private val dialogService: DialogService,
|
||||
private val interactionService: InteractionService,
|
||||
private val multiplayerService: MultiplayerService,
|
||||
private val sessionRegistry: SessionRegistry,
|
||||
private val characterStore: CharacterStore,
|
||||
scope: CoroutineScope,
|
||||
) : CoroutineProtocolHandler<GameProtocol>(GameProtocol, Side.SERVER, scope) {
|
||||
|
||||
init {
|
||||
onSuspend<JoinPacket> { event -> loginService.onJoinGame(event) }
|
||||
onSuspend<CreateCharacterPacket> { event -> loginService.onCreateCharacter(event) }
|
||||
onSuspend<RequestCharactersPacket> { event -> loginService.onCharacterRequest(event) }
|
||||
onSuspend<SelectCharacterPacket> { event -> loginService.onCharacterSelected(event) }
|
||||
onSuspend<RequestPlayerPacket> { event -> loginService.onRequestPlayer(event) }
|
||||
|
||||
onSuspend<MovementPacket> { event -> movementService.onMovement(event) }
|
||||
onSuspend<FaceDirectionPacket> { event -> movementService.onFaceDirection(event) }
|
||||
|
||||
onSuspend<EntityInteractPacket> { event -> interactionService.onEntityInteract(event) }
|
||||
onSuspend<InteractiveResponsePacket> { event -> dialogService.onInteractive(event) }
|
||||
onSuspend<DialogChoicePacket> { event -> dialogService.onDialogChoice(event) }
|
||||
|
||||
on<KeepAlivePacket> { event -> event.session.send(event.packet) }
|
||||
on<ChatMessagePacket> { event -> onChatMessage(event) }
|
||||
}
|
||||
|
||||
override fun onInactive() {
|
||||
val state = session.attributes[PLAYER_STATE] ?: return
|
||||
log.info { "Player ${state.characterId} disconnected." }
|
||||
val charId = state.characterId
|
||||
if (charId != null) {
|
||||
multiplayerService.broadcastExcept(session, EntityLeavePacket(charId))
|
||||
sessionRegistry.unbindCharacter(charId)
|
||||
}
|
||||
multiplayerService.broadcastMessage(
|
||||
ChatMessagePacket(
|
||||
type = ChatType.GAME_NOTIFICATIONS,
|
||||
language = Language.EN,
|
||||
message = "A player left the game.",
|
||||
sender = "",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
private fun onChatMessage(event: PacketEvent<ChatMessagePacket>) {
|
||||
val state = event.session.attributes[PLAYER_STATE]
|
||||
if (state == null) {
|
||||
log.warn { "Chat message from session without PlayerState" }
|
||||
return
|
||||
}
|
||||
val charId = state.characterId ?: return
|
||||
val sender = characterStore.getCharacter(charId)?.info?.name ?: "Unknown"
|
||||
val msg = event.packet
|
||||
log.info { "Chat [${msg.type}] $sender: ${msg.message}" }
|
||||
multiplayerService.broadcastMessage(
|
||||
ChatMessagePacket(
|
||||
type = msg.type,
|
||||
language = msg.language,
|
||||
message = msg.message,
|
||||
sender = sender,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
-140
@@ -1,140 +0,0 @@
|
||||
package de.fiereu.openmmo.server.game.protocol.game
|
||||
|
||||
import de.fiereu.openmmo.common.enums.ChatType
|
||||
import de.fiereu.openmmo.common.enums.Language
|
||||
import de.fiereu.openmmo.protocols.Protocol
|
||||
import de.fiereu.openmmo.protocols.game.packets.ChatMessagePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.CreateCharacterPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.DialogChoicePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.EntityInteractPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.EntityLeavePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.FaceDirectionPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.InteractiveResponsePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.JoinGamePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.KeepAlivePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.MovementPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.RequestCharactersPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.RequestPlayerPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.SelectCharacterPacket
|
||||
import de.fiereu.openmmo.server.config.ServerConfig
|
||||
import de.fiereu.openmmo.server.game.services.DialogService
|
||||
import de.fiereu.openmmo.server.game.services.InteractionService
|
||||
import de.fiereu.openmmo.server.game.services.LoginService
|
||||
import de.fiereu.openmmo.server.game.services.MapLoadService
|
||||
import de.fiereu.openmmo.server.game.services.MovementService
|
||||
import de.fiereu.openmmo.server.game.services.MultiplayerService
|
||||
import de.fiereu.openmmo.server.game.services.NpcService
|
||||
import de.fiereu.openmmo.server.game.services.PacketSender
|
||||
import de.fiereu.openmmo.server.game.services.WarpService
|
||||
import de.fiereu.openmmo.server.game.session.SessionManager
|
||||
import de.fiereu.openmmo.server.game.storage.CharacterStore
|
||||
import de.fiereu.openmmo.server.netty.handlers.ProtocolHandler
|
||||
import de.fiereu.openmmo.server.protocol.PacketEvent
|
||||
import de.fiereu.openmmo.server.protocol.respond
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
class GameProtocolHandler(
|
||||
protocol: Protocol,
|
||||
serverConfig: ServerConfig,
|
||||
private val coroutineScope: CoroutineScope,
|
||||
) : ProtocolHandler(protocol, serverConfig) {
|
||||
|
||||
private val packetSender = PacketSender(compressor)
|
||||
private val mapLoadService = MapLoadService(packetSender)
|
||||
private val warpService = WarpService(mapLoadService)
|
||||
private val npcService = NpcService()
|
||||
private val multiplayerService = MultiplayerService()
|
||||
private val movementService =
|
||||
MovementService(warpService, mapLoadService, npcService, multiplayerService)
|
||||
private val dialogService = DialogService(packetSender)
|
||||
private val interactionService = InteractionService(npcService, dialogService, packetSender)
|
||||
private val loginService =
|
||||
LoginService(mapLoadService, npcService, multiplayerService, packetSender)
|
||||
|
||||
override fun onActive(ctx: ChannelHandlerContext) {
|
||||
log.info { "Client ${ctx.channel().remoteAddress()} connected to game server." }
|
||||
}
|
||||
|
||||
override fun channelInactive(ctx: ChannelHandlerContext) {
|
||||
val session = SessionManager.getSessionByChannel(ctx.channel())
|
||||
if (session != null) {
|
||||
log.info { "Player ${session.characterId} disconnected." }
|
||||
|
||||
session.characterId?.let { charId ->
|
||||
multiplayerService.broadcastExcept(ctx.channel(), EntityLeavePacket(charId))
|
||||
}
|
||||
|
||||
multiplayerService.broadcastMessage(
|
||||
ChatMessagePacket(
|
||||
ChatType.GAME_NOTIFICATIONS,
|
||||
Language.EN,
|
||||
"A player left the game.",
|
||||
"",
|
||||
))
|
||||
SessionManager.removeSession(ctx.channel())
|
||||
}
|
||||
super.channelInactive(ctx)
|
||||
}
|
||||
|
||||
@Suppress("unchecked_cast")
|
||||
override fun onPacketReceived(event: PacketEvent<*>) {
|
||||
coroutineScope.launch {
|
||||
when (event.packet) {
|
||||
is JoinGamePacket -> loginService.onJoinGame(event as PacketEvent<JoinGamePacket>)
|
||||
is CreateCharacterPacket ->
|
||||
loginService.onCreateCharacter(event as PacketEvent<CreateCharacterPacket>)
|
||||
is RequestCharactersPacket ->
|
||||
loginService.onCharacterRequest(event as PacketEvent<RequestCharactersPacket>)
|
||||
is SelectCharacterPacket ->
|
||||
loginService.onCharacterSelected(event as PacketEvent<SelectCharacterPacket>)
|
||||
is RequestPlayerPacket ->
|
||||
loginService.onRequestPlayer(event as PacketEvent<RequestPlayerPacket>)
|
||||
is MovementPacket -> movementService.onMovement(event as PacketEvent<MovementPacket>)
|
||||
is FaceDirectionPacket ->
|
||||
movementService.onFaceDirection(event as PacketEvent<FaceDirectionPacket>)
|
||||
is KeepAlivePacket -> onKeepAlive(event as PacketEvent<KeepAlivePacket>)
|
||||
is EntityInteractPacket ->
|
||||
interactionService.onEntityInteract(event as PacketEvent<EntityInteractPacket>)
|
||||
is InteractiveResponsePacket ->
|
||||
dialogService.onInteractive(event as PacketEvent<InteractiveResponsePacket>)
|
||||
is DialogChoicePacket ->
|
||||
dialogService.onDialogChoice(event as PacketEvent<DialogChoicePacket>)
|
||||
is ChatMessagePacket -> onChatMessage(event as PacketEvent<ChatMessagePacket>)
|
||||
else -> log.warn { "Unhandled game packet type: ${event.packet::class.simpleName}" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun onKeepAlive(event: PacketEvent<KeepAlivePacket>) {
|
||||
event.respond(event.packet)
|
||||
}
|
||||
|
||||
private fun onChatMessage(event: PacketEvent<ChatMessagePacket>) {
|
||||
val msg = event.packet
|
||||
val sender = SessionManager.getSessionByChannel(event.ctx.channel())
|
||||
|
||||
if (sender == null) {
|
||||
log.warn { "Chat message from unknown sender" }
|
||||
return
|
||||
}
|
||||
|
||||
val charId = sender.characterId ?: return
|
||||
val stored = CharacterStore.getCharacter(charId)
|
||||
val senderName = stored?.info?.name ?: "Unknown"
|
||||
|
||||
log.info { "Chat [${msg.type}] $senderName: ${msg.message}" }
|
||||
|
||||
multiplayerService.broadcastMessage(
|
||||
ChatMessagePacket(
|
||||
type = msg.type,
|
||||
language = msg.language,
|
||||
message = msg.message,
|
||||
sender = senderName,
|
||||
))
|
||||
}
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
package de.fiereu.openmmo.server.game.protocol.game.ext
|
||||
|
||||
import de.fiereu.openmmo.protocols.game.packets.JoinGamePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.builders.JoinResponsePacketBuilder
|
||||
import de.fiereu.openmmo.server.protocol.PacketEvent
|
||||
import de.fiereu.openmmo.server.protocol.respond
|
||||
|
||||
fun PacketEvent<JoinGamePacket>.reject() {
|
||||
this.respond(JoinResponsePacketBuilder.reject())
|
||||
}
|
||||
|
||||
fun PacketEvent<JoinGamePacket>.accept() = JoinResponsePacketBuilder.accept()
|
||||
|
||||
fun JoinResponsePacketBuilder.buildAndRespond(event: PacketEvent<JoinGamePacket>) {
|
||||
event.respond(this.build())
|
||||
}
|
||||
+45
-48
@@ -1,20 +1,21 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.openmmo.protocols.game.packets.DialogChoicePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.DialogStatePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.InteractiveResponsePacket
|
||||
import de.fiereu.network.PacketEvent
|
||||
import de.fiereu.network.SessionContext
|
||||
import de.fiereu.openmmo.net.game.packets.DialogChoicePacket
|
||||
import de.fiereu.openmmo.net.game.packets.DialogStatePacket
|
||||
import de.fiereu.openmmo.net.game.packets.InteractivePacket
|
||||
import de.fiereu.openmmo.net.game.packets.InteractiveResponsePacket
|
||||
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
||||
import de.fiereu.openmmo.server.game.session.ScriptPage
|
||||
import de.fiereu.openmmo.server.game.session.SessionManager
|
||||
import de.fiereu.openmmo.server.protocol.PacketEvent
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.netty.buffer.Unpooled
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
class DialogService(
|
||||
private val packetSender: PacketSender,
|
||||
) {
|
||||
@Singleton
|
||||
class DialogService @Inject constructor() {
|
||||
|
||||
fun scriptParams(script: String): List<ScriptPage>? {
|
||||
if (script == "0x0") return null
|
||||
@@ -42,7 +43,7 @@ class DialogService(
|
||||
}
|
||||
|
||||
fun sendInteractive(
|
||||
ctx: ChannelHandlerContext,
|
||||
ctx: SessionContext,
|
||||
id: Int,
|
||||
entityId: Long,
|
||||
type: Int = 0x04,
|
||||
@@ -50,64 +51,60 @@ class DialogService(
|
||||
unk2: Int = 0x1F10,
|
||||
unk3: Int = 0x0708,
|
||||
) {
|
||||
val buf = Unpooled.buffer(19)
|
||||
buf.writeByte(id)
|
||||
buf.writeByte(type)
|
||||
buf.writeShortLE(unk1)
|
||||
buf.writeShortLE(unk2)
|
||||
buf.writeLongLE(entityId)
|
||||
buf.writeShortLE(unk3)
|
||||
buf.writeShortLE(0)
|
||||
buf.writeByte(0)
|
||||
val raw = ByteArray(buf.readableBytes())
|
||||
buf.readBytes(raw)
|
||||
buf.release()
|
||||
packetSender.sendRaw(ctx, 0x21u, raw)
|
||||
ctx.send(
|
||||
InteractivePacket(
|
||||
id = id,
|
||||
type = type,
|
||||
unk1 = unk1,
|
||||
unk2 = unk2,
|
||||
targetEntityId = entityId,
|
||||
unk3 = unk3,
|
||||
unk4 = 0,
|
||||
))
|
||||
}
|
||||
|
||||
fun onInteractive(event: PacketEvent<InteractiveResponsePacket>) {
|
||||
val session = SessionManager.getSessionByChannel(event.ctx.channel())
|
||||
if (session == null) return
|
||||
val charId = session.characterId ?: return
|
||||
val ctx = event.session
|
||||
val state = ctx.attributes[PLAYER_STATE] ?: return
|
||||
val charId = state.characterId ?: return
|
||||
|
||||
if (session.inDialog) {
|
||||
if (state.inDialog) {
|
||||
val respId = event.packet.id
|
||||
log.info {
|
||||
"Interactive response: id=$respId for char $charId (page ${session.dialogPageIndex + 1}/${session.dialogPages.size})"
|
||||
"Interactive response: id=$respId for char $charId (page ${state.dialogPageIndex + 1}/${state.dialogPages.size})"
|
||||
}
|
||||
val nextPageIndex = session.dialogPageIndex + 1
|
||||
if (nextPageIndex < session.dialogPages.size) {
|
||||
val seqId = session.dialogSeqId++
|
||||
val page = session.dialogPages[nextPageIndex]
|
||||
session.dialogPageIndex = nextPageIndex
|
||||
val nextPageIndex = state.dialogPageIndex + 1
|
||||
if (nextPageIndex < state.dialogPages.size) {
|
||||
val seqId = state.dialogSeqId++
|
||||
val page = state.dialogPages[nextPageIndex]
|
||||
state.dialogPageIndex = nextPageIndex
|
||||
sendInteractive(
|
||||
event.ctx,
|
||||
ctx,
|
||||
seqId,
|
||||
session.dialogNpcEntityId,
|
||||
state.dialogNpcEntityId,
|
||||
page.type,
|
||||
page.unk1,
|
||||
page.unk2,
|
||||
page.unk3,
|
||||
)
|
||||
event.ctx.channel().flush()
|
||||
} else {
|
||||
event.ctx.channel().writeAndFlush(DialogStatePacket(false))
|
||||
session.inDialog = false
|
||||
session.dialogNpcEntityId = 0
|
||||
session.dialogPages = emptyList()
|
||||
session.dialogPageIndex = 0
|
||||
ctx.send(DialogStatePacket(false))
|
||||
state.inDialog = false
|
||||
state.dialogNpcEntityId = 0
|
||||
state.dialogPages = emptyList()
|
||||
state.dialogPageIndex = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun onDialogChoice(event: PacketEvent<DialogChoicePacket>) {
|
||||
val session = SessionManager.getSessionByChannel(event.ctx.channel())
|
||||
if (session == null) return
|
||||
val ctx = event.session
|
||||
val state = ctx.attributes[PLAYER_STATE] ?: return
|
||||
log.info { "Dialog choice received: unk1=${event.packet.unk1}, unk2=${event.packet.unk2}" }
|
||||
if (session.inDialog) {
|
||||
event.ctx.channel().writeAndFlush(DialogStatePacket(false))
|
||||
session.inDialog = false
|
||||
session.dialogNpcEntityId = 0
|
||||
if (state.inDialog) {
|
||||
ctx.send(DialogStatePacket(false))
|
||||
state.inDialog = false
|
||||
state.dialogNpcEntityId = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
+40
-68
@@ -1,39 +1,42 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.network.PacketEvent
|
||||
import de.fiereu.openmmo.common.enums.Direction
|
||||
import de.fiereu.openmmo.protocols.game.packets.DialogStatePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.EntityInteractPacket
|
||||
import de.fiereu.openmmo.server.game.session.SessionManager
|
||||
import de.fiereu.openmmo.maps.MapManager
|
||||
import de.fiereu.openmmo.net.game.packets.DialogStatePacket
|
||||
import de.fiereu.openmmo.net.game.packets.EntityInteractPacket
|
||||
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
||||
import de.fiereu.openmmo.server.game.storage.CharacterStore
|
||||
import de.fiereu.openmmo.server.game.world.MapManager
|
||||
import de.fiereu.openmmo.server.protocol.PacketEvent
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.netty.buffer.Unpooled
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
class InteractionService(
|
||||
@Singleton
|
||||
class InteractionService
|
||||
@Inject
|
||||
constructor(
|
||||
private val npcService: NpcService,
|
||||
private val dialogService: DialogService,
|
||||
private val packetSender: PacketSender,
|
||||
private val mapManager: MapManager,
|
||||
private val characterStore: CharacterStore,
|
||||
) {
|
||||
|
||||
fun onEntityInteract(event: PacketEvent<EntityInteractPacket>) {
|
||||
val session = SessionManager.getSessionByChannel(event.ctx.channel())
|
||||
if (session == null) return
|
||||
val charId = session.characterId ?: return
|
||||
val ctx = event.session
|
||||
val state = ctx.attributes[PLAYER_STATE] ?: return
|
||||
val charId = state.characterId ?: return
|
||||
|
||||
val npcEntityId = event.packet.entityId
|
||||
val stored = CharacterStore.getCharacter(charId) ?: return
|
||||
val stored = characterStore.getCharacter(charId) ?: return
|
||||
val currentMap =
|
||||
MapManager.getMap(
|
||||
mapManager.getMap(
|
||||
stored.info.positionRegionId,
|
||||
stored.info.positionBankId,
|
||||
stored.info.positionMapId,
|
||||
)
|
||||
if (currentMap == null) return
|
||||
) ?: return
|
||||
|
||||
// Find which NPC this entityId belongs to
|
||||
val bankId = stored.info.positionBankId.toInt()
|
||||
val mapId = stored.info.positionMapId.toInt()
|
||||
for (npc in currentMap.npcs) {
|
||||
@@ -46,25 +49,15 @@ class InteractionService(
|
||||
if (npc.script != "0x0") {
|
||||
val params = dialogService.scriptParams(npc.script)
|
||||
if (params != null) {
|
||||
log.info {
|
||||
"NPC entityIdx=${npc.entityIdx} script=${npc.script} — starting dialog (${params.size} pages)"
|
||||
}
|
||||
session.inDialog = true
|
||||
session.dialogNpcEntityId = npcEntityId
|
||||
session.dialogPages = params
|
||||
session.dialogPageIndex = 0
|
||||
val seqId = session.dialogSeqId++
|
||||
state.inDialog = true
|
||||
state.dialogNpcEntityId = npcEntityId
|
||||
state.dialogPages = params
|
||||
state.dialogPageIndex = 0
|
||||
val seqId = state.dialogSeqId++
|
||||
val page = params[0]
|
||||
event.ctx.channel().write(DialogStatePacket(true))
|
||||
val confirmBuf = Unpooled.buffer(9)
|
||||
confirmBuf.writeLongLE(npcEntityId)
|
||||
confirmBuf.writeByte(0xFF)
|
||||
val confirmBytes = ByteArray(9)
|
||||
confirmBuf.readBytes(confirmBytes)
|
||||
confirmBuf.release()
|
||||
packetSender.sendRaw(event.ctx, 0x07u, confirmBytes)
|
||||
ctx.send(DialogStatePacket(true))
|
||||
dialogService.sendInteractive(
|
||||
event.ctx,
|
||||
ctx,
|
||||
seqId,
|
||||
npcEntityId,
|
||||
page.type,
|
||||
@@ -72,61 +65,45 @@ class InteractionService(
|
||||
page.unk2,
|
||||
page.unk3,
|
||||
)
|
||||
event.ctx.channel().flush()
|
||||
} else {
|
||||
log.info { "NPC entityIdx=${npc.entityIdx} script=${npc.script} — no known params" }
|
||||
}
|
||||
} else {
|
||||
log.info { "NPC entityIdx=${npc.entityIdx} has no script (0x0) — no interaction" }
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// No NPC found — check for bg_event at tile player is facing
|
||||
val facingX =
|
||||
when (session.facingDirection) {
|
||||
when (state.facingDirection) {
|
||||
Direction.RIGHT -> stored.info.positionX.toInt() + 1
|
||||
Direction.LEFT -> stored.info.positionX.toInt() - 1
|
||||
else -> stored.info.positionX.toInt()
|
||||
}
|
||||
val facingY =
|
||||
when (session.facingDirection) {
|
||||
when (state.facingDirection) {
|
||||
Direction.UP -> stored.info.positionY.toInt() - 1
|
||||
Direction.DOWN -> stored.info.positionY.toInt() + 1
|
||||
else -> stored.info.positionY.toInt()
|
||||
}
|
||||
val facingDirOk: (String) -> Boolean = { dir ->
|
||||
dir == "BG_EVENT_PLAYER_FACING_ANY" ||
|
||||
(dir == "BG_EVENT_PLAYER_FACING_NORTH" && session.facingDirection == Direction.UP) ||
|
||||
(dir == "BG_EVENT_PLAYER_FACING_SOUTH" && session.facingDirection == Direction.DOWN) ||
|
||||
(dir == "BG_EVENT_PLAYER_FACING_WEST" && session.facingDirection == Direction.LEFT) ||
|
||||
(dir == "BG_EVENT_PLAYER_FACING_EAST" && session.facingDirection == Direction.RIGHT)
|
||||
(dir == "BG_EVENT_PLAYER_FACING_NORTH" && state.facingDirection == Direction.UP) ||
|
||||
(dir == "BG_EVENT_PLAYER_FACING_SOUTH" && state.facingDirection == Direction.DOWN) ||
|
||||
(dir == "BG_EVENT_PLAYER_FACING_WEST" && state.facingDirection == Direction.LEFT) ||
|
||||
(dir == "BG_EVENT_PLAYER_FACING_EAST" && state.facingDirection == Direction.RIGHT)
|
||||
}
|
||||
val bgEvent =
|
||||
currentMap.bgEvents.find { it.x == facingX && it.y == facingY && facingDirOk(it.facingDir) }
|
||||
if (bgEvent != null) {
|
||||
val params = dialogService.scriptParams(bgEvent.script)
|
||||
if (params != null) {
|
||||
log.info {
|
||||
"BG event script=${bgEvent.script} at ($facingX,$facingY) — starting dialog (${params.size} pages)"
|
||||
}
|
||||
session.inDialog = true
|
||||
session.dialogNpcEntityId = npcEntityId
|
||||
session.dialogPages = params
|
||||
session.dialogPageIndex = 0
|
||||
val seqId = session.dialogSeqId++
|
||||
state.inDialog = true
|
||||
state.dialogNpcEntityId = npcEntityId
|
||||
state.dialogPages = params
|
||||
state.dialogPageIndex = 0
|
||||
val seqId = state.dialogSeqId++
|
||||
val page = params[0]
|
||||
event.ctx.channel().write(DialogStatePacket(true))
|
||||
val confirmBuf = Unpooled.buffer(9)
|
||||
confirmBuf.writeLongLE(npcEntityId)
|
||||
confirmBuf.writeByte(0xFF)
|
||||
val confirmBytes = ByteArray(9)
|
||||
confirmBuf.readBytes(confirmBytes)
|
||||
confirmBuf.release()
|
||||
packetSender.sendRaw(event.ctx, 0x07u, confirmBytes)
|
||||
ctx.send(DialogStatePacket(true))
|
||||
dialogService.sendInteractive(
|
||||
event.ctx,
|
||||
ctx,
|
||||
seqId,
|
||||
npcEntityId,
|
||||
page.type,
|
||||
@@ -134,15 +111,10 @@ class InteractionService(
|
||||
page.unk2,
|
||||
page.unk3,
|
||||
)
|
||||
event.ctx.channel().flush()
|
||||
} else {
|
||||
log.info {
|
||||
"BG event interaction: script=${bgEvent.script} at ($facingX,$facingY) — no known params"
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
log.info { "Entity interaction for entity $npcEntityId — not found on current map" }
|
||||
log.info { "Entity interaction for entity $npcEntityId not found on current map" }
|
||||
}
|
||||
}
|
||||
+118
-212
@@ -1,84 +1,99 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.network.PacketEvent
|
||||
import de.fiereu.network.SessionContext
|
||||
import de.fiereu.openmmo.common.CharacterInfo
|
||||
import de.fiereu.openmmo.common.Pokemon
|
||||
import de.fiereu.openmmo.common.enums.ChatType
|
||||
import de.fiereu.openmmo.common.enums.EntityStatus
|
||||
import de.fiereu.openmmo.common.enums.Language
|
||||
import de.fiereu.openmmo.common.enums.PokemonContainer
|
||||
import de.fiereu.openmmo.protocols.game.packets.CharacterEntry
|
||||
import de.fiereu.openmmo.protocols.game.packets.CharacterListPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.ChatMessagePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.CreateCharacterPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.JoinGamePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.LoadEntityPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.PokemonContainerPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.RenderScreenPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.RequestCharactersPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.RequestPlayerPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.SelectCharacterPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.SelectedCharacterPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.codecs.SkinSet
|
||||
import de.fiereu.openmmo.server.game.protocol.game.ext.accept
|
||||
import de.fiereu.openmmo.server.game.protocol.game.ext.buildAndRespond
|
||||
import de.fiereu.openmmo.server.game.session.SessionManager
|
||||
import de.fiereu.openmmo.maps.MapManager
|
||||
import de.fiereu.openmmo.net.game.codecs.SkinSet
|
||||
import de.fiereu.openmmo.net.game.packets.CharacterEntry
|
||||
import de.fiereu.openmmo.net.game.packets.CharactersListPacket
|
||||
import de.fiereu.openmmo.net.game.packets.ChatMessagePacket
|
||||
import de.fiereu.openmmo.net.game.packets.CreateCharacterPacket
|
||||
import de.fiereu.openmmo.net.game.packets.JoinPacket
|
||||
import de.fiereu.openmmo.net.game.packets.JoinResponsePacket
|
||||
import de.fiereu.openmmo.net.game.packets.LoadEntityPacket
|
||||
import de.fiereu.openmmo.net.game.packets.NewAuthData
|
||||
import de.fiereu.openmmo.net.game.packets.PokemonContainerPacket
|
||||
import de.fiereu.openmmo.net.game.packets.RenderScreenPacket
|
||||
import de.fiereu.openmmo.net.game.packets.RequestCharactersPacket
|
||||
import de.fiereu.openmmo.net.game.packets.RequestPlayerPacket
|
||||
import de.fiereu.openmmo.net.game.packets.SelectCharacterPacket
|
||||
import de.fiereu.openmmo.net.game.packets.SelectedCharacterPacket
|
||||
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
||||
import de.fiereu.openmmo.server.game.session.PlayerState
|
||||
import de.fiereu.openmmo.server.game.session.SessionRegistry
|
||||
import de.fiereu.openmmo.server.game.storage.CharacterStore
|
||||
import de.fiereu.openmmo.server.game.world.MapManager
|
||||
import de.fiereu.openmmo.server.protocol.PacketEvent
|
||||
import de.fiereu.openmmo.server.protocol.respond
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import java.time.LocalDateTime
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
class LoginService(
|
||||
@Singleton
|
||||
class LoginService
|
||||
@Inject
|
||||
constructor(
|
||||
private val mapLoadService: MapLoadService,
|
||||
private val npcService: NpcService,
|
||||
private val multiplayerService: MultiplayerService,
|
||||
private val packetSender: PacketSender,
|
||||
private val sessionRegistry: SessionRegistry,
|
||||
private val mapManager: MapManager,
|
||||
private val characterStore: CharacterStore,
|
||||
) {
|
||||
|
||||
fun onJoinGame(event: PacketEvent<JoinGamePacket>) {
|
||||
fun onJoinGame(event: PacketEvent<JoinPacket>) {
|
||||
val ctx = event.session
|
||||
val join = event.packet
|
||||
log.info { "Player joined the game." }
|
||||
|
||||
val authData = join.authData
|
||||
var userId = 0
|
||||
if (authData is de.fiereu.openmmo.protocols.game.packets.NewAuthData) {
|
||||
if (authData is NewAuthData) {
|
||||
userId = authData.userId
|
||||
log.info { "User $userId joined with session key (${authData.sessionKey.size} bytes)" }
|
||||
}
|
||||
|
||||
if (userId > 0) {
|
||||
SessionManager.createSession(event.ctx.channel(), userId)
|
||||
ctx.attributes[PLAYER_STATE] = PlayerState(userId = userId)
|
||||
sessionRegistry.register(ctx)
|
||||
log.info { "Session created for user $userId" }
|
||||
}
|
||||
|
||||
event.accept().withPlaytime(1337).withRewardPoints(420).withBalance(187).buildAndRespond(event)
|
||||
ctx.send(JoinResponsePacket.acceptNow(playtime = 1337, rewardPoints = 420, balance = 187))
|
||||
}
|
||||
|
||||
fun onCreateCharacter(event: PacketEvent<CreateCharacterPacket>) {
|
||||
val session = SessionManager.getSessionByChannel(event.ctx.channel())
|
||||
if (session == null) {
|
||||
val ctx = event.session
|
||||
val state = ctx.attributes[PLAYER_STATE]
|
||||
if (state == null) {
|
||||
log.warn { "Create character from unknown session" }
|
||||
return
|
||||
}
|
||||
val name = event.packet.name
|
||||
log.info { "Creating character '$name' for userId=${session.userId}" }
|
||||
CharacterStore.createCharacter(session.userId, name)
|
||||
log.info { "Created character '$name'" }
|
||||
event.respond(buildCharacterList(session.userId))
|
||||
log.info { "Creating character '$name' for userId=${state.userId}" }
|
||||
characterStore.createCharacter(state.userId, name)
|
||||
ctx.send(buildCharacterList(state.userId))
|
||||
}
|
||||
|
||||
fun onCharacterRequest(event: PacketEvent<RequestCharactersPacket>) {
|
||||
val session = SessionManager.getSessionByChannel(event.ctx.channel())
|
||||
if (session != null) {
|
||||
event.respond(buildCharacterList(session.userId))
|
||||
val ctx = event.session
|
||||
val state = ctx.attributes[PLAYER_STATE]
|
||||
if (state != null) {
|
||||
ctx.send(buildCharacterList(state.userId))
|
||||
} else {
|
||||
log.warn { "Character request from unauthenticated session" }
|
||||
event.respond(CharacterListPacket(emptyList()))
|
||||
ctx.send(CharactersListPacket(emptyList()))
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildCharacterList(userId: Int): CharacterListPacket {
|
||||
val characters = CharacterStore.getCharactersByUser(userId)
|
||||
private fun buildCharacterList(userId: Int): CharactersListPacket {
|
||||
val characters = characterStore.getCharactersByUser(userId)
|
||||
val entries =
|
||||
characters.map { stored ->
|
||||
CharacterEntry(
|
||||
@@ -88,42 +103,42 @@ class LoginService(
|
||||
pokemon = stored.pokemon.take(1),
|
||||
)
|
||||
}
|
||||
return CharacterListPacket(entries)
|
||||
return CharactersListPacket(entries)
|
||||
}
|
||||
|
||||
fun onCharacterSelected(event: PacketEvent<SelectCharacterPacket>) {
|
||||
val ctx = event.session
|
||||
val charId = event.packet.characterId
|
||||
val stored = CharacterStore.getCharacter(charId)
|
||||
|
||||
val stored = characterStore.getCharacter(charId)
|
||||
if (stored == null) {
|
||||
log.warn { "Character $charId not found" }
|
||||
return
|
||||
}
|
||||
|
||||
val session = SessionManager.getSessionByChannel(event.ctx.channel())
|
||||
if (session == null) {
|
||||
val state = ctx.attributes[PLAYER_STATE]
|
||||
if (state == null) {
|
||||
log.warn { "No session for channel" }
|
||||
return
|
||||
}
|
||||
|
||||
SessionManager.setActiveCharacter(event.ctx.channel(), charId)
|
||||
state.characterId = charId
|
||||
sessionRegistry.bindCharacter(ctx, charId)
|
||||
log.info { "Player selected character '${stored.info.name}' (id=$charId)" }
|
||||
|
||||
val info = stored.info
|
||||
val now = LocalDateTime.now()
|
||||
val updatedInfo = info.copy(lastLogin = now)
|
||||
CharacterStore.updateCharacter(updatedInfo)
|
||||
characterStore.updateCharacter(updatedInfo)
|
||||
|
||||
val containers =
|
||||
mapOf(
|
||||
PokemonContainer.PARTY to stored.pokemon,
|
||||
PokemonContainer.PC to stored.pcStorage,
|
||||
PokemonContainer.BATTLE_BOX_1 to emptyList<de.fiereu.openmmo.common.Pokemon>(),
|
||||
PokemonContainer.BATTLE_BOX_2 to emptyList<de.fiereu.openmmo.common.Pokemon>(),
|
||||
PokemonContainer.DAYCARE to emptyList<de.fiereu.openmmo.common.Pokemon>(),
|
||||
PokemonContainer.BATTLE_BOX_1 to emptyList<Pokemon>(),
|
||||
PokemonContainer.BATTLE_BOX_2 to emptyList<Pokemon>(),
|
||||
PokemonContainer.DAYCARE to emptyList<Pokemon>(),
|
||||
)
|
||||
for ((container, pokemon) in containers) {
|
||||
event.respond(
|
||||
ctx.send(
|
||||
PokemonContainerPacket(
|
||||
container = container,
|
||||
hasChange = true,
|
||||
@@ -132,19 +147,8 @@ class LoginService(
|
||||
))
|
||||
}
|
||||
|
||||
sendRawPackets(event, info)
|
||||
preloadMapAndJoin(event, charId, session)
|
||||
}
|
||||
|
||||
private fun sendRawPackets(
|
||||
event: PacketEvent<SelectCharacterPacket>,
|
||||
info: de.fiereu.openmmo.common.CharacterInfo
|
||||
) {
|
||||
packetSender.sendRaw(event.ctx, 0x40u, bytesOf(0x01, 0x01, 0x00, 0x00))
|
||||
packetSender.sendRaw(event.ctx, 0x55u, bytesOf(0x00, 0x00, 0x00))
|
||||
|
||||
listOf(PokemonContainer.UNKNOWN_13, PokemonContainer.UNKNOWN_14).forEach { container ->
|
||||
event.respond(
|
||||
ctx.send(
|
||||
PokemonContainerPacket(
|
||||
container = container,
|
||||
hasChange = true,
|
||||
@@ -153,27 +157,8 @@ class LoginService(
|
||||
))
|
||||
}
|
||||
|
||||
packetSender.sendRaw(event.ctx, 0x67u, bytesOf(0x00, 0x00))
|
||||
packetSender.sendRaw(event.ctx, 0x63u, bytesOf(0x00, 0x00))
|
||||
packetSender.sendRaw(event.ctx, 0x98u, bytesOf(0x00, 0x00, 0x00, 0x00, 0x00, 0x00))
|
||||
packetSender.sendRaw(
|
||||
event.ctx, 0x0Au, bytesOf(0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00))
|
||||
packetSender.sendRaw(event.ctx, 0x29u, bytesOf(0x00, 0x00, 0x00))
|
||||
packetSender.sendRaw(event.ctx, 0x29u, hexToBytes("0101006F080100"))
|
||||
packetSender.sendRaw(event.ctx, 0x29u, bytesOf(0x02, 0x00, 0x00))
|
||||
packetSender.sendRaw(event.ctx, 0x29u, bytesOf(0x03, 0x00, 0x00))
|
||||
packetSender.sendRaw(event.ctx, 0x29u, bytesOf(0x04, 0x00, 0x00))
|
||||
packetSender.sendRaw(event.ctx, 0x29u, bytesOf(0x0A, 0x00, 0x00))
|
||||
packetSender.sendRaw(event.ctx, 0x29u, bytesOf(0x80, 0x00, 0x00))
|
||||
packetSender.sendRaw(event.ctx, 0x1Cu, bytesOf(0x00, 0x00))
|
||||
|
||||
event.respond(SelectedCharacterPacket(info))
|
||||
|
||||
packetSender.sendRaw(event.ctx, 0xF1u, bytesOf(0x05))
|
||||
packetSender.sendRaw(event.ctx, 0x4Au, bytesOf(0x00))
|
||||
packetSender.sendRaw(event.ctx, 0x4Du, bytesOf(0x18))
|
||||
|
||||
event.respond(
|
||||
ctx.send(SelectedCharacterPacket(info))
|
||||
ctx.send(
|
||||
ChatMessagePacket(
|
||||
ChatType.GAME_NOTIFICATIONS,
|
||||
Language.EN,
|
||||
@@ -181,97 +166,29 @@ class LoginService(
|
||||
"",
|
||||
))
|
||||
|
||||
packetSender.sendRaw(
|
||||
event.ctx,
|
||||
0xF5u,
|
||||
bytesOf(
|
||||
0x77,
|
||||
0x1A,
|
||||
0x00,
|
||||
0x00,
|
||||
0x81,
|
||||
0x00,
|
||||
0x05,
|
||||
0x4A,
|
||||
0x00,
|
||||
0x6F,
|
||||
0x00,
|
||||
0x73,
|
||||
0x00,
|
||||
0x75,
|
||||
0x00,
|
||||
0x6B,
|
||||
0x00,
|
||||
0x65,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x10,
|
||||
))
|
||||
|
||||
packetSender.sendRaw(
|
||||
event.ctx,
|
||||
0x4Fu,
|
||||
hexToBytes(
|
||||
"B8009502029202019102029002008F02008C02008902008802028702008602008502008402008302008202008102008002027F02017E02027D02017B02017802037602037402037102026D02026C02016402026102015C02035602015102014F02014302013D02033B02033302023102022B02022902022102021602011202010E0202F10101F00103EE0100ED0100EC0102EB0100EA0100E90100E80101E70100E60100E50101E40100E30100E20100E10100E00100DF0103DE0102DB0103DA0102D90101D80101D50102D40101D10102D00102CE0102CD0101CC0103C60103C20102C00101BD0101BA0102B50101B40103B30103AE0102AD0102A80103A701029701038E01018B01028801018501038201008101018001007F01007E01007D01007C01007B01007A01007901007801017501016401035E01035601014E01034A01024401023F01033401022E01012901032301021E01011A0103170101100103040103010103FE0003FB0000FA0000F90000F80001F50001F40002F30001F20002ED0003E90002E80002E60001E30001E20003DD0003D60002D40001D30003D00003CF0002CD0002C70002C50002C40002C30002BA0002B20003AB0003A90002A000029700009600009500019200039100018F00038D00038700038600038200017900017100016E00016B00036500035E00015B00025200035000014C00034900024400024100033E00033B00033700033100032A00032600022400032200021F0002090003030002080600000F00000C00000B00000D00000700000800000E0000"))
|
||||
|
||||
packetSender.sendRaw(event.ctx, 0xD3u, bytesOf(0x00))
|
||||
|
||||
packetSender.sendRaw(
|
||||
event.ctx,
|
||||
0xFCu,
|
||||
hexToBytes(
|
||||
"10D6B660234D938A916F030CD1B64F9CDD0100040100007F0601000000000000000000000000000000621E0A"))
|
||||
|
||||
packetSender.sendRaw(event.ctx, 0x59u, bytesOf(0x00, 0x00))
|
||||
packetSender.sendRaw(event.ctx, 0x59u, bytesOf(0x01, 0x00))
|
||||
|
||||
packetSender.sendRaw(
|
||||
event.ctx,
|
||||
0x6Eu,
|
||||
bytesOf(
|
||||
0x05,
|
||||
0x92,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0xF5,
|
||||
0x00,
|
||||
0x00))
|
||||
|
||||
packetSender.sendRaw(event.ctx, 0xB9u, bytesOf(0x00))
|
||||
preloadMapAndJoin(ctx, state, info)
|
||||
}
|
||||
|
||||
private fun preloadMapAndJoin(
|
||||
event: PacketEvent<SelectCharacterPacket>,
|
||||
charId: Long,
|
||||
session: de.fiereu.openmmo.server.game.session.Session,
|
||||
ctx: SessionContext,
|
||||
state: PlayerState,
|
||||
info: CharacterInfo,
|
||||
) {
|
||||
val info = CharacterStore.getCharacter(charId)?.info ?: return
|
||||
|
||||
val map = MapManager.getMap(info.positionRegionId, info.positionBankId, info.positionMapId)
|
||||
val map = mapManager.getMap(info.positionRegionId, info.positionBankId, info.positionMapId)
|
||||
if (map != null) {
|
||||
event.respond(MapManager.createLoadMapPacket(map, reloadPlayer = true, deleteCache = true))
|
||||
mapLoadService.preloadConnectedMaps(event.ctx, map)
|
||||
ctx.send(mapManager.createLoadMapPacket(map, reloadPlayer = true, deleteCache = true))
|
||||
mapLoadService.preloadConnectedMaps(ctx, map)
|
||||
} else {
|
||||
log.warn {
|
||||
"Map not found for position ${info.positionRegionId}:${info.positionBankId}:${info.positionMapId}"
|
||||
}
|
||||
}
|
||||
|
||||
session.regionId = info.positionRegionId.toInt()
|
||||
session.bankId = info.positionBankId.toInt()
|
||||
session.mapId = info.positionMapId.toInt()
|
||||
session.x = info.positionX
|
||||
session.y = info.positionY
|
||||
state.regionId = info.positionRegionId.toInt()
|
||||
state.bankId = info.positionBankId.toInt()
|
||||
state.mapId = info.positionMapId.toInt()
|
||||
state.x = info.positionX
|
||||
state.y = info.positionY
|
||||
|
||||
multiplayerService.broadcastMessage(
|
||||
ChatMessagePacket(
|
||||
@@ -283,72 +200,68 @@ class LoginService(
|
||||
}
|
||||
|
||||
fun onRequestPlayer(event: PacketEvent<RequestPlayerPacket>) {
|
||||
log.info { "Received RequestPlayerPacket" }
|
||||
val session = SessionManager.getSessionByChannel(event.ctx.channel())
|
||||
if (session == null) {
|
||||
val ctx = event.session
|
||||
val state = ctx.attributes[PLAYER_STATE]
|
||||
if (state == null) {
|
||||
log.warn { "RequestPlayer from unknown session" }
|
||||
return
|
||||
}
|
||||
|
||||
val charId = session.characterId
|
||||
val charId = state.characterId
|
||||
if (charId == null) {
|
||||
log.warn { "RequestPlayer without active character" }
|
||||
return
|
||||
}
|
||||
val stored = CharacterStore.getCharacter(charId)
|
||||
val stored = characterStore.getCharacter(charId)
|
||||
if (stored == null) {
|
||||
log.warn { "RequestPlayer for unknown character $charId" }
|
||||
return
|
||||
}
|
||||
val info = stored.info
|
||||
|
||||
log.info { "Sending LoadEntity + 0x90 for character '${info.name}'" }
|
||||
val facing = session.facingDirection
|
||||
log.info { "Sending LoadEntity for character '${info.name}'" }
|
||||
val facing = state.facingDirection
|
||||
val loadEntity = mapLoadService.createLoadEntity(info, facing)
|
||||
ctx.send(loadEntity)
|
||||
|
||||
event.ctx.channel().writeAndFlush(loadEntity)
|
||||
mapLoadService.send90(event.ctx, loadEntity.entityId, loadEntity.skin)
|
||||
npcService.spawnNpcsForMap(event.ctx, info.positionBankId.toInt(), info.positionMapId.toInt())
|
||||
npcService.spawnNpcsForMap(ctx, info.positionBankId.toInt(), info.positionMapId.toInt())
|
||||
|
||||
val bankId = info.positionBankId.toInt()
|
||||
val mapId = info.positionMapId.toInt()
|
||||
val regionId = info.positionRegionId.toInt()
|
||||
session.regionId = regionId
|
||||
session.bankId = bankId
|
||||
session.mapId = mapId
|
||||
session.x = info.positionX
|
||||
session.y = info.positionY
|
||||
state.regionId = regionId
|
||||
state.bankId = bankId
|
||||
state.mapId = mapId
|
||||
state.x = info.positionX
|
||||
state.y = info.positionY
|
||||
|
||||
val currentParty = CharacterStore.getCharacter(charId)?.pokemon ?: emptyList()
|
||||
val currentParty = characterStore.getCharacter(charId)?.pokemon ?: emptyList()
|
||||
val hasFollower = currentParty.isNotEmpty()
|
||||
val followerDexId = (currentParty.firstOrNull()?.dexId ?: 0).toShort()
|
||||
|
||||
// Send LoadEntity of each existing player in this map to the new player
|
||||
val others = SessionManager.getOthersInMap(charId, regionId, bankId, mapId)
|
||||
val others = sessionRegistry.getOthersInMap(charId, regionId, bankId, mapId)
|
||||
for (other in others) {
|
||||
val otherStored = CharacterStore.getCharacter(other.characterId ?: continue) ?: continue
|
||||
val otherParty = otherStored.pokemon
|
||||
event.ctx
|
||||
.channel()
|
||||
.write(
|
||||
LoadEntityPacket(
|
||||
entityId = other.characterId!!,
|
||||
skin = SkinSet(),
|
||||
name = otherStored.info.name,
|
||||
regionId = other.regionId,
|
||||
bankId = other.bankId,
|
||||
mapId = other.mapId,
|
||||
x = other.x.toInt(),
|
||||
y = other.y.toInt(),
|
||||
z = 0,
|
||||
facing = other.facingDirection,
|
||||
status = de.fiereu.openmmo.common.enums.EntityStatus.NONE,
|
||||
hasFollower = otherParty.isNotEmpty(),
|
||||
followerDexId = (otherParty.firstOrNull()?.dexId ?: 0).toShort(),
|
||||
))
|
||||
val otherState = other.attributes[PLAYER_STATE] ?: continue
|
||||
val otherCharId = otherState.characterId ?: continue
|
||||
val otherStored = characterStore.getCharacter(otherCharId) ?: continue
|
||||
val otherParty: List<Pokemon> = otherStored.pokemon
|
||||
ctx.send(
|
||||
LoadEntityPacket(
|
||||
entityId = otherCharId,
|
||||
skin = SkinSet(),
|
||||
name = otherStored.info.name,
|
||||
regionId = otherState.regionId,
|
||||
bankId = otherState.bankId,
|
||||
mapId = otherState.mapId,
|
||||
x = otherState.x.toInt(),
|
||||
y = otherState.y.toInt(),
|
||||
z = 0,
|
||||
facing = otherState.facingDirection,
|
||||
status = EntityStatus.NONE,
|
||||
hasFollower = otherParty.isNotEmpty(),
|
||||
followerDexId = (otherParty.firstOrNull()?.dexId ?: 0).toShort(),
|
||||
))
|
||||
}
|
||||
|
||||
// Broadcast LoadEntity of new player to all existing players in this map
|
||||
val selfEntity =
|
||||
LoadEntityPacket(
|
||||
entityId = charId,
|
||||
@@ -361,22 +274,15 @@ class LoginService(
|
||||
y = info.positionY.toInt(),
|
||||
z = 0,
|
||||
facing = facing,
|
||||
status = de.fiereu.openmmo.common.enums.EntityStatus.NONE,
|
||||
status = EntityStatus.NONE,
|
||||
hasFollower = hasFollower,
|
||||
followerDexId = followerDexId,
|
||||
)
|
||||
for (other in others) {
|
||||
other.channel.writeAndFlush(selfEntity)
|
||||
other.send(selfEntity)
|
||||
}
|
||||
|
||||
event.ctx.channel().writeAndFlush(RenderScreenPacket(true))
|
||||
ctx.send(RenderScreenPacket(true))
|
||||
log.info { "Player $charId spawned in bank=$bankId map=$mapId; ${others.size} others present" }
|
||||
}
|
||||
|
||||
private fun bytesOf(vararg ints: Int) = ByteArray(ints.size) { ints[it].toByte() }
|
||||
|
||||
private fun hexToBytes(hex: String): ByteArray {
|
||||
val hexStr = hex.replace(" ", "").replace("\n", "")
|
||||
return ByteArray(hexStr.length / 2) { hexStr.substring(it * 2, it * 2 + 2).toInt(16).toByte() }
|
||||
}
|
||||
}
|
||||
+24
-54
@@ -1,19 +1,23 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.network.SessionContext
|
||||
import de.fiereu.openmmo.common.CharacterInfo
|
||||
import de.fiereu.openmmo.common.enums.Direction
|
||||
import de.fiereu.openmmo.protocols.game.packets.LoadEntityPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.MapData
|
||||
import de.fiereu.openmmo.protocols.game.packets.codecs.SkinSet
|
||||
import de.fiereu.openmmo.server.game.world.MapDef
|
||||
import de.fiereu.openmmo.server.game.world.MapManager
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.netty.buffer.Unpooled
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import de.fiereu.openmmo.common.enums.EntityStatus
|
||||
import de.fiereu.openmmo.maps.MapDef
|
||||
import de.fiereu.openmmo.maps.MapManager
|
||||
import de.fiereu.openmmo.net.game.codecs.SkinSet
|
||||
import de.fiereu.openmmo.net.game.packets.LoadEntityPacket
|
||||
import de.fiereu.openmmo.net.game.packets.MapData
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
class MapLoadService(private val packetSender: PacketSender) {
|
||||
@Singleton
|
||||
class MapLoadService
|
||||
@Inject
|
||||
constructor(
|
||||
private val mapManager: MapManager,
|
||||
) {
|
||||
|
||||
fun createLoadEntity(
|
||||
info: CharacterInfo,
|
||||
@@ -31,47 +35,17 @@ class MapLoadService(private val packetSender: PacketSender) {
|
||||
y = info.positionY.toInt(),
|
||||
z = z,
|
||||
facing = facing,
|
||||
status = de.fiereu.openmmo.common.enums.EntityStatus.NONE,
|
||||
status = EntityStatus.NONE,
|
||||
hasFollower = false,
|
||||
followerDexId = 0,
|
||||
)
|
||||
}
|
||||
|
||||
fun send90(ctx: ChannelHandlerContext, entityId: Long, skin: SkinSet) {
|
||||
val raw90Buf = Unpooled.buffer()
|
||||
raw90Buf.writeLongLE(entityId)
|
||||
raw90Buf.writeByte(0x01)
|
||||
raw90Buf.writeByte(0x00)
|
||||
raw90Buf.writeByte(0x4C)
|
||||
raw90Buf.writeByte(0x03)
|
||||
raw90Buf.writeBytes(
|
||||
byteArrayOf(
|
||||
0x13,
|
||||
0xA8.toByte(),
|
||||
0x0A,
|
||||
0x00,
|
||||
0x03,
|
||||
0x8C.toByte(),
|
||||
0x01,
|
||||
0x38.toByte(),
|
||||
0x02,
|
||||
0x4C,
|
||||
0x01))
|
||||
val raw90 = ByteArray(raw90Buf.readableBytes())
|
||||
raw90Buf.readBytes(raw90)
|
||||
raw90Buf.release()
|
||||
log.info {
|
||||
">> TX 0x90 payload (${raw90.size}b): ${raw90.joinToString("") { "%02X".format(it) }}"
|
||||
}
|
||||
packetSender.sendRaw(ctx, 0x90u, raw90)
|
||||
}
|
||||
|
||||
fun preloadConnectedMaps(
|
||||
ctx: ChannelHandlerContext,
|
||||
ctx: SessionContext,
|
||||
map: MapDef,
|
||||
depth: Int = 2,
|
||||
reloadPlayer: Boolean = false,
|
||||
flush: Boolean = true,
|
||||
) {
|
||||
val loaded = mutableSetOf<String>()
|
||||
loaded.add("${map.bankId}:${map.mapId}")
|
||||
@@ -80,22 +54,18 @@ class MapLoadService(private val packetSender: PacketSender) {
|
||||
for (conn in connections) {
|
||||
val key = "${conn.targetBank}:${conn.targetMap}"
|
||||
if (!loaded.add(key)) continue
|
||||
val connected = MapManager.getMap(1, conn.targetBank.toByte(), conn.targetMap.toByte())
|
||||
val connected = mapManager.getMap(1, conn.targetBank, conn.targetMap)
|
||||
if (connected != null) {
|
||||
ctx.channel()
|
||||
.write(
|
||||
MapManager.createLoadMapPacket(
|
||||
connected,
|
||||
reloadPlayer = reloadPlayer,
|
||||
deleteCache = false,
|
||||
))
|
||||
ctx.send(
|
||||
mapManager.createLoadMapPacket(
|
||||
connected,
|
||||
reloadPlayer = reloadPlayer,
|
||||
deleteCache = false,
|
||||
))
|
||||
preload(connected.connections, remaining - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
preload(map.connections, depth)
|
||||
if (flush) {
|
||||
ctx.channel().flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
+62
-85
@@ -1,59 +1,61 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.network.PacketEvent
|
||||
import de.fiereu.network.SessionContext
|
||||
import de.fiereu.openmmo.common.enums.Direction
|
||||
import de.fiereu.openmmo.protocols.game.packets.EntityMovePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.FaceDirectionPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.MapData
|
||||
import de.fiereu.openmmo.protocols.game.packets.MovementPacket
|
||||
import de.fiereu.openmmo.server.game.session.SessionManager
|
||||
import de.fiereu.openmmo.maps.MapManager
|
||||
import de.fiereu.openmmo.net.game.packets.EntityMovePacket
|
||||
import de.fiereu.openmmo.net.game.packets.FaceDirectionPacket
|
||||
import de.fiereu.openmmo.net.game.packets.MapData
|
||||
import de.fiereu.openmmo.net.game.packets.MovementPacket
|
||||
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
||||
import de.fiereu.openmmo.server.game.storage.CharacterStore
|
||||
import de.fiereu.openmmo.server.game.world.MapManager
|
||||
import de.fiereu.openmmo.server.protocol.PacketEvent
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
class MovementService(
|
||||
@Singleton
|
||||
class MovementService
|
||||
@Inject
|
||||
constructor(
|
||||
private val warpService: WarpService,
|
||||
private val mapLoadService: MapLoadService,
|
||||
private val npcService: NpcService,
|
||||
private val multiplayerService: MultiplayerService,
|
||||
private val mapManager: MapManager,
|
||||
private val characterStore: CharacterStore,
|
||||
) {
|
||||
private val sequenceCounter = AtomicInteger(0)
|
||||
|
||||
fun onMovement(event: PacketEvent<MovementPacket>) {
|
||||
val session = SessionManager.getSessionByChannel(event.ctx.channel())
|
||||
if (session == null) return
|
||||
|
||||
val charId = session.characterId ?: return
|
||||
val ctx = event.session
|
||||
val state = ctx.attributes[PLAYER_STATE] ?: return
|
||||
val charId = state.characterId ?: return
|
||||
val msg = event.packet
|
||||
|
||||
log.info { "Movement: char=$charId, x=${msg.x}, y=${msg.y}, dir=${msg.direction}" }
|
||||
session.facingDirection = msg.direction
|
||||
state.facingDirection = msg.direction
|
||||
|
||||
// Store previous position for detecting wall bumps
|
||||
val prevStored = CharacterStore.getCharacter(charId)
|
||||
val prevStored = characterStore.getCharacter(charId)
|
||||
val prevX = prevStored?.info?.positionX?.toInt()
|
||||
val prevY = prevStored?.info?.positionY?.toInt()
|
||||
val isWallBump = prevX == msg.x && prevY == msg.y
|
||||
|
||||
// Check for warps and edge transitions BEFORE updating position.
|
||||
val stored = CharacterStore.getCharacter(charId)
|
||||
val stored = characterStore.getCharacter(charId)
|
||||
if (stored != null) {
|
||||
val currentMap =
|
||||
MapManager.getMap(
|
||||
mapManager.getMap(
|
||||
stored.info.positionRegionId,
|
||||
stored.info.positionBankId,
|
||||
stored.info.positionMapId,
|
||||
)
|
||||
if (currentMap != null) {
|
||||
// Skip warp check for the first movement after a warp
|
||||
if (session.justWarped) {
|
||||
session.justWarped = false
|
||||
if (state.justWarped) {
|
||||
state.justWarped = false
|
||||
} else {
|
||||
// Direct warp: player walked onto a warp tile (not just standing on it)
|
||||
val warp =
|
||||
currentMap.warps.find { w ->
|
||||
w.x == msg.x &&
|
||||
@@ -62,14 +64,11 @@ class MovementService(
|
||||
!isWallBump
|
||||
}
|
||||
if (warp != null) {
|
||||
log.info {
|
||||
"DIRECT WARP at (${msg.x}, ${msg.y}) facing ${msg.direction} → ${warp.targetRegionId}:${warp.targetBankId}:${warp.targetMapId} (${warp.targetX}, ${warp.targetY})"
|
||||
}
|
||||
warpService.executeWarp(event.ctx, charId, warp)
|
||||
log.info { "DIRECT WARP at (${msg.x}, ${msg.y}) facing ${msg.direction}" }
|
||||
warpService.executeWarp(ctx, charId, warp)
|
||||
return
|
||||
}
|
||||
|
||||
// Adjacent warp: player is standing in front of a door tile, facing it
|
||||
val adjX =
|
||||
msg.x +
|
||||
when (msg.direction) {
|
||||
@@ -91,37 +90,29 @@ class MovementService(
|
||||
(w.facingDirection == null || w.facingDirection == msg.direction)
|
||||
}
|
||||
if (adjWarp != null) {
|
||||
log.info {
|
||||
"ADJACENT WARP at (${msg.x}, ${msg.y}) facing ${msg.direction} → tile ($adjX, $adjY) → ${adjWarp.targetRegionId}:${adjWarp.targetBankId}:${adjWarp.targetMapId} (${adjWarp.targetX}, ${adjWarp.targetY})"
|
||||
}
|
||||
warpService.executeWarp(event.ctx, charId, adjWarp)
|
||||
log.info { "ADJACENT WARP at (${msg.x}, ${msg.y}) facing ${msg.direction}" }
|
||||
warpService.executeWarp(ctx, charId, adjWarp)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Map edge transition
|
||||
if (prevX != null && prevY != null && !isWallBump) {
|
||||
val dx = kotlin.math.abs(msg.x - prevX)
|
||||
val dy = kotlin.math.abs(msg.y - prevY)
|
||||
log.info {
|
||||
"EDGE CHECK: prev=($prevX, $prevY) msg=(${msg.x}, ${msg.y}) dx=$dx dy=$dy map=${currentMap.bankId}:${currentMap.mapId} h=${currentMap.height} w=${currentMap.width}"
|
||||
}
|
||||
if (dx > 1 || dy > 1) {
|
||||
val gbaDirection =
|
||||
when {
|
||||
prevY == 0 -> 2 // North
|
||||
prevY == currentMap.height - 1 -> 1 // South
|
||||
prevX == 0 -> 3 // West
|
||||
prevX == currentMap.width - 1 -> 4 // East
|
||||
prevY == 0 -> 2
|
||||
prevY == currentMap.height - 1 -> 1
|
||||
prevX == 0 -> 3
|
||||
prevX == currentMap.width - 1 -> 4
|
||||
else -> null
|
||||
}
|
||||
log.info { "BIG JUMP: gbaDirection=$gbaDirection" }
|
||||
if (gbaDirection != null) {
|
||||
val connection = currentMap.connections.find { it.direction == gbaDirection }
|
||||
log.info { "Connection found: $connection" }
|
||||
if (connection != null) {
|
||||
val targetMap =
|
||||
MapManager.getMap(
|
||||
mapManager.getMap(
|
||||
1,
|
||||
connection.targetBank.toByte(),
|
||||
connection.targetMap.toByte(),
|
||||
@@ -131,27 +122,16 @@ class MovementService(
|
||||
when (gbaDirection) {
|
||||
3 -> targetMap.width - 1
|
||||
4 -> 0
|
||||
else ->
|
||||
(prevX - connection.unknown).coerceIn(
|
||||
0,
|
||||
targetMap.width - 1,
|
||||
)
|
||||
else -> (prevX - connection.unknown).coerceIn(0, targetMap.width - 1)
|
||||
}
|
||||
val targetY =
|
||||
when (gbaDirection) {
|
||||
1 -> 0
|
||||
2 -> targetMap.height - 1
|
||||
else ->
|
||||
(prevY - connection.unknown).coerceIn(
|
||||
0,
|
||||
targetMap.height - 1,
|
||||
)
|
||||
else -> (prevY - connection.unknown).coerceIn(0, targetMap.height - 1)
|
||||
}
|
||||
log.info {
|
||||
"MAP TRANSITION prev=($prevX, $prevY) dir=${msg.direction} → bank=${connection.targetBank} map=${connection.targetMap} ($targetX, $targetY)"
|
||||
}
|
||||
edgeTransition(
|
||||
event.ctx,
|
||||
ctx,
|
||||
charId,
|
||||
connection,
|
||||
targetX.toByte(),
|
||||
@@ -167,10 +147,9 @@ class MovementService(
|
||||
}
|
||||
}
|
||||
|
||||
// No transition: normal movement update + EntityMove broadcast
|
||||
CharacterStore.updatePosition(charId, msg.x.toShort(), msg.y.toShort())
|
||||
session.x = msg.x.toShort()
|
||||
session.y = msg.y.toShort()
|
||||
characterStore.updatePosition(charId, msg.x.toShort(), msg.y.toShort())
|
||||
state.x = msg.x.toShort()
|
||||
state.y = msg.y.toShort()
|
||||
if (!isWallBump) {
|
||||
val seq = sequenceCounter.incrementAndGet().toByte()
|
||||
val movePkt =
|
||||
@@ -181,21 +160,20 @@ class MovementService(
|
||||
direction = msg.direction,
|
||||
seq = seq,
|
||||
)
|
||||
event.ctx.channel().writeAndFlush(movePkt)
|
||||
multiplayerService.broadcastExcept(event.ctx.channel(), movePkt)
|
||||
ctx.send(movePkt)
|
||||
multiplayerService.broadcastExcept(ctx, movePkt)
|
||||
}
|
||||
}
|
||||
|
||||
fun onFaceDirection(event: PacketEvent<FaceDirectionPacket>) {
|
||||
val session = SessionManager.getSessionByChannel(event.ctx.channel())
|
||||
if (session == null) return
|
||||
|
||||
val charId = session.characterId ?: return
|
||||
val ctx = event.session
|
||||
val state = ctx.attributes[PLAYER_STATE] ?: return
|
||||
val charId = state.characterId ?: return
|
||||
val msg = event.packet
|
||||
session.facingDirection = msg.direction
|
||||
state.facingDirection = msg.direction
|
||||
|
||||
val seq = sequenceCounter.incrementAndGet().toByte()
|
||||
val stored = CharacterStore.getCharacter(charId) ?: return
|
||||
val stored = characterStore.getCharacter(charId) ?: return
|
||||
val movePkt =
|
||||
EntityMovePacket(
|
||||
entityId = charId,
|
||||
@@ -204,12 +182,12 @@ class MovementService(
|
||||
direction = msg.direction,
|
||||
seq = seq,
|
||||
)
|
||||
event.ctx.channel().writeAndFlush(movePkt)
|
||||
multiplayerService.broadcastExcept(event.ctx.channel(), movePkt)
|
||||
ctx.send(movePkt)
|
||||
multiplayerService.broadcastExcept(ctx, movePkt)
|
||||
}
|
||||
|
||||
private fun edgeTransition(
|
||||
ctx: ChannelHandlerContext,
|
||||
ctx: SessionContext,
|
||||
charId: Long,
|
||||
connection: MapData.GbaConnection,
|
||||
targetX: Byte,
|
||||
@@ -218,14 +196,16 @@ class MovementService(
|
||||
) {
|
||||
val targetBank = connection.targetBank.toByte()
|
||||
val targetMap = connection.targetMap.toByte()
|
||||
val map = MapManager.getMap(1, targetBank, targetMap) ?: return
|
||||
val map = mapManager.getMap(1, targetBank, targetMap) ?: return
|
||||
|
||||
val session = SessionManager.getSessionByCharacterId(charId)
|
||||
session?.bankId = targetBank.toInt()
|
||||
session?.mapId = targetMap.toInt()
|
||||
session?.x = targetX.toShort()
|
||||
session?.y = targetY.toShort()
|
||||
CharacterStore.updatePosition(
|
||||
val state = ctx.attributes[PLAYER_STATE]
|
||||
if (state != null) {
|
||||
state.bankId = targetBank.toInt()
|
||||
state.mapId = targetMap.toInt()
|
||||
state.x = targetX.toShort()
|
||||
state.y = targetY.toShort()
|
||||
}
|
||||
characterStore.updatePosition(
|
||||
charId,
|
||||
targetX.toShort(),
|
||||
targetY.toShort(),
|
||||
@@ -233,9 +213,7 @@ class MovementService(
|
||||
targetMap,
|
||||
)
|
||||
|
||||
ctx.channel()
|
||||
.write(MapManager.createLoadMapPacket(map, reloadPlayer = false, deleteCache = false))
|
||||
mapLoadService.preloadConnectedMaps(ctx, map, depth = 1, flush = false)
|
||||
mapLoadService.preloadConnectedMaps(ctx, map, depth = 1)
|
||||
npcService.spawnNpcsForMap(ctx, targetBank.toInt(), targetMap.toInt())
|
||||
|
||||
val seq = sequenceCounter.incrementAndGet().toByte()
|
||||
@@ -247,9 +225,8 @@ class MovementService(
|
||||
direction = direction,
|
||||
seq = seq,
|
||||
)
|
||||
ctx.channel().write(movePkt)
|
||||
multiplayerService.broadcastExcept(ctx.channel(), movePkt)
|
||||
ctx.channel().flush()
|
||||
ctx.send(movePkt)
|
||||
multiplayerService.broadcastExcept(ctx, movePkt)
|
||||
log.info { "Player $charId edge-transitioned to bank=$targetBank map=$targetMap" }
|
||||
}
|
||||
}
|
||||
+19
-12
@@ -1,24 +1,31 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.openmmo.server.game.session.SessionManager
|
||||
import io.netty.channel.Channel
|
||||
import de.fiereu.network.SessionContext
|
||||
import de.fiereu.openmmo.server.game.session.SessionRegistry
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
class MultiplayerService {
|
||||
@Singleton
|
||||
class MultiplayerService
|
||||
@Inject
|
||||
constructor(
|
||||
private val sessionRegistry: SessionRegistry,
|
||||
) {
|
||||
|
||||
fun broadcastMessage(packet: Any) {
|
||||
for (characterId in SessionManager.getOnlineCharacterIds()) {
|
||||
val s = SessionManager.getSessionByCharacterId(characterId) ?: continue
|
||||
if (s.channel.isActive) {
|
||||
s.channel.writeAndFlush(packet)
|
||||
for (characterId in sessionRegistry.onlineCharacterIds()) {
|
||||
val ctx = sessionRegistry.getByCharacterId(characterId) ?: continue
|
||||
if (ctx.channel.isActive) {
|
||||
ctx.send(packet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun broadcastExcept(channel: Channel, packet: Any) {
|
||||
for (characterId in SessionManager.getOnlineCharacterIds()) {
|
||||
val s = SessionManager.getSessionByCharacterId(characterId) ?: continue
|
||||
if (s.channel.isActive && s.channel != channel) {
|
||||
s.channel.writeAndFlush(packet)
|
||||
fun broadcastExcept(exclude: SessionContext, packet: Any) {
|
||||
for (characterId in sessionRegistry.onlineCharacterIds()) {
|
||||
val ctx = sessionRegistry.getByCharacterId(characterId) ?: continue
|
||||
if (ctx.channel.isActive && ctx.channel != exclude.channel) {
|
||||
ctx.send(packet)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.openmmo.protocols.game.packets.NpcSpawnPacket
|
||||
import de.fiereu.openmmo.server.game.world.MapManager
|
||||
import de.fiereu.network.SessionContext
|
||||
import de.fiereu.openmmo.maps.MapManager
|
||||
import de.fiereu.openmmo.net.game.packets.NpcSpawnPacket
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
class NpcService {
|
||||
@Singleton
|
||||
class NpcService
|
||||
@Inject
|
||||
constructor(
|
||||
private val mapManager: MapManager,
|
||||
) {
|
||||
|
||||
private val npcEntityIdCounter = AtomicLong(0x1A69000000000000L)
|
||||
private val npcEntityIds = mutableMapOf<String, Long>()
|
||||
@@ -17,13 +24,9 @@ class NpcService {
|
||||
return npcEntityIds["$bankId:$mapId:$entityIdx"]
|
||||
}
|
||||
|
||||
fun spawnNpcsForMap(ctx: ChannelHandlerContext, bankId: Int, mapId: Int) {
|
||||
val map = MapManager.getMap(1, bankId.toByte(), mapId.toByte())
|
||||
if (map == null) return
|
||||
val entityIds = mutableListOf<Long>()
|
||||
fun spawnNpcsForMap(ctx: SessionContext, bankId: Int, mapId: Int) {
|
||||
val map = mapManager.getMap(1, bankId, mapId) ?: return
|
||||
|
||||
// Petalburg Woods (bank=74, map=11) — send all 16 NPCs (0-15),
|
||||
// using exact values from real server packets.db rows 3895-3910
|
||||
if (bankId == 74 && mapId == 11) {
|
||||
data class NpcEntry(
|
||||
val entityIdx: Int,
|
||||
@@ -34,7 +37,7 @@ class NpcService {
|
||||
val unk3: Int,
|
||||
val unk4: Int,
|
||||
val facing: Int,
|
||||
val unk6: Int
|
||||
val unk6: Int,
|
||||
)
|
||||
val all16 =
|
||||
listOf(
|
||||
@@ -59,7 +62,7 @@ class NpcService {
|
||||
for (npc in all16) {
|
||||
val entityId = baseEntityId or npc.entityIdx.toLong()
|
||||
log.info {
|
||||
">> Petalburg NpcSpawn[${npc.entityIdx}] entId=0x${entityId.toString(16)} pos=(${npc.x},${npc.y}) gfx=${npc.gfx} unk3=${npc.unk3} unk4=${npc.unk4}"
|
||||
">> Petalburg NpcSpawn[${npc.entityIdx}] entId=0x${entityId.toString(16)} pos=(${npc.x},${npc.y}) gfx=${npc.gfx}"
|
||||
}
|
||||
val spawnPacket =
|
||||
NpcSpawnPacket(
|
||||
@@ -77,13 +80,11 @@ class NpcService {
|
||||
unk5 = 2,
|
||||
unk6 = npc.unk6,
|
||||
)
|
||||
ctx.channel().write(spawnPacket)
|
||||
entityIds.add(entityId)
|
||||
ctx.send(spawnPacket)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// All other maps: send visible NPCs from MapDef
|
||||
for (npc in map.npcs) {
|
||||
val key = "$bankId:$mapId:${npc.entityIdx}"
|
||||
val entityId = npcEntityIds.getOrPut(key) { npcEntityIdCounter.incrementAndGet() }
|
||||
@@ -110,8 +111,7 @@ class NpcService {
|
||||
unk5 = 2,
|
||||
unk6 = 8,
|
||||
)
|
||||
ctx.channel().write(spawnPacket)
|
||||
entityIds.add(entityId)
|
||||
ctx.send(spawnPacket)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.openmmo.server.netty.Compressor
|
||||
import io.netty.buffer.Unpooled
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
|
||||
class PacketSender(private val compressor: Compressor) {
|
||||
|
||||
fun sendRaw(ctx: ChannelHandlerContext, opcode: UByte, data: ByteArray) {
|
||||
val buffer = ctx.alloc().buffer()
|
||||
buffer.writeByte(opcode.toInt())
|
||||
val dataBuf = Unpooled.wrappedBuffer(data)
|
||||
buffer.writeBytes(compressor.compress(dataBuf))
|
||||
ctx.write(buffer)
|
||||
}
|
||||
|
||||
fun sendRawAndFlush(ctx: ChannelHandlerContext, opcode: UByte, data: ByteArray) {
|
||||
val buffer = ctx.alloc().buffer()
|
||||
buffer.writeByte(opcode.toInt())
|
||||
val dataBuf = Unpooled.wrappedBuffer(data)
|
||||
buffer.writeBytes(compressor.compress(dataBuf))
|
||||
ctx.writeAndFlush(buffer)
|
||||
}
|
||||
}
|
||||
@@ -1,35 +1,41 @@
|
||||
package de.fiereu.openmmo.server.game.services
|
||||
|
||||
import de.fiereu.network.SessionContext
|
||||
import de.fiereu.openmmo.common.enums.Direction
|
||||
import de.fiereu.openmmo.protocols.game.packets.EntityLeavePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.MapTransitionAckPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.MapTransitionPacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.RenderScreenPacket
|
||||
import de.fiereu.openmmo.server.game.session.SessionManager
|
||||
import de.fiereu.openmmo.maps.MapManager
|
||||
import de.fiereu.openmmo.maps.WarpTile
|
||||
import de.fiereu.openmmo.net.game.packets.EntityLeavePacket
|
||||
import de.fiereu.openmmo.net.game.packets.MapTransitionAckPacket
|
||||
import de.fiereu.openmmo.net.game.packets.MapTransitionPacket
|
||||
import de.fiereu.openmmo.net.game.packets.RenderScreenPacket
|
||||
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
|
||||
import de.fiereu.openmmo.server.game.session.SessionRegistry
|
||||
import de.fiereu.openmmo.server.game.storage.CharacterStore
|
||||
import de.fiereu.openmmo.server.game.world.MapManager
|
||||
import de.fiereu.openmmo.server.game.world.WarpExitRules
|
||||
import de.fiereu.openmmo.server.game.world.WarpTile
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
private val log = KotlinLogging.logger {}
|
||||
|
||||
class WarpService(
|
||||
@Singleton
|
||||
class WarpService
|
||||
@Inject
|
||||
constructor(
|
||||
private val mapLoadService: MapLoadService,
|
||||
private val mapManager: MapManager,
|
||||
private val characterStore: CharacterStore,
|
||||
private val sessionRegistry: SessionRegistry,
|
||||
) {
|
||||
|
||||
fun executeWarp(ctx: ChannelHandlerContext, charId: Long, warp: WarpTile) {
|
||||
val session = SessionManager.getSessionByCharacterId(charId)
|
||||
if (session != null) {
|
||||
session.justWarped = true
|
||||
}
|
||||
val stored = CharacterStore.getCharacter(charId) ?: return
|
||||
|
||||
val destMap = MapManager.getMap(warp.targetRegionId, warp.targetBankId, warp.targetMapId)
|
||||
fun executeWarp(ctx: SessionContext, charId: Long, warp: WarpTile) {
|
||||
val state = ctx.attributes[PLAYER_STATE]
|
||||
state?.justWarped = true
|
||||
val stored = characterStore.getCharacter(charId) ?: return
|
||||
|
||||
val destMap = mapManager.getMap(warp.targetRegionId, warp.targetBankId, warp.targetMapId)
|
||||
val sourceMap =
|
||||
MapManager.getMap(
|
||||
mapManager.getMap(
|
||||
stored.info.positionRegionId, stored.info.positionBankId, stored.info.positionMapId)
|
||||
|
||||
val knownOverride =
|
||||
@@ -42,21 +48,19 @@ class WarpService(
|
||||
destMap = destMap,
|
||||
destX = warp.targetX,
|
||||
destY = warp.targetY,
|
||||
sourceMap = sourceMap)
|
||||
sourceMap = sourceMap,
|
||||
)
|
||||
|
||||
session?.facingDirection = warpFacing
|
||||
state?.facingDirection = warpFacing
|
||||
|
||||
var offsetX = warp.targetX
|
||||
var offsetY = warp.targetY
|
||||
|
||||
val shouldAutoStepOffWarp =
|
||||
knownOverride?.autoStep
|
||||
?: WarpExitRules.shouldAutoStep(
|
||||
sourceMap = sourceMap,
|
||||
destMap = destMap,
|
||||
)
|
||||
if (shouldAutoStepOffWarp) {
|
||||
val destWarp = destMap?.warps?.find { it.x == offsetX && it.y == offsetY }
|
||||
?: WarpExitRules.shouldAutoStep(sourceMap = sourceMap, destMap = destMap)
|
||||
if (shouldAutoStepOffWarp && destMap != null) {
|
||||
val destWarp = destMap.warps.find { it.x == offsetX && it.y == offsetY }
|
||||
if (destWarp != null) {
|
||||
when (warpFacing) {
|
||||
Direction.UP -> offsetY--
|
||||
@@ -64,8 +68,8 @@ class WarpService(
|
||||
Direction.LEFT -> offsetX--
|
||||
Direction.RIGHT -> offsetX++
|
||||
}
|
||||
offsetX = offsetX.coerceIn(0, destMap!!.width - 1)
|
||||
offsetY = offsetY.coerceIn(0, destMap!!.height - 1)
|
||||
offsetX = offsetX.coerceIn(0, destMap.width - 1)
|
||||
offsetY = offsetY.coerceIn(0, destMap.height - 1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,10 +78,7 @@ class WarpService(
|
||||
?: warp.targetElevation
|
||||
|
||||
log.info {
|
||||
"WARP EXIT: source=${sourceMap?.bankId}:${sourceMap?.mapId} ${sourceMap?.mapType} " +
|
||||
"dest=${destMap?.bankId}:${destMap?.mapId} ${destMap?.mapType} " +
|
||||
"target=(${warp.targetX},${warp.targetY}) final=($offsetX,$offsetY) " +
|
||||
"z=$playerZ facing=$warpFacing autoStep=$shouldAutoStepOffWarp"
|
||||
"WARP EXIT: source=${sourceMap?.bankId}:${sourceMap?.mapId} dest=${destMap?.bankId}:${destMap?.mapId} target=(${warp.targetX},${warp.targetY}) final=($offsetX,$offsetY) z=$playerZ facing=$warpFacing autoStep=$shouldAutoStepOffWarp"
|
||||
}
|
||||
|
||||
val newInfo =
|
||||
@@ -88,43 +89,34 @@ class WarpService(
|
||||
positionX = offsetX.toShort(),
|
||||
positionY = offsetY.toShort(),
|
||||
)
|
||||
CharacterStore.updateCharacter(newInfo)
|
||||
characterStore.updateCharacter(newInfo)
|
||||
|
||||
// Multiplayer: broadcast EntityLeave to old-map players before position update
|
||||
val oldRegionId = session?.regionId
|
||||
val oldBankId = session?.bankId
|
||||
val oldMapId = session?.mapId
|
||||
val oldRegionId = state?.regionId
|
||||
val oldBankId = state?.bankId
|
||||
val oldMapId = state?.mapId
|
||||
if (oldBankId != null && oldMapId != null) {
|
||||
val oldMapPlayers =
|
||||
SessionManager.getOthersInMap(charId, oldRegionId ?: 1, oldBankId, oldMapId)
|
||||
sessionRegistry.getOthersInMap(charId, oldRegionId ?: 1, oldBankId, oldMapId)
|
||||
for (other in oldMapPlayers) {
|
||||
other.channel.writeAndFlush(EntityLeavePacket(charId))
|
||||
other.send(EntityLeavePacket(charId))
|
||||
}
|
||||
}
|
||||
|
||||
// Update session position for multiplayer tracking
|
||||
if (session != null) {
|
||||
session.regionId = warp.targetRegionId.toInt()
|
||||
session.bankId = warp.targetBankId.toInt()
|
||||
session.mapId = warp.targetMapId.toInt()
|
||||
session.x = offsetX.toShort()
|
||||
session.y = offsetY.toShort()
|
||||
if (state != null) {
|
||||
state.regionId = warp.targetRegionId.toInt()
|
||||
state.bankId = warp.targetBankId.toInt()
|
||||
state.mapId = warp.targetMapId.toInt()
|
||||
state.x = offsetX.toShort()
|
||||
state.y = offsetY.toShort()
|
||||
}
|
||||
|
||||
// Real server warp sequence (from packets.db):
|
||||
ctx.channel().writeAndFlush(MapTransitionPacket())
|
||||
ctx.channel().writeAndFlush(RenderScreenPacket(false))
|
||||
ctx.channel().writeAndFlush(mapLoadService.createLoadEntity(newInfo, warpFacing, playerZ))
|
||||
ctx.channel().writeAndFlush(MapTransitionAckPacket(0))
|
||||
ctx.send(MapTransitionPacket())
|
||||
ctx.send(RenderScreenPacket(false))
|
||||
ctx.send(mapLoadService.createLoadEntity(newInfo, warpFacing, playerZ))
|
||||
ctx.send(MapTransitionAckPacket(0))
|
||||
|
||||
if (destMap != null) {
|
||||
ctx.channel()
|
||||
.writeAndFlush(
|
||||
MapManager.createLoadMapPacket(
|
||||
destMap,
|
||||
reloadPlayer = true,
|
||||
deleteCache = true,
|
||||
))
|
||||
ctx.send(mapManager.createLoadMapPacket(destMap, reloadPlayer = true, deleteCache = true))
|
||||
mapLoadService.preloadConnectedMaps(ctx, destMap, depth = 1, reloadPlayer = true)
|
||||
} else {
|
||||
log.warn {
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
package de.fiereu.openmmo.server.game.session
|
||||
|
||||
import de.fiereu.openmmo.common.enums.Direction
|
||||
import io.netty.channel.Channel
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
data class ScriptPage(
|
||||
val type: Int = 0x04,
|
||||
val unk1: Int,
|
||||
val unk2: Int,
|
||||
val unk3: Int,
|
||||
)
|
||||
|
||||
data class Session(
|
||||
val channel: Channel,
|
||||
val userId: Int,
|
||||
var characterId: Long? = null,
|
||||
var justWarped: Boolean = false,
|
||||
var facingDirection: Direction = Direction.DOWN,
|
||||
var inDialog: Boolean = false,
|
||||
var dialogNpcEntityId: Long = 0,
|
||||
var dialogSeqId: Int = 0,
|
||||
var dialogPages: List<ScriptPage> = emptyList(),
|
||||
var dialogPageIndex: Int = 0,
|
||||
var regionId: Int = 1,
|
||||
var bankId: Int = 51,
|
||||
var mapId: Int = 3,
|
||||
var x: Short = 4,
|
||||
var y: Short = 2,
|
||||
)
|
||||
|
||||
object SessionManager {
|
||||
private val sessionsByChannel = ConcurrentHashMap<Channel, Session>()
|
||||
private val sessionsByCharacter = ConcurrentHashMap<Long, Session>()
|
||||
|
||||
fun createSession(channel: Channel, userId: Int): Session {
|
||||
removeSession(channel)
|
||||
val session = Session(channel, userId)
|
||||
sessionsByChannel[channel] = session
|
||||
return session
|
||||
}
|
||||
|
||||
fun getSessionByChannel(channel: Channel): Session? = sessionsByChannel[channel]
|
||||
|
||||
fun getSessionByCharacterId(characterId: Long): Session? = sessionsByCharacter[characterId]
|
||||
|
||||
fun setActiveCharacter(channel: Channel, characterId: Long) {
|
||||
val session = sessionsByChannel[channel] ?: return
|
||||
sessionsByCharacter[characterId] = session
|
||||
session.characterId = characterId
|
||||
}
|
||||
|
||||
fun removeSession(channel: Channel) {
|
||||
val session = sessionsByChannel.remove(channel)
|
||||
if (session != null && session.characterId != null) {
|
||||
sessionsByCharacter.remove(session.characterId)
|
||||
}
|
||||
}
|
||||
|
||||
fun getOnlineCharacterIds(): Set<Long> = sessionsByCharacter.keys
|
||||
|
||||
fun isOnline(characterId: Long): Boolean = sessionsByCharacter.containsKey(characterId)
|
||||
|
||||
fun getOthersInMap(
|
||||
excludeCharacterId: Long,
|
||||
regionId: Int,
|
||||
bankId: Int,
|
||||
mapId: Int,
|
||||
): List<Session> =
|
||||
sessionsByCharacter.values.filter { session ->
|
||||
session.characterId != excludeCharacterId &&
|
||||
session.regionId == regionId &&
|
||||
session.bankId == bankId &&
|
||||
session.mapId == mapId
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package de.fiereu.openmmo.server.game.session
|
||||
|
||||
import de.fiereu.openmmo.common.enums.Direction
|
||||
|
||||
data class ScriptPage(
|
||||
val type: Int = 0x04,
|
||||
val unk1: Int,
|
||||
val unk2: Int,
|
||||
val unk3: Int,
|
||||
)
|
||||
|
||||
data class PlayerState(
|
||||
val userId: Int,
|
||||
var characterId: Long? = null,
|
||||
var justWarped: Boolean = false,
|
||||
var facingDirection: Direction = Direction.DOWN,
|
||||
var inDialog: Boolean = false,
|
||||
var dialogNpcEntityId: Long = 0,
|
||||
var dialogSeqId: Int = 0,
|
||||
var dialogPages: List<ScriptPage> = emptyList(),
|
||||
var dialogPageIndex: Int = 0,
|
||||
var regionId: Int = 1,
|
||||
var bankId: Int = 51,
|
||||
var mapId: Int = 3,
|
||||
var x: Short = 4,
|
||||
var y: Short = 2,
|
||||
)
|
||||
@@ -0,0 +1,5 @@
|
||||
package de.fiereu.openmmo.server.game.session
|
||||
|
||||
import de.fiereu.network.SessionAttribute
|
||||
|
||||
val PLAYER_STATE = SessionAttribute.of<PlayerState>("playerState")
|
||||
@@ -0,0 +1,54 @@
|
||||
package de.fiereu.openmmo.server.game.session
|
||||
|
||||
import de.fiereu.network.SessionContext
|
||||
import io.netty.channel.Channel
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class SessionRegistry @Inject constructor() {
|
||||
private val sessionsByChannel = ConcurrentHashMap<Channel, SessionContext>()
|
||||
private val sessionsByCharacter = ConcurrentHashMap<Long, SessionContext>()
|
||||
|
||||
fun register(ctx: SessionContext) {
|
||||
sessionsByChannel[ctx.channel] = ctx
|
||||
}
|
||||
|
||||
fun unregister(ctx: SessionContext) {
|
||||
sessionsByChannel.remove(ctx.channel)
|
||||
val state = ctx.attributes[PLAYER_STATE]
|
||||
val charId = state?.characterId
|
||||
if (charId != null) {
|
||||
sessionsByCharacter.remove(charId)
|
||||
}
|
||||
}
|
||||
|
||||
fun bindCharacter(ctx: SessionContext, characterId: Long) {
|
||||
sessionsByCharacter[characterId] = ctx
|
||||
}
|
||||
|
||||
fun unbindCharacter(characterId: Long) {
|
||||
sessionsByCharacter.remove(characterId)
|
||||
}
|
||||
|
||||
fun getByCharacterId(id: Long): SessionContext? = sessionsByCharacter[id]
|
||||
|
||||
fun getOthersInMap(
|
||||
excludeCharacterId: Long,
|
||||
regionId: Int,
|
||||
bankId: Int,
|
||||
mapId: Int,
|
||||
): List<SessionContext> =
|
||||
sessionsByCharacter.values.filter { ctx ->
|
||||
val state = ctx.attributes[PLAYER_STATE] ?: return@filter false
|
||||
state.characterId != excludeCharacterId &&
|
||||
state.regionId == regionId &&
|
||||
state.bankId == bankId &&
|
||||
state.mapId == mapId
|
||||
}
|
||||
|
||||
fun onlineCharacterIds(): Set<Long> = sessionsByCharacter.keys
|
||||
|
||||
fun isOnline(id: Long): Boolean = sessionsByCharacter.containsKey(id)
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package de.fiereu.openmmo.server.game.session
|
||||
|
||||
import de.fiereu.network.SessionContext
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter
|
||||
|
||||
class SessionRegistryHandler(
|
||||
private val registry: SessionRegistry,
|
||||
private val session: SessionContext,
|
||||
) : ChannelInboundHandlerAdapter() {
|
||||
|
||||
override fun channelActive(ctx: ChannelHandlerContext) {
|
||||
registry.register(session)
|
||||
ctx.fireChannelActive()
|
||||
}
|
||||
|
||||
override fun channelInactive(ctx: ChannelHandlerContext) {
|
||||
registry.unregister(session)
|
||||
ctx.fireChannelInactive()
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import de.fiereu.openmmo.common.Pokemon
|
||||
import java.time.LocalDateTime
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.atomic.AtomicLong
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
data class StoredCharacter(
|
||||
val info: CharacterInfo,
|
||||
@@ -13,11 +15,11 @@ data class StoredCharacter(
|
||||
val items: MutableMap<Int, Int>,
|
||||
)
|
||||
|
||||
object CharacterStore {
|
||||
@Singleton
|
||||
class CharacterStore @Inject constructor() {
|
||||
private val characters = ConcurrentHashMap<Long, StoredCharacter>()
|
||||
private val charactersByUser = ConcurrentHashMap<Int, MutableList<Long>>()
|
||||
private val nextCharId = AtomicLong(1)
|
||||
private val nextPokemonId = AtomicLong(1)
|
||||
|
||||
init {
|
||||
ensureTestCharacter()
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -2,6 +2,7 @@ package de.fiereu.openmmo.server.game.world
|
||||
|
||||
import de.fiereu.openmmo.common.enums.Direction
|
||||
import de.fiereu.openmmo.common.enums.MapType
|
||||
import de.fiereu.openmmo.maps.MapDef
|
||||
|
||||
data class WarpExitOverride(
|
||||
val facing: Direction,
|
||||
@@ -19,11 +20,9 @@ object WarpExitRules {
|
||||
): Direction {
|
||||
if (destMap == null) return Direction.DOWN
|
||||
|
||||
// Future proper tile-behavior support.
|
||||
when (destTileBehavior) {
|
||||
"MB_NON_ANIMATED_DOOR",
|
||||
"MB_ANIMATED_DOOR" -> return Direction.DOWN
|
||||
|
||||
"MB_NORTH_ARROW_WARP" -> return Direction.DOWN
|
||||
"MB_SOUTH_ARROW_WARP" -> return Direction.UP
|
||||
"MB_WEST_ARROW_WARP" -> return Direction.RIGHT
|
||||
@@ -32,27 +31,22 @@ object WarpExitRules {
|
||||
|
||||
val sourceMapType = sourceMap?.mapType
|
||||
|
||||
// Temporary hardcoded special cases until tile behavior is parsed.
|
||||
getKnownOverride(sourceMap, destMap, destX, destY)?.let {
|
||||
return it.facing
|
||||
}
|
||||
|
||||
// Entering a cave/tunnel from outside.
|
||||
if (isUndergroundMap(destMap.mapType) && !isUndergroundMap(sourceMapType)) {
|
||||
return Direction.UP
|
||||
}
|
||||
|
||||
// Entering a building from outside.
|
||||
if (isBuildingMap(destMap.mapType) && !isBuildingMap(sourceMapType)) {
|
||||
return Direction.UP
|
||||
}
|
||||
|
||||
// Exiting a building to outside.
|
||||
if (isBuildingMap(sourceMapType) && !isBuildingMap(destMap.mapType)) {
|
||||
return Direction.DOWN
|
||||
}
|
||||
|
||||
// Room-to-room.
|
||||
if (isBuildingMap(sourceMapType) && isBuildingMap(destMap.mapType)) {
|
||||
return Direction.DOWN
|
||||
}
|
||||
@@ -68,7 +62,6 @@ object WarpExitRules {
|
||||
): WarpExitOverride? {
|
||||
if (destMap == null) return null
|
||||
|
||||
// Rusturf Tunnel: face UP into the tunnel, no auto-step.
|
||||
if (destMap.bankId.toInt() == 74 && destMap.mapId.toInt() == 4) {
|
||||
return when {
|
||||
destX == 4 && destY == 10 -> WarpExitOverride(Direction.UP, autoStep = false)
|
||||
@@ -78,17 +71,14 @@ object WarpExitRules {
|
||||
}
|
||||
}
|
||||
|
||||
// Petalburg Woods: facing only, no auto-step.
|
||||
if (destMap.bankId.toInt() == 74 && destMap.mapId.toInt() == 11) {
|
||||
return when {
|
||||
(destX == 14 && destY == 5) || (destX == 15 && destY == 5) ->
|
||||
WarpExitOverride(Direction.DOWN, autoStep = false)
|
||||
|
||||
(destX == 16 && destY == 38) ||
|
||||
(destX == 17 && destY == 38) ||
|
||||
(destX == 36 && destY == 38) ||
|
||||
(destX == 37 && destY == 38) -> WarpExitOverride(Direction.UP, autoStep = false)
|
||||
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
@@ -124,10 +114,7 @@ object WarpExitRules {
|
||||
val enteringUnderground =
|
||||
destMap.mapType == MapType.UNDERGROUND && sourceMap.mapType != MapType.UNDERGROUND
|
||||
|
||||
// House/building exit to outside.
|
||||
if (sourceBuilding && !destBuilding) return true
|
||||
|
||||
// Cave/tunnel entrance from outside.
|
||||
if (enteringUnderground) return true
|
||||
|
||||
return false
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
game.private.pem
|
||||
game.public.pem
|
||||
@@ -1 +1,8 @@
|
||||
server.port = 2106
|
||||
server {
|
||||
host = "0.0.0.0"
|
||||
port = 7777
|
||||
checksumSize = 2
|
||||
rootKeyResource = "game.private.pem"
|
||||
sessionSecret = "dev-only-secret-do-not-use-in-production"
|
||||
sessionSecret = ${?OPENMMO_SESSION_SECRET}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
create extension if not exists pgcrypto;
|
||||
|
||||
create table "character" (
|
||||
id bigserial not null,
|
||||
user_id int not null,
|
||||
name varchar(16) not null unique,
|
||||
name_prefix varchar(16) not null default '',
|
||||
rival_sex smallint not null default 0,
|
||||
created_at timestamp not null default current_timestamp,
|
||||
last_login timestamp not null default current_timestamp,
|
||||
money int not null default 3000,
|
||||
permissions int not null default 0,
|
||||
remaining_safari_steps smallint not null default 0,
|
||||
remaining_safari_balls smallint not null default 0,
|
||||
pc_extra_slots smallint not null default 0,
|
||||
battle_box_extra_slots smallint not null default 0,
|
||||
template_amount smallint not null default 0,
|
||||
position_region_id smallint not null default 0,
|
||||
position_bank_id smallint not null default 1,
|
||||
position_map_id smallint not null default 1,
|
||||
position_x smallint not null default 100,
|
||||
position_y smallint not null default 100,
|
||||
repel_left smallint not null default 0,
|
||||
repel_item_id smallint not null default 0,
|
||||
lure_left smallint not null default 0,
|
||||
lure_item_id smallint not null default 0,
|
||||
primary key (id)
|
||||
);
|
||||
|
||||
create table pokemon (
|
||||
id bigserial not null,
|
||||
character_id bigint not null,
|
||||
container smallint not null default 0,
|
||||
container_slot smallint not null default 0,
|
||||
dex_id int not null,
|
||||
seed int not null,
|
||||
ot varchar(16) not null,
|
||||
nickname varchar(32) not null,
|
||||
level smallint not null default 1,
|
||||
hp smallint not null default 10,
|
||||
xp int not null default 0,
|
||||
ivs int not null default 0,
|
||||
ev_hp smallint not null default 0,
|
||||
ev_atk smallint not null default 0,
|
||||
ev_def smallint not null default 0,
|
||||
ev_spatk smallint not null default 0,
|
||||
ev_spdef smallint not null default 0,
|
||||
ev_spd smallint not null default 0,
|
||||
move1_id smallint not null default 0,
|
||||
move1_pp smallint not null default 0,
|
||||
move2_id smallint not null default 0,
|
||||
move2_pp smallint not null default 0,
|
||||
move3_id smallint not null default 0,
|
||||
move3_pp smallint not null default 0,
|
||||
move4_id smallint not null default 0,
|
||||
move4_pp smallint not null default 0,
|
||||
is_shiny boolean not null default false,
|
||||
has_hidden_ability boolean not null default false,
|
||||
is_alpha boolean not null default false,
|
||||
is_secret boolean not null default false,
|
||||
is_fateful_encounter boolean not null default false,
|
||||
is_raid_encounter boolean not null default false,
|
||||
caught_at timestamp not null default current_timestamp,
|
||||
primary key (id),
|
||||
foreign key (character_id) references "character"(id) on delete cascade
|
||||
);
|
||||
|
||||
create table item (
|
||||
id bigserial not null,
|
||||
character_id bigint not null,
|
||||
item_id int not null,
|
||||
amount int not null default 1,
|
||||
primary key (id),
|
||||
foreign key (character_id) references "character"(id) on delete cascade
|
||||
);
|
||||
@@ -1,11 +1,10 @@
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="TRACE">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
</configuration>
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package de.fiereu.openmmo.server.game.handler
|
||||
|
||||
import de.fiereu.openmmo.net.game.packets.ChatMessagePacket
|
||||
import de.fiereu.openmmo.net.game.packets.JoinPacket
|
||||
import de.fiereu.openmmo.net.game.packets.KeepAlivePacket
|
||||
import de.fiereu.openmmo.net.game.packets.MovementPacket
|
||||
import de.fiereu.openmmo.server.game.config.GameServerConfig
|
||||
import de.fiereu.openmmo.server.game.di.DaggerGameServerComponent
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
|
||||
class GameAppHandlerTest :
|
||||
FunSpec({
|
||||
test("dagger graph builds and handler registers expected packet types") {
|
||||
val config =
|
||||
GameServerConfig(
|
||||
host = "127.0.0.1",
|
||||
port = 0,
|
||||
checksumSize = 2,
|
||||
rootKeyResource = "game.private.pem",
|
||||
sessionSecret = "test-secret".toByteArray(),
|
||||
)
|
||||
val component = DaggerGameServerComponent.factory().create(config)
|
||||
val handler = component.handlerProvider().get()
|
||||
handler.isRegistered(JoinPacket::class) shouldBe true
|
||||
handler.isRegistered(MovementPacket::class) shouldBe true
|
||||
handler.isRegistered(ChatMessagePacket::class) shouldBe true
|
||||
handler.isRegistered(KeepAlivePacket::class) shouldBe true
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user