AiredEpisodeOrderComparer.cs 4.7 KB

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