feat: platforms and games from IGDB are now imported successfully

This commit is contained in:
Michael Green
2023-05-01 00:42:23 +10:00
parent fb7b0a7eb2
commit 3b3cf3c239
19 changed files with 1530 additions and 478 deletions

View File

@@ -7,51 +7,26 @@ using IGDB.Models;
namespace gaseous_server.Classes.Metadata
{
public class Platforms
public class Platforms
{
public Platforms()
const string fieldList = "fields abbreviation,alternative_name,category,checksum,created_at,generation,name,platform_family,platform_logo,slug,summary,updated_at,url,versions,websites;";
public Platforms()
{
}
public static Platform UnknownPlatform
{
get
{
Platform unkownPlatform = new Platform
{
Id = 0,
Abbreviation = "",
AlternativeName = "",
Category = PlatformCategory.Computer,
Checksum = "",
CreatedAt = DateTime.UtcNow,
Generation = 1,
Name = "Unknown",
PlatformFamily = new IdentityOrValue<PlatformFamily>(0),
PlatformLogo = new IdentityOrValue<PlatformLogo>(0),
Slug = "Unknown",
Summary = "",
UpdatedAt = DateTime.UtcNow,
Url = "",
Versions = new IdentitiesOrValues<PlatformVersion>(),
Websites = new IdentitiesOrValues<PlatformWebsite>()
};
return unkownPlatform;
}
}
private static IGDBClient igdb = new IGDBClient(
// Found in Twitch Developer portal for your app
Config.IGDB.ClientId,
Config.IGDB.Secret
);
public static Platform GetPlatform(int Id)
public static Platform? GetPlatform(long Id)
{
if (Id == 0)
{
return UnknownPlatform;
return null;
}
else
{
@@ -69,7 +44,15 @@ namespace gaseous_server.Classes.Metadata
private static async Task<Platform> _GetPlatform(SearchUsing searchUsing, object searchValue)
{
// check database first
Platform? platform = DBGetPlatform(searchUsing, searchValue);
Storage.CacheStatus? cacheStatus = new Storage.CacheStatus();
if (searchUsing == SearchUsing.id)
{
cacheStatus = Storage.GetCacheStatus("platform", (long)searchValue);
}
else
{
cacheStatus = Storage.GetCacheStatus("platform", (string)searchValue);
}
// set up where clause
string WhereClause = "";
@@ -85,51 +68,39 @@ namespace gaseous_server.Classes.Metadata
throw new Exception("Invalid search type");
}
if (platform == null)
Platform returnValue = new Platform();
switch (cacheStatus)
{
// get platform metadata
var results = await igdb.QueryAsync<Platform>(IGDBClient.Endpoints.Platforms, query: "fields abbreviation,alternative_name,category,checksum,created_at,generation,name,platform_family,platform_logo,slug,summary,updated_at,url,versions,websites; " + WhereClause + ";");
var result = results.First();
DBInsertPlatform(result, true);
// get platform logo
if (result.PlatformLogo != null)
{
PlatformLogos.GetPlatformLogo((long)result.PlatformLogo.Id, Path.Combine(Config.LibraryConfiguration.LibraryMetadataDirectory_Platform(result), "platform_logo.jpg"));
}
// get platform versions
foreach (long platformVersionId in result.Versions.Ids)
{
PlatformVersions.GetPlatformVersion(platformVersionId, result);
}
return result;
}
else
{
return platform;
case Storage.CacheStatus.NotPresent:
returnValue = await GetObjectFromServer(WhereClause);
Storage.NewCacheValue(returnValue);
UpdateSubClasses(returnValue);
return returnValue;
case Storage.CacheStatus.Expired:
returnValue = await GetObjectFromServer(WhereClause);
Storage.NewCacheValue(returnValue, true);
UpdateSubClasses(returnValue);
return returnValue;
case Storage.CacheStatus.Current:
return Storage.GetCacheValue<Platform>(returnValue, "id", (long)searchValue);
default:
throw new Exception("How did you get here?");
}
}
private static Platform? DBGetPlatform(SearchUsing searchUsing, object searchValue)
private static void UpdateSubClasses(Platform platform)
{
Dictionary<string, object> dbDict = new Dictionary<string, object>();
switch (searchUsing)
if (platform.Versions != null)
{
case SearchUsing.id:
dbDict.Add("id", searchValue);
foreach (long PlatformVersionId in platform.Versions.Ids)
{
PlatformVersion platformVersion = PlatformVersions.GetPlatformVersion(PlatformVersionId, platform);
}
}
return _DBGetPlatform("SELECT * FROM platforms WHERE id = @id", dbDict);
case SearchUsing.slug:
dbDict.Add("slug", searchValue);
return _DBGetPlatform("SELECT * FROM platforms WHERE slug = @slug", dbDict);
default:
throw new Exception("Invalid Search Type");
if (platform.PlatformLogo != null)
{
PlatformLogo platformLogo = PlatformLogos.GetPlatformLogo(platform.PlatformLogo.Id, Config.LibraryConfiguration.LibraryMetadataDirectory_Platform(platform));
}
}
@@ -139,104 +110,13 @@ namespace gaseous_server.Classes.Metadata
slug
}
private static Platform? _DBGetPlatform(string sql, Dictionary<string, object> searchParams)
private static async Task<Platform> GetObjectFromServer(string WhereClause)
{
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
// get platform metadata
var results = await igdb.QueryAsync<Platform>(IGDBClient.Endpoints.Platforms, query: fieldList + " " + WhereClause + ";");
var result = results.First();
DataTable dbResponse = db.ExecuteCMD(sql, searchParams);
if (dbResponse.Rows.Count > 0)
{
return ConvertDataRowToPlatform(dbResponse.Rows[0]);
}
else
{
return null;
}
}
private static Platform ConvertDataRowToPlatform(DataRow PlatformDR)
{
Platform returnPlatform = new Platform
{
Id = (long)(UInt64)PlatformDR["id"],
Abbreviation = (string?)PlatformDR["abbreviation"],
AlternativeName = (string?)PlatformDR["alternative_name"],
Category = (PlatformCategory)PlatformDR["category"],
Checksum = (string?)PlatformDR["checksum"],
CreatedAt = (DateTime?)PlatformDR["created_at"],
Generation = (int?)PlatformDR["generation"],
Name = (string?)PlatformDR["name"],
PlatformFamily = new IdentityOrValue<PlatformFamily>((int?)PlatformDR["platform_family"]),
PlatformLogo = new IdentityOrValue<PlatformLogo>((int?)PlatformDR["platform_logo"]),
Slug = (string?)PlatformDR["slug"],
Summary = (string?)PlatformDR["summary"],
UpdatedAt = (DateTime?)PlatformDR["updated_at"],
Url = (string?)PlatformDR["url"],
Versions = Newtonsoft.Json.JsonConvert.DeserializeObject<IdentitiesOrValues<PlatformVersion>>((string?)PlatformDR["versions"]),
Websites = Newtonsoft.Json.JsonConvert.DeserializeObject<IdentitiesOrValues<PlatformWebsite>>((string?)PlatformDR["websites"])
};
return returnPlatform;
}
private static void DBInsertPlatform(Platform PlatformItem, bool Insert)
{
Database db = new gaseous_tools.Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
string sql = "INSERT INTO platforms (id, abbreviation, alternative_name, category, checksum, created_at, generation, name, platform_family, platform_logo, slug, summary, updated_at, url, versions, websites, dateAdded, lastUpdated) VALUES (@id, @abbreviation, @alternative_name, @category, @checksum, @created_at, @generation, @name, @platform_family, @platform_logo, @slug, @summary, @updated_at, @url, @versions, @websites, @lastUpdated, @lastUpdated)";
if (Insert == false)
{
sql = "UPDATE platforms SET abbreviation=@abbreviation, alternative_name=@alternative_name, category=@category, checksum=@checksum, created_at=@created_at, generation=@generation, name=@name, platform_family=@platform_family, platform_logo=@platform_logo, slug=@slug, summary=@summary, updated_at=@updated_at, url=@url, versions=@versions, websites=@websites, lastUpdated=@lastUpdated WHERE id=@id";
}
Dictionary<string, object> dbDict = new Dictionary<string, object>();
dbDict.Add("id", PlatformItem.Id);
dbDict.Add("abbreviation", Common.ReturnValueIfNull(PlatformItem.Abbreviation, ""));
dbDict.Add("alternative_name", Common.ReturnValueIfNull(PlatformItem.AlternativeName, ""));
dbDict.Add("category", Common.ReturnValueIfNull(PlatformItem.Category, PlatformCategory.Computer));
dbDict.Add("checksum", Common.ReturnValueIfNull(PlatformItem.Checksum, ""));
dbDict.Add("created_at", Common.ReturnValueIfNull(PlatformItem.CreatedAt, DateTime.UtcNow));
dbDict.Add("generation", Common.ReturnValueIfNull(PlatformItem.Generation, 1));
dbDict.Add("name", Common.ReturnValueIfNull(PlatformItem.Name, ""));
if (PlatformItem.PlatformFamily == null)
{
dbDict.Add("platform_family", 0);
}
else
{
dbDict.Add("platform_family", Common.ReturnValueIfNull(PlatformItem.PlatformFamily.Id, 0));
}
if (PlatformItem.PlatformLogo == null)
{
dbDict.Add("platform_logo", 0);
}
else
{
dbDict.Add("platform_logo", Common.ReturnValueIfNull(PlatformItem.PlatformLogo.Id, 0));
}
dbDict.Add("slug", Common.ReturnValueIfNull(PlatformItem.Slug, ""));
dbDict.Add("summary", Common.ReturnValueIfNull(PlatformItem.Summary, ""));
dbDict.Add("updated_at", Common.ReturnValueIfNull(PlatformItem.UpdatedAt, DateTime.UtcNow));
dbDict.Add("url", Common.ReturnValueIfNull(PlatformItem.Url, ""));
dbDict.Add("lastUpdated", DateTime.UtcNow);
string EmptyJson = "{\"Ids\": [], \"Values\": null}";
if (PlatformItem.Versions == null)
{
dbDict.Add("versions", EmptyJson);
}
else
{
dbDict.Add("versions", Newtonsoft.Json.JsonConvert.SerializeObject(PlatformItem.Versions));
}
if (PlatformItem.Websites == null)
{
dbDict.Add("websites", EmptyJson);
}
else
{
dbDict.Add("websites", Newtonsoft.Json.JsonConvert.SerializeObject(PlatformItem.Websites));
}
db.ExecuteCMD(sql, dbDict);
return result;
}
}
}