Skip to content

Commit

Permalink
Close #6
Browse files Browse the repository at this point in the history
  • Loading branch information
liebki committed Aug 17, 2022
1 parent 77172f7 commit ba5d2ef
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 35 deletions.
59 changes: 47 additions & 12 deletions RawgNET/RawgAccessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ internal class RawgAccessManager
internal static Game RawgRequest(string name, string rawgkey, bool getAchievements)
{
Game? GameReturnValue = null;
Task<Game> QueryTryGame = Task.Run(() => QueryGame(name, rawgkey));
QueryTryGame.Wait();
if (object.Equals(QueryTryGame.Result.BackgroundImage, null))
Task<Game> GameQueryResult = Task.Run(() => QueryGame(name, rawgkey));
GameQueryResult.Wait();
if (object.Equals(GameQueryResult.Result.BackgroundImage, null))
{
Task<GameFallback> FallbackQueryTry = Task.Run(() => QueryFallback(name, rawgkey));
FallbackQueryTry.Wait();
if (FallbackQueryTry.Result.Redirect)
Task<GameFallback> GameFallbackquery = Task.Run(() => QueryFallback(name, rawgkey));
GameFallbackquery.Wait();
if (GameFallbackquery.Result.Redirect)
{
Task<Game> SecondQueryTryGame = Task.Run(() => QueryGame(FallbackQueryTry.Result.Slug, rawgkey));
QueryTryGame.Wait();
QueryTryGame = SecondQueryTryGame;
Task<Game> GameRequeryResult = Task.Run(() => QueryGame(GameFallbackquery.Result.Slug, rawgkey));
GameQueryResult.Wait();
GameQueryResult = GameRequeryResult;
}
}
if (!object.Equals(QueryTryGame.Result.BackgroundImage, null))
if (!object.Equals(GameQueryResult.Result.BackgroundImage, null))
{
GameReturnValue = QueryTryGame.Result;
GameReturnValue = GameQueryResult.Result;
if (object.Equals(GameReturnValue.Metacritic, null))
{
GameReturnValue.Metacritic = 0;
Expand All @@ -43,7 +43,7 @@ internal static Game RawgRequest(string name, string rawgkey, bool getAchievemen
if (getAchievements)
{
Task<GameAchievement> gameAchievementQuery = Task.Run(() => QueryAchievements(GameReturnValue.Slug, rawgkey));
QueryTryGame.Wait();
GameQueryResult.Wait();

if (gameAchievementQuery.Result.Count > 0)
{
Expand All @@ -61,6 +61,41 @@ internal static Game RawgRequest(string name, string rawgkey, bool getAchievemen
return GameReturnValue;
}

/// <summary>
/// A method, to see if a game exists in the first place
/// </summary>
/// <param name="name">Name of the game we'd like to query</param>
/// <param name="rawgkey">Your API-Key</param>
/// <returns></returns>
internal static bool RawgRequestGameExists(string name, string rawgkey)
{
Task<Game> GameQueryResult = Task.Run(() => QueryGame(name, rawgkey));
GameQueryResult.Wait();

if (object.Equals(GameQueryResult.Result.BackgroundImage, null))
{
Task<GameFallback> GameFallbackquery = Task.Run(() => QueryFallback(name, rawgkey));
GameFallbackquery.Wait();

if (GameFallbackquery.Result.Redirect)
{
Task<Game> GameRequeryResult = Task.Run(() => QueryGame(GameFallbackquery.Result.Slug, rawgkey));
GameQueryResult.Wait();
GameQueryResult = GameRequeryResult;

if (object.Equals(GameQueryResult.Result.BackgroundImage, null))
{
return false;
}
}
else
{
return false;
}
}
return true;
}

/// <summary>
/// Recursive method, to get every achievement of a game
/// </summary>
Expand Down
48 changes: 31 additions & 17 deletions RawgNET/RawgClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,46 @@ public void Dispose()
}

/// <summary>
/// The publicy available method that is called
/// Method to query if a game exists, where key and name are checked first
/// </summary>
/// <param name="gamename">Name of the game we'd like to query</param>
/// <param name="getAchievements">If we want to query for the achievements (takes a second longer)</param>
/// <returns></returns>
public Game GetGameData(string gamename, bool getAchievements)
/// <returns>A boolean, if the game exists or not</returns>
public bool IsGameExisting(string gamename)
{
Game game;
if (!object.Equals(options.APIKEY, null) && options.APIKEY.Length > 10)
if (object.Equals(options.APIKEY, null) || options.APIKEY.Length < 30 || options.APIKEY.Contains(' '))
{
if (!object.Equals(gamename, null) && gamename.Length > 1)
{
game = RawgAccessManager.RawgRequest(gamename, options.APIKEY, getAchievements);
}
else
if (object.Equals(gamename, null) || gamename.Length < 1)
{
NullReferenceException nullReferenceException = new("The name of the game is empty or in the wrong format!");
throw nullReferenceException;
NullReferenceException ErrorGame = new("The name of the game is empty or in the wrong format!");
throw ErrorGame;
}

NullReferenceException ErrorKey = new("ApiKey is empty or in the wrong format!");
throw ErrorKey;
}
else
return RawgAccessManager.RawgRequestGameExists(gamename, options.APIKEY);
}

/// <summary>
/// Method to query a game, where key and name are checked first
/// </summary>
/// <param name="gamename">Name of the game we'd like to query</param>
/// <param name="getAchievements">If we want to query for the achievements (takes a second longer)</param>
/// <returns>Returns a game object, which contains the data</returns>
public Game GetGameData(string gamename, bool getAchievements)
{
if (object.Equals(options.APIKEY, null) || options.APIKEY.Length < 30 || options.APIKEY.Contains(' '))
{
NullReferenceException nullReferenceException = new("ApiKey is empty or in the wrong format!");
throw nullReferenceException;
if (object.Equals(gamename, null) || gamename.Length < 1)
{
NullReferenceException ErrorGame = new("The name of the game is empty or in the wrong format!");
throw ErrorGame;
}

NullReferenceException ErrorKey = new("ApiKey is empty or in the wrong format!");
throw ErrorKey;
}
return game;
return RawgAccessManager.RawgRequest(gamename, options.APIKEY, getAchievements);
}
}
}
19 changes: 13 additions & 6 deletions RawgNetDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ private static void Main(string[] args)
string query = "gtav";
Console.WriteLine($"Querying for: {query}");

Game game = client.GetGameData(query, true);
if (!object.Equals(game, null))
if (client.IsGameExisting(query))
{
Console.WriteLine($"Output for: {game.Name} | {game.NameOriginal}\n");
Game game = client.GetGameData(query, true);
if (!object.Equals(game, null))
{
Console.WriteLine($"Output for: {game.Name} | {game.NameOriginal}\n");
}
Console.WriteLine($"Achievements {Environment.NewLine}--------------");
foreach (Result item in game.Achievements)
{
Console.WriteLine($"------ {Environment.NewLine} Name: {item.Name} {Environment.NewLine} Description: {item.Description} {Environment.NewLine} Image: {item.Image} {Environment.NewLine}");
}
}
Console.WriteLine($"Achievements {Environment.NewLine}--------------");
foreach (Result item in game.Achievements)
else
{
Console.WriteLine($"------ {Environment.NewLine} Name: {item.Name} {Environment.NewLine} Description: {item.Description} {Environment.NewLine} Image: {item.Image} {Environment.NewLine}");
Console.WriteLine("Game does not exist");
}
}
}
Expand Down

0 comments on commit ba5d2ef

Please sign in to comment.