AiredEpisodeOrderComparer.cs 4.7 KB

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