using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Dto;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace MediaBrowser.Api
{
///
/// Class GetSimilarGames
///
[Route("/Games/{Id}/Similar", "GET", Summary = "Finds games similar to a given game.")]
public class GetSimilarGames : BaseGetSimilarItemsFromItem
{
}
///
/// Class GetGameSystemSummaries
///
[Route("/Games/SystemSummaries", "GET", Summary = "Finds games similar to a given game.")]
public class GetGameSystemSummaries : IReturn>
{
///
/// Gets or sets the user id.
///
/// The user id.
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string UserId { get; set; }
}
///
/// Class GetGameSystemSummaries
///
[Route("/Games/PlayerIndex", "GET", Summary = "Gets an index of players (1-x) and the number of games listed under each")]
public class GetPlayerIndex : IReturn>
{
///
/// Gets or sets the user id.
///
/// The user id.
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public string UserId { get; set; }
}
///
/// Class GamesService
///
[Authenticated]
public class GamesService : BaseApiService
{
///
/// The _user manager
///
private readonly IUserManager _userManager;
///
/// The _user data repository
///
private readonly IUserDataManager _userDataRepository;
///
/// The _library manager
///
private readonly ILibraryManager _libraryManager;
///
/// The _item repo
///
private readonly IItemRepository _itemRepo;
///
/// The _dto service
///
private readonly IDtoService _dtoService;
///
/// Initializes a new instance of the class.
///
/// The user manager.
/// The user data repository.
/// The library manager.
/// The item repo.
/// The dto service.
public GamesService(IUserManager userManager, IUserDataManager userDataRepository, ILibraryManager libraryManager, IItemRepository itemRepo, IDtoService dtoService)
{
_userManager = userManager;
_userDataRepository = userDataRepository;
_libraryManager = libraryManager;
_itemRepo = itemRepo;
_dtoService = dtoService;
}
///
/// Gets the specified request.
///
/// The request.
/// System.Object.
public object Get(GetGameSystemSummaries request)
{
var user = request.UserId == null ? null : _userManager.GetUserById(request.UserId);
var query = new InternalItemsQuery(user)
{
IncludeItemTypes = new[] { typeof(GameSystem).Name }
};
var parentIds = new string[] { } ;
var gameSystems = _libraryManager.GetItemList(query, parentIds)
.Cast()
.ToList();
var result = gameSystems
.Select(i => GetSummary(i, user))
.ToList();
return ToOptimizedSerializedResultUsingCache(result);
}
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
public object Get(GetPlayerIndex request)
{
var user = request.UserId == null ? null : _userManager.GetUserById(request.UserId);
var query = new InternalItemsQuery(user)
{
IncludeItemTypes = new[] { typeof(Game).Name }
};
var parentIds = new string[] { };
var games = _libraryManager.GetItemList(query, parentIds)
.Cast()
.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);
}
///
/// Gets the summary.
///
/// The system.
/// The user.
/// GameSystemSummary.
private GameSystemSummary GetSummary(GameSystem system, User user)
{
var summary = new GameSystemSummary
{
Name = system.GameSystemName,
DisplayName = system.Name
};
var items = user == null ?
system.GetRecursiveChildren(i => i is Game) :
system.GetRecursiveChildren(user, new InternalItemsQuery(user)
{
IncludeItemTypes = new[] { typeof(Game).Name }
});
var games = items.Cast().ToList();
summary.ClientInstalledGameCount = games.Count(i => i.IsPlaceHolder);
summary.GameCount = games.Count;
summary.GameFileExtensions = games.Where(i => !i.IsPlaceHolder).Select(i => Path.GetExtension(i.Path))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
return summary;
}
///
/// Gets the specified request.
///
/// The request.
/// System.Object.
public object Get(GetSimilarGames request)
{
var dtoOptions = GetDtoOptions(request);
var result = SimilarItemsHelper.GetSimilarItemsResult(dtoOptions, _userManager,
_itemRepo,
_libraryManager,
_userDataRepository,
_dtoService,
Logger,
request, new[] { typeof(Game) },
SimilarItemsHelper.GetSimiliarityScore);
return ToOptimizedSerializedResultUsingCache(result);
}
}
}