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);
case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous:
ConfigureHasheousClient();
ConfigureHasheousClient(ref hasheous);
return await HasheousAPI<T>(Endpoint.ToString(), "slug", Slug);
@@ -416,7 +416,7 @@ namespace gaseous_server.Classes.Metadata
return await IGDBAPI<T>(EndpointString, fieldList, query);
case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous:
ConfigureHasheousClient();
ConfigureHasheousClient(ref hasheous);
return await HasheousAPI<T>(Endpoint.ToString(), "id", Id.ToString());
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
hasheous = new HasheousClient.Hasheous();
@@ -853,7 +853,7 @@ namespace gaseous_server.Classes.Metadata
/// <typeparam name="T">The type of object to convert to</typeparam>
/// <param name="input">The object to convert</param>
/// <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
@@ -978,7 +978,7 @@ namespace gaseous_server.Classes.Metadata
private async Task<bool?> _DownloadFile(Uri uri, string DestinationFile)
{
ConfigureHasheousClient();
ConfigureHasheousClient(ref hasheous);
string DestinationDirectory = new FileInfo(DestinationFile).Directory.FullName;
if (!Directory.Exists(DestinationDirectory))

View File

@@ -1,5 +1,6 @@
using System;
using System.Data;
using System.Security.Cryptography.X509Certificates;
using gaseous_server.Models;
using IGDB;
using IGDB.Models;
@@ -527,7 +528,16 @@ namespace gaseous_server.Classes.Metadata
}
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:
return new Game[0];

View File

@@ -36,27 +36,20 @@ namespace gaseous_server.Controllers
private static async Task<List<Platform>> _SearchForPlatform(string SearchString)
{
string searchBody = "";
string searchFields = "fields abbreviation,alternative_name,category,checksum,created_at,generation,name,platform_family,platform_logo,slug,summary,updated_at,url,versions,websites; ";
searchBody += "where name ~ *\"" + SearchString + "\"*;";
// search the database for the requested platforms
Database db = new Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
string query = "SELECT `Id` FROM Platform WHERE `Name` LIKE '%" + SearchString + "%';";
DataTable data = db.ExecuteCMD(query);
List<Platform>? searchCache = Communications.GetSearchCache<List<Platform>>(searchFields, searchBody);
if (searchCache == null)
List<Platform> platforms = new List<Platform>();
foreach (DataRow row in data.Rows)
{
// cache miss
// get Platform metadata from data source
Communications comms = new Communications();
var results = await comms.APIComm<Platform>(IGDBClient.Endpoints.Platforms, searchFields, searchBody);
Platform platform = Platforms.GetPlatform((long)row["Id"], false, false);
Communications.SetSearchCache<List<Platform>>(searchFields, searchBody, results.ToList());
platforms.Add(platform);
}
return results.ToList();
}
else
{
return searchCache;
}
return platforms;
}
[MapToApiVersion("1.0")]
@@ -72,56 +65,77 @@ namespace gaseous_server.Controllers
private static async Task<List<GaseousGame>> _SearchForGame(long PlatformId, string SearchString)
{
string searchBody = "";
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)
switch (Communications.MetadataSource)
{
// cache miss
// get Game metadata from data source
Communications comms = new Communications();
var results = await comms.APIComm<Game>(IGDBClient.Endpoints.Games, searchFields, searchBody);
case HasheousClient.Models.MetadataModel.MetadataSources.IGDB:
string searchBody = "";
string searchFields = "fields *; ";
searchBody += "search \"" + SearchString + "\";";
searchBody += "where platforms = (" + PlatformId + ");";
searchBody += "limit 100;";
List<GaseousGame> games = new List<GaseousGame>();
foreach (Game game in results.ToList())
{
Storage.CacheStatus cacheStatus = Storage.GetCacheStatus("Game", (long)game.Id);
switch (cacheStatus)
List<GaseousGame>? searchCache = Communications.GetSearchCache<List<GaseousGame>>(searchFields, searchBody);
if (searchCache == null)
{
case Storage.CacheStatus.NotPresent:
Storage.NewCacheValue(game, false);
break;
// cache miss
// get Game metadata from data source
Communications comms = new Communications();
var results = await comms.APIComm<Game>(IGDBClient.Endpoints.Games, searchFields, searchBody);
case Storage.CacheStatus.Expired:
Storage.NewCacheValue(game, true);
break;
List<GaseousGame> games = new List<GaseousGame>();
foreach (Game game in results.ToList())
{
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;
}
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 hGamesToReturn;
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="gaseous-signature-parser" Version="2.2.1" />
<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="sharpcompress" Version="0.37.2" />
<PackageReference Include="Squid-Box.SevenZipSharp" Version="1.6.2.24" />