jellyfin/MediaBrowser.Api/GamesService.cs

189 lines
6.4 KiB
C#
Raw Normal View History

2013-10-05 20:03:08 -07:00
using System.Globalization;
using MediaBrowser.Controller.Dto;
2013-09-04 10:02:19 -07:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
2013-09-21 14:00:04 -07:00
using MediaBrowser.Model.Dto;
2013-12-07 08:52:38 -07:00
using ServiceStack;
2013-09-21 14:00:04 -07:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace MediaBrowser.Api
{
/// <summary>
/// Class GetSimilarGames
/// </summary>
[Route("/Games/{Id}/Similar", "GET")]
[Api(Description = "Finds games similar to a given game.")]
2013-08-09 08:55:22 -07:00
public class GetSimilarGames : BaseGetSimilarItemsFromItem
{
}
2013-09-21 14:00:04 -07:00
/// <summary>
/// Class GetGameSystemSummaries
/// </summary>
[Route("/Games/SystemSummaries", "GET")]
[Api(Description = "Finds games similar to a given game.")]
public class GetGameSystemSummaries : IReturn<List<GameSystemSummary>>
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public Guid? UserId { get; set; }
}
2013-10-05 20:03:08 -07:00
/// <summary>
/// Class GetGameSystemSummaries
/// </summary>
[Route("/Games/PlayerIndex", "GET")]
[Api(Description = "Gets an index of players (1-x) and the number of games listed under each")]
public class GetPlayerIndex : IReturn<List<ItemIndex>>
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public Guid? UserId { get; set; }
}
/// <summary>
/// Class GamesService
/// </summary>
public class GamesService : BaseApiService
{
/// <summary>
/// The _user manager
/// </summary>
private readonly IUserManager _userManager;
/// <summary>
/// The _user data repository
/// </summary>
2013-10-02 09:08:58 -07:00
private readonly IUserDataManager _userDataRepository;
/// <summary>
/// The _library manager
/// </summary>
private readonly ILibraryManager _libraryManager;
2013-09-21 14:00:04 -07:00
/// <summary>
/// The _item repo
/// </summary>
2013-06-18 12:16:27 -07:00
private readonly IItemRepository _itemRepo;
2013-09-21 14:00:04 -07:00
/// <summary>
/// The _dto service
/// </summary>
2013-09-04 10:02:19 -07:00
private readonly IDtoService _dtoService;
2013-06-18 12:16:27 -07:00
/// <summary>
2013-06-18 12:16:27 -07:00
/// Initializes a new instance of the <see cref="GamesService" /> class.
/// </summary>
/// <param name="userManager">The user manager.</param>
/// <param name="userDataRepository">The user data repository.</param>
/// <param name="libraryManager">The library manager.</param>
2013-06-18 12:16:27 -07:00
/// <param name="itemRepo">The item repo.</param>
2013-09-21 14:00:04 -07:00
/// <param name="dtoService">The dto service.</param>
2013-10-02 09:08:58 -07:00
public GamesService(IUserManager userManager, IUserDataManager userDataRepository, ILibraryManager libraryManager, IItemRepository itemRepo, IDtoService dtoService)
{
_userManager = userManager;
_userDataRepository = userDataRepository;
_libraryManager = libraryManager;
2013-06-18 12:16:27 -07:00
_itemRepo = itemRepo;
2013-09-04 10:02:19 -07:00
_dtoService = dtoService;
}
2013-09-21 14:00:04 -07:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetGameSystemSummaries request)
{
var gameSystems = GetAllLibraryItems(request.UserId, _userManager, _libraryManager)
.OfType<GameSystem>()
.ToList();
var user = request.UserId == null ? null : _userManager.GetUserById(request.UserId.Value);
var result = gameSystems
.Select(i => GetSummary(i, user))
.ToList();
return ToOptimizedSerializedResultUsingCache(result);
2013-09-21 14:00:04 -07:00
}
2013-10-05 20:03:08 -07:00
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
public object Get(GetPlayerIndex request)
{
var games = GetAllLibraryItems(request.UserId, _userManager, _libraryManager)
.OfType<Game>()
.ToList();
var lookup = games
.ToLookup(i => i.PlayersSupported ?? -1)
.OrderBy(i => i.Key)
.Select(i => new ItemIndex
{
ItemCount = i.Count(),
Name = i.Key == -1 ? string.Empty : i.Key.ToString(UsCulture)
})
.ToList();
return ToOptimizedSerializedResultUsingCache(lookup);
2013-10-05 20:03:08 -07:00
}
2013-09-21 14:00:04 -07:00
/// <summary>
/// Gets the summary.
/// </summary>
/// <param name="system">The system.</param>
/// <param name="user">The user.</param>
/// <returns>GameSystemSummary.</returns>
private GameSystemSummary GetSummary(GameSystem system, User user)
{
var summary = new GameSystemSummary
{
2013-09-22 09:49:55 -07:00
Name = system.GameSystemName,
DisplayName = system.Name
2013-09-21 14:00:04 -07:00
};
var items = user == null ? system.RecursiveChildren : system.GetRecursiveChildren(user);
var games = items.OfType<Game>().ToList();
summary.ClientInstalledGameCount = games.Count(i => !i.IsPlaceHolder);
2013-09-22 15:42:21 -07:00
2013-09-21 14:00:04 -07:00
summary.GameCount = games.Count;
summary.GameFileExtensions = games.Where(i => !i.IsPlaceHolder).Select(i => Path.GetExtension(i.Path))
2013-09-21 14:00:04 -07:00
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
return summary;
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetSimilarGames request)
{
2013-08-08 13:18:05 -07:00
var result = SimilarItemsHelper.GetSimilarItemsResult(_userManager,
2013-06-18 12:16:27 -07:00
_itemRepo,
_libraryManager,
_userDataRepository,
2013-09-04 10:02:19 -07:00
_dtoService,
Logger,
request, item => item is Game,
SimilarItemsHelper.GetSimiliarityScore);
return ToOptimizedSerializedResultUsingCache(result);
}
}
}