2013-11-20 21:08:12 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-11-20 17:10:02 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
|
using MediaBrowser.Controller.Sorting;
|
|
|
|
|
using MediaBrowser.Model.Querying;
|
2013-11-20 21:08:12 +00:00
|
|
|
|
using System;
|
2013-11-20 17:10:02 +00:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2013-11-20 21:08:12 +00:00
|
|
|
|
if (x.PremiereDate.HasValue && y.PremiereDate.HasValue)
|
2013-11-20 17:10:02 +00:00
|
|
|
|
{
|
2013-11-20 21:08:12 +00:00
|
|
|
|
var val = DateTime.Compare(x.PremiereDate.Value, y.PremiereDate.Value);
|
|
|
|
|
|
|
|
|
|
if (val != 0)
|
|
|
|
|
{
|
2013-11-27 19:04:19 +00:00
|
|
|
|
//return val;
|
2013-11-20 21:08:12 +00:00
|
|
|
|
}
|
2013-11-20 17:10:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2013-11-27 19:04:19 +00:00
|
|
|
|
var isXSpecial = (x.PhysicalSeasonNumber ?? -1) == 0;
|
|
|
|
|
var isYSpecial = (y.PhysicalSeasonNumber ?? -1) == 0;
|
2013-11-20 17:10:02 +00:00
|
|
|
|
|
|
|
|
|
if (isXSpecial && isYSpecial)
|
|
|
|
|
{
|
|
|
|
|
return CompareSpecials(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isXSpecial && !isYSpecial)
|
|
|
|
|
{
|
|
|
|
|
return CompareEpisodes(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-08 21:31:14 +00:00
|
|
|
|
if (!isXSpecial)
|
2013-11-20 17:10:02 +00:00
|
|
|
|
{
|
|
|
|
|
return CompareEpisodeToSpecial(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-27 19:04:19 +00:00
|
|
|
|
return CompareEpisodeToSpecial(y, x) * -1;
|
2013-11-20 17:10:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int CompareEpisodeToSpecial(Episode x, Episode y)
|
|
|
|
|
{
|
2013-11-27 19:04:19 +00:00
|
|
|
|
var xSeason = x.PhysicalSeasonNumber ?? -1;
|
2013-11-20 17:10:02 +00:00
|
|
|
|
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
|
2013-11-27 19:04:19 +00:00
|
|
|
|
var xEpisode = x.IndexNumber ?? -1;
|
|
|
|
|
xEpisode++;
|
2013-12-08 21:31:14 +00:00
|
|
|
|
|
2013-11-27 19:04:19 +00:00
|
|
|
|
var yEpisode = y.AirsBeforeEpisodeNumber ?? 10000;
|
2013-11-20 17:10:02 +00:00
|
|
|
|
|
2013-12-08 21:31:14 +00:00
|
|
|
|
// Sometimes they'll both have a value.
|
|
|
|
|
// For example AirsAfterSeasonNumber=1, AirsBeforeSeasonNumber=2, AirsBeforeEpisodeNumber=1
|
|
|
|
|
// The episode should be displayed at the end of season 1
|
|
|
|
|
if (y.AirsAfterSeasonNumber.HasValue && y.AirsBeforeSeasonNumber.HasValue && y.AirsBeforeSeasonNumber.Value > y.AirsAfterSeasonNumber.Value)
|
|
|
|
|
{
|
|
|
|
|
yEpisode = 10000;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-20 17:10:02 +00:00
|
|
|
|
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)
|
2013-11-23 20:01:09 +00:00
|
|
|
|
var val = (item.AirsAfterSeasonNumber ?? item.AirsBeforeSeasonNumber ?? 0) * 1000000000;
|
2013-11-20 17:10:02 +00:00
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
{
|
2013-11-27 19:04:19 +00:00
|
|
|
|
var xValue = ((x.PhysicalSeasonNumber ?? -1) * 1000) + (x.IndexNumber ?? -1);
|
|
|
|
|
var yValue = ((y.PhysicalSeasonNumber ?? -1) * 1000) + (y.IndexNumber ?? -1);
|
2013-11-20 17:10:02 +00:00
|
|
|
|
|
|
|
|
|
return xValue.CompareTo(yValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The name.</value>
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return ItemSortBy.AiredEpisodeOrder; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|