AiredEpisodeOrderComparer.cs 4.6 KB

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