From 574d3bc39e160daef73c7affa64114a09984c123 Mon Sep 17 00:00:00 2001 From: Michael Green <84688932+michael-j-green@users.noreply.github.com> Date: Fri, 18 Oct 2024 10:25:48 +1100 Subject: [PATCH] WIP --- gaseous-server/Classes/Config.cs | 11 + gaseous-server/Classes/DatabaseMigration.cs | 6 +- gaseous-server/Classes/Metadata/AgeRating.cs | 63 +-- .../Metadata/AgeRatingContentDescriptions.cs | 58 +-- .../Classes/Metadata/AlternativeNames.cs | 58 +-- gaseous-server/Classes/Metadata/Artworks.cs | 52 +-- .../Classes/Metadata/Collections.cs | 58 +-- .../Classes/Metadata/Communications.cs | 430 +++++++++++++++++- gaseous-server/Classes/Metadata/Company.cs | 58 +-- .../Classes/Metadata/CompanyLogos.cs | 52 +-- gaseous-server/Classes/Metadata/Covers.cs | 52 +-- .../Classes/Metadata/ExternalGames.cs | 58 +-- gaseous-server/Classes/Metadata/Franchises.cs | 58 +-- gaseous-server/Classes/Metadata/GameModes.cs | 52 +-- gaseous-server/Classes/Metadata/GameVideos.cs | 58 +-- gaseous-server/Classes/Metadata/Games.cs | 75 +-- gaseous-server/Classes/Metadata/Genres.cs | 52 +-- .../Classes/Metadata/InvolvedCompany.cs | 75 +-- .../Classes/Metadata/MultiplayerModes.cs | 52 +-- .../Classes/Metadata/PlatformLogos.cs | 62 +-- .../Classes/Metadata/PlatformVersions.cs | 52 +-- gaseous-server/Classes/Metadata/Platforms.cs | 67 +-- .../Classes/Metadata/PlayerPerspectives.cs | 52 +-- .../Classes/Metadata/ReleaseDates.cs | 58 +-- .../Classes/Metadata/Screenshots.cs | 52 +-- gaseous-server/Classes/Metadata/Themes.cs | 52 +-- gaseous-server/Classes/Roms.cs | 3 +- .../Controllers/V1.0/SystemController.cs | 1 + gaseous-server/gaseous-server.csproj | 2 +- gaseous-server/wwwroot/pages/home.html | 3 +- 30 files changed, 763 insertions(+), 1019 deletions(-) diff --git a/gaseous-server/Classes/Config.cs b/gaseous-server/Classes/Config.cs index 9b5dacd..22c0a58 100644 --- a/gaseous-server/Classes/Config.cs +++ b/gaseous-server/Classes/Config.cs @@ -677,6 +677,14 @@ namespace gaseous_server.Classes public class MetadataAPI { + public static string _HasheousClientAPIKey + { + get + { + return "Pna5SRcbJ6R8aasytab_6vZD0aBKDGNZKRz4WY4xArpfZ-3mdZq0hXIGyy0AD43b"; + } + } + private static HasheousClient.Models.MetadataModel.MetadataSources _MetadataSource { get @@ -749,6 +757,9 @@ namespace gaseous_server.Classes public string HasheousAPIKey = _HasheousAPIKey; + [JsonIgnore] + public string HasheousClientAPIKey = _HasheousClientAPIKey; + public int MaxLibraryScanWorkers = _MaxLibraryScanWorkers; public string HasheousHost = _HasheousHost; diff --git a/gaseous-server/Classes/DatabaseMigration.cs b/gaseous-server/Classes/DatabaseMigration.cs index 0bb712c..0579abd 100644 --- a/gaseous-server/Classes/DatabaseMigration.cs +++ b/gaseous-server/Classes/DatabaseMigration.cs @@ -197,7 +197,11 @@ namespace gaseous_server.Classes libraryRootPath += Path.DirectorySeparatorChar; } - bool GetLastThreeElements = (bool)row["DefaultLibrary"]; + bool GetLastThreeElements = false; + if ((int)row["DefaultLibrary"] == 1) + { + GetLastThreeElements = true; + } foreach (DataRow romRow in romData.Rows) { diff --git a/gaseous-server/Classes/Metadata/AgeRating.cs b/gaseous-server/Classes/Metadata/AgeRating.cs index 1f69a41..18b88d1 100644 --- a/gaseous-server/Classes/Metadata/AgeRating.cs +++ b/gaseous-server/Classes/Metadata/AgeRating.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Reflection; using System.Text.Json.Serialization; using IGDB; @@ -7,9 +8,9 @@ using Microsoft.CodeAnalysis.Classification; namespace gaseous_server.Classes.Metadata { - public class AgeRatings + public class AgeRatings { - const string fieldList = "fields category,checksum,content_descriptions,rating,rating_cover_url,synopsis;"; + public const string fieldList = "fields category,checksum,content_descriptions,rating,rating_cover_url,synopsis;"; public AgeRatings() { @@ -23,61 +24,33 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetAgeRatings(SearchUsing.id, Id); + Task RetVal = _GetAgeRatings((long)Id); return RetVal.Result; } } - public static AgeRating GetAgeRatings(string Slug) - { - Task RetVal = _GetAgeRatings(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetAgeRatings(SearchUsing searchUsing, object searchValue) + private static async Task _GetAgeRatings(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("AgeRating", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("AgeRating", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("AgeRating", (long)searchValue); AgeRating returnValue = new AgeRating(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); UpdateSubClasses(returnValue); - break; + break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -102,17 +75,11 @@ namespace gaseous_server.Classes.Metadata } } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get AgeRatings metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.AgeRating, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.AgeRating, searchValue); var result = results.First(); return result; @@ -157,7 +124,7 @@ namespace gaseous_server.Classes.Metadata db.ExecuteNonQuery(sql); // loop all age groups - foreach(KeyValuePair ageGrouping in AgeGroups.AgeGroupingsFlat) + foreach (KeyValuePair ageGrouping in AgeGroups.AgeGroupingsFlat) { AgeGroups.AgeGroupItem ageGroupItem = ageGrouping.Value; var properties = ageGroupItem.GetType().GetProperties(); @@ -170,7 +137,7 @@ namespace gaseous_server.Classes.Metadata { AgeRatingCategory ageRatingCategory = (AgeRatingCategory)Enum.Parse(typeof(AgeRatingCategory), prop.Name); List ageRatingTitles = (List)prop.GetValue(ageGroupItem); - + foreach (AgeRatingTitle ageRatingTitle in ageRatingTitles) { dbDict.Clear(); @@ -186,6 +153,6 @@ namespace gaseous_server.Classes.Metadata } } } - } + } } diff --git a/gaseous-server/Classes/Metadata/AgeRatingContentDescriptions.cs b/gaseous-server/Classes/Metadata/AgeRatingContentDescriptions.cs index d27df81..596c70b 100644 --- a/gaseous-server/Classes/Metadata/AgeRatingContentDescriptions.cs +++ b/gaseous-server/Classes/Metadata/AgeRatingContentDescriptions.cs @@ -5,9 +5,9 @@ using IGDB.Models; namespace gaseous_server.Classes.Metadata { - public class AgeRatingContentDescriptions + public class AgeRatingContentDescriptions { - const string fieldList = "fields category,checksum,description;"; + public const string fieldList = "fields category,checksum,description;"; public AgeRatingContentDescriptions() { @@ -21,60 +21,32 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetAgeRatingContentDescriptions(SearchUsing.id, Id); + Task RetVal = _GetAgeRatingContentDescriptions((long)Id); return RetVal.Result; } } - public static AgeRatingContentDescription GetAgeRatingContentDescriptions(string Slug) - { - Task RetVal = _GetAgeRatingContentDescriptions(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetAgeRatingContentDescriptions(SearchUsing searchUsing, object searchValue) + private static async Task _GetAgeRatingContentDescriptions(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("AgeRatingContentDescription", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("AgeRatingContentDescription", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("AgeRatingContentDescription", searchValue); AgeRatingContentDescription returnValue = new AgeRatingContentDescription(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); - break; + break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -88,21 +60,15 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get AgeRatingContentDescriptionContentDescriptions metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.AgeRatingContentDescriptions, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.AgeRatingContentDescription, searchValue); var result = results.First(); return result; } - } + } } diff --git a/gaseous-server/Classes/Metadata/AlternativeNames.cs b/gaseous-server/Classes/Metadata/AlternativeNames.cs index be5a7bf..d8543b5 100644 --- a/gaseous-server/Classes/Metadata/AlternativeNames.cs +++ b/gaseous-server/Classes/Metadata/AlternativeNames.cs @@ -5,9 +5,9 @@ using IGDB.Models; namespace gaseous_server.Classes.Metadata { - public class AlternativeNames + public class AlternativeNames { - const string fieldList = "fields checksum,comment,game,name;"; + public const string fieldList = "fields checksum,comment,game,name;"; public AlternativeNames() { @@ -21,60 +21,32 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetAlternativeNames(SearchUsing.id, Id); + Task RetVal = _GetAlternativeNames((long)Id); return RetVal.Result; } } - public static AlternativeName GetAlternativeNames(string Slug) - { - Task RetVal = _GetAlternativeNames(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetAlternativeNames(SearchUsing searchUsing, object searchValue) + private static async Task _GetAlternativeNames(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("AlternativeName", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("AlternativeName", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("AlternativeName", searchValue); AlternativeName returnValue = new AlternativeName(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); - break; + break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -88,21 +60,15 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get AlternativeNames metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.AlternativeNames, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.AlternativeName, searchValue); var result = results.First(); return result; } - } + } } diff --git a/gaseous-server/Classes/Metadata/Artworks.cs b/gaseous-server/Classes/Metadata/Artworks.cs index f93b507..2c1be5f 100644 --- a/gaseous-server/Classes/Metadata/Artworks.cs +++ b/gaseous-server/Classes/Metadata/Artworks.cs @@ -7,7 +7,7 @@ namespace gaseous_server.Classes.Metadata { public class Artworks { - const string fieldList = "fields alpha_channel,animated,checksum,game,height,image_id,url,width;"; + public const string fieldList = "fields alpha_channel,animated,checksum,game,height,image_id,url,width;"; public Artworks() { @@ -21,43 +21,15 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetArtwork(SearchUsing.id, Id, ImagePath, GetImages); + Task RetVal = _GetArtwork((long)Id, ImagePath, GetImages); return RetVal.Result; } } - public static Artwork GetArtwork(string Slug, string ImagePath, bool GetImages) - { - Task RetVal = _GetArtwork(SearchUsing.slug, Slug, ImagePath, GetImages); - return RetVal.Result; - } - - private static async Task _GetArtwork(SearchUsing searchUsing, object searchValue, string ImagePath, bool GetImages = true) + private static async Task _GetArtwork(long searchValue, string ImagePath, bool GetImages = true) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("Artwork", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("Artwork", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("Artwork", searchValue); Artwork returnValue = new Artwork(); bool forceImageDownload = false; @@ -65,14 +37,14 @@ namespace gaseous_server.Classes.Metadata switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause, ImagePath); + returnValue = await GetObjectFromServer(searchValue, ImagePath); Storage.NewCacheValue(returnValue); forceImageDownload = true; break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause, ImagePath); + returnValue = await GetObjectFromServer(searchValue, ImagePath); Storage.NewCacheValue(returnValue, true); // check if old value is different from the new value - only download if it's different @@ -84,7 +56,7 @@ namespace gaseous_server.Classes.Metadata } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -111,17 +83,11 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause, string ImagePath) + private static async Task GetObjectFromServer(long searchValue, string ImagePath) { // get Artwork metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.Artworks, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.Artwork, searchValue); var result = results.First(); return result; diff --git a/gaseous-server/Classes/Metadata/Collections.cs b/gaseous-server/Classes/Metadata/Collections.cs index 0531850..2f9682d 100644 --- a/gaseous-server/Classes/Metadata/Collections.cs +++ b/gaseous-server/Classes/Metadata/Collections.cs @@ -5,9 +5,9 @@ using IGDB.Models; namespace gaseous_server.Classes.Metadata { - public class Collections + public class Collections { - const string fieldList = "fields as_child_relations,as_parent_relations,checksum,created_at,games,name,slug,type,updated_at,url;"; + public const string fieldList = "fields as_child_relations,as_parent_relations,checksum,created_at,games,name,slug,type,updated_at,url;"; public Collections() { @@ -21,60 +21,32 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetCollections(SearchUsing.id, Id); + Task RetVal = _GetCollections((long)Id); return RetVal.Result; } } - public static Collection GetCollections(string Slug) - { - Task RetVal = _GetCollections(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetCollections(SearchUsing searchUsing, object searchValue) + private static async Task _GetCollections(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("Collection", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("Collection", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("Collection", searchValue); Collection returnValue = new Collection(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); - break; + break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -88,21 +60,15 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get Collections metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.Collections, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.Collection, searchValue); var result = results.First(); return result; } - } + } } diff --git a/gaseous-server/Classes/Metadata/Communications.cs b/gaseous-server/Classes/Metadata/Communications.cs index dd0080f..ee1b0c4 100644 --- a/gaseous-server/Classes/Metadata/Communications.cs +++ b/gaseous-server/Classes/Metadata/Communications.cs @@ -2,6 +2,8 @@ using System.ComponentModel; using System.Data; using System.Drawing; using System.Net; +using System.Security.Policy; +using HasheousClient.Models.Metadata.IGDB; using Humanizer; using IGDB; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -24,12 +26,17 @@ namespace gaseous_server.Classes.Metadata client.DefaultRequestHeaders.Add("Accept-Encoding", "deflate"); } + // configure the IGDB client private static IGDBClient igdb = new IGDBClient( // Found in Twitch Developer portal for your app Config.IGDB.ClientId, Config.IGDB.Secret ); + // provide the hasheous client + private static HasheousClient.Hasheous hasheous = new HasheousClient.Hasheous(); + + private static HttpClient client = new HttpClient(); /// @@ -57,6 +64,18 @@ namespace gaseous_server.Classes.Metadata RateLimitRecoveryWaitTime = 10000; break; + + case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous: + // set rate limiter avoidance values + RateLimitAvoidanceWait = 1500; + RateLimitAvoidanceThreshold = 3; + RateLimitAvoidancePeriod = 1; + + // set rate limiter recovery values + RateLimitRecoveryWaitTime = 10000; + + break; + default: // leave all values at default break; @@ -159,8 +178,248 @@ namespace gaseous_server.Classes.Metadata private int RetryAttempts = 0; private int RetryAttemptsMax = 3; + public enum MetadataEndpoint + { + AgeGroup, + AgeRating, + AgeRatingContentDescription, + AlternativeName, + Artwork, + Collection, + Company, + CompanyLogo, + Cover, + ExternalGame, + Franchise, + GameMode, + Game, + GameVideo, + Genre, + InvolvedCompany, + MultiplayerMode, + PlatformLogo, + Platform, + PlatformVersion, + PlayerPerspective, + ReleaseDate, + Search, + Screenshot, + Theme + } + /// - /// Request data from the metadata API + /// Request data from the metadata API using a slug + /// + /// + /// The type of object to return + /// + /// + /// The endpoint to access - can only be either Platform or Game + /// + /// + /// The slug to query for + /// + /// + /// The object requested + /// + public async Task APIComm(MetadataEndpoint Endpoint, string Slug) + { + switch (_MetadataSource) + { + case HasheousClient.Models.MetadataModel.MetadataSources.None: + return null; + case HasheousClient.Models.MetadataModel.MetadataSources.IGDB: + string fieldList = ""; + string query = "where slug = \"" + Slug + "\""; + string EndpointString = ""; + + switch (Endpoint) + { + case MetadataEndpoint.Platform: + fieldList = Platforms.fieldList; + EndpointString = IGDBClient.Endpoints.Platforms; + break; + + case MetadataEndpoint.Game: + fieldList = Games.fieldList; + EndpointString = IGDBClient.Endpoints.Games; + break; + + default: + throw new Exception("Endpoint must be either Platform or Game"); + + } + + return await IGDBAPI(EndpointString, fieldList, query); + + case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous: + return null; + + default: + return null; + } + } + + /// + /// Request data from the metadata API using an id + /// + /// + /// The type of object to return + /// + /// + /// The endpoint to access + /// + /// + /// The Id to query for + /// + /// + /// The object requested + /// + public async Task APIComm(MetadataEndpoint Endpoint, long Id) + { + switch (_MetadataSource) + { + case HasheousClient.Models.MetadataModel.MetadataSources.None: + return null; + case HasheousClient.Models.MetadataModel.MetadataSources.IGDB: + string fieldList = ""; + string query = "where id = " + Id; + string EndpointString = ""; + + switch (Endpoint) + { + case MetadataEndpoint.AgeRating: + fieldList = AgeRatings.fieldList; + EndpointString = IGDBClient.Endpoints.AgeRating; + break; + + case MetadataEndpoint.AgeRatingContentDescription: + fieldList = AgeRatingContentDescriptions.fieldList; + EndpointString = IGDBClient.Endpoints.AgeRatingContentDescriptions; + break; + + case MetadataEndpoint.AlternativeName: + fieldList = AlternativeNames.fieldList; + EndpointString = IGDBClient.Endpoints.AlternativeNames; + break; + + case MetadataEndpoint.Artwork: + fieldList = Artworks.fieldList; + EndpointString = IGDBClient.Endpoints.Artworks; + break; + + case MetadataEndpoint.Collection: + fieldList = Collections.fieldList; + EndpointString = IGDBClient.Endpoints.Collections; + break; + + case MetadataEndpoint.Company: + fieldList = Companies.fieldList; + EndpointString = IGDBClient.Endpoints.Companies; + break; + + case MetadataEndpoint.CompanyLogo: + fieldList = CompanyLogos.fieldList; + EndpointString = IGDBClient.Endpoints.CompanyLogos; + break; + + case MetadataEndpoint.Cover: + fieldList = Covers.fieldList; + EndpointString = IGDBClient.Endpoints.Covers; + break; + + case MetadataEndpoint.ExternalGame: + fieldList = ExternalGames.fieldList; + EndpointString = IGDBClient.Endpoints.ExternalGames; + break; + + case MetadataEndpoint.Franchise: + fieldList = Franchises.fieldList; + EndpointString = IGDBClient.Endpoints.Franchies; + break; + + case MetadataEndpoint.GameMode: + fieldList = GameModes.fieldList; + EndpointString = IGDBClient.Endpoints.GameModes; + break; + + case MetadataEndpoint.Game: + fieldList = Games.fieldList; + EndpointString = IGDBClient.Endpoints.Games; + break; + + case MetadataEndpoint.GameVideo: + fieldList = GamesVideos.fieldList; + EndpointString = IGDBClient.Endpoints.GameVideos; + break; + + case MetadataEndpoint.Genre: + fieldList = Genres.fieldList; + EndpointString = IGDBClient.Endpoints.Genres; + break; + + case MetadataEndpoint.InvolvedCompany: + fieldList = InvolvedCompanies.fieldList; + EndpointString = IGDBClient.Endpoints.InvolvedCompanies; + break; + + case MetadataEndpoint.MultiplayerMode: + fieldList = MultiplayerModes.fieldList; + EndpointString = IGDBClient.Endpoints.MultiplayerModes; + break; + + case MetadataEndpoint.PlatformLogo: + fieldList = PlatformLogos.fieldList; + EndpointString = IGDBClient.Endpoints.PlatformLogos; + break; + + case MetadataEndpoint.Platform: + fieldList = Platforms.fieldList; + EndpointString = IGDBClient.Endpoints.Platforms; + break; + + case MetadataEndpoint.PlatformVersion: + fieldList = PlatformVersions.fieldList; + EndpointString = IGDBClient.Endpoints.PlatformVersions; + break; + + case MetadataEndpoint.PlayerPerspective: + fieldList = PlayerPerspectives.fieldList; + EndpointString = IGDBClient.Endpoints.PlayerPerspectives; + break; + + case MetadataEndpoint.ReleaseDate: + fieldList = ReleaseDates.fieldList; + EndpointString = IGDBClient.Endpoints.ReleaseDates; + break; + + case MetadataEndpoint.Screenshot: + fieldList = Screenshots.fieldList; + EndpointString = IGDBClient.Endpoints.Screenshots; + break; + + case MetadataEndpoint.Theme: + fieldList = Themes.fieldList; + EndpointString = IGDBClient.Endpoints.Themes; + break; + + default: + throw new Exception("Endpoint must be either Platform or Game"); + + } + + return await IGDBAPI(EndpointString, fieldList, query); + + case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous: + return null; + + default: + return null; + } + } + + /// + /// Request data from the metadata API - this is only valid for IGDB /// /// Type of object to return /// API endpoint segment to use @@ -175,6 +434,33 @@ namespace gaseous_server.Classes.Metadata return null; case HasheousClient.Models.MetadataModel.MetadataSources.IGDB: return await IGDBAPI(Endpoint, Fields, Query); + case HasheousClient.Models.MetadataModel.MetadataSources.Hasheous: + // configure the Hasheous client + hasheous = new HasheousClient.Hasheous(); + + // set the base URI + string HasheousHost = ""; + if (Config.MetadataConfiguration.HasheousHost == null) + { + HasheousHost = "https://hasheous.org/"; + } + else + { + HasheousHost = Config.MetadataConfiguration.HasheousHost; + } + + // set the client API key + HasheousClient.WebApp.HttpHelper.ClientKey = Config.MetadataConfiguration.HasheousClientAPIKey; + + // set the client secret + if (Config.MetadataConfiguration.HasheousAPIKey != null) + { + HasheousClient.WebApp.HttpHelper.APIKey = Config.MetadataConfiguration.HasheousAPIKey; + } + + // return from Hasheous + return await HasheousAPI(Endpoint, Fields, Query); + default: return null; } @@ -200,7 +486,8 @@ namespace gaseous_server.Classes.Metadata } // perform the actual API call - var results = await igdb.QueryAsync(Endpoint, query: Fields + " " + Query + ";"); + string queryString = Fields + " " + Query + ";"; + var results = await igdb.QueryAsync(Endpoint, query: queryString); // increment rate limiter avoidance call count RateLimitAvoidanceCallCount += 1; @@ -253,6 +540,145 @@ namespace gaseous_server.Classes.Metadata } } + /// + /// Access the HasheousAPI + /// + /// + /// The type of object to return + /// + /// + /// The endpoint to access + /// + /// + /// Can be either "slug" or "id" - note not all endpoints support slug + /// + /// + /// The "slug" or "id" to query for + /// + /// + /// The object requested + /// + private async Task HasheousAPI(string Endpoint, string Fields, string Query) + { + Logging.Log(Logging.LogType.Debug, "API Connection", "Accessing API for endpoint: " + Endpoint); + + if (RateLimitResumeTime > DateTime.UtcNow) + { + Logging.Log(Logging.LogType.Information, "API Connection", "IGDB rate limit hit. Pausing API communications until " + RateLimitResumeTime.ToString() + ". Attempt " + RetryAttempts + " of " + RetryAttemptsMax + " retries."); + Thread.Sleep(RateLimitRecoveryWaitTime); + } + + try + { + if (InRateLimitAvoidanceMode == true) + { + // sleep for a moment to help avoid hitting the rate limiter + Logging.Log(Logging.LogType.Information, "API Connection: Endpoint:" + Endpoint, "IGDB rate limit hit. Pausing API communications for " + RateLimitAvoidanceWait + " milliseconds to avoid rate limiter."); + Thread.Sleep(RateLimitAvoidanceWait); + } + + // perform the actual API call + //var results = await igdb.QueryAsync(Endpoint, query: Fields + " " + Query + ";"); + + T result1; + if (Fields == "slug") + { + result1 = hasheous.GetMetadataProxy(HasheousClient.Hasheous.MetadataProvider.IGDB, Query); + } + else if (Fields == "id") + { + result1 = hasheous.GetMetadataProxy(HasheousClient.Hasheous.MetadataProvider.IGDB, Query); + } + else + { + throw new Exception("Fields must be either 'slug' or 'id'"); + } + + var results1 = new T[] { result1 }; + + // increment rate limiter avoidance call count + RateLimitAvoidanceCallCount += 1; + + return results1; + } + catch (ApiException apiEx) + { + switch (apiEx.StatusCode) + { + case HttpStatusCode.TooManyRequests: + if (RetryAttempts >= RetryAttemptsMax) + { + Logging.Log(Logging.LogType.Warning, "API Connection", "IGDB rate limiter attempts expired. Aborting.", apiEx); + throw; + } + else + { + Logging.Log(Logging.LogType.Information, "API Connection", "IGDB API rate limit hit while accessing endpoint " + Endpoint, apiEx); + + RetryAttempts += 1; + + T result2; + if (Fields == "slug") + { + result2 = hasheous.GetMetadataProxy(HasheousClient.Hasheous.MetadataProvider.IGDB, Query); + } + else if (Fields == "id") + { + result2 = hasheous.GetMetadataProxy(HasheousClient.Hasheous.MetadataProvider.IGDB, Query); + } + else + { + throw new Exception("Fields must be either 'slug' or 'id'"); + } + + var results2 = new T[] { result2 }; + + return results2; + } + + case HttpStatusCode.Unauthorized: + Logging.Log(Logging.LogType.Information, "API Connection", "IGDB API unauthorised error while accessing endpoint " + Endpoint + ". Waiting " + RateLimitAvoidanceWait + " milliseconds and resetting IGDB client.", apiEx); + + Thread.Sleep(RateLimitAvoidanceWait); + + igdb = new IGDBClient( + // Found in Twitch Developer portal for your app + Config.IGDB.ClientId, + Config.IGDB.Secret + ); + + RetryAttempts += 1; + + T result3; + if (Fields == "slug") + { + result3 = hasheous.GetMetadataProxy(HasheousClient.Hasheous.MetadataProvider.IGDB, Query); + } + else if (Fields == "id") + { + result3 = hasheous.GetMetadataProxy(HasheousClient.Hasheous.MetadataProvider.IGDB, Query); + } + else + { + throw new Exception("Fields must be either 'slug' or 'id'"); + } + + var results3 = new T[] { result3 }; + + return results3; + + default: + Logging.Log(Logging.LogType.Warning, "API Connection", "Exception when accessing endpoint " + Endpoint, apiEx); + throw; + } + } + catch (Exception ex) + { + Logging.Log(Logging.LogType.Warning, "API Connection", "Exception when accessing endpoint " + Endpoint, ex); + throw; + } + } + /// /// Download from the specified uri /// diff --git a/gaseous-server/Classes/Metadata/Company.cs b/gaseous-server/Classes/Metadata/Company.cs index 288bb7b..df0103c 100644 --- a/gaseous-server/Classes/Metadata/Company.cs +++ b/gaseous-server/Classes/Metadata/Company.cs @@ -4,9 +4,9 @@ using IGDB.Models; namespace gaseous_server.Classes.Metadata { - public class Companies - { - const string fieldList = "fields change_date,change_date_category,changed_company_id,checksum,country,created_at,description,developed,logo,name,parent,published,slug,start_date,start_date_category,updated_at,url,websites;"; + public class Companies + { + public const string fieldList = "fields change_date,change_date_category,changed_company_id,checksum,country,created_at,description,developed,logo,name,parent,published,slug,start_date,start_date_category,updated_at,url,websites;"; public Companies() { @@ -20,61 +20,33 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetCompanies(SearchUsing.id, Id); + Task RetVal = _GetCompanies((long)Id); return RetVal.Result; } } - public static Company GetCompanies(string Slug) - { - Task RetVal = _GetCompanies(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetCompanies(SearchUsing searchUsing, object searchValue) + private static async Task _GetCompanies(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("Company", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("Company", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("Company", searchValue); Company returnValue = new Company(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); if (returnValue != null) { Storage.NewCacheValue(returnValue); } UpdateSubClasses(returnValue); break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -96,17 +68,11 @@ namespace gaseous_server.Classes.Metadata } } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get Companies metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.Companies, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.Company, searchValue); if (results.Length > 0) { var result = results.First(); @@ -117,7 +83,7 @@ namespace gaseous_server.Classes.Metadata { return null; } - + } } } diff --git a/gaseous-server/Classes/Metadata/CompanyLogos.cs b/gaseous-server/Classes/Metadata/CompanyLogos.cs index ac14620..b82a8a3 100644 --- a/gaseous-server/Classes/Metadata/CompanyLogos.cs +++ b/gaseous-server/Classes/Metadata/CompanyLogos.cs @@ -7,7 +7,7 @@ namespace gaseous_server.Classes.Metadata { public class CompanyLogos { - const string fieldList = "fields alpha_channel,animated,checksum,height,image_id,url,width;"; + public const string fieldList = "fields alpha_channel,animated,checksum,height,image_id,url,width;"; public CompanyLogos() { @@ -21,50 +21,22 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetCompanyLogo(SearchUsing.id, Id, ImagePath); + Task RetVal = _GetCompanyLogo((long)Id, ImagePath); return RetVal.Result; } } - public static CompanyLogo GetCompanyLogo(string Slug, string ImagePath) - { - Task RetVal = _GetCompanyLogo(SearchUsing.slug, Slug, ImagePath); - return RetVal.Result; - } - - private static async Task _GetCompanyLogo(SearchUsing searchUsing, object searchValue, string ImagePath) + private static async Task _GetCompanyLogo(long searchValue, string ImagePath) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("CompanyLogo", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("CompanyLogo", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("CompanyLogo", searchValue); CompanyLogo returnValue = new CompanyLogo(); bool forceImageDownload = false; switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause, ImagePath); + returnValue = await GetObjectFromServer(searchValue, ImagePath); if (returnValue != null) { Storage.NewCacheValue(returnValue); @@ -74,7 +46,7 @@ namespace gaseous_server.Classes.Metadata case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause, ImagePath); + returnValue = await GetObjectFromServer(searchValue, ImagePath); Storage.NewCacheValue(returnValue, true); // check if old value is different from the new value - only download if it's different @@ -86,7 +58,7 @@ namespace gaseous_server.Classes.Metadata } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -110,17 +82,11 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause, string ImagePath) + private static async Task GetObjectFromServer(long searchValue, string ImagePath) { // get Artwork metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.CompanyLogos, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.CompanyLogo, searchValue); var result = results.First(); return result; diff --git a/gaseous-server/Classes/Metadata/Covers.cs b/gaseous-server/Classes/Metadata/Covers.cs index 9069f53..b5bc9da 100644 --- a/gaseous-server/Classes/Metadata/Covers.cs +++ b/gaseous-server/Classes/Metadata/Covers.cs @@ -9,7 +9,7 @@ namespace gaseous_server.Classes.Metadata { public class Covers { - const string fieldList = "fields alpha_channel,animated,checksum,game,game_localization,height,image_id,url,width;"; + public const string fieldList = "fields alpha_channel,animated,checksum,game,game_localization,height,image_id,url,width;"; public Covers() { @@ -23,43 +23,15 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetCover(SearchUsing.id, Id, ImagePath, GetImages); + Task RetVal = _GetCover((long)Id, ImagePath, GetImages); return RetVal.Result; } } - public static Cover GetCover(string Slug, string ImagePath, bool GetImages) - { - Task RetVal = _GetCover(SearchUsing.slug, Slug, ImagePath, GetImages); - return RetVal.Result; - } - - private static async Task _GetCover(SearchUsing searchUsing, object searchValue, string ImagePath, bool GetImages = true) + private static async Task _GetCover(long searchValue, string ImagePath, bool GetImages = true) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("Cover", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("Cover", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("Cover", searchValue); Cover returnValue = new Cover(); bool forceImageDownload = false; @@ -67,14 +39,14 @@ namespace gaseous_server.Classes.Metadata switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause, ImagePath); + returnValue = await GetObjectFromServer(searchValue, ImagePath); Storage.NewCacheValue(returnValue); forceImageDownload = true; break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause, ImagePath); + returnValue = await GetObjectFromServer(searchValue, ImagePath); Storage.NewCacheValue(returnValue, true); // check if old value is different from the new value - only download if it's different @@ -86,7 +58,7 @@ namespace gaseous_server.Classes.Metadata } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -126,17 +98,11 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause, string ImagePath) + private static async Task GetObjectFromServer(long searchValue, string ImagePath) { // get Cover metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.Covers, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.Cover, searchValue); var result = results.First(); return result; diff --git a/gaseous-server/Classes/Metadata/ExternalGames.cs b/gaseous-server/Classes/Metadata/ExternalGames.cs index 9f2b612..00fe00d 100644 --- a/gaseous-server/Classes/Metadata/ExternalGames.cs +++ b/gaseous-server/Classes/Metadata/ExternalGames.cs @@ -5,9 +5,9 @@ using IGDB.Models; namespace gaseous_server.Classes.Metadata { - public class ExternalGames + public class ExternalGames { - const string fieldList = "fields category,checksum,countries,created_at,game,media,name,platform,uid,updated_at,url,year;"; + public const string fieldList = "fields category,checksum,countries,created_at,game,media,name,platform,uid,updated_at,url,year;"; public ExternalGames() { @@ -21,63 +21,35 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetExternalGames(SearchUsing.id, Id); + Task RetVal = _GetExternalGames((long)Id); return RetVal.Result; } } - public static ExternalGame GetExternalGames(string Slug) - { - Task RetVal = _GetExternalGames(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetExternalGames(SearchUsing searchUsing, object searchValue) + private static async Task _GetExternalGames(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("ExternalGame", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("ExternalGame", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("ExternalGame", searchValue); ExternalGame returnValue = new ExternalGame(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); if (returnValue != null) { Storage.NewCacheValue(returnValue); } - break; + break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -91,17 +63,11 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get ExternalGames metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.ExternalGames, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.ExternalGame, searchValue); if (results.Length > 0) { var result = results.First(); @@ -113,6 +79,6 @@ namespace gaseous_server.Classes.Metadata return null; } } - } + } } diff --git a/gaseous-server/Classes/Metadata/Franchises.cs b/gaseous-server/Classes/Metadata/Franchises.cs index 6e57ceb..a947455 100644 --- a/gaseous-server/Classes/Metadata/Franchises.cs +++ b/gaseous-server/Classes/Metadata/Franchises.cs @@ -5,9 +5,9 @@ using IGDB.Models; namespace gaseous_server.Classes.Metadata { - public class Franchises + public class Franchises { - const string fieldList = "fields checksum,created_at,games,name,slug,updated_at,url;"; + public const string fieldList = "fields checksum,created_at,games,name,slug,updated_at,url;"; public Franchises() { @@ -21,60 +21,32 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetFranchises(SearchUsing.id, Id); + Task RetVal = _GetFranchises((long)Id); return RetVal.Result; } } - public static Franchise GetFranchises(string Slug) - { - Task RetVal = _GetFranchises(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetFranchises(SearchUsing searchUsing, object searchValue) + private static async Task _GetFranchises(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("Franchise", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("Franchise", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("Franchise", searchValue); Franchise returnValue = new Franchise(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); - break; + break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -88,21 +60,15 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get FranchiseContentDescriptions metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.Franchies, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.Franchise, searchValue); var result = results.First(); return result; } - } + } } diff --git a/gaseous-server/Classes/Metadata/GameModes.cs b/gaseous-server/Classes/Metadata/GameModes.cs index 37697cc..947f4e6 100644 --- a/gaseous-server/Classes/Metadata/GameModes.cs +++ b/gaseous-server/Classes/Metadata/GameModes.cs @@ -7,7 +7,7 @@ namespace gaseous_server.Classes.Metadata { public class GameModes { - const string fieldList = "fields checksum,created_at,name,slug,updated_at,url;"; + public const string fieldList = "fields checksum,created_at,name,slug,updated_at,url;"; public GameModes() { @@ -21,60 +21,32 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetGame_Modes(SearchUsing.id, Id); + Task RetVal = _GetGame_Modes((long)Id); return RetVal.Result; } } - public static GameMode GetGame_Modes(string Slug) - { - Task RetVal = _GetGame_Modes(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetGame_Modes(SearchUsing searchUsing, object searchValue) + private static async Task _GetGame_Modes(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("GameMode", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("GameMode", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("GameMode", searchValue); GameMode returnValue = new GameMode(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -88,17 +60,11 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get Game_Modes metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.GameModes, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.GameMode, searchValue); var result = results.First(); return result; diff --git a/gaseous-server/Classes/Metadata/GameVideos.cs b/gaseous-server/Classes/Metadata/GameVideos.cs index b4bf4d8..0155818 100644 --- a/gaseous-server/Classes/Metadata/GameVideos.cs +++ b/gaseous-server/Classes/Metadata/GameVideos.cs @@ -5,9 +5,9 @@ using IGDB.Models; namespace gaseous_server.Classes.Metadata { - public class GamesVideos + public class GamesVideos { - const string fieldList = "fields checksum,game,name,video_id;"; + public const string fieldList = "fields checksum,game,name,video_id;"; public GamesVideos() { @@ -21,60 +21,32 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetGame_Videos(SearchUsing.id, Id); + Task RetVal = _GetGame_Videos((long)Id); return RetVal.Result; } } - public static GameVideo GetGame_Videos(string Slug) - { - Task RetVal = _GetGame_Videos(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetGame_Videos(SearchUsing searchUsing, object searchValue) + private static async Task _GetGame_Videos(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("GameVideo", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("GameVideo", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("GameVideo", searchValue); GameVideo returnValue = new GameVideo(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); - break; + break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -88,21 +60,15 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get Game_Videos metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.GameVideos, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.GameVideo, searchValue); var result = results.First(); return result; } - } + } } diff --git a/gaseous-server/Classes/Metadata/Games.cs b/gaseous-server/Classes/Metadata/Games.cs index 79904dd..20ed86c 100644 --- a/gaseous-server/Classes/Metadata/Games.cs +++ b/gaseous-server/Classes/Metadata/Games.cs @@ -8,7 +8,7 @@ namespace gaseous_server.Classes.Metadata { public class Games { - const string fieldList = "fields age_ratings,aggregated_rating,aggregated_rating_count,alternative_names,artworks,bundles,category,checksum,collections,cover,created_at,dlcs,expanded_games,expansions,external_games,first_release_date,follows,forks,franchise,franchises,game_engines,game_localizations,game_modes,genres,hypes,involved_companies,keywords,language_supports,multiplayer_modes,name,parent_game,platforms,player_perspectives,ports,rating,rating_count,release_dates,remakes,remasters,screenshots,similar_games,slug,standalone_expansions,status,storyline,summary,tags,themes,total_rating,total_rating_count,updated_at,url,version_parent,version_title,videos,websites;"; + public const string fieldList = "fields age_ratings,aggregated_rating,aggregated_rating_count,alternative_names,artworks,bundles,category,checksum,collections,cover,created_at,dlcs,expanded_games,expansions,external_games,first_release_date,follows,forks,franchise,franchises,game_engines,game_localizations,game_modes,genres,hypes,involved_companies,keywords,language_supports,multiplayer_modes,name,parent_game,platforms,player_perspectives,ports,rating,rating_count,release_dates,remakes,remasters,screenshots,similar_games,slug,standalone_expansions,status,storyline,summary,tags,themes,total_rating,total_rating_count,updated_at,url,version_parent,version_title,videos,websites;"; public Games() { @@ -45,14 +45,14 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetGame(SearchUsing.id, Id, getAllMetadata, followSubGames, forceRefresh); + Task RetVal = _GetGame(SearchUsing.Id, Id, getAllMetadata, followSubGames, forceRefresh); return RetVal.Result; } } public static Game GetGame(string Slug, bool getAllMetadata, bool followSubGames, bool forceRefresh) { - Task RetVal = _GetGame(SearchUsing.slug, Slug, getAllMetadata, followSubGames, forceRefresh); + Task RetVal = _GetGame(SearchUsing.Slug, Slug, getAllMetadata, followSubGames, forceRefresh); return RetVal.Result; } @@ -65,7 +65,7 @@ namespace gaseous_server.Classes.Metadata { // check database first Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) + if (searchUsing == SearchUsing.Id) { cacheStatus = Storage.GetCacheStatus("Game", (long)searchValue); } @@ -79,45 +79,43 @@ namespace gaseous_server.Classes.Metadata if (cacheStatus == Storage.CacheStatus.Current) { cacheStatus = Storage.CacheStatus.Expired; } } - string WhereClause = ""; - string searchField = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - searchField = "id"; - break; - case SearchUsing.slug: - WhereClause = "where slug = \"" + searchValue + "\""; - searchField = "slug"; - break; - default: - throw new Exception("Invalid search type"); - } - Game returnValue = new Game(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + if (searchUsing == SearchUsing.Id) + { + returnValue = await GetObjectFromServer((long)searchValue); + } + else + { + returnValue = await GetObjectFromServer((string)searchValue); + } Storage.NewCacheValue(returnValue); UpdateSubClasses(returnValue, getAllMetadata, followSubGames, forceRefresh); return returnValue; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + if (searchUsing == SearchUsing.Id) + { + returnValue = await GetObjectFromServer((long)searchValue); + } + else + { + returnValue = await GetObjectFromServer((string)searchValue); + } Storage.NewCacheValue(returnValue, true); UpdateSubClasses(returnValue, getAllMetadata, followSubGames, forceRefresh); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); - returnValue = Storage.GetCacheValue(returnValue, searchField, searchValue); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id/Slug: " + searchValue, ex); + returnValue = Storage.GetCacheValue(returnValue, searchUsing.ToString(), searchValue); } return returnValue; case Storage.CacheStatus.Current: - returnValue = Storage.GetCacheValue(returnValue, searchField, searchValue); + returnValue = Storage.GetCacheValue(returnValue, searchUsing.ToString(), searchValue); UpdateSubClasses(returnValue, false, false, false); return returnValue; default: @@ -309,17 +307,36 @@ namespace gaseous_server.Classes.Metadata private enum SearchUsing { - id, - slug + Id, + Slug } - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(string Slug) { // get Game metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.Games, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.Game, Slug); var result = results.First(); + result = MassageResult(result); + + return result; + } + + private static async Task GetObjectFromServer(long Id) + { + // get Game metadata + Communications comms = new Communications(); + var results = await comms.APIComm(Communications.MetadataEndpoint.Game, Id); + var result = results.First(); + + result = MassageResult(result); + + return result; + } + + private static Game MassageResult(Game result) + { // add artificial unknown platform mapping List platformIds = new List(); platformIds.Add(0); diff --git a/gaseous-server/Classes/Metadata/Genres.cs b/gaseous-server/Classes/Metadata/Genres.cs index e386be6..f65560c 100644 --- a/gaseous-server/Classes/Metadata/Genres.cs +++ b/gaseous-server/Classes/Metadata/Genres.cs @@ -7,7 +7,7 @@ namespace gaseous_server.Classes.Metadata { public class Genres { - const string fieldList = "fields checksum,created_at,name,slug,updated_at,url;"; + public const string fieldList = "fields checksum,created_at,name,slug,updated_at,url;"; public Genres() { @@ -21,60 +21,32 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetGenres(SearchUsing.id, Id); + Task RetVal = _GetGenres((long)Id); return RetVal.Result; } } - public static Genre GetGenres(string Slug) - { - Task RetVal = _GetGenres(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetGenres(SearchUsing searchUsing, object searchValue) + private static async Task _GetGenres(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("Genre", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("Genre", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("Genre", searchValue); Genre returnValue = new Genre(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -88,17 +60,11 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get Genres metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.Genres, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.Genre, searchValue); var result = results.First(); return result; diff --git a/gaseous-server/Classes/Metadata/InvolvedCompany.cs b/gaseous-server/Classes/Metadata/InvolvedCompany.cs index 0d75035..f43feba 100644 --- a/gaseous-server/Classes/Metadata/InvolvedCompany.cs +++ b/gaseous-server/Classes/Metadata/InvolvedCompany.cs @@ -4,9 +4,9 @@ using IGDB.Models; namespace gaseous_server.Classes.Metadata { - public class InvolvedCompanies - { - const string fieldList = "fields *;"; + public class InvolvedCompanies + { + public const string fieldList = "fields *;"; public InvolvedCompanies() { @@ -20,61 +20,33 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetInvolvedCompanies(SearchUsing.id, Id); + Task RetVal = _GetInvolvedCompanies((long)Id); return RetVal.Result; } } - public static InvolvedCompany GetInvolvedCompanies(string Slug) - { - Task RetVal = _GetInvolvedCompanies(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetInvolvedCompanies(SearchUsing searchUsing, object searchValue) + private static async Task _GetInvolvedCompanies(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("InvolvedCompany", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("InvolvedCompany", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("InvolvedCompany", searchValue); InvolvedCompany returnValue = new InvolvedCompany(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); UpdateSubClasses(returnValue); break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -96,31 +68,14 @@ namespace gaseous_server.Classes.Metadata } } - private enum SearchUsing + private static async Task GetObjectFromServer(long searchValue) { - id, - slug - } + // get Genres metadata + Communications comms = new Communications(); + var results = await comms.APIComm(Communications.MetadataEndpoint.InvolvedCompany, searchValue); + var result = results.First(); - private static async Task GetObjectFromServer(string WhereClause) - { - // get InvolvedCompanies metadata - try - { - Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.InvolvedCompanies, fieldList, WhereClause); - var result = results.First(); - - return result; - } - catch (Exception ex) - { - Logging.Log(Logging.LogType.Critical, "Involved Companies", "Failure when requesting involved companies."); - Logging.Log(Logging.LogType.Critical, "Involved Companies", "Field list: " + fieldList); - Logging.Log(Logging.LogType.Critical, "Involved Companies", "Where clause: " + WhereClause); - Logging.Log(Logging.LogType.Critical, "Involved Companies", "Error", ex); - throw; - } + return result; } } } diff --git a/gaseous-server/Classes/Metadata/MultiplayerModes.cs b/gaseous-server/Classes/Metadata/MultiplayerModes.cs index e89f945..a35fc3e 100644 --- a/gaseous-server/Classes/Metadata/MultiplayerModes.cs +++ b/gaseous-server/Classes/Metadata/MultiplayerModes.cs @@ -7,7 +7,7 @@ namespace gaseous_server.Classes.Metadata { public class MultiplayerModes { - const string fieldList = "fields campaigncoop,checksum,dropin,game,lancoop,offlinecoop,offlinecoopmax,offlinemax,onlinecoop,onlinecoopmax,onlinemax,platform,splitscreen,splitscreenonline;"; + public const string fieldList = "fields campaigncoop,checksum,dropin,game,lancoop,offlinecoop,offlinecoopmax,offlinemax,onlinecoop,onlinecoopmax,onlinemax,platform,splitscreen,splitscreenonline;"; public MultiplayerModes() { @@ -21,60 +21,32 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetGame_MultiplayerModes(SearchUsing.id, Id); + Task RetVal = _GetGame_MultiplayerModes((long)Id); return RetVal.Result; } } - public static MultiplayerMode GetGame_MultiplayerModes(string Slug) - { - Task RetVal = _GetGame_MultiplayerModes(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetGame_MultiplayerModes(SearchUsing searchUsing, object searchValue) + private static async Task _GetGame_MultiplayerModes(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("MultiplayerMode", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("MultiplayerMode", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("MultiplayerMode", searchValue); MultiplayerMode returnValue = new MultiplayerMode(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -88,17 +60,11 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get Game_MultiplayerModes metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.MultiplayerModes, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.MultiplayerMode, searchValue); var result = results.First(); return result; diff --git a/gaseous-server/Classes/Metadata/PlatformLogos.cs b/gaseous-server/Classes/Metadata/PlatformLogos.cs index 909f7f9..68fc5ad 100644 --- a/gaseous-server/Classes/Metadata/PlatformLogos.cs +++ b/gaseous-server/Classes/Metadata/PlatformLogos.cs @@ -5,9 +5,9 @@ using IGDB.Models; namespace gaseous_server.Classes.Metadata { - public class PlatformLogos + public class PlatformLogos { - const string fieldList = "fields alpha_channel,animated,checksum,height,image_id,url,width;"; + public const string fieldList = "fields alpha_channel,animated,checksum,height,image_id,url,width;"; public PlatformLogos() { @@ -21,62 +21,34 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetPlatformLogo(SearchUsing.id, Id, ImagePath); + Task RetVal = _GetPlatformLogo((long)Id, ImagePath); return RetVal.Result; } } - public static PlatformLogo GetPlatformLogo(string Slug, string ImagePath) - { - Task RetVal = _GetPlatformLogo(SearchUsing.slug, Slug, ImagePath); - return RetVal.Result; - } - - private static async Task _GetPlatformLogo(SearchUsing searchUsing, object searchValue, string ImagePath) + private static async Task _GetPlatformLogo(long searchValue, string ImagePath) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("PlatformLogo", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("PlatformLogo", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("PlatformLogo", searchValue); PlatformLogo returnValue = new PlatformLogo(); bool forceImageDownload = false; switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause, ImagePath); + returnValue = await GetObjectFromServer(searchValue, ImagePath); if (returnValue != null) { Storage.NewCacheValue(returnValue); forceImageDownload = true; } - break; + break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause, ImagePath); + returnValue = await GetObjectFromServer(searchValue, ImagePath); Storage.NewCacheValue(returnValue, true); - + // check if old value is different from the new value - only download if it's different PlatformLogo oldImage = Storage.GetCacheValue(returnValue, "id", (long)searchValue); if (oldImage.ImageId != returnValue.ImageId) @@ -86,10 +58,10 @@ namespace gaseous_server.Classes.Metadata } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } - break; + break; case Storage.CacheStatus.Current: returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); break; @@ -113,21 +85,15 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause, string ImagePath) + private static async Task GetObjectFromServer(long searchValue, string ImagePath) { // get Artwork metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.PlatformLogos, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.PlatformLogo, searchValue); var result = results.First(); return result; } - } + } } diff --git a/gaseous-server/Classes/Metadata/PlatformVersions.cs b/gaseous-server/Classes/Metadata/PlatformVersions.cs index 59722b4..8ad1ced 100644 --- a/gaseous-server/Classes/Metadata/PlatformVersions.cs +++ b/gaseous-server/Classes/Metadata/PlatformVersions.cs @@ -7,7 +7,7 @@ namespace gaseous_server.Classes.Metadata { public class PlatformVersions { - const string fieldList = "fields checksum,companies,connectivity,cpu,graphics,main_manufacturer,media,memory,name,online,os,output,platform_logo,platform_version_release_dates,resolutions,slug,sound,storage,summary,url;"; + public const string fieldList = "fields checksum,companies,connectivity,cpu,graphics,main_manufacturer,media,memory,name,online,os,output,platform_logo,platform_version_release_dates,resolutions,slug,sound,storage,summary,url;"; public PlatformVersions() { @@ -21,49 +21,21 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetPlatformVersion(SearchUsing.id, Id, ParentPlatform, GetImages); + Task RetVal = _GetPlatformVersion((long)Id, ParentPlatform, GetImages); return RetVal.Result; } } - public static PlatformVersion GetPlatformVersion(string Slug, Platform ParentPlatform, bool GetImages = false) - { - Task RetVal = _GetPlatformVersion(SearchUsing.slug, Slug, ParentPlatform, GetImages); - return RetVal.Result; - } - - private static async Task _GetPlatformVersion(SearchUsing searchUsing, object searchValue, Platform ParentPlatform, bool GetImages) + private static async Task _GetPlatformVersion(long searchValue, Platform ParentPlatform, bool GetImages) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("PlatformVersion", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("PlatformVersion", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("PlatformVersion", searchValue); PlatformVersion returnValue = new PlatformVersion(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); if (returnValue != null) { Storage.NewCacheValue(returnValue); @@ -73,13 +45,13 @@ namespace gaseous_server.Classes.Metadata case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); UpdateSubClasses(ParentPlatform, returnValue, GetImages); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } return returnValue; @@ -108,17 +80,11 @@ namespace gaseous_server.Classes.Metadata } } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get PlatformVersion metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.PlatformVersions, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.PlatformVersion, searchValue); if (results.Length > 0) { var result = results.First(); diff --git a/gaseous-server/Classes/Metadata/Platforms.cs b/gaseous-server/Classes/Metadata/Platforms.cs index e02ffda..60efb17 100644 --- a/gaseous-server/Classes/Metadata/Platforms.cs +++ b/gaseous-server/Classes/Metadata/Platforms.cs @@ -8,7 +8,7 @@ namespace gaseous_server.Classes.Metadata { public class 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 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() { @@ -41,7 +41,7 @@ namespace gaseous_server.Classes.Metadata { try { - Task RetVal = _GetPlatform(SearchUsing.id, Id, forceRefresh, GetImages); + Task RetVal = _GetPlatform(SearchUsing.Id, Id, forceRefresh, GetImages); return RetVal.Result; } catch (Exception ex) @@ -54,7 +54,7 @@ namespace gaseous_server.Classes.Metadata public static Platform GetPlatform(string Slug, bool forceRefresh = false, bool GetImages = false) { - Task RetVal = _GetPlatform(SearchUsing.slug, Slug, forceRefresh, GetImages); + Task RetVal = _GetPlatform(SearchUsing.Slug, Slug, forceRefresh, GetImages); return RetVal.Result; } @@ -62,7 +62,7 @@ namespace gaseous_server.Classes.Metadata { // check database first Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) + if (searchUsing == SearchUsing.Id) { cacheStatus = Storage.GetCacheStatus("Platform", (long)searchValue); } @@ -76,28 +76,18 @@ namespace gaseous_server.Classes.Metadata if (cacheStatus == Storage.CacheStatus.Current) { cacheStatus = Storage.CacheStatus.Expired; } } - // set up where clause - string WhereClause = ""; - string searchField = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - searchField = "id"; - break; - case SearchUsing.slug: - WhereClause = "where slug = \"" + searchValue + "\""; - searchField = "slug"; - break; - default: - throw new Exception("Invalid search type"); - } - Platform returnValue = new Platform(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + if (searchUsing == SearchUsing.Id) + { + returnValue = await GetObjectFromServer((long)searchValue); + } + else + { + returnValue = await GetObjectFromServer((string)searchValue); + } Storage.NewCacheValue(returnValue); UpdateSubClasses(returnValue, GetImages); AddPlatformMapping(returnValue); @@ -105,7 +95,14 @@ namespace gaseous_server.Classes.Metadata case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + if (searchUsing == SearchUsing.Id) + { + returnValue = await GetObjectFromServer((long)searchValue); + } + else + { + returnValue = await GetObjectFromServer((string)searchValue); + } Storage.NewCacheValue(returnValue, true); UpdateSubClasses(returnValue, GetImages); AddPlatformMapping(returnValue); @@ -113,11 +110,11 @@ namespace gaseous_server.Classes.Metadata } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); - return Storage.GetCacheValue(returnValue, searchField, searchValue); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id/Slug: " + searchValue, ex); + return Storage.GetCacheValue(returnValue, searchUsing.ToString(), searchValue); } case Storage.CacheStatus.Current: - return Storage.GetCacheValue(returnValue, searchField, searchValue); + return Storage.GetCacheValue(returnValue, searchUsing.ToString(), searchValue); default: throw new Exception("How did you get here?"); } @@ -177,15 +174,25 @@ namespace gaseous_server.Classes.Metadata private enum SearchUsing { - id, - slug + Id, + Slug } - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(string Slug) { // get platform metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.Platforms, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.Platform, Slug); + var result = results.First(); + + return result; + } + + private static async Task GetObjectFromServer(long Id) + { + // get platform metadata + Communications comms = new Communications(); + var results = await comms.APIComm(Communications.MetadataEndpoint.Platform, Id); var result = results.First(); return result; diff --git a/gaseous-server/Classes/Metadata/PlayerPerspectives.cs b/gaseous-server/Classes/Metadata/PlayerPerspectives.cs index 6455e2a..b3eb23d 100644 --- a/gaseous-server/Classes/Metadata/PlayerPerspectives.cs +++ b/gaseous-server/Classes/Metadata/PlayerPerspectives.cs @@ -7,7 +7,7 @@ namespace gaseous_server.Classes.Metadata { public class PlayerPerspectives { - const string fieldList = "fields checksum,created_at,name,slug,updated_at,url;"; + public const string fieldList = "fields checksum,created_at,name,slug,updated_at,url;"; public PlayerPerspectives() { @@ -21,62 +21,34 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetGame_PlayerPerspectives(SearchUsing.id, Id); + Task RetVal = _GetGame_PlayerPerspectives((long)Id); return RetVal.Result; } } - public static PlayerPerspective GetGame_PlayerPerspectives(string Slug) - { - Task RetVal = _GetGame_PlayerPerspectives(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetGame_PlayerPerspectives(SearchUsing searchUsing, object searchValue) + private static async Task _GetGame_PlayerPerspectives(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("PlayerPerspective", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("PlayerPerspective", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("PlayerPerspective", searchValue); PlayerPerspective returnValue = new PlayerPerspective(); bool forceImageDownload = false; switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); forceImageDownload = true; break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -90,17 +62,11 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get Game_PlayerPerspectives metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.PlayerPerspectives, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.PlayerPerspective, searchValue); var result = results.First(); return result; diff --git a/gaseous-server/Classes/Metadata/ReleaseDates.cs b/gaseous-server/Classes/Metadata/ReleaseDates.cs index 20c8e06..3ff922a 100644 --- a/gaseous-server/Classes/Metadata/ReleaseDates.cs +++ b/gaseous-server/Classes/Metadata/ReleaseDates.cs @@ -5,9 +5,9 @@ using IGDB.Models; namespace gaseous_server.Classes.Metadata { - public class ReleaseDates + public class ReleaseDates { - const string fieldList = "fields category,checksum,created_at,date,game,human,m,platform,region,status,updated_at,y;"; + public const string fieldList = "fields category,checksum,created_at,date,game,human,m,platform,region,status,updated_at,y;"; public ReleaseDates() { @@ -21,60 +21,32 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetReleaseDates(SearchUsing.id, Id); + Task RetVal = _GetReleaseDates((long)Id); return RetVal.Result; } } - public static ReleaseDate GetReleaseDates(string Slug) - { - Task RetVal = _GetReleaseDates(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetReleaseDates(SearchUsing searchUsing, object searchValue) + private static async Task _GetReleaseDates(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("ReleaseDate", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("ReleaseDate", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("ReleaseDate", searchValue); ReleaseDate returnValue = new ReleaseDate(); switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); - break; + break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -88,21 +60,15 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get ReleaseDates metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.ReleaseDates, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.ReleaseDate, searchValue); var result = results.First(); return result; } - } + } } diff --git a/gaseous-server/Classes/Metadata/Screenshots.cs b/gaseous-server/Classes/Metadata/Screenshots.cs index 05e5fc3..7bc3087 100644 --- a/gaseous-server/Classes/Metadata/Screenshots.cs +++ b/gaseous-server/Classes/Metadata/Screenshots.cs @@ -7,7 +7,7 @@ namespace gaseous_server.Classes.Metadata { public class Screenshots { - const string fieldList = "fields alpha_channel,animated,checksum,game,height,image_id,url,width;"; + public const string fieldList = "fields alpha_channel,animated,checksum,game,height,image_id,url,width;"; public Screenshots() { @@ -21,43 +21,15 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetScreenshot(SearchUsing.id, Id, ImagePath, GetImages); + Task RetVal = _GetScreenshot((long)Id, ImagePath, GetImages); return RetVal.Result; } } - public static Screenshot GetScreenshot(string Slug, string ImagePath, bool GetImages) - { - Task RetVal = _GetScreenshot(SearchUsing.slug, Slug, ImagePath, GetImages); - return RetVal.Result; - } - - private static async Task _GetScreenshot(SearchUsing searchUsing, object searchValue, string ImagePath, bool GetImages = true) + private static async Task _GetScreenshot(long searchValue, string ImagePath, bool GetImages = true) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("Screenshot", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("Screenshot", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("Screenshot", searchValue); Screenshot returnValue = new Screenshot(); bool forceImageDownload = false; @@ -65,14 +37,14 @@ namespace gaseous_server.Classes.Metadata switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause, ImagePath); + returnValue = await GetObjectFromServer(searchValue, ImagePath); Storage.NewCacheValue(returnValue); forceImageDownload = true; break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause, ImagePath); + returnValue = await GetObjectFromServer(searchValue, ImagePath); Storage.NewCacheValue(returnValue, true); // check if old value is different from the new value - only download if it's different @@ -84,7 +56,7 @@ namespace gaseous_server.Classes.Metadata } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); returnValue = Storage.GetCacheValue(returnValue, "id", (long)searchValue); } break; @@ -111,17 +83,11 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause, string ImagePath) + private static async Task GetObjectFromServer(long searchValue, string ImagePath) { // get Screenshot metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.Screenshots, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.Screenshot, searchValue); var result = results.First(); return result; diff --git a/gaseous-server/Classes/Metadata/Themes.cs b/gaseous-server/Classes/Metadata/Themes.cs index c4d1e7d..fe3c5c6 100644 --- a/gaseous-server/Classes/Metadata/Themes.cs +++ b/gaseous-server/Classes/Metadata/Themes.cs @@ -7,7 +7,7 @@ namespace gaseous_server.Classes.Metadata { public class Themes { - const string fieldList = "fields checksum,created_at,name,slug,updated_at,url;"; + public const string fieldList = "fields checksum,created_at,name,slug,updated_at,url;"; public Themes() { @@ -21,63 +21,35 @@ namespace gaseous_server.Classes.Metadata } else { - Task RetVal = _GetGame_Themes(SearchUsing.id, Id); + Task RetVal = _GetGame_Themes((long)Id); return RetVal.Result; } } - public static Theme GetGame_Themes(string Slug) - { - Task RetVal = _GetGame_Themes(SearchUsing.slug, Slug); - return RetVal.Result; - } - - private static async Task _GetGame_Themes(SearchUsing searchUsing, object searchValue) + private static async Task _GetGame_Themes(long searchValue) { // check database first - Storage.CacheStatus? cacheStatus = new Storage.CacheStatus(); - if (searchUsing == SearchUsing.id) - { - cacheStatus = Storage.GetCacheStatus("Theme", (long)searchValue); - } - else - { - cacheStatus = Storage.GetCacheStatus("Theme", (string)searchValue); - } - - // set up where clause - string WhereClause = ""; - switch (searchUsing) - { - case SearchUsing.id: - WhereClause = "where id = " + searchValue; - break; - case SearchUsing.slug: - WhereClause = "where slug = " + searchValue; - break; - default: - throw new Exception("Invalid search type"); - } + Storage.CacheStatus? cacheStatus = Storage.GetCacheStatus("Theme", searchValue); Theme returnValue = new Theme(); bool forceImageDownload = false; switch (cacheStatus) { case Storage.CacheStatus.NotPresent: - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue); forceImageDownload = true; break; case Storage.CacheStatus.Expired: try { - returnValue = await GetObjectFromServer(WhereClause); + returnValue = await GetObjectFromServer(searchValue); Storage.NewCacheValue(returnValue, true); return returnValue; } catch (Exception ex) { - Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. WhereClause: " + WhereClause, ex); + Logging.Log(Logging.LogType.Warning, "Metadata: " + returnValue.GetType().Name, "An error occurred while connecting to IGDB. Id: " + searchValue, ex); return Storage.GetCacheValue(returnValue, "id", (long)searchValue); } case Storage.CacheStatus.Current: @@ -90,17 +62,11 @@ namespace gaseous_server.Classes.Metadata return returnValue; } - private enum SearchUsing - { - id, - slug - } - - private static async Task GetObjectFromServer(string WhereClause) + private static async Task GetObjectFromServer(long searchValue) { // get Game_Themes metadata Communications comms = new Communications(); - var results = await comms.APIComm(IGDBClient.Endpoints.Themes, fieldList, WhereClause); + var results = await comms.APIComm(Communications.MetadataEndpoint.Theme, searchValue); var result = results.First(); return result; diff --git a/gaseous-server/Classes/Roms.cs b/gaseous-server/Classes/Roms.cs index a8b27bf..2cf567a 100644 --- a/gaseous-server/Classes/Roms.cs +++ b/gaseous-server/Classes/Roms.cs @@ -202,7 +202,8 @@ namespace gaseous_server.Classes } } - HasheousClient.WebApp.HttpHelper.AddHeader("X-API-Key", Config.MetadataConfiguration.HasheousAPIKey); + HasheousClient.WebApp.HttpHelper.APIKey = Config.MetadataConfiguration.HasheousAPIKey; + HasheousClient.WebApp.HttpHelper.ClientKey = Config.MetadataConfiguration.HasheousClientAPIKey; HasheousClient.Hasheous hasheousClient = new HasheousClient.Hasheous(); List metadataMatchList = new List(); metadataMatchList.Add(new MetadataMatch(HasheousClient.Models.MetadataSources.IGDB, platform.Slug, game.Slug)); diff --git a/gaseous-server/Controllers/V1.0/SystemController.cs b/gaseous-server/Controllers/V1.0/SystemController.cs index 74c9ce9..abaeaa3 100644 --- a/gaseous-server/Controllers/V1.0/SystemController.cs +++ b/gaseous-server/Controllers/V1.0/SystemController.cs @@ -73,6 +73,7 @@ namespace gaseous_server.Controllers [HttpGet] [Route("Version")] [ProducesResponseType(typeof(Dictionary), StatusCodes.Status200OK)] + [AllowAnonymous] public ActionResult GetSystemVersion() { Database db = new Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString); diff --git a/gaseous-server/gaseous-server.csproj b/gaseous-server/gaseous-server.csproj index 4d0020b..ed1edba 100644 --- a/gaseous-server/gaseous-server.csproj +++ b/gaseous-server/gaseous-server.csproj @@ -20,7 +20,7 @@ - + diff --git a/gaseous-server/wwwroot/pages/home.html b/gaseous-server/wwwroot/pages/home.html index 5927a03..7b9876a 100644 --- a/gaseous-server/wwwroot/pages/home.html +++ b/gaseous-server/wwwroot/pages/home.html @@ -1,6 +1,7 @@
-
+