public async Task <IActionResult> AutoLink([FromQuery] int start, [FromQuery] int take, [FromQuery] string auth = "")
        {
            if (auth != _config.AuthKey)
            {
                return(BadRequest(new Error
                {
                    ErrorName = "NotAuthorized",
                    ErrorMessage = "The auth key given doesn't match. Please check your key."
                }
                                  ));
            }
            _logger.LogInformation("Starting AutoLink...");
            // Get games in database
            using (var session = _db.store.OpenAsyncSession())
            {
                var query = await session
                            .Query <Game>()
                            .Where(x => x.ScreenScraperId == 0).OrderBy(x => x.ScrapEditorId, OrderingType.Long).Skip(start)
                            .Take(take).ToListAsync();

                Parallel.ForEach(query, new ParallelOptions {
                    MaxDegreeOfParallelism = 10
                }, (game) =>
                {
                    // Check on screenscraper the name of the game
                    var id = _api.GetGameId(game.ScrapInfos[0].Names[0].Value,
                                            game.ScrapInfos[0].Console, _api.GetDefaultUser());
                    if (id == long.MaxValue)
                    {
                        return;
                    }
                    session.Advanced.Patch(
                        game,
                        x => x.ScreenScraperId, id);
                    Thread.Sleep(250);
                });
                await session.SaveChangesAsync();

                _logger.LogInformation("Finished AutoLink !");
            }

            return(Ok(new { result = "Ok" }));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UploadGame([FromRoute] long id)
        {
            using (var session = _db.store.OpenAsyncSession())
            {
                //TODO: Add support for translators.
                var game = await session
                           .Query <Game>()
                           .FirstOrDefaultAsync(x => x.ScrapEditorId == id);

                if (game == null)
                {
                    return(NotFound(new Error
                    {
                        ErrorName = "NotFound",
                        ErrorMessage = "The document asked was not found."
                    }));
                }
                if (game.ScreenScraperId == 0)
                {
                    return(UnprocessableEntity(new Error
                    {
                        ErrorName = "GameNotLinked",
                        ErrorMessage = "The game is not linked to a ScreenScraper Id."
                    }));
                }
                // Save the choice
                if (game.SavedInfo.InternalId == 0)
                {
                    if (game.ScrapInfos.Count != 1)
                    {
                        return(UnprocessableEntity(new Error
                        {
                            ErrorName = "MultipleChoices",
                            ErrorMessage =
                                "There are multiple Scraps, so it's impossible to choose one. Select one before executing this command."
                        }));
                    }

                    game.SavedInfo = game.ScrapInfos.First();
                }
                // Downloading game data from ScreenScraper
                //TODO: Set the console ID for each console
                var ssgame = _api.GetScreenScraperInfo(game.ScreenScraperId, 91, _api.GetDefaultUser());
                //Check if changes needed

                //Description
                if (ssgame.Description != null && ssgame.Description.All(x => x.Region != "en"))
                {
                    _api.PublishText(
                        "description",
                        game.SavedInfo.Description.First().Value,
                        "EveryGameGoing via ScrapEditor",
                        game.ScreenScraperId,
                        _api.GetDefaultUser(),
                        lang: "en"
                        );
                }
                //Name
                if (ssgame.Names != null && ssgame.Names.All(x => x.Region != _manager.GetRegionId(game.SavedInfo.Names.First().Region)))
                {
                    _api.PublishText(
                        "name",
                        game.SavedInfo.Names.First().Value,
                        "EveryGameGoing via ScrapEditor",
                        game.ScreenScraperId,
                        _api.GetDefaultUser(),
                        _manager.GetRegionId(game.SavedInfo.Description.First().Region)
                        );
                }
                //Editor
                if (string.IsNullOrEmpty(ssgame.Editor))
                {
                    _api.PublishText(
                        "editeur",
                        game.SavedInfo.Editor,
                        "EveryGameGoing via ScrapEditor",
                        game.ScreenScraperId,
                        _api.GetDefaultUser()
                        );
                }
                //Developer
                if (string.IsNullOrEmpty(ssgame.Developer))
                {
                    _api.PublishText(
                        "developpeur",
                        game.SavedInfo.Developer,
                        "EveryGameGoing via ScrapEditor",
                        game.ScreenScraperId,
                        _api.GetDefaultUser()
                        );
                }
                //Players
                if (string.IsNullOrEmpty(ssgame.Developer) && !string.IsNullOrEmpty(game.SavedInfo.NbPlayers))
                {
                    _api.PublishText(
                        "players",
                        game.SavedInfo.NbPlayers,
                        "EveryGameGoing via ScrapEditor",
                        game.ScreenScraperId,
                        _api.GetDefaultUser()
                        );
                }
                // Release Date
                if (ssgame.ReleaseDate == null || ssgame.ReleaseDate.All(x => x.Region != _manager.GetRegionId(game.SavedInfo.Names.First().Region)))
                {
                    _api.PublishText(
                        "datessortie",
                        game.SavedInfo.ReleaseDate.First().Value,
                        "EveryGameGoing via ScrapEditor",
                        game.ScreenScraperId,
                        _api.GetDefaultUser(),
                        _manager.GetRegionId(game.SavedInfo.Description.First().Region)
                        );
                }
                game.IsUploadedToScreenScraper = true;
                await session.SaveChangesAsync();

                return(Ok(new
                {
                    result = "Ok"
                }));
            }
        }