AiredEpisodeOrderComparer.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 MediaBrowser.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.PhysicalSeasonNumber ?? -1) == 0;
  45. var isYSpecial = (y.PhysicalSeasonNumber ?? -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. var xSeason = x.PhysicalSeasonNumber ?? -1;
  63. var ySeason = y.AirsAfterSeasonNumber ?? y.AirsBeforeSeasonNumber ?? -1;
  64. if (xSeason != ySeason)
  65. {
  66. return xSeason.CompareTo(ySeason);
  67. }
  68. // Now we know they have the same season
  69. // Compare episode number
  70. // Add 1 to to non-specials to account for AirsBeforeEpisodeNumber
  71. var xEpisode = x.IndexNumber ?? -1;
  72. xEpisode++;
  73. var yEpisode = y.AirsBeforeEpisodeNumber ?? 10000;
  74. // Sometimes they'll both have a value.
  75. // For example AirsAfterSeasonNumber=1, AirsBeforeSeasonNumber=2, AirsBeforeEpisodeNumber=1
  76. // The episode should be displayed at the end of season 1
  77. if (y.AirsAfterSeasonNumber.HasValue && y.AirsBeforeSeasonNumber.HasValue && y.AirsBeforeSeasonNumber.Value > y.AirsAfterSeasonNumber.Value)
  78. {
  79. yEpisode = 10000;
  80. }
  81. return xEpisode.CompareTo(yEpisode);
  82. }
  83. private int CompareSpecials(Episode x, Episode y)
  84. {
  85. return GetSpecialCompareValue(x).CompareTo(GetSpecialCompareValue(y));
  86. }
  87. private int GetSpecialCompareValue(Episode item)
  88. {
  89. // First sort by season number
  90. // Since there are three sort orders, pad with 9 digits (3 for each, figure 1000 episode buffer should be enough)
  91. var val = (item.AirsAfterSeasonNumber ?? item.AirsBeforeSeasonNumber ?? 0) * 1000000000;
  92. // Second sort order is if it airs after the season
  93. if (item.AirsAfterSeasonNumber.HasValue)
  94. {
  95. val += 1000000;
  96. }
  97. // Third level is the episode number
  98. val += (item.AirsBeforeEpisodeNumber ?? 0) * 1000;
  99. // Finally, if that's still the same, last resort is the special number itself
  100. val += item.IndexNumber ?? 0;
  101. return val;
  102. }
  103. private int CompareEpisodes(Episode x, Episode y)
  104. {
  105. var xValue = ((x.PhysicalSeasonNumber ?? -1) * 1000) + (x.IndexNumber ?? -1);
  106. var yValue = ((y.PhysicalSeasonNumber ?? -1) * 1000) + (y.IndexNumber ?? -1);
  107. return xValue.CompareTo(yValue);
  108. }
  109. /// <summary>
  110. /// Gets the name.
  111. /// </summary>
  112. /// <value>The name.</value>
  113. public string Name
  114. {
  115. get { return ItemSortBy.AiredEpisodeOrder; }
  116. }
  117. }
  118. }