Пример #1
0
        public int AddImportBatch(PgnGame[] games)
        {
            int created = 0;

            _chessGamesDbContext.RunWithExtendedTimeout(() =>
            {
                _chessGamesDbContext.Database.ExecuteSqlCommand("TRUNCATE TABLE [GameImports]");

                games.ToList().ForEach(pgnGame =>
                {
                    var site        = GetOrCreateCachedEntity <Site>(pgnGame.Site);
                    var whitePlayer = GetOrCreateCachedEntity <Player>(pgnGame.White);
                    var blackPlayer = GetOrCreateCachedEntity <Player>(pgnGame.Black);
                    var @event      = GetOrCreateCachedEntity <Event>(pgnGame.Event);

                    var game = new GameImport
                    {
                        Event    = @event,
                        Black    = blackPlayer,
                        Site     = site,
                        White    = whitePlayer,
                        Round    = pgnGame.Round,
                        MoveText = NormaliseMoveText(pgnGame),
                        Date     = pgnGame.Date.ToString(),
//                        Result = pgnGame.Result // TODO: Mappers
                    };
                    _chessGamesDbContext.GameImports.Attach(game);
                });
                _chessGamesDbContext.SaveChanges();

                var sql = MergeNewGamesSql;
                created = _chessGamesDbContext.Database.ExecuteSqlCommand(sql);
            }, TimeSpan.FromMinutes(5));
            return(created);
        }
Пример #2
0
        private void GetGames_doWork(IProgress <int> progress)
        {
            string json = null;

            GogData.GamesOwned result = null;
            try
            {
                json   = webClient.DownloadString("https://embed.gog.com/user/data/games");
                result = JsonConvert.DeserializeObject <GogData.GamesOwned>(json);
                MessageBox.Show(result.owned.Count.ToString() + " Games in account.");
            }
            catch (Exception e)
            {
                handleApiExceptions(e, json);
                return;
            }

            int    skip          = 0;
            int    ok            = 0;
            int    j             = 0;
            string currentGameId = "";
            int    items         = result.owned.Count;

            foreach (string gameId in result.owned)
            {
                try
                {
                    currentGameId = gameId;

                    GameImport gameFound = gamesFound.Find(x => x._gogId.Equals(gameId));
                    if (gameFound != null)
                    {
                        if (skipGogId.Contains(gameId) || skipTitle.Contains(gameFound.title))
                        {
                            gamesFound.Remove(gameFound);
                            skip++;
                        }
                        continue;
                    }
                    else if (skipGogId.Contains(gameId))
                    {
                        skip++;
                        continue;
                    }


                    if (progress != null)
                    {
                        progress.Report((j * 100) / items);
                    }
                    j++;

                    json = webClient.DownloadString("https://embed.gog.com/account/gameDetails/"
                                                    + gameId.ToString() + ".json");

                    if (json.Equals("[]") || json.StartsWith("<"))
                    {
                        continue;
                    }
                    GogData.GameDetails details = JsonConvert.DeserializeObject <GogData.GameDetails>(json);

                    if (skipTitle.Contains(details.title))
                    {
                        skip++;
                        continue;
                    }

                    // get aditional details
                    //json = webClient.DownloadString("http://api.gog.com/products/"
                    //    + gameId.ToString() + "?expand=downloads,expanded_dlcs,description,screenshots,videos,related_products,changelog");
                    //GogData.MoreGameDetails moreDetails = JsonConvert.DeserializeObject<GogData.MoreGameDetails>(json);

                    List <GameDownload> downloads = new List <GameDownload>();

                    foreach (List <object> o in details.downloads)
                    {
                        string language = (string)o[0];
                        GogData.DownloadBySystem downloadsForLanguage =
                            JsonConvert.DeserializeObject <GogData.DownloadBySystem>(((JObject)o[1]).ToString());
                        List <GogData.Download> downloadsForWindows = downloadsForLanguage.windows;

                        foreach (GogData.Download d in downloadsForWindows)
                        {
                            downloads.Add(new GameDownload
                            {
                                _language = language,
                                _version  = d.version,
                                _link     = d.manualUrl,
                                _name     = d.name
                            });
                        }
                    }

                    if (downloads.Count == 0)
                    {
                        continue;
                    }

                    gamesFound.Add(new GameImport
                    {
                        _title            = details.title,
                        _importTitle      = details.title,
                        _import           = true,
                        _gameDetails      = details,
                        _downloads        = downloads,
                        _selectedDownload = downloads[0],
                        _gogId            = gameId
                    });
                    ok++;
                }
                catch (Exception e)
                {
                    if (e is JsonReaderException || e is JsonSerializationException)
                    {
                        MessageBox.Show("Error parsing game with id " + gameId + ".\n\n" +
                                        e.GetType().ToString() + ": " + e.Message);
                    }
                    else
                    {
                        handleApiExceptions(e, null);
                    }
                    continue;
                }
            }
            MessageBox.Show(ok.ToString() + "new games found, " + skip.ToString() + " skipped.");
        }
