AiredEpisodeOrderComparer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 && isYSpecial)
  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. return xEpisode.CompareTo(yEpisode);
  75. }
  76. private int CompareSpecials(Episode x, Episode y)
  77. {
  78. return GetSpecialCompareValue(x).CompareTo(GetSpecialCompareValue(y));
  79. }
  80. private int GetSpecialCompareValue(Episode item)
  81. {
  82. // First sort by season number
  83. // Since there are three sort orders, pad with 9 digits (3 for each, figure 1000 episode buffer should be enough)
  84. var val = (item.AirsAfterSeasonNumber ?? item.AirsBeforeSeasonNumber ?? 0) * 1000000000;
  85. // Second sort order is if it airs after the season
  86. if (item.AirsAfterSeasonNumber.HasValue)
  87. {
  88. val += 1000000;
  89. }
  90. // Third level is the episode number
  91. val += (item.AirsBeforeEpisodeNumber ?? 0) * 1000;
  92. // Finally, if that's still the same, last resort is the special number itself
  93. val += item.IndexNumber ?? 0;
  94. return val;
  95. }
  96. private int CompareEpisodes(Episode x, Episode y)
  97. {
  98. var xValue = ((x.PhysicalSeasonNumber ?? -1) * 1000) + (x.IndexNumber ?? -1);
  99. var yValue = ((y.PhysicalSeasonNumber ?? -1) * 1000) + (y.IndexNumber ?? -1);
  100. return xValue.CompareTo(yValue);
  101. }
  102. /// <summary>
  103. /// Gets the name.
  104. /// </summary>
  105. /// <value>The name.</value>
  106. public string Name
  107. {
  108. get { return ItemSortBy.AiredEpisodeOrder; }
  109. }
  110. }
  111. }