Exemplo n.º 1
0
        public async Task <ServicesResponse <List <ExpandedGame> > > Get(string gameName)
        {
            List <Game> searchResult = null;

            gameName = gameName.ToLower().Trim();
            gameName = Regex.Replace(gameName, @"\s+", " ");
            var cachePath = Path.Combine(IGDB.CacheDirectory, cacheDir, Playnite.Common.Paths.GetSafeFilename(gameName) + ".json");

            lock (CacheLock)
            {
                if (System.IO.File.Exists(cachePath))
                {
                    var fileInfo = new FileInfo(cachePath);
                    fileInfo.Refresh();
                    if ((DateTime.Now - fileInfo.LastWriteTime).TotalHours <= IGDB.SearchCacheTimeout)
                    {
                        searchResult = JsonConvert.DeserializeObject <List <Game> >(System.IO.File.ReadAllText(cachePath));
                    }
                }
            }

            if (searchResult == null)
            {
                var libraryStringResult = await IGDB.SendStringRequest("games", $"search \"{HttpUtility.UrlDecode(gameName)}\"; fields id; limit 40;");

                searchResult = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult);
                lock (CacheLock)
                {
                    Playnite.Common.FileSystem.PrepareSaveFile(cachePath);
                    System.IO.File.WriteAllText(cachePath, libraryStringResult);
                }
            }

            var finalResult = new List <ExpandedGame>();

            using (var gameController = new GameController(appSettings))
            {
                for (int i = 0; i < searchResult.Count; i++)
                {
                    Game result = null;
                    try
                    {
                        result = (await gameController.Get(searchResult[i].id)).Data;
                    }
                    catch (Exception e)
                    {
                        logger.Error(e, $"Failed to get game {searchResult[i].id}");
                        continue;
                    }

                    if (result.id == 0)
                    {
                        continue;
                    }

                    var xpanded = new ExpandedGame()
                    {
                        id   = result.id,
                        name = result.name,
                        first_release_date = result.first_release_date * 1000
                    };

                    if (result.alternative_names?.Any() == true)
                    {
                        xpanded.alternative_names = new List <AlternativeName>();
                        foreach (var nameId in result.alternative_names)
                        {
                            xpanded.alternative_names.Add((await AlternativeNameController.GetItem(nameId)).Data);
                        }
                    }

                    finalResult.Add(xpanded);
                }
            }

            return(new ServicesResponse <List <ExpandedGame> >(finalResult));
        }
