AiredEpisodeOrderComparer.cs 4.4 KB

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