Skip to content

Commit

Permalink
feat: Implement support for passing ITrackQueueItem for initial track
Browse files Browse the repository at this point in the history
  • Loading branch information
angelobreuer committed Feb 6, 2024
1 parent c2b63c9 commit 8efdba5
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 6 deletions.
21 changes: 21 additions & 0 deletions src/Lavalink4NET.Tests/Players/LavalinkPlayerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
using Lavalink4NET.Clients;
using Lavalink4NET.Filters;
using Lavalink4NET.Players;
using Lavalink4NET.Players.Queued;
using Lavalink4NET.Protocol.Models;
using Lavalink4NET.Protocol.Models.Filters;
using Lavalink4NET.Protocol.Payloads.Events;
using Lavalink4NET.Protocol.Requests;
using Lavalink4NET.Rest;
using Lavalink4NET.Rest.Entities.Tracks;
using Lavalink4NET.Tracks;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -859,13 +861,32 @@ private static PlayerProperties<CustomTracingLavalinkPlayer, LavalinkPlayerOptio
Lifecycle: Mock.Of<IPlayerLifecycle>(),
ApiClient: apiClientMock.Object,
InitialState: playerModel,
InitialTrack: playerModel.CurrentTrack is null
? null
: new TrackQueueItem(new TrackReference(RestoreTrack(playerModel.CurrentTrack))),
Label: "Player",
VoiceChannelId: 0,
SessionId: sessionId,
Options: Options.Create(options ?? new LavalinkPlayerOptions()),
Logger: NullLogger<CustomTracingLavalinkPlayer>.Instance);
}

private static LavalinkTrack RestoreTrack(TrackModel track) => new()
{
Author = track.Information.Author,
Identifier = track.Information.Identifier,
Title = track.Information.Title,
Duration = track.Information.Duration,
IsLiveStream = track.Information.IsLiveStream,
IsSeekable = track.Information.IsSeekable,
Uri = track.Information.Uri,
SourceName = track.Information.SourceName,
StartPosition = track.Information.Position,
ArtworkUri = track.Information.ArtworkUri,
Isrc = track.Information.Isrc,
AdditionalInformation = track.AdditionalInformation,
};

private static TrackInformationModel CreateDummyTrack()
{
return new TrackInformationModel(
Expand Down
24 changes: 24 additions & 0 deletions src/Lavalink4NET.Tests/Players/QueuedLavalinkPlayerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Lavalink4NET.Protocol.Payloads.Events;
using Lavalink4NET.Protocol.Requests;
using Lavalink4NET.Rest;
using Lavalink4NET.Tracks;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -75,6 +76,7 @@ public async Task TestPlayerIsStartedOnSkipAsync()
LifecycleNotifier: null),
Lifecycle: Mock.Of<IPlayerLifecycle>(),
ApiClient: apiClientMock.Object,
InitialTrack: null,
InitialState: playerModel,
Label: "Player",
VoiceChannelId: 0,
Expand Down Expand Up @@ -147,6 +149,7 @@ public async Task TestPlayerStartsSecondTrackIfNoneIsPlayingButCountIsTwoOnSkipA
Lifecycle: Mock.Of<IPlayerLifecycle>(),
ApiClient: apiClientMock.Object,
InitialState: playerModel,
InitialTrack: null,
Label: "Player",
VoiceChannelId: 0,
SessionId: sessionId,
Expand Down Expand Up @@ -215,6 +218,7 @@ public async Task TestPlayerStopsOnSkipIfQueueIsEmptyAsync()
Lifecycle: Mock.Of<IPlayerLifecycle>(),
ApiClient: apiClientMock.Object,
InitialState: playerModel,
InitialTrack: null,
Label: "Player",
VoiceChannelId: 0,
SessionId: sessionId,
Expand Down Expand Up @@ -289,6 +293,7 @@ public async Task TestPlayerPlaysNextAfterTrackEndAsync()
Lifecycle: Mock.Of<IPlayerLifecycle>(),
ApiClient: apiClientMock.Object,
InitialState: playerModel,
InitialTrack: new TrackQueueItem(new TrackReference(RestoreTrack(playerModel.CurrentTrack!))),
Label: "Player",
VoiceChannelId: 0,
SessionId: sessionId,
Expand Down Expand Up @@ -368,6 +373,7 @@ public async Task TestPlayerRepeatsTrackIfRepeatModeIsTrackAsync()
Lifecycle: Mock.Of<IPlayerLifecycle>(),
ApiClient: apiClientMock.Object,
InitialState: playerModel,
InitialTrack: new TrackQueueItem(new TrackReference(RestoreTrack(playerModel.CurrentTrack!))),
Label: "Player",
VoiceChannelId: 0,
SessionId: sessionId,
Expand Down Expand Up @@ -451,6 +457,7 @@ public async Task TestPlayerRepeatsTrackIfRepeatModeIsTrackOnSkipAsync()
Lifecycle: Mock.Of<IPlayerLifecycle>(),
ApiClient: apiClientMock.Object,
InitialState: playerModel,
InitialTrack: new TrackQueueItem(new TrackReference(RestoreTrack(playerModel.CurrentTrack!))),
Label: "Player",
VoiceChannelId: 0,
SessionId: sessionId,
Expand Down Expand Up @@ -525,6 +532,7 @@ public async Task TestPlayerStopsTrackIfRepeatModeIsTrackButNoTrackIsPlayingOnSk
Lifecycle: Mock.Of<IPlayerLifecycle>(),
ApiClient: apiClientMock.Object,
InitialState: playerModel,
InitialTrack: null,
Label: "Player",
VoiceChannelId: 0,
SessionId: sessionId,
Expand All @@ -543,6 +551,22 @@ await player
apiClientMock.Verify();
}