Exemplo n.º 2
0
        public async Task <ServicesResponse <ExpandedGame> > Get(ulong gameId)
        {
            var game = (await new GameController().Get(gameId)).Data;

            if (game.id == 0)
            {
                new ServicesResponse <ExpandedGame>(new ExpandedGame());
            }

            var parsedGame = new ExpandedGame()
            {
                id                 = game.id,
                name               = game.name,
                slug               = game.slug,
                url                = game.url,
                summary            = game.summary,
                storyline          = game.storyline,
                popularity         = game.popularity,
                version_title      = game.version_title,
                category           = game.category,
                first_release_date = game.first_release_date,
                rating             = game.rating,
                aggregated_rating  = game.aggregated_rating,
                total_rating       = game.total_rating
            };

            if (game.alternative_names?.Any() == true)
            {
                parsedGame.alternative_names = new List <AlternativeName>();
                foreach (var nameId in game.alternative_names)
                {
                    parsedGame.alternative_names.Add((await AlternativeNameController.GetItem(nameId)).Data);
                }
            }

            if (game.involved_companies?.Any() == true)
            {
                parsedGame.involved_companies = new List <ExpandedInvolvedCompany>();
                foreach (var companyId in game.involved_companies)
                {
                    parsedGame.involved_companies.Add((await InvolvedCompanyController.GetItem(companyId)).Data);
                }
            }

            if (game.genres?.Any() == true)
            {
                parsedGame.genres_v3 = new List <Genre>();
                foreach (var genreId in game.genres)
                {
                    parsedGame.genres_v3.Add((await GenreController.GetItem(genreId)).Data);
                }
            }

            if (game.websites?.Any() == true)
            {
                parsedGame.websites = new List <Website>();
                foreach (var websiteId in game.websites)
                {
                    parsedGame.websites.Add((await WebsiteController.GetItem(websiteId)).Data);
                }
            }

            if (game.game_modes?.Any() == true)
            {
                parsedGame.game_modes_v3 = new List <GameMode>();
                foreach (var modeId in game.game_modes)
                {
                    parsedGame.game_modes_v3.Add((await GameModeController.GetItem(modeId)).Data);
                }
            }

            if (game.cover > 0)
            {
                parsedGame.cover_v3 = (await CoverController.GetItem(game.cover)).Data;
            }

            // fallback properties for 4.x
            parsedGame.cover              = parsedGame.cover_v3?.url;
            parsedGame.publishers         = parsedGame.involved_companies?.Where(a => a.publisher == true).Select(a => a.company.name).ToList();
            parsedGame.developers         = parsedGame.involved_companies?.Where(a => a.developer == true).Select(a => a.company.name).ToList();
            parsedGame.genres             = parsedGame.genres_v3?.Select(a => a.name).ToList();
            parsedGame.game_modes         = parsedGame.game_modes_v3?.Select(a => a.name).ToList();
            parsedGame.first_release_date = parsedGame.first_release_date * 1000;

            return(new ServicesResponse <ExpandedGame>(parsedGame));
        }
Exemplo n.º 3
0
        public async static Task <ExpandedGame> GetExpandedGame(ulong gameId)
        {
            var game = (await GameController.GetItem(gameId)).Data;

            if (game.id == 0)
            {
                new ExpandedGame();
            }

            var parsedGame = new ExpandedGame()
            {
                id                 = game.id,
                name               = game.name,
                slug               = game.slug,
                url                = game.url,
                summary            = game.summary,
                storyline          = game.storyline,
                popularity         = game.popularity,
                version_title      = game.version_title,
                category           = game.category,
                first_release_date = game.first_release_date * 1000,
                rating             = game.rating,
                aggregated_rating  = game.aggregated_rating,
                total_rating       = game.total_rating
            };

            if (game.alternative_names?.Any() == true)
            {
                parsedGame.alternative_names = new List <AlternativeName>();
                foreach (var nameId in game.alternative_names)
                {
                    parsedGame.alternative_names.Add((await AlternativeNameController.GetItem(nameId)).Data);
                }
            }

            if (game.involved_companies?.Any() == true)
            {
                parsedGame.involved_companies = new List <ExpandedInvolvedCompany>();
                foreach (var companyId in game.involved_companies)
                {
                    parsedGame.involved_companies.Add((await InvolvedCompanyController.GetItem(companyId)).Data);
                }
            }

            if (game.genres?.Any() == true)
            {
                parsedGame.genres_v3 = new List <Genre>();
                foreach (var genreId in game.genres)
                {
                    parsedGame.genres_v3.Add((await GenreController.GetItem(genreId)).Data);
                }
            }

            if (game.websites?.Any() == true)
            {
                parsedGame.websites = new List <Website>();
                foreach (var websiteId in game.websites)
                {
                    parsedGame.websites.Add((await WebsiteController.GetItem(websiteId)).Data);
                }
            }

            if (game.game_modes?.Any() == true)
            {
                parsedGame.game_modes_v3 = new List <GameMode>();
                foreach (var modeId in game.game_modes)
                {
                    parsedGame.game_modes_v3.Add((await GameModeController.GetItem(modeId)).Data);
                }
            }

            if (game.player_perspectives?.Any() == true)
            {
                parsedGame.player_perspectives = new List <PlayerPerspective>();
                foreach (var persId in game.player_perspectives)
                {
                    parsedGame.player_perspectives.Add((await PlayerPerspectiveController.GetItem(persId)).Data);
                }
            }

            if (game.cover > 0)
            {
                parsedGame.cover_v3 = (await CoverController.GetItem(game.cover)).Data;
            }

            if (game.artworks?.Any() == true)
            {
                parsedGame.artworks = new List <GameImage>();
                foreach (var artworkId in game.artworks)
                {
                    parsedGame.artworks.Add((await ArtworkController.GetItem(artworkId)).Data);
                }
            }

            if (game.screenshots?.Any() == true)
            {
                parsedGame.screenshots = new List <GameImage>();
                foreach (var screenshotId in game.screenshots)
                {
                    parsedGame.screenshots.Add((await ScreenshotController.GetItem(screenshotId)).Data);
                }
            }

            // fallback properties for 4.x
            parsedGame.cover      = parsedGame.cover_v3?.url;
            parsedGame.publishers = parsedGame.involved_companies?.Where(a => a.publisher == true).Select(a => a.company.name).ToList();
            parsedGame.developers = parsedGame.involved_companies?.Where(a => a.developer == true).Select(a => a.company.name).ToList();
            parsedGame.genres     = parsedGame.genres_v3?.Select(a => a.name).ToList();
            parsedGame.game_modes = parsedGame.game_modes_v3?.Select(a => a.name).ToList();
            return(parsedGame);
        }
