public async Task <IActionResult> MassUpload([FromRoute] int nb, [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."
                }
                                  ));
            }
            // Query games
            using (var session = _db.store.OpenAsyncSession())
            {
                var query = await session.Query <Game>().Where(x => x.ScreenScraperId != 0 && x.IsUploadedToScreenScraper == false)
                            .OrderBy(x => x.ScrapEditorId, OrderingType.Long)
                            .Take(nb).ToListAsync();

                foreach (var game in query)
                {
                    session.Advanced.IgnoreChangesFor(game);
                    //TODO: Add support for translators.
                    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"))
                    {
                        if (game.SavedInfo.Description.First().Value != "No short description is available for this item yet. ")
                        {
                            _api.PublishText(
                                "description",
                                game.SavedInfo.Description.First().Value,
                                "EveryGameGoing via ScrapEditor",
                                game.ScreenScraperId,
                                _api.GetDefaultUser(),
                                lang: "en"
                                );
                        }
                    }

                    //Name
                    if (ssgame.Description == 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.GetRegionIdName(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.NbPlayers) && !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)
                            );
                    }
                    session.Advanced.Patch <Game, bool>(game.Id, x => x.IsUploadedToScreenScraper, true);
                }
                await session.SaveChangesAsync();
            }
            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"
                }));
            }
        }