1
1
package dev.schlaubi.lavakord.audio.internal
2
2
3
3
import dev.arbjerg.lavalink.protocol.v4.*
4
- import dev.schlaubi.lavakord.audio.Event
5
- import dev.schlaubi.lavakord.audio.TrackEndEvent
6
- import dev.schlaubi.lavakord.audio.TrackStartEvent
7
- import dev.schlaubi.lavakord.audio.on
4
+ import dev.schlaubi.lavakord.audio.*
8
5
import dev.schlaubi.lavakord.audio.player.Equalizer
9
6
import dev.schlaubi.lavakord.audio.player.Filters
10
7
import dev.schlaubi.lavakord.audio.player.PlayOptions
11
8
import dev.schlaubi.lavakord.audio.player.Player
12
9
import dev.schlaubi.lavakord.rest.models.FiltersObject
13
10
import dev.schlaubi.lavakord.rest.models.toLavalink
14
11
import dev.schlaubi.lavakord.rest.updatePlayer
15
- import kotlinx.atomicfu.AtomicBoolean
16
12
import kotlinx.atomicfu.atomic
17
13
import kotlinx.coroutines.CoroutineScope
18
14
import kotlinx.coroutines.flow.Flow
@@ -22,13 +18,11 @@ import kotlinx.datetime.Instant
22
18
import kotlin.time.Duration
23
19
import kotlin.time.Duration.Companion.milliseconds
24
20
25
- internal class WebsocketPlayer (node : NodeImpl , internal val guildId : ULong ) : Player {
26
- internal var node: NodeImpl = node
27
- private set
21
+ internal class WebsocketPlayer (private val link : Link , internal val guildId : ULong ) : Player, CoroutineScope by link {
28
22
override var playingTrack: Track ? = null
29
- override val coroutineScope: CoroutineScope
30
- get() = node.coroutineScope
31
23
override var paused: Boolean = false
24
+ override val coroutineScope: CoroutineScope
25
+ get() = this
32
26
private var lastPosition: Duration = 0 .milliseconds
33
27
private var updateTime: Instant = Instant .DISTANT_PAST
34
28
override val positionDuration: Duration
@@ -57,7 +51,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
57
51
}
58
52
59
53
override val events: Flow <Event >
60
- get() = node .events.filter { it.guildId == guildId }
54
+ get() = link.lavakord .events.filter { it.guildId == guildId }
61
55
62
56
init {
63
57
on(consumer = ::handleNewTrack)
@@ -76,7 +70,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
76
70
playOptionsBuilder : PlayOptions .() -> Unit
77
71
) {
78
72
val options = PlayOptions ().apply (playOptionsBuilder)
79
- node.updatePlayer(
73
+ link. node.updatePlayer(
80
74
guildId, options.noReplace, PlayerUpdate (
81
75
encodedTrack = track.toOmissible(),
82
76
identifier = identifier.toOmissible(),
@@ -104,7 +98,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
104
98
}
105
99
106
100
override suspend fun stopTrack () {
107
- node.updatePlayer(
101
+ link. node.updatePlayer(
108
102
guildId,
109
103
request = PlayerUpdate (encodedTrack = Omissible (null ))
110
104
)
@@ -113,7 +107,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
113
107
114
108
override suspend fun pause (doPause : Boolean ) {
115
109
if (paused == doPause) return
116
- node.updatePlayer(
110
+ link. node.updatePlayer(
117
111
guildId,
118
112
request = PlayerUpdate (paused = doPause.toOmissible())
119
113
)
@@ -123,7 +117,7 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
123
117
override suspend fun seekTo (position : Long ) {
124
118
checkNotNull(playingTrack) { " Not currently playing anything" }
125
119
126
- node.updatePlayer(
120
+ link. node.updatePlayer(
127
121
guildId,
128
122
request = PlayerUpdate (position = position.toOmissible())
129
123
)
@@ -138,12 +132,11 @@ internal class WebsocketPlayer(node: NodeImpl, internal val guildId: ULong) : Pl
138
132
lastPosition = state.position.milliseconds
139
133
}
140
134
141
- internal suspend fun recreatePlayer (node : NodeImpl , voiceState : VoiceState ? ) {
142
- this .node = node
135
+ internal suspend fun recreatePlayer (voiceState : VoiceState ? ) {
143
136
val position = if (playingTrack == null ) null else positionDuration.inWholeMilliseconds
144
137
145
138
isRecreating.value = true
146
- node.updatePlayer(
139
+ link. node.updatePlayer(
147
140
guildId, noReplace = false , PlayerUpdate (
148
141
encodedTrack = playingTrack?.encoded.toOmissible(),
149
142
identifier = Omissible .omitted(),
0 commit comments