From f99eec2eecb5b0a3f7924bb522536384801fb20b Mon Sep 17 00:00:00 2001 From: Michael Green <84688932+michael-j-green@users.noreply.github.com> Date: Tue, 22 Oct 2024 23:37:34 +1100 Subject: [PATCH] WIP - All server communications can now use Hasheous --- .../Classes/Metadata/Communications.cs | 10 +- gaseous-server/Classes/Metadata/Games.cs | 12 +- .../Controllers/V1.0/SearchController.cs | 128 ++++++++++-------- gaseous-server/gaseous-server.csproj | 2 +- 4 files changed, 88 insertions(+), 64 deletions(-) diff --git a/gaseous-server/Classes/Metadata/Communications.cs b/gaseous-server/Classes/Metadata/Communications.cs index afa96ba..d1eb14a 100644 --- a/gaseous-server/Classes/Metadata/Communications.cs +++ b/gaseous-server/Classes/Metadata/Communications.cs @@ -256,7 +256,7 @@ namespace gaseous_server.Classes.Metadata return await IGDBAPI(EndpointString, fieldList, query); case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous: - ConfigureHasheousClient(); + ConfigureHasheousClient(ref hasheous); return await HasheousAPI(Endpoint.ToString(), "slug", Slug); @@ -416,7 +416,7 @@ namespace gaseous_server.Classes.Metadata return await IGDBAPI(EndpointString, fieldList, query); case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous: - ConfigureHasheousClient(); + ConfigureHasheousClient(ref hasheous); return await HasheousAPI(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 /// The type of object to convert to /// The object to convert /// The converted object - public T ConvertToIGDBModel(object input) + public static T ConvertToIGDBModel(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 _DownloadFile(Uri uri, string DestinationFile) { - ConfigureHasheousClient(); + ConfigureHasheousClient(ref hasheous); string DestinationDirectory = new FileInfo(DestinationFile).Directory.FullName; if (!Directory.Exists(DestinationDirectory)) diff --git a/gaseous-server/Classes/Metadata/Games.cs b/gaseous-server/Classes/Metadata/Games.cs index 4eef302..d41af72 100644 --- a/gaseous-server/Classes/Metadata/Games.cs +++ b/gaseous-server/Classes/Metadata/Games.cs @@ -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.Hasheous.MetadataProvider.IGDB, PlatformId.ToString(), SearchString); + + List hGames = new List(); + foreach (HasheousClient.Models.Metadata.IGDB.Game hResult in hResults) + { + hGames.Add(Communications.ConvertToIGDBModel(hResult)); + } + + return hGames.ToArray(); default: return new Game[0]; diff --git a/gaseous-server/Controllers/V1.0/SearchController.cs b/gaseous-server/Controllers/V1.0/SearchController.cs index 455867f..e08f529 100644 --- a/gaseous-server/Controllers/V1.0/SearchController.cs +++ b/gaseous-server/Controllers/V1.0/SearchController.cs @@ -36,27 +36,20 @@ namespace gaseous_server.Controllers private static async Task> _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? searchCache = Communications.GetSearchCache>(searchFields, searchBody); - - if (searchCache == null) + List platforms = new List(); + foreach (DataRow row in data.Rows) { - // cache miss - // get Platform metadata from data source - Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.Platforms, searchFields, searchBody); + Platform platform = Platforms.GetPlatform((long)row["Id"], false, false); - Communications.SetSearchCache>(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> _SearchForGame(long PlatformId, string SearchString) { - string searchBody = ""; - string searchFields = "fields *; "; - searchBody += "search \"" + SearchString + "\";"; - searchBody += "where platforms = (" + PlatformId + ");"; - searchBody += "limit 100;"; - - List? searchCache = Communications.GetSearchCache>(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(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 games = new List(); - foreach (Game game in results.ToList()) - { - Storage.CacheStatus cacheStatus = Storage.GetCacheStatus("Game", (long)game.Id); - switch (cacheStatus) + List? searchCache = Communications.GetSearchCache>(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(IGDBClient.Endpoints.Games, searchFields, searchBody); - case Storage.CacheStatus.Expired: - Storage.NewCacheValue(game, true); - break; + List games = new List(); + 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>(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 gamesToReturn = new List(); + 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 hSearch = hasheous.GetMetadataProxy_SearchGame(HasheousClient.Hasheous.MetadataProvider.IGDB, PlatformId.ToString(), SearchString).ToList(); - Communications.SetSearchCache>(searchFields, searchBody, games); + List hGamesToReturn = new List(); + foreach (HasheousClient.Models.Metadata.IGDB.Game game in hSearch) + { + IGDB.Models.Game tempGame = Communications.ConvertToIGDBModel(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 gamesToReturn = new List(); - 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(); } } } diff --git a/gaseous-server/gaseous-server.csproj b/gaseous-server/gaseous-server.csproj index 1363587..99eef6e 100644 --- a/gaseous-server/gaseous-server.csproj +++ b/gaseous-server/gaseous-server.csproj @@ -20,7 +20,7 @@ - +