aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Sorting
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Server.Implementations/Sorting')
-rw-r--r--MediaBrowser.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs138
-rw-r--r--MediaBrowser.Server.Implementations/Sorting/AlbumCountComparer.cs2
-rw-r--r--MediaBrowser.Server.Implementations/Sorting/BudgetComparer.cs7
-rw-r--r--MediaBrowser.Server.Implementations/Sorting/EpisodeCountComparer.cs2
-rw-r--r--MediaBrowser.Server.Implementations/Sorting/MovieCountComparer.cs2
-rw-r--r--MediaBrowser.Server.Implementations/Sorting/MusicVideoCountComparer.cs2
-rw-r--r--MediaBrowser.Server.Implementations/Sorting/RevenueComparer.cs7
-rw-r--r--MediaBrowser.Server.Implementations/Sorting/SeriesCountComparer.cs2
-rw-r--r--MediaBrowser.Server.Implementations/Sorting/SongCountComparer.cs2
-rw-r--r--MediaBrowser.Server.Implementations/Sorting/TrailerCountComparer.cs2
10 files changed, 157 insertions, 9 deletions
diff --git a/MediaBrowser.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs b/MediaBrowser.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs
new file mode 100644
index 000000000..76971342a
--- /dev/null
+++ b/MediaBrowser.Server.Implementations/Sorting/AiredEpisodeOrderComparer.cs
@@ -0,0 +1,138 @@
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Entities.TV;
+using MediaBrowser.Controller.Sorting;
+using MediaBrowser.Model.Querying;
+using System;
+
+namespace MediaBrowser.Server.Implementations.Sorting
+{
+ class AiredEpisodeOrderComparer : IBaseItemComparer
+ {
+ /// <summary>
+ /// Compares the specified x.
+ /// </summary>
+ /// <param name="x">The x.</param>
+ /// <param name="y">The y.</param>
+ /// <returns>System.Int32.</returns>
+ public int Compare(BaseItem x, BaseItem y)
+ {
+ if (x.PremiereDate.HasValue && y.PremiereDate.HasValue)
+ {
+ var val = DateTime.Compare(x.PremiereDate.Value, y.PremiereDate.Value);
+
+ if (val != 0)
+ {
+ //return val;
+ }
+ }
+
+ var episode1 = x as Episode;
+ var episode2 = y as Episode;
+
+ if (episode1 == null)
+ {
+ if (episode2 == null)
+ {
+ return 0;
+ }
+
+ return 1;
+ }
+
+ if (episode2 == null)
+ {
+ return -1;
+ }
+
+ return Compare(episode1, episode2);
+ }
+
+ private int Compare(Episode x, Episode y)
+ {
+ var isXSpecial = (x.PhysicalSeasonNumber ?? -1) == 0;
+ var isYSpecial = (y.PhysicalSeasonNumber ?? -1) == 0;
+
+ if (isXSpecial && isYSpecial)
+ {
+ return CompareSpecials(x, y);
+ }
+
+ if (!isXSpecial && !isYSpecial)
+ {
+ return CompareEpisodes(x, y);
+ }
+
+ if (!isXSpecial && isYSpecial)
+ {
+ return CompareEpisodeToSpecial(x, y);
+ }
+
+ return CompareEpisodeToSpecial(y, x) * -1;
+ }
+
+ private int CompareEpisodeToSpecial(Episode x, Episode y)
+ {
+ var xSeason = x.PhysicalSeasonNumber ?? -1;
+ var ySeason = y.AirsAfterSeasonNumber ?? y.AirsBeforeSeasonNumber ?? -1;
+
+ if (xSeason != ySeason)
+ {
+ return xSeason.CompareTo(ySeason);
+ }
+
+ // Now we know they have the same season
+
+ // Compare episode number
+
+ // Add 1 to to non-specials to account for AirsBeforeEpisodeNumber
+ var xEpisode = x.IndexNumber ?? -1;
+ xEpisode++;
+ var yEpisode = y.AirsBeforeEpisodeNumber ?? 10000;
+
+ return xEpisode.CompareTo(yEpisode);
+ }
+
+ private int CompareSpecials(Episode x, Episode y)
+ {
+ return GetSpecialCompareValue(x).CompareTo(GetSpecialCompareValue(y));
+ }
+
+ private int GetSpecialCompareValue(Episode item)
+ {
+ // First sort by season number
+ // Since there are three sort orders, pad with 9 digits (3 for each, figure 1000 episode buffer should be enough)
+ var val = (item.AirsAfterSeasonNumber ?? item.AirsBeforeSeasonNumber ?? 0) * 1000000000;
+
+ // Second sort order is if it airs after the season
+ if (item.AirsAfterSeasonNumber.HasValue)
+ {
+ val += 1000000;
+ }
+
+ // Third level is the episode number
+ val += (item.AirsBeforeEpisodeNumber ?? 0) * 1000;
+
+ // Finally, if that's still the same, last resort is the special number itself
+ val += item.IndexNumber ?? 0;
+
+ return val;
+ }
+
+ private int CompareEpisodes(Episode x, Episode y)
+ {
+ var xValue = ((x.PhysicalSeasonNumber ?? -1) * 1000) + (x.IndexNumber ?? -1);
+ var yValue = ((y.PhysicalSeasonNumber ?? -1) * 1000) + (y.IndexNumber ?? -1);
+
+ return xValue.CompareTo(yValue);
+ }
+
+ /// <summary>
+ /// Gets the name.
+ /// </summary>
+ /// <value>The name.</value>
+ public string Name
+ {
+ get { return ItemSortBy.AiredEpisodeOrder; }
+ }
+ }
+}
diff --git a/MediaBrowser.Server.Implementations/Sorting/AlbumCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/AlbumCountComparer.cs
index 8e24bc52d..e35ba00f2 100644
--- a/MediaBrowser.Server.Implementations/Sorting/AlbumCountComparer.cs
+++ b/MediaBrowser.Server.Implementations/Sorting/AlbumCountComparer.cs
@@ -48,7 +48,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
if (itemByName != null)
{
- var counts = itemByName.GetItemByNameCounts(User);
+ var counts = itemByName.GetItemByNameCounts(User.Id);
if (counts != null)
{
diff --git a/MediaBrowser.Server.Implementations/Sorting/BudgetComparer.cs b/MediaBrowser.Server.Implementations/Sorting/BudgetComparer.cs
index d2dac6549..87a7325c6 100644
--- a/MediaBrowser.Server.Implementations/Sorting/BudgetComparer.cs
+++ b/MediaBrowser.Server.Implementations/Sorting/BudgetComparer.cs
@@ -19,7 +19,12 @@ namespace MediaBrowser.Server.Implementations.Sorting
private double GetValue(BaseItem x)
{
- return x.Budget ?? 0;
+ var hasBudget = x as IHasBudget;
+ if (hasBudget != null)
+ {
+ return hasBudget.Budget ?? 0;
+ }
+ return 0;
}
/// <summary>
diff --git a/MediaBrowser.Server.Implementations/Sorting/EpisodeCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/EpisodeCountComparer.cs
index 7731e59d2..b3fd8a023 100644
--- a/MediaBrowser.Server.Implementations/Sorting/EpisodeCountComparer.cs
+++ b/MediaBrowser.Server.Implementations/Sorting/EpisodeCountComparer.cs
@@ -48,7 +48,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
if (itemByName != null)
{
- var counts = itemByName.GetItemByNameCounts(User);
+ var counts = itemByName.GetItemByNameCounts(User.Id);
if (counts != null)
{
diff --git a/MediaBrowser.Server.Implementations/Sorting/MovieCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/MovieCountComparer.cs
index 51f39a02f..605f4d1af 100644
--- a/MediaBrowser.Server.Implementations/Sorting/MovieCountComparer.cs
+++ b/MediaBrowser.Server.Implementations/Sorting/MovieCountComparer.cs
@@ -48,7 +48,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
if (itemByName != null)
{
- var counts = itemByName.GetItemByNameCounts(User);
+ var counts = itemByName.GetItemByNameCounts(User.Id);
if (counts != null)
{
diff --git a/MediaBrowser.Server.Implementations/Sorting/MusicVideoCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/MusicVideoCountComparer.cs
index 889658459..6c9c5534d 100644
--- a/MediaBrowser.Server.Implementations/Sorting/MusicVideoCountComparer.cs
+++ b/MediaBrowser.Server.Implementations/Sorting/MusicVideoCountComparer.cs
@@ -48,7 +48,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
if (itemByName != null)
{
- var counts = itemByName.GetItemByNameCounts(User);
+ var counts = itemByName.GetItemByNameCounts(User.Id);
if (counts != null)
{
diff --git a/MediaBrowser.Server.Implementations/Sorting/RevenueComparer.cs b/MediaBrowser.Server.Implementations/Sorting/RevenueComparer.cs
index e9d7912a1..6caa27ac3 100644
--- a/MediaBrowser.Server.Implementations/Sorting/RevenueComparer.cs
+++ b/MediaBrowser.Server.Implementations/Sorting/RevenueComparer.cs
@@ -19,7 +19,12 @@ namespace MediaBrowser.Server.Implementations.Sorting
private double GetValue(BaseItem x)
{
- return x.Revenue ?? 0;
+ var hasBudget = x as IHasBudget;
+ if (hasBudget != null)
+ {
+ return hasBudget.Revenue ?? 0;
+ }
+ return 0;
}
/// <summary>
diff --git a/MediaBrowser.Server.Implementations/Sorting/SeriesCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/SeriesCountComparer.cs
index 13d2932cb..8567e400c 100644
--- a/MediaBrowser.Server.Implementations/Sorting/SeriesCountComparer.cs
+++ b/MediaBrowser.Server.Implementations/Sorting/SeriesCountComparer.cs
@@ -48,7 +48,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
if (itemByName != null)
{
- var counts = itemByName.GetItemByNameCounts(User);
+ var counts = itemByName.GetItemByNameCounts(User.Id);
if (counts != null)
{
diff --git a/MediaBrowser.Server.Implementations/Sorting/SongCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/SongCountComparer.cs
index b12e1322a..85b849a21 100644
--- a/MediaBrowser.Server.Implementations/Sorting/SongCountComparer.cs
+++ b/MediaBrowser.Server.Implementations/Sorting/SongCountComparer.cs
@@ -48,7 +48,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
if (itemByName != null)
{
- var counts = itemByName.GetItemByNameCounts(User);
+ var counts = itemByName.GetItemByNameCounts(User.Id);
if (counts != null)
{
diff --git a/MediaBrowser.Server.Implementations/Sorting/TrailerCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/TrailerCountComparer.cs
index b6f67410a..a13875674 100644
--- a/MediaBrowser.Server.Implementations/Sorting/TrailerCountComparer.cs
+++ b/MediaBrowser.Server.Implementations/Sorting/TrailerCountComparer.cs
@@ -48,7 +48,7 @@ namespace MediaBrowser.Server.Implementations.Sorting
if (itemByName != null)
{
- var counts = itemByName.GetItemByNameCounts(User);
+ var counts = itemByName.GetItemByNameCounts(User.Id);
if (counts != null)
{