From 6d8ea1bf5dd3088cfea479dfe3806994040ab867 Mon Sep 17 00:00:00 2001 From: toasterofbread Date: Thu, 14 Nov 2024 16:35:53 +0000 Subject: [PATCH] Add repeat_threshold_ms param to player.seekToPrevious, improve ServerActions --- .../dev/toastbits/spms/mpv/MpvClientImpl.kt | 16 ++++++-- .../dev/toastbits/spms/player/Player.kt | 11 ++++- .../spms/player/headless/HeadlessPlayer.kt | 20 ++++++---- .../spms/socketapi/server/ServerAction.kt | 40 +++++++++---------- .../socketapi/server/ServerActionAddItem.kt | 10 ++++- .../server/ServerActionClearQueue.kt | 9 ++++- .../server/ServerActionGetClients.kt | 9 ++++- .../server/ServerActionGetProperty.kt | 2 +- .../socketapi/server/ServerActionGetStatus.kt | 9 ++++- .../socketapi/server/ServerActionMoveItem.kt | 10 ++++- .../socketapi/server/ServerActionPause.kt | 9 ++++- .../spms/socketapi/server/ServerActionPlay.kt | 9 ++++- .../socketapi/server/ServerActionPlayPause.kt | 9 ++++- .../server/ServerActionReadyToPlay.kt | 22 ++++++---- .../server/ServerActionRemoveItem.kt | 9 ++++- .../server/ServerActionSeekToItem.kt | 11 ++++- .../server/ServerActionSeekToNext.kt | 9 ++++- .../server/ServerActionSeekToPrevious.kt | 25 +++++++----- .../server/ServerActionSeekToTime.kt | 11 ++++- .../server/ServerActionSetRepeatMode.kt | 12 +++++- 20 files changed, 185 insertions(+), 77 deletions(-) diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/mpv/MpvClientImpl.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/mpv/MpvClientImpl.kt index b915979..0ccfe86 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/mpv/MpvClientImpl.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/mpv/MpvClientImpl.kt @@ -6,6 +6,7 @@ import okio.FileSystem import okio.Path.Companion.toPath import dev.toastbits.spms.socketapi.shared.SpMsPlayerEvent import dev.toastbits.spms.player.VideoInfoProvider +import dev.toastbits.spms.player.shouldRepeatOnSeekToPrevious import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsPlayerRepeatMode import dev.toastbits.spms.socketapi.shared.SpMsPlayerState @@ -13,6 +14,7 @@ import dev.toastbits.spms.PLATFORM import gen.libmpv.LibMpv import kotlin.math.roundToInt import kotlin.time.* +import kotlin.time.Duration private const val URL_PREFIX: String = "spmp://" @@ -128,12 +130,18 @@ abstract class MpvClientImpl( return false } - override fun seekToPrevious(): Boolean { - if (runCommand("playlist-prev", check_result = false) == 0) { - onEvent(SpMsPlayerEvent.ItemTransition(current_item_index)) + override fun seekToPrevious(repeat_threshold: Duration?): Boolean { + if (shouldRepeatOnSeekToPrevious(repeat_threshold)) { + seekToTime(0) return true } - return false + else { + if (runCommand("playlist-prev", check_result = false) == 0) { + onEvent(SpMsPlayerEvent.ItemTransition(current_item_index)) + return true + } + return false + } } override fun setRepeatMode(repeat_mode: SpMsPlayerRepeatMode) { diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/player/Player.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/player/Player.kt index 68fd6fc..f13e419 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/player/Player.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/player/Player.kt @@ -3,6 +3,8 @@ package dev.toastbits.spms.player import dev.toastbits.spms.socketapi.shared.SpMsPlayerEvent import dev.toastbits.spms.socketapi.shared.SpMsPlayerRepeatMode import dev.toastbits.spms.socketapi.shared.SpMsPlayerState +import kotlin.time.Duration +import kotlin.time.Duration.Companion.milliseconds data class PlayerStreamInfo( val url: String, @@ -32,7 +34,7 @@ interface Player { fun seekToTime(position_ms: Long) fun seekToItem(index: Int, position_ms: Long = 0) fun seekToNext(): Boolean - fun seekToPrevious(): Boolean + fun seekToPrevious(repeat_threshold: Duration? = null): Boolean fun setRepeatMode(repeat_mode: SpMsPlayerRepeatMode) fun getItem(): String? @@ -45,3 +47,10 @@ interface Player { fun setVolume(value: Double) } + +fun Player.shouldRepeatOnSeekToPrevious(repeat_threshold: Duration?): Boolean = + repeat_threshold != null + && ( + repeat_threshold == Duration.ZERO + || current_position_ms.milliseconds >= repeat_threshold + ) diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/player/headless/HeadlessPlayer.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/player/headless/HeadlessPlayer.kt index 78adaf9..7319c30 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/player/headless/HeadlessPlayer.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/player/headless/HeadlessPlayer.kt @@ -12,6 +12,7 @@ import dev.toastbits.spms.socketapi.shared.SpMsPlayerState import dev.toastbits.spms.player.* import dev.toastbits.spms.ReentrantLock import kotlin.time.* +import kotlin.time.Duration abstract class HeadlessPlayer(private val enable_logging: Boolean = true): Player { protected abstract fun getCachedItemDuration(item_id: String): Duration? @@ -260,18 +261,23 @@ abstract class HeadlessPlayer(private val enable_logging: Boolean = true): Playe } } - override fun seekToPrevious(): Boolean { + override fun seekToPrevious(repeat_threshold: Duration?): Boolean { withLock { log("seekToPrevious()") - if (current_item_index == 0) { + + if (shouldRepeatOnSeekToPrevious(repeat_threshold)) { + seekToTime(0) + return true + } + else if (current_item_index == 0) { return false } + else { + performSeekToItem(current_item_index - 1) + onItemTransition(current_item_index) - performSeekToItem(current_item_index - 1) - - onItemTransition(current_item_index) - - return true + return true + } } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerAction.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerAction.kt index eb2b0ac..4f1db34 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerAction.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerAction.kt @@ -22,26 +22,26 @@ sealed class ServerAction( companion object { private val ALL: List = listOf( - ServerActionGetStatus(), - ServerActionGetClients(), - ServerActionGetProperty(), - - ServerActionPause(), - ServerActionPlay(), - ServerActionPlayPause(), - - ServerActionSeekToItem(), - ServerActionSeekToNext(), - ServerActionSeekToPrevious(), - ServerActionSeekToTime(), - ServerActionSetRepeatMode(), - - ServerActionAddItem(), - ServerActionMoveItem(), - ServerActionRemoveItem(), - ServerActionClearQueue(), - - ServerActionReadyToPlay() + ServerActionGetStatus, + ServerActionGetClients, + ServerActionGetProperty, + + ServerActionPause, + ServerActionPlay, + ServerActionPlayPause, + + ServerActionSeekToItem, + ServerActionSeekToNext, + ServerActionSeekToPrevious, + ServerActionSeekToTime, + ServerActionSetRepeatMode, + + ServerActionAddItem, + ServerActionMoveItem, + ServerActionRemoveItem, + ServerActionClearQueue, + + ServerActionReadyToPlay ) fun getAll(): List = ALL diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionAddItem.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionAddItem.kt index 4e5896d..deea6c2 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionAddItem.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionAddItem.kt @@ -5,8 +5,9 @@ import kotlinx.serialization.json.int import kotlinx.serialization.json.jsonPrimitive import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID +import dev.toastbits.spms.player.Player -class ServerActionAddItem: ServerAction( +object ServerActionAddItem: ServerAction( identifier = "addItem", name = { server_actions.add_item_name }, help = { server_actions.add_item_help }, @@ -29,7 +30,12 @@ class ServerActionAddItem: ServerAction( val item_id: String = context.getParameterValue("item_id")!!.jsonPrimitive.content val index: Int = context.getParameterValue("index")?.jsonPrimitive?.int ?: -1 - server.player.addItem(item_id, index) + execute(server.player, item_id, index) + return null } + + fun execute(player: Player, item_id: String, index: Int) { + player.addItem(item_id, index) + } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionClearQueue.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionClearQueue.kt index 1011d11..92d6e89 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionClearQueue.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionClearQueue.kt @@ -3,15 +3,20 @@ package dev.toastbits.spms.socketapi.server import kotlinx.serialization.json.JsonElement import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID +import dev.toastbits.spms.player.Player -class ServerActionClearQueue: ServerAction( +object ServerActionClearQueue: ServerAction( identifier = "clearQueue", name = { server_actions.clear_queue_name }, help = { server_actions.clear_queue_help }, parameters = emptyList() ) { override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? { - server.player.clearQueue() + execute(server.player) return null } + + fun execute(player: Player) { + player.clearQueue() + } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionGetClients.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionGetClients.kt index c776c0d..900c012 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionGetClients.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionGetClients.kt @@ -5,15 +5,20 @@ import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.encodeToJsonElement import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID +import dev.toastbits.spms.socketapi.shared.SpMsClientInfo -class ServerActionGetClients: ServerAction( +object ServerActionGetClients: ServerAction( identifier = "clients", name = { server_actions.clients_name }, help = { server_actions.clients_help }, parameters = emptyList() ) { override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement { - val clients = server.getClients(client) + val clients: List = execute(server, client) return Json.encodeToJsonElement(clients) } + + fun execute(server: SpMs, client: SpMsClientID): List { + return server.getClients(client) + } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionGetProperty.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionGetProperty.kt index 67ba491..8bc12e5 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionGetProperty.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionGetProperty.kt @@ -4,7 +4,7 @@ import kotlinx.serialization.json.JsonElement import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID -class ServerActionGetProperty: ServerAction( +object ServerActionGetProperty: ServerAction( identifier = "getProperty", name = { "TODO" }, help = { "TODO" }, diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionGetStatus.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionGetStatus.kt index 22f4bf5..feeb201 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionGetStatus.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionGetStatus.kt @@ -14,16 +14,21 @@ import dev.toastbits.spms.socketapi.shared.SpMsPlayerState import dev.toastbits.spms.getCacheDir import dev.toastbits.spms.localisation.SpMsLocalisation import dev.toastbits.spms.PLATFORM +import dev.toastbits.spms.player.Player import kotlin.time.Duration -class ServerActionGetStatus: ServerAction( +object ServerActionGetStatus: ServerAction( identifier = "status", name = { server_actions.status_name }, help = { server_actions.status_help }, parameters = emptyList() ) { override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement { - return Json.encodeToJsonElement(server.player.getCurrentStateJson()) + return execute(server.player) + } + + fun execute(player: Player): JsonElement { + return Json.encodeToJsonElement(player.getCurrentStateJson()) } override fun formatResult(result: JsonElement, localisation: SpMsLocalisation): String { diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionMoveItem.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionMoveItem.kt index 0909684..da81a78 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionMoveItem.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionMoveItem.kt @@ -5,8 +5,9 @@ import kotlinx.serialization.json.int import kotlinx.serialization.json.jsonPrimitive import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID +import dev.toastbits.spms.player.Player -class ServerActionMoveItem: ServerAction( +object ServerActionMoveItem: ServerAction( identifier = "moveItem", name = { server_actions.move_item_name }, help = { server_actions.move_item_help }, @@ -29,7 +30,12 @@ class ServerActionMoveItem: ServerAction( val from: Int = context.getParameterValue("from")!!.jsonPrimitive.int val to: Int = context.getParameterValue("to")!!.jsonPrimitive.int - server.player.moveItem(from, to) + execute(server.player, from, to) + return null } + + fun execute(player: Player, from: Int, to: Int) { + player.moveItem(from, to) + } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionPause.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionPause.kt index 2bbaa3c..5a82616 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionPause.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionPause.kt @@ -3,15 +3,20 @@ package dev.toastbits.spms.socketapi.server import kotlinx.serialization.json.JsonElement import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID +import dev.toastbits.spms.player.Player -class ServerActionPause: ServerAction( +object ServerActionPause: ServerAction( name = { server_actions.pause_name }, identifier = "pause", help = { server_actions.pause_help }, parameters = emptyList() ) { override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? { - server.player.pause() + execute(server.player) return null } + + fun execute(player: Player) { + player.pause() + } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionPlay.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionPlay.kt index ea3f7cc..7266658 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionPlay.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionPlay.kt @@ -3,15 +3,20 @@ package dev.toastbits.spms.socketapi.server import kotlinx.serialization.json.JsonElement import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID +import dev.toastbits.spms.player.Player -class ServerActionPlay: ServerAction( +object ServerActionPlay: ServerAction( identifier = "play", name = { server_actions.play_name }, help = { server_actions.play_help }, parameters = emptyList() ) { override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? { - server.player.play() + execute(server.player) return null } + + fun execute(player: Player) { + player.play() + } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionPlayPause.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionPlayPause.kt index 4890182..c60c731 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionPlayPause.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionPlayPause.kt @@ -3,15 +3,20 @@ package dev.toastbits.spms.socketapi.server import kotlinx.serialization.json.JsonElement import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID +import dev.toastbits.spms.player.Player -class ServerActionPlayPause: ServerAction( +object ServerActionPlayPause: ServerAction( identifier = "playPause", name = { server_actions.play_pause_name }, help = { server_actions.play_pause_help }, parameters = emptyList() ) { override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? { - server.player.playPause() + execute(server.player) return null } + + fun execute(player: Player) { + player.playPause() + } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionReadyToPlay.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionReadyToPlay.kt index 000b30c..16563a2 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionReadyToPlay.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionReadyToPlay.kt @@ -7,8 +7,9 @@ import kotlinx.serialization.json.long import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID import kotlin.time.Duration +import kotlin.time.Duration.Companion.milliseconds -class ServerActionReadyToPlay: ServerAction( +object ServerActionReadyToPlay: ServerAction( identifier = "readyToPlay", name = { server_actions.ready_to_play_name }, help = { server_actions.ready_to_play_help }, @@ -35,14 +36,21 @@ class ServerActionReadyToPlay: ServerAction( ) ) { override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? { + val item_index: Int = context.getParameterValue("item_index")!!.jsonPrimitive.int + val item_id: String = context.getParameterValue("item_id")!!.jsonPrimitive.content + val item_duration: Duration = context.getParameterValue("item_duration_ms")!!.jsonPrimitive.long.milliseconds + + execute(server, client, item_index, item_id, item_duration) + + return null + } + + fun execute(server: SpMs, client: SpMsClientID, item_index: Int, item_id: String, item_duration: Duration) { server.onClientReadyToPlay( client, - context.getParameterValue("item_index")!!.jsonPrimitive.int, - context.getParameterValue("item_id")!!.jsonPrimitive.content, - with (Duration) { - context.getParameterValue("item_duration_ms")!!.jsonPrimitive.long.milliseconds - } + item_index, + item_id, + item_duration ) - return null } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionRemoveItem.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionRemoveItem.kt index c0068e1..8f6de9e 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionRemoveItem.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionRemoveItem.kt @@ -5,8 +5,9 @@ import kotlinx.serialization.json.int import kotlinx.serialization.json.jsonPrimitive import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID +import dev.toastbits.spms.player.Player -class ServerActionRemoveItem: ServerAction( +object ServerActionRemoveItem: ServerAction( identifier = "removeItem", name = { server_actions.remove_item_name }, help = { server_actions.remove_item_help }, @@ -21,7 +22,11 @@ class ServerActionRemoveItem: ServerAction( ) { override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? { val from: Int = context.getParameterValue("from")!!.jsonPrimitive.int - server.player.removeItem(from) + execute(server.player, from) return null } + + fun execute(player: Player, from: Int) { + player.removeItem(from) + } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToItem.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToItem.kt index aaa54e3..6dde222 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToItem.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToItem.kt @@ -3,8 +3,9 @@ package dev.toastbits.spms.socketapi.server import kotlinx.serialization.json.* import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID +import dev.toastbits.spms.player.Player -class ServerActionSeekToItem: ServerAction( +object ServerActionSeekToItem: ServerAction( identifier = "seekToItem", name = { server_actions.seek_to_item_name }, help = { server_actions.seek_to_item_help }, @@ -26,7 +27,13 @@ class ServerActionSeekToItem: ServerAction( override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? { val index: Int = context.getParameterValue("index")!!.jsonPrimitive.int val position_ms: Long? = context.getParameterValue("position_ms")?.jsonPrimitive?.longOrNull - server.player.seekToItem(index, position_ms ?: 0) + + execute(server.player, index, position_ms) + return null } + + fun execute(player: Player, index: Int, position_ms: Long? = null) { + player.seekToItem(index, position_ms ?: 0) + } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToNext.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToNext.kt index 7669cef..cac8a86 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToNext.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToNext.kt @@ -3,15 +3,20 @@ package dev.toastbits.spms.socketapi.server import kotlinx.serialization.json.JsonElement import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID +import dev.toastbits.spms.player.Player -class ServerActionSeekToNext: ServerAction( +object ServerActionSeekToNext: ServerAction( identifier = "seekToNext", name = { server_actions.seek_to_next_name }, help = { server_actions.seek_to_next_help }, parameters = emptyList() ) { override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? { - server.player.seekToNext() + execute(server.player) return null } + + fun execute(player: Player) { + player.seekToNext() + } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToPrevious.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToPrevious.kt index 51e9099..2f458cf 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToPrevious.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToPrevious.kt @@ -3,8 +3,11 @@ package dev.toastbits.spms.socketapi.server import kotlinx.serialization.json.* import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID +import dev.toastbits.spms.player.Player +import kotlin.time.Duration +import kotlin.time.Duration.Companion.milliseconds -class ServerActionSeekToPrevious: ServerAction( +object ServerActionSeekToPrevious: ServerAction( identifier = "seekToPrevious", name = { server_actions.seek_to_previous_name }, help = { server_actions.seek_to_previous_help }, @@ -18,18 +21,18 @@ class ServerActionSeekToPrevious: ServerAction( ) ) { override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? { - val repeat_threshold_ms: Long = context.getParameterValue("repeat_threshold_ms")?.jsonPrimitive?.longOrNull ?: -1 + val repeat_threshold_ms: Long? = context.getParameterValue("repeat_threshold_ms")?.jsonPrimitive?.longOrNull - if ( - repeat_threshold_ms == 0L - || (repeat_threshold_ms > 0L && server.player.current_position_ms >= repeat_threshold_ms) - ) { - server.player.seekToTime(0) - } - else { - server.player.seekToPrevious() - } + val repeat_threshold: Duration? = + if (repeat_threshold_ms == null || repeat_threshold_ms < 0) null + else repeat_threshold_ms.milliseconds + + execute(server.player, repeat_threshold) return null } + + fun execute(player: Player, repeat_threshold: Duration? = null) { + player.seekToPrevious(repeat_threshold) + } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToTime.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToTime.kt index aab25a5..8ac8e5a 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToTime.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSeekToTime.kt @@ -5,8 +5,9 @@ import kotlinx.serialization.json.jsonPrimitive import kotlinx.serialization.json.long import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID +import dev.toastbits.spms.player.Player -class ServerActionSeekToTime: ServerAction( +object ServerActionSeekToTime: ServerAction( identifier = "seekToTime", name = { server_actions.seek_to_time_name }, help = { server_actions.seek_to_time_help }, @@ -21,7 +22,13 @@ class ServerActionSeekToTime: ServerAction( ) { override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? { val position_ms: Long = context.getParameterValue("position_ms")!!.jsonPrimitive.long - server.player.seekToTime(position_ms) + + execute(server.player, position_ms) + return null } + + fun execute(player: Player, position_ms: Long) { + player.seekToTime(position_ms) + } } diff --git a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSetRepeatMode.kt b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSetRepeatMode.kt index 75553ae..cbef67c 100644 --- a/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSetRepeatMode.kt +++ b/library/src/commonMain/kotlin/dev/toastbits/spms/socketapi/server/ServerActionSetRepeatMode.kt @@ -6,8 +6,9 @@ import kotlinx.serialization.json.jsonPrimitive import dev.toastbits.spms.server.SpMs import dev.toastbits.spms.socketapi.shared.SpMsClientID import dev.toastbits.spms.socketapi.shared.SpMsPlayerRepeatMode +import dev.toastbits.spms.player.Player -class ServerActionSetRepeatMode: ServerAction( +object ServerActionSetRepeatMode: ServerAction( identifier = "setRepeatMode", name = { server_actions.set_repeat_mode_name }, help = { server_actions.set_repeat_mode_help }, @@ -22,7 +23,14 @@ class ServerActionSetRepeatMode: ServerAction( ) { override fun execute(server: SpMs, client: SpMsClientID, context: ActionContext): JsonElement? { val repeat_mode_index: Int = context.getParameterValue("repeat_mode")!!.jsonPrimitive.int - server.player.setRepeatMode(SpMsPlayerRepeatMode.entries[repeat_mode_index]) + val repeat_mode: SpMsPlayerRepeatMode = SpMsPlayerRepeatMode.entries[repeat_mode_index] + + execute(server.player, repeat_mode) + return null } + + fun execute(player: Player, repeat_mode: SpMsPlayerRepeatMode) { + player.setRepeatMode(repeat_mode) + } }