WIP - All server communications can now use Hasheous

This commit is contained in:
Michael Green
2024-10-22 23:37:34 +11:00
parent b3ca94b217
commit f99eec2eec
4 changed files with 88 additions and 64 deletions

View File

@@ -256,7 +256,7 @@ namespace gaseous_server.Classes.Metadata
return await IGDBAPI<T>(EndpointString, fieldList, query); return await IGDBAPI<T>(EndpointString, fieldList, query);
case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous: case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous:
ConfigureHasheousClient(); ConfigureHasheousClient(ref hasheous);
return await HasheousAPI<T>(Endpoint.ToString(), "slug", Slug); return await HasheousAPI<T>(Endpoint.ToString(), "slug", Slug);
@@ -416,7 +416,7 @@ namespace gaseous_server.Classes.Metadata
return await IGDBAPI<T>(EndpointString, fieldList, query); return await IGDBAPI<T>(EndpointString, fieldList, query);
case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous: case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous:
ConfigureHasheousClient(); ConfigureHasheousClient(ref hasheous);
return await HasheousAPI<T>(Endpoint.ToString(), "id", Id.ToString()); return await HasheousAPI<T>(Endpoint.ToString(), "id", Id.ToString());
default: default:
@@ -424,7 +424,7 @@ namespace gaseous_server.Classes.Metadata
} }
} }
private void ConfigureHasheousClient() public static void ConfigureHasheousClient(ref HasheousClient.Hasheous hasheous)
{ {
// configure the Hasheous client // configure the Hasheous client
hasheous = new HasheousClient.Hasheous(); hasheous = new HasheousClient.Hasheous();
@@ -853,7 +853,7 @@ namespace gaseous_server.Classes.Metadata
/// <typeparam name="T">The type of object to convert to</typeparam> /// <typeparam name="T">The type of object to convert to</typeparam>
/// <param name="input">The object to convert</param> /// <param name="input">The object to convert</param>
/// <returns>The converted object</returns> /// <returns>The converted object</returns>
public T ConvertToIGDBModel<T>(object input) public static T ConvertToIGDBModel<T>(object input)
{ {
// loop through the properties of intput and copy all strings to an output object of type T // loop through the properties of intput and copy all strings to an output object of type T
@@ -978,7 +978,7 @@ namespace gaseous_server.Classes.Metadata
private async Task<bool?> _DownloadFile(Uri uri, string DestinationFile) private async Task<bool?> _DownloadFile(Uri uri, string DestinationFile)
{ {
ConfigureHasheousClient(); ConfigureHasheousClient(ref hasheous);
string DestinationDirectory = new FileInfo(DestinationFile).Directory.FullName; string DestinationDirectory = new FileInfo(DestinationFile).Directory.FullName;
if (!Directory.Exists(DestinationDirectory)) if (!Directory.Exists(DestinationDirectory))

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Data; using System.Data;
using System.Security.Cryptography.X509Certificates;
using gaseous_server.Models; using gaseous_server.Models;
using IGDB; using IGDB;
using IGDB.Models; using IGDB.Models;
@@ -527,7 +528,16 @@ namespace gaseous_server.Classes.Metadata
} }
case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous: case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous:
return new Game[0]; HasheousClient.Hasheous hasheous = new HasheousClient.Hasheous();
HasheousClient.Models.Metadata.IGDB.Game[] hResults = hasheous.GetMetadataProxy_SearchGame<HasheousClient.Models.Metadata.IGDB.Game>(HasheousClient.Hasheous.MetadataProvider.IGDB, PlatformId.ToString(), SearchString);
List<Game> hGames = new List<Game>();
foreach (HasheousClient.Models.Metadata.IGDB.Game hResult in hResults)
{
hGames.Add(Communications.ConvertToIGDBModel<Game>(hResult));
}
return hGames.ToArray();
default: default:
return new Game[0]; return new Game[0];

View File

@@ -36,27 +36,20 @@ namespace gaseous_server.Controllers
private static async Task<List<Platform>> _SearchForPlatform(string SearchString) private static async Task<List<Platform>> _SearchForPlatform(string SearchString)
{ {
string searchBody = ""; // search the database for the requested platforms
string searchFields = "fields abbreviation,alternative_name,category,checksum,created_at,generation,name,platform_family,platform_logo,slug,summary,updated_at,url,versions,websites; "; Database db = new Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
searchBody += "where name ~ *\"" + SearchString + "\"*;"; string query = "SELECT `Id` FROM Platform WHERE `Name` LIKE '%" + SearchString + "%';";
DataTable data = db.ExecuteCMD(query);
List<Platform>? searchCache = Communications.GetSearchCache<List<Platform>>(searchFields, searchBody); List<Platform> platforms = new List<Platform>();
foreach (DataRow row in data.Rows)
if (searchCache == null)
{ {
// cache miss Platform platform = Platforms.GetPlatform((long)row["Id"], false, false);
// get Platform metadata from data source
Communications comms = new Communications();
var results = await comms.APIComm<Platform>(IGDBClient.Endpoints.Platforms, searchFields, searchBody);
Communications.SetSearchCache<List<Platform>>(searchFields, searchBody, results.ToList()); platforms.Add(platform);
}
return results.ToList(); return platforms;
}
else
{
return searchCache;
}
} }
[MapToApiVersion("1.0")] [MapToApiVersion("1.0")]
@@ -72,56 +65,77 @@ namespace gaseous_server.Controllers
private static async Task<List<GaseousGame>> _SearchForGame(long PlatformId, string SearchString) private static async Task<List<GaseousGame>> _SearchForGame(long PlatformId, string SearchString)
{ {
string searchBody = ""; switch (Communications.MetadataSource)
string searchFields = "fields *; ";
searchBody += "search \"" + SearchString + "\";";
searchBody += "where platforms = (" + PlatformId + ");";
searchBody += "limit 100;";
List<GaseousGame>? searchCache = Communications.GetSearchCache<List<GaseousGame>>(searchFields, searchBody);
if (searchCache == null)
{ {
// cache miss case HasheousClient.Models.MetadataModel.MetadataSources.IGDB:
// get Game metadata from data source string searchBody = "";
Communications comms = new Communications(); string searchFields = "fields *; ";
var results = await comms.APIComm<Game>(IGDBClient.Endpoints.Games, searchFields, searchBody); searchBody += "search \"" + SearchString + "\";";
searchBody += "where platforms = (" + PlatformId + ");";
searchBody += "limit 100;";
List<GaseousGame> games = new List<GaseousGame>(); List<GaseousGame>? searchCache = Communications.GetSearchCache<List<GaseousGame>>(searchFields, searchBody);
foreach (Game game in results.ToList())
{ if (searchCache == null)
Storage.CacheStatus cacheStatus = Storage.GetCacheStatus("Game", (long)game.Id);
switch (cacheStatus)
{ {
case Storage.CacheStatus.NotPresent: // cache miss
Storage.NewCacheValue(game, false); // get Game metadata from data source
break; Communications comms = new Communications();
var results = await comms.APIComm<Game>(IGDBClient.Endpoints.Games, searchFields, searchBody);
case Storage.CacheStatus.Expired: List<GaseousGame> games = new List<GaseousGame>();
Storage.NewCacheValue(game, true); foreach (Game game in results.ToList())
break; {
Storage.CacheStatus cacheStatus = Storage.GetCacheStatus("Game", (long)game.Id);
switch (cacheStatus)
{
case Storage.CacheStatus.NotPresent:
Storage.NewCacheValue(game, false);
break;
case Storage.CacheStatus.Expired:
Storage.NewCacheValue(game, true);
break;
}
games.Add(new GaseousGame(game));
}
Communications.SetSearchCache<List<GaseousGame>>(searchFields, searchBody, games);
return games;
}
else
{
// get full version of results from database
// this is a hacky workaround due to the readonly nature of IGDB.Model.Game IdentityOrValue fields
List<GaseousGame> gamesToReturn = new List<GaseousGame>();
foreach (GaseousGame game in searchCache)
{
Game tempGame = Games.GetGame((long)game.Id, false, false, false);
gamesToReturn.Add(new GaseousGame(tempGame));
}
return gamesToReturn;
} }
games.Add(new GaseousGame(game)); case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous:
} HasheousClient.Hasheous hasheous = new HasheousClient.Hasheous();
Communications.ConfigureHasheousClient(ref hasheous);
List<HasheousClient.Models.Metadata.IGDB.Game> hSearch = hasheous.GetMetadataProxy_SearchGame<HasheousClient.Models.Metadata.IGDB.Game>(HasheousClient.Hasheous.MetadataProvider.IGDB, PlatformId.ToString(), SearchString).ToList<HasheousClient.Models.Metadata.IGDB.Game>();
Communications.SetSearchCache<List<GaseousGame>>(searchFields, searchBody, games); List<GaseousGame> hGamesToReturn = new List<GaseousGame>();
foreach (HasheousClient.Models.Metadata.IGDB.Game game in hSearch)
{
IGDB.Models.Game tempGame = Communications.ConvertToIGDBModel<IGDB.Models.Game>(game);
hGamesToReturn.Add(new GaseousGame(tempGame));
}
return games; return hGamesToReturn;
}
else
{
// get full version of results from database
// this is a hacky workaround due to the readonly nature of IGDB.Model.Game IdentityOrValue fields
List<GaseousGame> gamesToReturn = new List<GaseousGame>();
foreach (GaseousGame game in searchCache)
{
Game tempGame = Games.GetGame((long)game.Id, false, false, false);
gamesToReturn.Add(new GaseousGame(tempGame));
}
return gamesToReturn; default:
return new List<GaseousGame>();
} }
} }
} }

View File

@@ -20,7 +20,7 @@
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" /> <PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="gaseous-signature-parser" Version="2.2.1" /> <PackageReference Include="gaseous-signature-parser" Version="2.2.1" />
<PackageReference Include="gaseous.IGDB" Version="1.0.2" /> <PackageReference Include="gaseous.IGDB" Version="1.0.2" />
<PackageReference Include="hasheous-client" Version="1.1.2" /> <PackageReference Include="hasheous-client" Version="1.1.3" />
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="13.8.0" /> <PackageReference Include="Magick.NET-Q8-AnyCPU" Version="13.8.0" />
<PackageReference Include="sharpcompress" Version="0.37.2" /> <PackageReference Include="sharpcompress" Version="0.37.2" />
<PackageReference Include="Squid-Box.SevenZipSharp" Version="1.6.2.24" /> <PackageReference Include="Squid-Box.SevenZipSharp" Version="1.6.2.24" />