Skip to content

Commit

Permalink
feat: PointedWorldPositions
Browse files Browse the repository at this point in the history
  • Loading branch information
CoasterFreakDE committed Apr 30, 2024
1 parent e077bb4 commit ca26fa0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ import kotlinx.serialization.Serializable
@Serializable
data class PlayerPositionUpdate(
val playerUid: String,
val position: WorldPosition
val position: PointedWorldPosition
) : RabbitSendable
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package de.themeparkcraft.audioserver.common.interfaces

import kotlinx.serialization.Serializable

/**
* Represents a pointed position in the world, specified by its x, y, z coordinates, the world it belongs to,
* and the yaw and pitch rotations.
*
* @see WorldPosition
*/
class PointedWorldPosition(
x: Double, y: Double, z: Double, world: String,
@Serializable
class PointedWorldPosition (
val _x: Double, val _y: Double, val _z: Double, val _world: String,
/**
* The yaw rotation of a pointed world position.
*
Expand All @@ -31,4 +34,10 @@ class PointedWorldPosition(
* @see WorldPosition
*/
val pitch: Float
) : WorldPosition(x, y, z, world)
) : WorldPosition(_x, _y, _z, _world) {

override fun toString(): String {
return "PointedWorldPosition(x=$x, y=$y, z=$z, world='$world', yaw=$yaw, pitch=$pitch)"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package de.themeparkcraft.audioserver.common.interfaces

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromByteArray
import kotlinx.serialization.encodeToByteArray
import kotlinx.serialization.protobuf.ProtoBuf

Expand All @@ -14,9 +13,4 @@ sealed interface RabbitSendable {
return ProtoBuf.encodeToByteArray(this)
}

@OptIn(ExperimentalSerializationApi::class)
fun decode(data: ByteArray): RabbitSendable {
return ProtoBuf.decodeFromByteArray(data)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ open class WorldPosition(
* Represents the name of the world.
*/
val world: String
): RabbitSendable
): RabbitSendable {

override fun toString(): String {
return "WorldPosition(x=$x, y=$y, z=$z, world='$world')"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class RabbitClient(rabbitConfiguration: RabbitConfiguration) {
fun sendMessage(rabbitSendable: RabbitSendable) {
CoroutineScope(Dispatchers.Default).launch {
val message = rabbitSendable.encode()
val combinedMessage = (rabbitSendable::class.java.simpleName + ":").toByteArray(Charset.forName("UTF-8")) + message
channel.basicPublish(EXCHANGE_NAME, ROUTIING_KEY, null, message)
}
}
Expand All @@ -77,15 +76,9 @@ class RabbitClient(rabbitConfiguration: RabbitConfiguration) {
@OptIn(ExperimentalSerializationApi::class)
fun deserializeRabbitMessage(message: ByteArray): RabbitSendable {
val messageString = message.toString(Charset.forName("UTF-8"))
getLogger().info("Received message: $messageString")
// val splitIndex = messageString.indexOf(":")
// val className = messageString.substring(0, splitIndex)
// val messageBody = messageString.substring(splitIndex + 1).toByteArray(Charset.forName("UTF-8"))
getLogger().info("Deserializing message: $messageString")
try {
// val customClass = Class.forName("de.themeparkcraft.audioserver.common.interfaces.$className")
val sendable = ProtoBuf.decodeFromByteArray <RabbitSendable>(message)

return sendable
return ProtoBuf.decodeFromByteArray <RabbitSendable>(message)
} catch (e: Exception) {
getLogger().error("Failed to deserialize message: $messageString", e)
throw e
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kotlin.code.style=official
audioServerVersion=0.4-Snapshot
audioServerVersion=0.5-Snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package de.themeparkcraft.audioserver

import de.themeparkcraft.audioserver.common.data.RabbitConfiguration
import de.themeparkcraft.audioserver.common.extensions.getLogger
import de.themeparkcraft.audioserver.common.interfaces.PlayerPositionUpdate
import de.themeparkcraft.audioserver.common.rabbit.RabbitClient
import de.themeparkcraft.audioserver.utils.Environment
import kotlin.system.exitProcess
Expand All @@ -25,8 +26,14 @@ class AudioServerStandalone {
rabbitClient = RabbitClient(rabbitConfiguration)

rabbitClient.withListener { message, delivery ->
val rabbitSendable = rabbitClient.deserializeRabbitMessage(delivery.body)
getLogger().info("Received message: ${rabbitSendable.javaClass.simpleName}")
when (val rabbitSendable = rabbitClient.deserializeRabbitMessage(delivery.body)) {
is PlayerPositionUpdate -> {
getLogger().info("Received PlayerPositionUpdate message: ${rabbitSendable.playerUid} at ${rabbitSendable.position}")
}
else -> {
getLogger().warn("Received unknown RabbitSendable message: $rabbitSendable")
}
}
}

getLogger().info("AudioServer standalone started.")
Expand Down

0 comments on commit ca26fa0

Please sign in to comment.