Пример #3
0
        private void GetGames_doWork(IProgress <int> progress, IProgress <String> msg)
        {
            string json = null;

            // get ids of agmes owned
            GogDataStructures.GamesOwned userGames = null;
            try
            {
                json      = webClient.DownloadString("https://embed.gog.com/user/data/games");
                userGames = JsonConvert.DeserializeObject <GogDataStructures.GamesOwned>(json);
            }
            catch (Exception e)
            {
                handleApiExceptions(e, json);
                return;
            }

            if (userGames == null || userGames.owned == null)
            {
                MessageBox.Show("Could not retrieve owned games.");
                return;
            }
            if (userGames.owned.Count == 0)
            {
                MessageBox.Show("No games found.");
                return;
            }

            // Debug Code: reduce number of games for faster testing
#if DEBUG
            reduceResults(userGames, 10);
#endif

            // get game info
            int noGamesOwned  = userGames.owned.Count;
            int noGamesToScan = noGamesOwned - gamesFound.Count;
            int counter       = 0;
            int skipped       = 0;
            int failure       = 0;

            string currentGameId = "";
            string currentGame   = "Unknown";

            Dictionary <string, string> errorMessages = new Dictionary <string, string>();

            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                Error = (sender, e) => handleParsingError(sender, e, errorMessages, currentGame),
            };

            foreach (string gameId in userGames.owned)
            {
                string messageText = "Game " + (counter + 1).ToString() + " of " + noGamesOwned.ToString();
                // report progress
                if (progress != null)
                {
                    progress.Report((counter * 100) / noGamesToScan);
                }

                currentGameId = gameId;

                // don't scan games already found
                GameImport gameFound = gamesFound.Find(x => x._gogId.Equals(gameId));
                if (gameFound != null)
                {
                    // if set to skip, also remove game from games already found
                    if (skipGogId.Contains(gameId) || skipTitle.Contains(gameFound.title))
                    {
                        gamesFound.Remove(gameFound);
                    }
                    messageText += ": " + gameFound.title;
                    if (msg != null)
                    {
                        msg.Report(messageText);
                    }
                    continue;
                }
                counter++;

                // skip games by id
                if (skipGogId.Contains(gameId))
                {
                    skipped++;
                    messageText = "Skipping " + messageText;
                    if (msg != null)
                    {
                        msg.Report(messageText);
                    }
                    continue;
                }

                GogDataStructures.GameDetails details = null;

                try
                {
                    json = webClient.DownloadString("https://embed.gog.com/account/gameDetails/"
                                                    + gameId.ToString() + ".json");

                    if (json == null || json.Equals("[]"))
                    {
                        appendError(errorMessages, "Game with Id " + gameId.ToString(), "No game information found (skip game).");
                        failure++;
                        messageText += ": Unknown";
                        if (msg != null)
                        {
                            msg.Report(messageText);
                        }
                        continue;
                    }

                    // try to get game title
                    JsonTextReader reader     = new JsonTextReader(new StringReader(json));
                    bool           titleFound = false;
                    while (!titleFound)
                    {
                        reader.Read();
                        if (reader.TokenType == JsonToken.PropertyName && reader.Value != null && reader.Value.Equals("title"))
                        {
                            titleFound = true;
                            break;
                        }
                    }
                    reader.Read();
                    if (reader.TokenType != JsonToken.String || reader.Value == null || reader.Value.Equals(""))
                    {
                        titleFound = false;
                    }
                    if (!titleFound)
                    {
                        appendError(errorMessages, "Game with Id " + gameId.ToString(), "Invalid game information: game has no title (skip game).");
                        failure++;
                        messageText += ": Unknown";
                        if (msg != null)
                        {
                            msg.Report(messageText);
                        }
                        continue;
                    }
                    else
                    {
                        currentGame = reader.Value.ToString();
                    }

                    // report curent game
                    messageText += ": " + currentGame;
                    if (msg != null)
                    {
                        msg.Report(messageText);
                    }

                    // skip duplicate games by title
                    if (skipTitle.Contains(currentGame))
                    {
                        skipped++;
                        continue;
                    }

                    // parse game details
                    details = JsonConvert.DeserializeObject <GogDataStructures.GameDetails>(json, settings);
                }
                catch (Exception e)
                {
                    if (e is JsonReaderException || e is JsonSerializationException)
                    {
                        appendError(errorMessages, currentGame, "Error parsing game with id " + gameId + " (skip game): " + e.ToString());
                        failure++;
                        continue;
                    }
                    else if (e is NullReferenceException)
                    {
                        appendError(errorMessages, currentGame, "Error parsing object " + e.Source + " (skip game).");
                        failure++;
                        continue;
                    }
                }

                if (details == null)
                {
                    appendError(errorMessages, currentGame, "Could not retrieve game details (skip game).");
                    failure++;
                    continue;
                }

                Debug.Assert(details.title.Equals(currentGame));

                // get aditional details
                //json = webClient.DownloadString("http://api.gog.com/products/"
                //    + gameId.ToString() + "?expand=downloads,expanded_dlcs,description,screenshots,videos,related_products,changelog");
                //GogData.MoreGameDetails moreDetails = JsonConvert.DeserializeObject<GogData.MoreGameDetails>(json);

                // get Downloads
                List <GameDownload> downloads = null;
                if (mode.Equals(Mode.linkToDownload))
                {
                    downloads = getDownloads(details);
                    if (downloads == null || downloads.Count == 0)
                    {
                        appendError(errorMessages, currentGame, "No Downloads found (skip game).");
                        failure++;
                        continue;
                    }
                }

                // add game to list of found games (stored during launchbox session)
                gamesFound.Add(new GameImport
                {
                    _title            = currentGame,
                    _importTitle      = details.title,
                    _import           = true,
                    _gameDetails      = details,
                    _downloads        = downloads,
                    _selectedDownload = downloads != null ? downloads[0] : null,
                    _gogId            = gameId
                });
            }

            // show errors
            if (errorMessages.Count > 0)
            {
                this.errorForm = new ErrorForm(userGames.owned.Count.ToString() + " games found, " + gamesFound.Count.ToString() + " ready for import, " + skipped.ToString() + " skipped, "
                                               + failure.ToString() + " failed to scan, " + (errorMessages.Count - failure).ToString() + " scanned with errors.", errorMessages);
            }
            else
            {
                MessageBox.Show(gamesFound.Count.ToString() + " games found, " + skipped.ToString() + " skipped.");
            }
        }