diff --git a/Shoko.Server/Services/GeneratedPlaylistService.cs b/Shoko.Server/Services/GeneratedPlaylistService.cs index 78e284bab..2d99ca4f6 100644 --- a/Shoko.Server/Services/GeneratedPlaylistService.cs +++ b/Shoko.Server/Services/GeneratedPlaylistService.cs @@ -348,13 +348,28 @@ public bool TryParsePlaylist(string[] items, out IReadOnlyList<(IReadOnlyList episodes, IReadOnlyList videos)> GetListForVideo(IVideo video) { - var episode = video.Episodes - .OrderBy(episode => episode.Type) - .ThenBy(episode => episode.EpisodeNumber) - .FirstOrDefault(); - return episode is not null ? [([episode], [video])] : []; + var crossReferences = video.CrossReferences; + var seriesOrder = crossReferences + .OrderBy(xref => xref.Order) + .Select(xref => xref.AnidbAnimeID) + .Distinct() + .ToArray(); + var episodes = crossReferences + .DistinctBy(xref => xref.AnidbEpisodeID) + .Select(xref => (xref, episode: xref.ShokoEpisode!)) + .Where(tuple => tuple.episode is not null) + .GroupBy(tuple => tuple.episode.Type) + .OrderBy(groupBy => Array.IndexOf(_videoToEpisodeGroupPreference, groupBy.Key)) + .First() + .OrderBy(tuple => Array.IndexOf(seriesOrder, tuple.xref.AnidbAnimeID)) + .ThenBy(tuple => tuple.episode.EpisodeNumber) + .Select(tuple => tuple.episode) + .ToList() as IReadOnlyList; + return episodes is { Count: > 0 } ? [(episodes, [video])] : []; } public FileStreamResult GeneratePlaylist(