| | 1 | | #pragma warning disable CS1591 |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using Jellyfin.Data.Enums; |
| | 5 | | using MediaBrowser.Controller.Entities; |
| | 6 | | using MediaBrowser.Controller.Entities.TV; |
| | 7 | | using MediaBrowser.Controller.Sorting; |
| | 8 | | using MediaBrowser.Model.Querying; |
| | 9 | |
|
| | 10 | | namespace Emby.Server.Implementations.Sorting |
| | 11 | | { |
| | 12 | | public class AiredEpisodeOrderComparer : IBaseItemComparer |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Gets the name. |
| | 16 | | /// </summary> |
| | 17 | | /// <value>The name.</value> |
| 1 | 18 | | public ItemSortBy Type => ItemSortBy.AiredEpisodeOrder; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Compares the specified x. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="x">The x.</param> |
| | 24 | | /// <param name="y">The y.</param> |
| | 25 | | /// <returns>System.Int32.</returns> |
| | 26 | | public int Compare(BaseItem? x, BaseItem? y) |
| | 27 | | { |
| 48 | 28 | | ArgumentNullException.ThrowIfNull(x); |
| | 29 | |
|
| 47 | 30 | | ArgumentNullException.ThrowIfNull(y); |
| | 31 | |
|
| 46 | 32 | | var episode1 = x as Episode; |
| 46 | 33 | | var episode2 = y as Episode; |
| | 34 | |
|
| 46 | 35 | | if (episode1 is null) |
| | 36 | | { |
| 3 | 37 | | if (episode2 is null) |
| | 38 | | { |
| 2 | 39 | | return 0; |
| | 40 | | } |
| | 41 | |
|
| 1 | 42 | | return 1; |
| | 43 | | } |
| | 44 | |
|
| 43 | 45 | | if (episode2 is null) |
| | 46 | | { |
| 1 | 47 | | return -1; |
| | 48 | | } |
| | 49 | |
|
| 42 | 50 | | return Compare(episode1, episode2); |
| | 51 | | } |
| | 52 | |
|
| | 53 | | private int Compare(Episode x, Episode y) |
| | 54 | | { |
| 42 | 55 | | var isXSpecial = (x.ParentIndexNumber ?? -1) == 0; |
| 42 | 56 | | var isYSpecial = (y.ParentIndexNumber ?? -1) == 0; |
| | 57 | |
|
| 42 | 58 | | if (isXSpecial && isYSpecial) |
| | 59 | | { |
| 4 | 60 | | return CompareSpecials(x, y); |
| | 61 | | } |
| | 62 | |
|
| 38 | 63 | | if (!isXSpecial && !isYSpecial) |
| | 64 | | { |
| 14 | 65 | | return CompareEpisodes(x, y); |
| | 66 | | } |
| | 67 | |
|
| 24 | 68 | | if (!isXSpecial) |
| | 69 | | { |
| 12 | 70 | | return CompareEpisodeToSpecial(x, y); |
| | 71 | | } |
| | 72 | |
|
| 12 | 73 | | return CompareEpisodeToSpecial(y, x) * -1; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | private static int CompareEpisodeToSpecial(Episode x, Episode y) |
| | 77 | | { |
| | 78 | | // http://thetvdb.com/wiki/index.php?title=Special_Episodes |
| | 79 | |
|
| 24 | 80 | | var xSeason = x.ParentIndexNumber ?? -1; |
| 24 | 81 | | var ySeason = y.AirsAfterSeasonNumber ?? y.AirsBeforeSeasonNumber ?? -1; |
| | 82 | |
|
| 24 | 83 | | if (xSeason != ySeason) |
| | 84 | | { |
| 14 | 85 | | return xSeason.CompareTo(ySeason); |
| | 86 | | } |
| | 87 | |
|
| | 88 | | // Special comes after episode |
| 10 | 89 | | if (y.AirsAfterSeasonNumber.HasValue) |
| | 90 | | { |
| 2 | 91 | | return -1; |
| | 92 | | } |
| | 93 | |
|
| 8 | 94 | | var yEpisode = y.AirsBeforeEpisodeNumber; |
| | 95 | |
|
| | 96 | | // Special comes before the season |
| 8 | 97 | | if (!yEpisode.HasValue) |
| | 98 | | { |
| 2 | 99 | | return 1; |
| | 100 | | } |
| | 101 | |
|
| | 102 | | // Compare episode number |
| 6 | 103 | | var xEpisode = x.IndexNumber; |
| | 104 | |
|
| 6 | 105 | | if (!xEpisode.HasValue) |
| | 106 | | { |
| | 107 | | // Can't really compare if this happens |
| 2 | 108 | | return 0; |
| | 109 | | } |
| | 110 | |
|
| | 111 | | // Special comes before episode |
| 4 | 112 | | if (xEpisode.Value == yEpisode.Value) |
| | 113 | | { |
| 2 | 114 | | return 1; |
| | 115 | | } |
| | 116 | |
|
| 2 | 117 | | return xEpisode.Value.CompareTo(yEpisode.Value); |
| | 118 | | } |
| | 119 | |
|
| | 120 | | private int CompareSpecials(Episode x, Episode y) |
| | 121 | | { |
| 4 | 122 | | return GetSpecialCompareValue(x).CompareTo(GetSpecialCompareValue(y)); |
| | 123 | | } |
| | 124 | |
|
| | 125 | | private static long GetSpecialCompareValue(Episode item) |
| | 126 | | { |
| | 127 | | // First sort by season number |
| | 128 | | // Since there are three sort orders, pad with 9 digits (3 for each, figure 1000 episode buffer should be en |
| 8 | 129 | | var val = (item.AirsAfterSeasonNumber ?? item.AirsBeforeSeasonNumber ?? 0) * 1000000000L; |
| | 130 | |
|
| | 131 | | // Second sort order is if it airs after the season |
| 8 | 132 | | if (item.AirsAfterSeasonNumber.HasValue) |
| | 133 | | { |
| 0 | 134 | | val += 1000000; |
| | 135 | | } |
| | 136 | |
|
| | 137 | | // Third level is the episode number |
| 8 | 138 | | val += (item.AirsBeforeEpisodeNumber ?? 0) * 1000; |
| | 139 | |
|
| | 140 | | // Finally, if that's still the same, last resort is the special number itself |
| 8 | 141 | | val += item.IndexNumber ?? 0; |
| | 142 | |
|
| 8 | 143 | | return val; |
| | 144 | | } |
| | 145 | |
|
| | 146 | | private static int CompareEpisodes(Episode x, Episode y) |
| | 147 | | { |
| 14 | 148 | | var xValue = ((x.ParentIndexNumber ?? -1) * 1000) + (x.IndexNumber ?? -1); |
| 14 | 149 | | var yValue = ((y.ParentIndexNumber ?? -1) * 1000) + (y.IndexNumber ?? -1); |
| 14 | 150 | | var comparisonResult = xValue.CompareTo(yValue); |
| | 151 | | // If equal, compare premiere dates |
| 14 | 152 | | if (comparisonResult == 0 && x.PremiereDate.HasValue && y.PremiereDate.HasValue) |
| | 153 | | { |
| 6 | 154 | | comparisonResult = DateTime.Compare(x.PremiereDate.Value, y.PremiereDate.Value); |
| | 155 | | } |
| | 156 | |
|
| 14 | 157 | | return comparisonResult; |
| | 158 | | } |
| | 159 | | } |
| | 160 | | } |