Skip to content

Commit

Permalink
2.1.0
Browse files Browse the repository at this point in the history
- Update to Kotlin 1.5.31
- Update to kx.ser 1.3.0
- Acknowledge last stats update when calculating position
  • Loading branch information
DRSchlaubi committed Sep 28, 2021
1 parent c7020ab commit 3e47aad
Show file tree
Hide file tree
Showing 26 changed files with 94 additions and 106 deletions.
23 changes: 1 addition & 22 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
}

group = "dev.schlaubi.lavakord"
version = "2.0.2"
version = "2.1.0"

allprojects {
repositories {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "me.schlaubi"
version = "2.0.2"
version = "2.1.0"

repositories {
mavenCentral()
Expand Down
3 changes: 2 additions & 1 deletion buildSrc/src/main/kotlin/Compiler.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
object ExpermientalAnnotations {
const val requiresOptIn = "kotlin.RequiresOptIn"
const val experimentalTime = "kotlin.time.ExperimentalTime"
const val experimentalSerialization = "kotlinx.serialization.ExperimentalSerializationApi"
}

object Jvm {
const val target = "1.8"
}
}
6 changes: 3 additions & 3 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.kotlin.dsl.maven

object Versions {
const val kotlin = "1.5.30"
const val ktor = "1.6.2"
const val kotlin = "1.5.31"
const val ktor = "1.6.3"
const val coroutines = "1.5.2"
const val kotlinxSerialization = "1.2.2"
const val kotlinxSerialization = "1.3.0"
const val kotlinLogging = "2.0.11"
const val kord = "0.8.0-M5"
const val atomicFu = "0.16.3"
Expand Down
8 changes: 5 additions & 3 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
`maven-publish`
}

version = "2.0.2"
version = "2.1.0"

kotlin {
explicitApi()
Expand Down Expand Up @@ -34,8 +34,9 @@ kotlin {

sourceSets {
all {
languageSettings.useExperimentalAnnotation(ExpermientalAnnotations.requiresOptIn)
languageSettings.useExperimentalAnnotation(ExpermientalAnnotations.experimentalTime)
languageSettings.optIn(ExpermientalAnnotations.requiresOptIn)
languageSettings.optIn(ExpermientalAnnotations.experimentalTime)
languageSettings.optIn(ExpermientalAnnotations.experimentalSerialization)
repositories {
mavenCentral()
}
Expand All @@ -45,6 +46,7 @@ kotlin {
dependencies {
api(Dependencies.coroutines)
api(Dependencies.kotlinxSerialization)
api("org.jetbrains.kotlinx:kotlinx-datetime:0.3.0")

implementation(Dependencies.`ktor-io`)
implementation(Dependencies.`ktor-utils`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public interface Node : EventSource<TrackEvent>, Closeable {
/**
* The [LavaKord] instance which created this node.
*/
public val lavakord: dev.schlaubi.lavakord.LavaKord
public val lavakord: LavaKord

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import io.ktor.http.*
import io.ktor.util.*
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.launch
import kotlinx.serialization.modules.plus

/**
* Abstract implementation of [LavaKord].
Expand Down Expand Up @@ -49,8 +50,7 @@ public abstract class AbstractLavakord internal constructor(
protected val linksMap: MutableMap<Long, Link> = mutableMapOf()

internal val json = kotlinx.serialization.json.Json {
serializersModule = RoutePlannerModule
classDiscriminator = "class"
serializersModule = RoutePlannerModule + GatewayModule
ignoreUnknownKeys = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.schlaubi.lavakord.audio.player.Filters
import dev.schlaubi.lavakord.audio.player.FiltersApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonClassDiscriminator
import kotlinx.serialization.modules.SerializersModule
import dev.schlaubi.lavakord.audio.StatsEvent as PublicStatsEvent

Expand All @@ -20,6 +21,7 @@ internal val GatewayModule = SerializersModule {
}

@Serializable
@JsonClassDiscriminator("op")
internal sealed class GatewayPayload {

abstract val guildId: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ internal class NodeImpl(
private val resumeKey = generateResumeKey()
override var available: Boolean = true
override var lastStatsEvent: GatewayPayload.StatsEvent? = null
private var eventPublisher: MutableSharedFlow<TrackEvent> = MutableSharedFlow(extraBufferCapacity = Channel.UNLIMITED)
private var eventPublisher: MutableSharedFlow<TrackEvent> =
MutableSharedFlow(extraBufferCapacity = Channel.UNLIMITED)
private lateinit var session: DefaultClientWebSocketSession
override val coroutineScope: CoroutineScope
get() = lavakord
Expand Down Expand Up @@ -102,11 +103,11 @@ internal class NodeImpl(
}

internal suspend fun send(command: GatewayPayload) {
val jsonCommand = json.encodeToString(command)
val jsonCommand = lavakord.json.encodeToString(command)
if (command is SanitizablePayload<*>) { // sanitize tokens or keys
val sanitizedCommand by lazy { command.sanitize() }
LOG.trace { "Sending command $sanitizedCommand" }
LOG.trace { "Gateway >>> ${json.encodeToString(sanitizedCommand)}" }
LOG.trace { "Gateway >>> ${lavakord.json.encodeToString(sanitizedCommand)}" }
} else {
LOG.trace { "Sending command $command" }
LOG.trace { "Gateway >>> $jsonCommand" }
Expand All @@ -118,7 +119,7 @@ internal class NodeImpl(
val text = frame.readText()
LOG.trace { "Gateway <<< $text" }
val payload = try {
json.decodeFromString<GatewayPayload>(text)
lavakord.json.decodeFromString<GatewayPayload>(text)
} catch (e: SerializationException) {
LOG.warn(e) { "Error whilst handling websocket packet" }
return
Expand Down Expand Up @@ -163,12 +164,6 @@ internal class NodeImpl(
}

internal companion object {
private val json = kotlinx.serialization.json.Json {
classDiscriminator = "op"
serializersModule = GatewayModule
ignoreUnknownKeys = true
}

private fun generateResumeKey(): String {
val allowedChars = ('A'..'Z') + ('a'..'z') + ('0'..'9')
return (1..25)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,25 @@ import dev.schlaubi.lavakord.audio.player.Track
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlin.time.Duration

internal class WebsocketPlayer(internal val node: NodeImpl, internal val guildId: Long) : Player {
override var playingTrack: Track? = null
override val coroutineScope: CoroutineScope
get() = node.coroutineScope
override var paused: Boolean = false
override var position: Long = -1
private var updateTime: Long = 0
private var lastPosition: Duration = Duration.milliseconds(0)
private var updateTime: Instant = Instant.DISTANT_PAST
override val positionDuration: Duration
get() {
val now = Clock.System.now()
val elapsedSinceUpdate = now - updateTime

return lastPosition + elapsedSinceUpdate
}

override var volume: Int = 100

@FiltersApi
Expand Down Expand Up @@ -55,13 +66,13 @@ internal class WebsocketPlayer(internal val node: NodeImpl, internal val guildId
}

private fun handleNewTrack(event: TrackStartEvent) {
position = event.track.position.inWholeMilliseconds
lastPosition = event.track.position
playingTrack = event.track
}

private fun handleTrackEnd(@Suppress("UNUSED_PARAMETER") event: TrackEndEvent) {
playingTrack = null
position = -1
lastPosition = Duration.milliseconds(0)
}

override suspend fun stopTrack() {
Expand Down Expand Up @@ -91,7 +102,7 @@ internal class WebsocketPlayer(internal val node: NodeImpl, internal val guildId
}

internal fun provideState(state: GatewayPayload.PlayerUpdateEvent.State) {
updateTime = state.time
position = state.position ?: 0
updateTime = Instant.fromEpochMilliseconds(state.time)
lastPosition = state.position?.let { Duration.milliseconds(it) } ?: Duration.milliseconds(0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public interface Player : EventSource<TrackEvent> {
public val playingTrack: Track?
public val paused: Boolean
public val volume: Int
public val positionDuration: Duration
public val position: Long
get() = positionDuration.inWholeMilliseconds

public val equalizers: Map<Int, Float>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal suspend inline fun <reified T> Node.post(
restClient.get<T>(urlBuilder.build()) { addHeader(this@post); accept(ContentType.Application.JavaScript); block(this) }

private fun HttpRequestBuilder.addHeader(socket: Node) {
headers["Authorization"] = socket.authenticationHeader
header(HttpHeaders.Authorization, socket.authenticationHeader)
}

internal fun Node.buildUrl(block: URLBuilder.() -> Unit) = URLBuilder(host)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.schlaubi.lavakord.rest
import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.audio.Node
import dev.schlaubi.lavakord.audio.player.Track
import io.ktor.util.*

/**
* Maps a [List] of [TrackResponse.PartialTrack]s to a List of [Track]s.
Expand All @@ -28,6 +29,7 @@ public suspend fun Link.loadItem(query: String): TrackResponse = node.loadItem(q
*
* @see TrackResponse
*/
@OptIn(InternalAPI::class) // There is no other way for parameters
public suspend fun Node.loadItem(query: String): TrackResponse = get {
path("loadtracks")
parameters.append("identifier", query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonClassDiscriminator
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic

Expand All @@ -32,6 +33,7 @@ internal val RoutePlannerModule = SerializersModule {
*/
@Serializable
@Polymorphic
@JsonClassDiscriminator("class")
public sealed class RoutePlannerStatus<T : RoutePlannerStatus.Data> {

public abstract val details: Data
Expand Down
14 changes: 7 additions & 7 deletions core/src/commonTest/kotlin/json/CommandsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class CommandsTest {
@JsName("testPlayCommand")
@Test
fun `test play command serialization`() {
test<GatewayPayload.PlayCommand>(PLAY_COMMAND) {
testPayload<GatewayPayload.PlayCommand>(PLAY_COMMAND) {
startTime shouldBe 60000
endTime shouldBe 120000
volume shouldBe 100
Expand All @@ -22,45 +22,45 @@ class CommandsTest {
@JsName("testStopCommand")
@Test
fun `test stop command serialization`() {
test<GatewayPayload.StopCommand>(STOP_COMMAND)
testPayload<GatewayPayload.StopCommand>(STOP_COMMAND)
}


@JsName("testPauseCommand")
@Test
fun `test pause command serialization`() {
test<GatewayPayload.PauseCommand>(PAUSE_COMMAND) {
testPayload<GatewayPayload.PauseCommand>(PAUSE_COMMAND) {
pause shouldBe true
}
}

@JsName("testSeekCommand")
@Test
fun `test seek command serialization`() {
test<GatewayPayload.SeekCommand>(SEEK_COMMAND) {
testPayload<GatewayPayload.SeekCommand>(SEEK_COMMAND) {
position shouldBe 60000
}
}

@JsName("testVolumeCommand")
@Test
fun `test volume command serialization`() {
test<GatewayPayload.VolumeCommand>(VOLUME_COMMAND) {
testPayload<GatewayPayload.VolumeCommand>(VOLUME_COMMAND) {
volume shouldBe 125
}
}

@JsName("testEqCommand")
@Test
fun `test equalizer command serialization`() {
test<GatewayPayload.EqualizerCommand>(EQUALIZER_COMMAND) {
testPayload<GatewayPayload.EqualizerCommand>(EQUALIZER_COMMAND) {
bands shouldBe listOf(GatewayPayload.EqualizerCommand.Band(0, 0.2F))
}
}

@JsName("testDestroyCommand")
@Test
fun `test destroy command serialization`() {
test<GatewayPayload.DestroyCommand>(DESTROY_COMMAND)
testPayload<GatewayPayload.DestroyCommand>(DESTROY_COMMAND)
}
}
Loading

0 comments on commit 3e47aad

Please sign in to comment.