예제 #1
0
        public async Task ImportAsync()
        {
            var sw = new Stopwatch();

            try
            {
                sw.Start();
                ImportProgressChanged?.Invoke($"Starting Bulk Frontend import for frontend path: {FrontEnd.FePath}");
                //_uow.EnsureCreated(); //Don't need this run migrate instead in context

                ImportProgressChanged?.Invoke("populating systems");
                IEnumerable <GameSystem> systems = await ImportSystemsAsync();

                ImportProgressChanged?.Invoke("populated systems");

                int sysImportCount = 0; int failedSysImportCount = 0;
                int gameImportCount = 0; int failedGameImportCount = 0;
                foreach (var system in systems)
                {
                    try
                    {
                        ImportProgressChanged?.Invoke($"populatingsystem: {system.Name}");

                        //Get an existing system or insert one
                        var existingSystemId = (await _uow.GamingSystemRepository
                                                .GetAsync(x => x.Name == system.Name)).FirstOrDefault()?.Id;

                        //Get games for this system
                        IEnumerable <Game> games = null;
                        ImportProgressChanged?.Invoke($"populating games for: {system.Name}");
                        try { games = await FrontEnd.GetGamesAsync(system.Name); }
                        catch (Exception ex)
                        {
                            ImportProgressChanged?.Invoke($"no games / database exists for: {system.Name}. {ex.Message}");
                            failedGameImportCount++;
                            continue;
                        }

                        //Favorites
                        IEnumerable <string> favorites = null;
                        try { favorites = await FrontEnd.GetFavoritesAsync(system.Name); }
                        catch (Exception ex) { ImportProgressChanged?.Invoke($"Favorites Error: {ex.Message}"); };

                        //Rocketlauncher Stats
                        IEnumerable <Stat> systemGameStats = null;
                        try { systemGameStats = await _rocketLauncher.GetStatsAsync(system.Name); }
                        catch (Exception ex) { ImportProgressChanged?.Invoke($"Stats Error: {ex.Message}"); }

                        ImportProgressChanged?.Invoke($"inserting games: {system.Name}");
                        int gameCount = 0;
                        int i         = 0;

                        //await ImportGenresAsync(games);
                        //await ImportManufacturersAsync(games);

                        foreach (var game in games)
                        {
                            if (existingSystemId != null)
                            {
                                game.SystemId = existingSystemId.HasValue ? existingSystemId.Value : 0;
                                game.System   = null;
                            }

                            //Add or find genre
                            int?genreId = (await _uow.GenreRepository.GetAsync(x => x.Name == game.Genre.Name)).FirstOrDefault()?.Id;
                            if (genreId.HasValue)
                            {
                                game.GenreId = genreId.Value;
                                game.Genre   = null;
                            }

                            //Add or find manufacturer
                            int?manuId = (await _uow.ManufacturerRepository.GetAsync(x => x.Name == game.Manufacturer.Name)).FirstOrDefault()?.Id;
                            if (manuId.HasValue)
                            {
                                game.ManufacturerId = manuId.Value;
                                game.Manufacturer   = null;
                            }

                            //Is Favorite?
                            game.Favourite = !string.IsNullOrWhiteSpace(favorites?.FirstOrDefault(x => x == game.FileName));

                            //Has RL stats
                            var rlGameStat = systemGameStats?.FirstOrDefault(x => x.Rom == game.FileName);
                            if (rlGameStat != null)
                            {
                                game.LastPlayed  = rlGameStat.LastTimePlayed;
                                game.TimesPlayed = rlGameStat.TimesPlayed;
                                game.TimePlayed  = rlGameStat.TotalTimePlayed;
                                ImportProgressChanged?.Invoke($"Added stats from Rockelauncher: {game.FileName}");
                            }

                            //Insert game
                            ImportProgressChanged?.Invoke($"inserting game: {game.FileName} - {game.Description}, is favorite {game.Favourite}");
                            await _uow.GamesRepository.InsertAsync(game);

                            gameCount++;
                            i++;

                            try
                            {
                                await _uow.SaveAsync();

                                gameImportCount++;
                            }
                            catch (DbUpdateException dbupdateEx)
                            {
                                if (dbupdateEx.Entries?.Count > 0)
                                {
                                    var errGame = dbupdateEx.Entries?[0].Entity as Game;
                                    ImportProgressChanged?.Invoke($"Exception: {errGame?.FileName} {errGame?.System.Name}");
                                }
                                _uow.GamesRepository.Delete(game);
                                ImportProgressChanged?.Invoke($"{dbupdateEx.Message}");
                                ImportProgressChanged?.Invoke($"{dbupdateEx.InnerException?.Message}");
                            }
                        }

                        //await _uow.SaveAsync();
                        i = 0;
                        sysImportCount++;
                        ImportProgressChanged?.Invoke($"System imported: {gameCount} entries");
                        _uow.DetatchTrackedEntites();
                    }
                    catch (Exception ex)
                    {
                        failedSysImportCount++;
                        ImportProgressChanged?.Invoke($"{ex.Message}");
                        continue;
                    }
                }

                ImportProgressChanged?.Invoke($"import completed: Systems / Failed: {sysImportCount}:{failedSysImportCount}, " +
                                              $"Games / Failed: {gameImportCount} : {failedGameImportCount}");
            }
            catch (Exception ex)
            {
                ImportProgressChanged?.Invoke($"{ex.Message}");
                ImportProgressChanged?.Invoke($"{ex.InnerException?.Message}");
                throw;
            }
            finally
            {
                sw.Stop();
                ImportProgressChanged?.Invoke($"Completed Time: {sw.Elapsed.ToString()}");
            }
        }
예제 #2
0
        public async Task GetRocketlaunchGameStats(string systemName, int expectedCount)
        {
            var stats = await _rl.GetStatsAsync(systemName);

            Assert.True(stats?.Count() == expectedCount);
        }