added JoinResponsePacket.kt + Builders + ext
added game protocol compression
This commit is contained in:
1 parent
f5e18d8540
commit
88e888dfc9
16 files changed
+272
-16
No files matched your search
@@ -4,6 +4,9 @@ import de.fiereu.openmmo.protocols.Protocol
|
||||
import de.fiereu.openmmo.protocols.game.packets.JoinGamePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.JoinPacketDeserializer
|
||||
import de.fiereu.openmmo.protocols.game.packets.JoinPacketSerializer
|
||||
import de.fiereu.openmmo.protocols.game.packets.JoinResponsePacket
|
||||
import de.fiereu.openmmo.protocols.game.packets.JoinResponsePacketDeserializer
|
||||
import de.fiereu.openmmo.protocols.game.packets.JoinResponsePacketSerializer
|
||||
import de.fiereu.openmmo.protocols.incomingPacket
|
||||
import de.fiereu.openmmo.protocols.outgoingPacket
|
||||
|
||||
@@ -14,16 +17,20 @@ import de.fiereu.openmmo.protocols.outgoingPacket
|
||||
abstract class GameProtocol : Protocol() {
|
||||
override val async: Boolean
|
||||
get() = true
|
||||
override val compressed: Boolean
|
||||
get() = true
|
||||
}
|
||||
|
||||
class GameServerProtocol : GameProtocol() {
|
||||
init {
|
||||
incomingPacket(0x01u, JoinPacketDeserializer())
|
||||
outgoingPacket(0x01u, JoinResponsePacketSerializer(), JoinResponsePacket::class)
|
||||
}
|
||||
}
|
||||
|
||||
class GameClientProtocol : GameProtocol() {
|
||||
init {
|
||||
outgoingPacket(0x01u, JoinPacketSerializer(), JoinGamePacket::class)
|
||||
incomingPacket(0x01u, JoinResponsePacketDeserializer())
|
||||
}
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package de.fiereu.openmmo.protocols.game.packets
|
||||
|
||||
import de.fiereu.openmmo.protocols.PacketDeserializer
|
||||
import de.fiereu.openmmo.protocols.PacketSerializer
|
||||
import de.fiereu.openmmo.protocols.readUtf16LE
|
||||
import de.fiereu.openmmo.protocols.writeUtf16LE
|
||||
import io.netty.buffer.ByteBuf
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
|
||||
/*
|
||||
* Some info to the packet:
|
||||
* The packet is sent in response to a JoinGamePacket BUT
|
||||
* seems to also be sent as some kind of time sync packet
|
||||
*/
|
||||
data class JoinResponsePacket(
|
||||
val canJoin: Boolean,
|
||||
val stats: GameStats? = null
|
||||
// we dont need TimeInfo here since it can be calculated on the fly
|
||||
) {
|
||||
data class GameStats(
|
||||
val playtime: Int,
|
||||
val rewardPoints: Int,
|
||||
val balance: Int
|
||||
)
|
||||
}
|
||||
|
||||
class JoinResponsePacketSerializer : PacketSerializer<JoinResponsePacket> {
|
||||
override fun serialize(packet: JoinResponsePacket, buffer: ByteBuf) {
|
||||
buffer.writeBoolean(packet.canJoin)
|
||||
if (!packet.canJoin) return // no further data is sent
|
||||
|
||||
// the following fields are not used by the client, so we send empty/default values
|
||||
buffer.writeUtf16LE("")
|
||||
buffer.writeByte(0)
|
||||
|
||||
requireNotNull(packet.stats) { "Stats must be provided if canJoin is true" }
|
||||
buffer.writeIntLE(packet.stats.playtime) // Playtime
|
||||
buffer.writeIntLE(packet.stats.rewardPoints) // Reward Points
|
||||
buffer.writeIntLE(packet.stats.balance) // Balance (pokeyen obvsly)
|
||||
|
||||
// start of current day in epoch seconds
|
||||
// used to calculate the current hour
|
||||
buffer.writeIntLE(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toEpochSecond().toInt())
|
||||
// Used to calculate server time / desync
|
||||
buffer.writeIntLE(((System.currentTimeMillis() / 1000).toInt()))
|
||||
}
|
||||
}
|
||||
|
||||
class JoinResponsePacketDeserializer : PacketDeserializer<JoinResponsePacketDeserializer.JoinResponsePacket> {
|
||||
data class JoinResponsePacket(
|
||||
val canJoin: Boolean,
|
||||
val stats: GameStats? = null,
|
||||
val time: TimeInfo? = null
|
||||
) {
|
||||
|
||||
data class GameStats(
|
||||
val playtime: Int,
|
||||
val rewardPoints: Int,
|
||||
val balance: Int
|
||||
)
|
||||
|
||||
data class TimeInfo(
|
||||
val serverDayStartSecond: Int,
|
||||
val serverCurrentSecond: Int
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
override fun deserialize(buffer: ByteBuf): JoinResponsePacket {
|
||||
if (!buffer.readBoolean()) {
|
||||
return JoinResponsePacket(false, null)
|
||||
}
|
||||
|
||||
// unused
|
||||
buffer.readUtf16LE()
|
||||
buffer.readByte()
|
||||
|
||||
val playtime = buffer.readUnsignedIntLE().toInt()
|
||||
val rewardPoints = buffer.readUnsignedIntLE().toInt()
|
||||
val balance = buffer.readUnsignedIntLE().toInt()
|
||||
|
||||
val serverDayStartSecond = buffer.readUnsignedIntLE().toInt()
|
||||
val serverCurrentSecond = buffer.readUnsignedIntLE().toInt()
|
||||
|
||||
return JoinResponsePacket(
|
||||
true,
|
||||
JoinResponsePacket.GameStats(playtime, rewardPoints, balance),
|
||||
JoinResponsePacket.TimeInfo(serverDayStartSecond, serverCurrentSecond)
|
||||
)
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package de.fiereu.openmmo.protocols.game.packets.builders
|
||||
|
||||
import de.fiereu.openmmo.protocols.game.packets.JoinResponsePacket
|
||||
|
||||
class JoinResponsePacketBuilder(
|
||||
private val canJoin: Boolean
|
||||
) {
|
||||
private var playtime: Int = 0
|
||||
private var rewardPoints: Int = 0
|
||||
private var balance: Int = 0
|
||||
|
||||
companion object {
|
||||
fun reject(): JoinResponsePacket {
|
||||
return JoinResponsePacket(false)
|
||||
}
|
||||
|
||||
fun accept(): JoinResponsePacketBuilder {
|
||||
return JoinResponsePacketBuilder(true)
|
||||
}
|
||||
}
|
||||
|
||||
fun withPlaytime(playtime: Int): JoinResponsePacketBuilder {
|
||||
this.playtime = playtime
|
||||
return this
|
||||
}
|
||||
|
||||
fun withRewardPoints(rewardPoints: Int): JoinResponsePacketBuilder {
|
||||
this.rewardPoints = rewardPoints
|
||||
return this
|
||||
}
|
||||
|
||||
fun withBalance(balance: Int): JoinResponsePacketBuilder {
|
||||
this.balance = balance
|
||||
return this
|
||||
}
|
||||
|
||||
fun build(): JoinResponsePacket {
|
||||
require(canJoin) { "Cannot build a JoinResponsePacket with canJoin=false. Use JoinResponsePacketBuilder.reject() instead." }
|
||||
return JoinResponsePacket(
|
||||
canJoin = true,
|
||||
stats = JoinResponsePacket.GameStats(
|
||||
playtime = playtime,
|
||||
rewardPoints = rewardPoints,
|
||||
balance = balance
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user