private static LavalinkTrack RestoreTrack(TrackModel track) => new()
{
Author = track.Information.Author,
Identifier = track.Information.Identifier,
Title = track.Information.Title,
Duration = track.Information.Duration,
IsLiveStream = track.Information.IsLiveStream,
IsSeekable = track.Information.IsSeekable,
Uri = track.Information.Uri,
SourceName = track.Information.SourceName,
StartPosition = track.Information.Position,
ArtworkUri = track.Information.ArtworkUri,
Isrc = track.Information.Isrc,
AdditionalInformation = track.AdditionalInformation,
};

private static TrackInformationModel CreateDummyTrack()
{
return new TrackInformationModel(
Expand Down
2 changes: 2 additions & 0 deletions src/Lavalink4NET/Players/IPlayerProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface IPlayerProperties<out TPlayer, out TOptions>

PlayerInformationModel InitialState { get; }

ITrackQueueItem? InitialTrack { get; }

string Label { get; }

ILogger<TPlayer> Logger { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Lavalink4NET/Players/LavalinkPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public LavalinkPlayer(IPlayerProperties<LavalinkPlayer, LavalinkPlayerOptions> p
}

_currentItem = _nextTrack = properties.InitialState.CurrentTrack is not null
? new TrackQueueItem(new TrackReference(RestoreTrack(properties.InitialState.CurrentTrack)))
? properties.Options.Value.InitialTrack ?? new TrackQueueItem(new TrackReference(RestoreTrack(properties.InitialState.CurrentTrack)))
: null;

Refresh(properties.InitialState);
Expand Down
9 changes: 5 additions & 4 deletions src/Lavalink4NET/Players/LavalinkPlayerHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private async ValueTask<TPlayer> CreatePlayerAsync(CancellationToken cancellatio
var playerSession = await _playerContext.SessionProvider
.GetSessionAsync(_guildId, cancellationToken)
.ConfigureAwait(false);

var playerProperties = new PlayerUpdateProperties
{
VoiceState = new VoiceStateProperties(
Expand All @@ -188,12 +188,12 @@ private async ValueTask<TPlayer> CreatePlayerAsync(CancellationToken cancellatio

if (_options.Value.InitialTrack is not null)
{
var initialTrack = _options.Value.InitialTrack.Value;
var initialTrack = _options.Value.InitialTrack;
var loadOptions = _options.Value.InitialLoadOptions;

if (initialTrack.IsPresent)
if (initialTrack.Reference.IsPresent)
{
playerProperties = playerProperties with { TrackData = initialTrack.Track.ToString(), };
playerProperties = playerProperties with { TrackData = initialTrack.Track!.ToString(), };
}
else
{
Expand Down Expand Up @@ -231,6 +231,7 @@ private async ValueTask<TPlayer> CreatePlayerAsync(CancellationToken cancellatio
var properties = new PlayerProperties<TPlayer, TOptions>(
Context: _playerContext,
VoiceChannelId: _voiceState.Value.VoiceChannelId!.Value,
InitialTrack: _options.Value.InitialTrack,
InitialState: initialState,
Label: label,
SessionId: playerSession.SessionId,
Expand Down
2 changes: 1 addition & 1 deletion src/Lavalink4NET/Players/LavalinkPlayerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public record class LavalinkPlayerOptions

public string? Label { get; set; }

public TrackReference? InitialTrack { get; set; }
public ITrackQueueItem? InitialTrack { get; set; }

public TrackLoadOptions InitialLoadOptions { get; set; }

Expand Down
1 change: 1 addition & 0 deletions src/Lavalink4NET/Players/PlayerProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

internal sealed record class PlayerProperties<TPlayer, TOptions>(
PlayerContext Context,
ITrackQueueItem? InitialTrack,
PlayerInformationModel InitialState,
string Label,
ulong VoiceChannelId,
Expand Down

0 comments on commit 8efdba5

Please sign in to comment.