Exemplo n.º 4
0
        public static async Task <List <ExpandedGame> > GetSearchResults(string searchString)
        {
            List <Game> searchResult = null;

            searchString = ModelsUtils.GetIgdbSearchString(searchString);
            var cachePath = Path.Combine(IGDB.CacheDirectory, cacheDir, Playnite.Common.Paths.GetSafeFilename(searchString) + ".json");

            lock (CacheLock)
            {
                if (System.IO.File.Exists(cachePath))
                {
                    var fileInfo = new FileInfo(cachePath);
                    fileInfo.Refresh();
                    if ((DateTime.Now - fileInfo.LastWriteTime).TotalHours <= IGDB.SearchCacheTimeout)
                    {
                        using (var fs = new FileStream(cachePath, FileMode.Open, FileAccess.Read))
                            using (var sr = new StreamReader(fs))
                                using (var reader = new JsonTextReader(sr))
                                {
                                    searchResult = jsonSerializer.Deserialize <List <Game> >(reader);
                                }
                    }
                }
            }

            if (searchResult == null)
            {
                var libraryStringResult = await IGDB.SendStringRequest("games", $"search \"{HttpUtility.UrlDecode(searchString)}\"; fields id; limit 40;");

                searchResult = JsonConvert.DeserializeObject <List <Game> >(libraryStringResult);
                lock (CacheLock)
                {
                    Playnite.Common.FileSystem.PrepareSaveFile(cachePath);
                    System.IO.File.WriteAllText(cachePath, libraryStringResult);
                }
            }

            var finalResult = new List <ExpandedGame>();

            for (int i = 0; i < searchResult.Count; i++)
            {
                Game result = null;
                try
                {
                    result = (await GameController.GetItem(searchResult[i].id)).Data;
                }
                catch (Exception e)
                {
                    logger.Error(e, $"Failed to get game {searchResult[i].id}");
                    continue;
                }

                if (result.id == 0)
                {
                    continue;
                }

                var xpanded = new ExpandedGame()
                {
                    id   = result.id,
                    name = result.name,
                    first_release_date = result.first_release_date * 1000
                };

                if (result.alternative_names?.Any() == true)
                {
                    xpanded.alternative_names = new List <AlternativeName>();
                    foreach (var nameId in result.alternative_names)
                    {
                        xpanded.alternative_names.Add((await AlternativeNameController.GetItem(nameId)).Data);
                    }
                }

                finalResult.Add(xpanded);
            }

            return(finalResult);
        }