AiredEpisodeOrderComparer.cs 4.6 KB

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