Skip to content

Commit

Permalink
Update internal track cache on track start
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Dec 3, 2023
1 parent e6919c8 commit 12615a3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
10 changes: 9 additions & 1 deletion src/main/kotlin/dev/arbjerg/lavalink/client/IUpdatablePlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ interface IUpdatablePlayer {
*
* @return The updated builder, useful for chaining
*/
fun applyTrack(track: Track?): PlayerUpdateBuilder
fun setTrack(track: Track?): PlayerUpdateBuilder

@Deprecated(
message = "Use setTrack instead",
replaceWith = ReplaceWith("setTrack(track)")
)
fun applyTrack(track: Track?): PlayerUpdateBuilder {
return setTrack(track)
}

/**
* Shortcut for setting the encoded track to {@code null}. This will also clear the user data.
Expand Down
12 changes: 7 additions & 5 deletions src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class LavalinkPlayer(private val node: LavalinkNode, protocolPlayer: Player) : I
*
* To get the current position of the track, use [position].
*/
val track = protocolPlayer.track
var track = protocolPlayer.track
internal set

/**
* Number between 0 and 1000, where 100 is 100% volume.
Expand All @@ -30,15 +31,16 @@ class LavalinkPlayer(private val node: LavalinkNode, protocolPlayer: Player) : I

val position: Long
get() {
val checkedTrack = track
return when {
track == null -> 0
checkedTrack == null -> 0
paused -> state.position
else -> min(state.position + (System.currentTimeMillis() - state.time), track.info.length)
else -> min(state.position + (System.currentTimeMillis() - state.time), checkedTrack.info.length)
}
}

override fun applyTrack(track: Track?) = PlayerUpdateBuilder(node, guildId)
.applyTrack(track)
override fun setTrack(track: Track?) = PlayerUpdateBuilder(node, guildId)
.setTrack(track)

override fun stopTrack() = PlayerUpdateBuilder(node, guildId)
.stopTrack()
Expand Down
16 changes: 10 additions & 6 deletions src/main/kotlin/dev/arbjerg/lavalink/client/PlayerUpdateBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.arbjerg.lavalink.client
import dev.arbjerg.lavalink.internal.toLavalinkPlayer
import dev.arbjerg.lavalink.protocol.v4.*
import kotlinx.serialization.json.JsonObject
import org.checkerframework.checker.optional.qual.Present
import reactor.core.publisher.Mono
import kotlin.math.max
import kotlin.math.min
Expand All @@ -19,7 +20,7 @@ class PlayerUpdateBuilder internal constructor(private val node: LavalinkNode, p
private var state: Omissible<VoiceState> = Omissible.omitted()
private var noReplace = false

override fun applyTrack(track: Track?): PlayerUpdateBuilder {
override fun setTrack(track: Track?): PlayerUpdateBuilder {
this.encodedTrack = Omissible.of(track?.encoded)
this.trackUserData = track?.userData.toOmissible()
return this
Expand Down Expand Up @@ -107,11 +108,14 @@ class PlayerUpdateBuilder internal constructor(private val node: LavalinkNode, p

@Suppress("MemberVisibilityCanBePrivate")
fun build() = PlayerUpdate(
track = PlayerUpdateTrack(
encodedTrack,
identifier,
trackUserData,
).toOmissible(),
track = if (encodedTrack is Omissible.Present || identifier.isPresent())
PlayerUpdateTrack(
encodedTrack,
identifier,
trackUserData,
).toOmissible()
else
Omissible.omitted(),
position = position,
endTime = endTime,
volume = volume,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos
}

Message.Op.Event -> {
event as Message.EmittedEvent

if (event is Message.EmittedEvent.TrackStartEvent) {
node.playerCache[event.guildId.toLong()]?.track = event.track
}

// TODO: handle websocket closed event from discord?
node.penalties.handleTrackEvent(event as Message.EmittedEvent)
node.penalties.handleTrackEvent(event)
}

else -> {
Expand Down

0 comments on commit 12615a3

Please sign in to comment.