2020-05-29 09:28:19 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 19:22:24 +00:00
|
|
|
using System;
|
2023-11-09 21:00:13 +00:00
|
|
|
using Jellyfin.Data.Enums;
|
2019-01-06 20:50:43 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-11-20 17:10:02 +00:00
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
using MediaBrowser.Controller.Sorting;
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
|
2016-11-03 06:37:52 +00:00
|
|
|
namespace Emby.Server.Implementations.Sorting
|
2013-11-20 17:10:02 +00:00
|
|
|
{
|
2019-02-15 22:05:14 +00:00
|
|
|
public class AiredEpisodeOrderComparer : IBaseItemComparer
|
2013-11-20 17:10:02 +00:00
|
|
|
{
|
2021-10-02 17:23:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the name.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The name.</value>
|
2023-11-09 21:00:13 +00:00
|
|
|
public ItemSortBy Type => ItemSortBy.AiredEpisodeOrder;
|
2021-10-02 17:23:05 +00:00
|
|
|
|
2013-11-20 17:10:02 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Compares the specified x.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The x.</param>
|
|
|
|
/// <param name="y">The y.</param>
|
|
|
|
/// <returns>System.Int32.</returns>
|
2021-05-20 19:28:18 +00:00
|
|
|
public int Compare(BaseItem? x, BaseItem? y)
|
2013-11-20 17:10:02 +00:00
|
|
|
{
|
2022-10-06 18:21:23 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(x);
|
2019-01-06 20:50:43 +00:00
|
|
|
|
2022-10-06 18:21:23 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(y);
|
2019-01-06 20:50:43 +00:00
|
|
|
|
2013-11-20 17:10:02 +00:00
|
|
|
var episode1 = x as Episode;
|
|
|
|
var episode2 = y as Episode;
|
|
|
|
|
2022-12-05 14:00:20 +00:00
|
|
|
if (episode1 is null)
|
2013-11-20 17:10:02 +00:00
|
|
|
{
|
2022-12-05 14:00:20 +00:00
|
|
|
if (episode2 is null)
|
2013-11-20 17:10:02 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-12-05 14:00:20 +00:00
|
|
|
if (episode2 is null)
|
2013-11-20 17:10:02 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Compare(episode1, episode2);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int Compare(Episode x, Episode y)
|
|
|
|
{
|
2016-05-02 05:32:04 +00:00
|
|
|
var isXSpecial = (x.ParentIndexNumber ?? -1) == 0;
|
|
|
|
var isYSpecial = (y.ParentIndexNumber ?? -1) == 0;
|
2013-11-20 17:10:02 +00:00
|
|
|
|
|
|
|
if (isXSpecial && isYSpecial)
|
|
|
|
{
|
|
|
|
return CompareSpecials(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isXSpecial && !isYSpecial)
|
|
|
|
{
|
|
|
|
return CompareEpisodes(x, y);
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:31:14 +00:00
|
|
|
if (!isXSpecial)
|
2013-11-20 17:10:02 +00:00
|
|
|
{
|
|
|
|
return CompareEpisodeToSpecial(x, y);
|
|
|
|
}
|
|
|
|
|
2013-11-27 19:04:19 +00:00
|
|
|
return CompareEpisodeToSpecial(y, x) * -1;
|
2013-11-20 17:10:02 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
private static int CompareEpisodeToSpecial(Episode x, Episode y)
|
2013-11-20 17:10:02 +00:00
|
|
|
{
|
2015-01-27 22:45:59 +00:00
|
|
|
// http://thetvdb.com/wiki/index.php?title=Special_Episodes
|
|
|
|
|
2016-05-02 05:32:04 +00:00
|
|
|
var xSeason = x.ParentIndexNumber ?? -1;
|
2013-11-20 17:10:02 +00:00
|
|
|
var ySeason = y.AirsAfterSeasonNumber ?? y.AirsBeforeSeasonNumber ?? -1;
|
|
|
|
|
|
|
|
if (xSeason != ySeason)
|
|
|
|
{
|
|
|
|
return xSeason.CompareTo(ySeason);
|
|
|
|
}
|
|
|
|
|
2013-12-26 17:14:57 +00:00
|
|
|
// Special comes after episode
|
|
|
|
if (y.AirsAfterSeasonNumber.HasValue)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-20 17:10:02 +00:00
|
|
|
|
2013-12-26 17:14:57 +00:00
|
|
|
var yEpisode = y.AirsBeforeEpisodeNumber;
|
|
|
|
|
|
|
|
// Special comes before the season
|
|
|
|
if (!yEpisode.HasValue)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2013-11-20 17:10:02 +00:00
|
|
|
|
2013-12-26 17:14:57 +00:00
|
|
|
// Compare episode number
|
|
|
|
var xEpisode = x.IndexNumber;
|
2013-12-08 21:31:14 +00:00
|
|
|
|
2013-12-26 17:14:57 +00:00
|
|
|
if (!xEpisode.HasValue)
|
|
|
|
{
|
|
|
|
// Can't really compare if this happens
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-20 17:10:02 +00:00
|
|
|
|
2013-12-26 17:14:57 +00:00
|
|
|
// Special comes before episode
|
|
|
|
if (xEpisode.Value == yEpisode.Value)
|
2013-12-08 21:31:14 +00:00
|
|
|
{
|
2013-12-26 17:14:57 +00:00
|
|
|
return 1;
|
2013-12-08 21:31:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-26 17:14:57 +00:00
|
|
|
return xEpisode.Value.CompareTo(yEpisode.Value);
|
2013-11-20 17:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private int CompareSpecials(Episode x, Episode y)
|
|
|
|
{
|
|
|
|
return GetSpecialCompareValue(x).CompareTo(GetSpecialCompareValue(y));
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:18:27 +00:00
|
|
|
private static long GetSpecialCompareValue(Episode item)
|
2013-11-20 17:10:02 +00:00
|
|
|
{
|
|
|
|
// First sort by season number
|
|
|
|
// Since there are three sort orders, pad with 9 digits (3 for each, figure 1000 episode buffer should be enough)
|
2021-02-17 20:18:27 +00:00
|
|
|
var val = (item.AirsAfterSeasonNumber ?? item.AirsBeforeSeasonNumber ?? 0) * 1000000000L;
|
2013-11-20 17:10:02 +00:00
|
|
|
|
|
|
|
// Second sort order is if it airs after the season
|
|
|
|
if (item.AirsAfterSeasonNumber.HasValue)
|
|
|
|
{
|
|
|
|
val += 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Third level is the episode number
|
|
|
|
val += (item.AirsBeforeEpisodeNumber ?? 0) * 1000;
|
|
|
|
|
|
|
|
// Finally, if that's still the same, last resort is the special number itself
|
|
|
|
val += item.IndexNumber ?? 0;
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2019-01-06 20:50:43 +00:00
|
|
|
private static int CompareEpisodes(Episode x, Episode y)
|
2013-11-20 17:10:02 +00:00
|
|
|
{
|
2020-07-24 14:37:54 +00:00
|
|
|
var xValue = ((x.ParentIndexNumber ?? -1) * 1000) + (x.IndexNumber ?? -1);
|
|
|
|
var yValue = ((y.ParentIndexNumber ?? -1) * 1000) + (y.IndexNumber ?? -1);
|
2021-09-13 15:32:12 +00:00
|
|
|
var comparisonResult = xValue.CompareTo(yValue);
|
|
|
|
// If equal, compare premiere dates
|
|
|
|
if (comparisonResult == 0 && x.PremiereDate.HasValue && y.PremiereDate.HasValue)
|
2021-09-12 22:05:37 +00:00
|
|
|
{
|
2021-09-13 15:32:12 +00:00
|
|
|
comparisonResult = DateTime.Compare(x.PremiereDate.Value, y.PremiereDate.Value);
|
2021-09-12 22:05:37 +00:00
|
|
|
}
|
2013-11-20 17:10:02 +00:00
|
|
|
|
2021-09-13 15:32:18 +00:00
|
|
|
return comparisonResult;
|
2013-11-20 17:10:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|