86 lines
2.6 KiB
C#
86 lines
2.6 KiB
C#
using IGDB;
|
|
using IGDB.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Text;
|
|
|
|
namespace LANCommander.Server.Services
|
|
{
|
|
public class IGDBService : BaseService
|
|
{
|
|
private const string DefaultFields = "*";
|
|
private IGDBClient Client;
|
|
|
|
public bool Authenticated = false;
|
|
|
|
private string ClientId { get; set; }
|
|
private string ClientSecret { get; set; }
|
|
|
|
public IGDBService(ILogger<IGDBService> logger) : base(logger)
|
|
{
|
|
Authenticate();
|
|
}
|
|
|
|
public void Authenticate()
|
|
{
|
|
ClientId = Settings.IGDBClientId;
|
|
ClientSecret = Settings.IGDBClientSecret;
|
|
|
|
try
|
|
{
|
|
if (String.IsNullOrWhiteSpace(ClientId) || String.IsNullOrWhiteSpace(ClientSecret))
|
|
throw new Exception("Invalid IGDB credentials");
|
|
|
|
Client = new IGDBClient(ClientId, ClientSecret);
|
|
Authenticated = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Authenticated = false;
|
|
}
|
|
}
|
|
|
|
public async Task<Game> GetAsync(long id, params string[] additionalFields)
|
|
{
|
|
var fields = DefaultFields.Split(',').ToList();
|
|
|
|
fields.AddRange(additionalFields);
|
|
|
|
var games = await Client.QueryAsync<Game>(IGDBClient.Endpoints.Games, $"fields {String.Join(',', fields)}; where id = {id};");
|
|
|
|
if (games == null)
|
|
return null;
|
|
|
|
return games.FirstOrDefault();
|
|
}
|
|
|
|
public async Task<IEnumerable<Game>> SearchAsync(string input, int limit = 10, int offset = 0, params string[] additionalFields)
|
|
{
|
|
var fields = DefaultFields.Split(',').ToList();
|
|
|
|
fields.AddRange(additionalFields);
|
|
|
|
int[] categories = new int[]
|
|
{
|
|
(int)Category.MainGame,
|
|
(int)Category.Port,
|
|
(int)Category.StandaloneExpansion,
|
|
(int)Category.Expansion,
|
|
(int)Category.Mod,
|
|
(int)Category.Remake,
|
|
(int)Category.Remaster
|
|
};
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
sb.Append($"search \"{input}\";");
|
|
sb.Append($"fields {String.Join(',', fields)};");
|
|
sb.Append($"limit {limit};");
|
|
sb.Append($"offset {offset};");
|
|
sb.Append($"where category = ({String.Join(',', categories)});");
|
|
|
|
var games = await Client.QueryAsync<Game>(IGDBClient.Endpoints.Games, sb.ToString());
|
|
|
|
return games.AsEnumerable();
|
|
}
|
|
}
|
|
}
|