AiredEpisodeOrderComparer.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. // Special comes after episode
  69. if (y.AirsAfterSeasonNumber.HasValue)
  70. {
  71. return -1;
  72. }
  73. var yEpisode = y.AirsBeforeEpisodeNumber;
  74. // Special comes before the season
  75. if (!yEpisode.HasValue)
  76. {
  77. return 1;
  78. }
  79. // Compare episode number
  80. var xEpisode = x.IndexNumber;
  81. if (!xEpisode.HasValue)
  82. {
  83. // Can't really compare if this happens
  84. return 0;
  85. }
  86. // Special comes before episode
  87. if (xEpisode.Value == yEpisode.Value)
  88. {
  89. return 1;
  90. }
  91. return xEpisode.Value.CompareTo(yEpisode.Value);
  92. }
  93. private int CompareSpecials(Episode x, Episode y)
  94. {
  95. return GetSpecialCompareValue(x).CompareTo(GetSpecialCompareValue(y));
  96. }
  97. private int GetSpecialCompareValue(Episode item)
  98. {
  99. // First sort by season number
  100. // Since there are three sort orders, pad with 9 digits (3 for each, figure 1000 episode buffer should be enough)
  101. var val = (item.AirsAfterSeasonNumber ?? item.AirsBeforeSeasonNumber ?? 0) * 1000000000;
  102. // Second sort order is if it airs after the season
  103. if (item.AirsAfterSeasonNumber.HasValue)
  104. {
  105. val += 1000000;
  106. }
  107. // Third level is the episode number
  108. val += (item.AirsBeforeEpisodeNumber ?? 0) * 1000;
  109. // Finally, if that's still the same, last resort is the special number itself
  110. val += item.IndexNumber ?? 0;
  111. return val;
  112. }
  113. private int CompareEpisodes(Episode x, Episode y)
  114. {
  115. var xValue = ((x.PhysicalSeasonNumber ?? -1) * 1000) + (x.IndexNumber ?? -1);
  116. var yValue = ((y.PhysicalSeasonNumber ?? -1) * 1000) + (y.IndexNumber ?? -1);
  117. return xValue.CompareTo(yValue);
  118. }
  119. /// <summary>
  120. /// Gets the name.
  121. /// </summary>
  122. /// <value>The name.</value>
  123. public string Name
  124. {
  125. get { return ItemSortBy.AiredEpisodeOrder; }
  126. }
  127. }
  128. }