public async Task GetProperYDKString_ReturnDecklistWithFilledFieldsAsync()
        {
            using (var dbInMemory = new YgoProAnalyticsDatabase(_getOptionsForSqlInMemoryTesting <YgoProAnalyticsDatabase>()))
            {
                dbInMemory.Database.EnsureCreated();
                var archetype = new Archetype(Archetype.Default, true);
                _addPSYFrameDriver(dbInMemory, archetype);
                _addMokeyMokeyKing(dbInMemory, archetype);
                _addChecksumDragon(dbInMemory, archetype);
                _addPankratops(dbInMemory, archetype);
                await dbInMemory.SaveChangesAsync();

                _converter = new YDKToDecklistConverter(dbInMemory, _downloaderMock.Object, _adminConfigMock.Object);
                var decklist = _converter.Convert(GetProperYDKString());
                decklist.Name      = "Test";
                decklist.Archetype = new Archetype(Archetype.Default, true);
                dbInMemory.Decklists.Add(decklist);
                await dbInMemory.SaveChangesAsync();

                var decklistFromDb = dbInMemory.Decklists.First();

                Assert.Multiple(() =>
                {
                    //tests if a beta card (Cheksum dragon with betaid:100336006) is converted to 94136469
                    Assert.IsNotNull(decklistFromDb.MainDeck.FirstOrDefault(x => x.PassCode == 94136469));
                    Assert.AreEqual(decklistFromDb.MainDeck, decklist.MainDeck);
                    Assert.AreEqual(decklistFromDb.ExtraDeck, decklist.ExtraDeck);
                    Assert.AreEqual(decklistFromDb.SideDeck, decklist.SideDeck);
                });
            }
        }
        public async Task DeckContainsBetaCard_OfficialIDAppears_ConvertCardToOfficial()
        {
            using (var dbInMemory = new YgoProAnalyticsDatabase(_getOptionsForSqlInMemoryTesting <YgoProAnalyticsDatabase>()))
            {
                dbInMemory.Database.EnsureCreated();
                _addPSYFrameDriver(dbInMemory);
                _addMokeyMokeyKing(dbInMemory);
                _addChecksumDragon(dbInMemory);
                _addPankratops(dbInMemory);
                var banlist = new Banlist("2019.10 TCG", 1);
                banlist.ForbiddenCards.Add(_checksumDragon);
                dbInMemory.Banlists.Add(banlist);
                await dbInMemory.SaveChangesAsync();

                var decklist = _converterMock.Object.Convert(GetProperYDKString());
                decklist.Name      = "Lunalight";
                decklist.Archetype = _defaultArchetype;
                dbInMemory.Decklists.Add(decklist);

                await dbInMemory.SaveChangesAsync();

                IBetaCardToOfficialConverter _converter = new BetaCardToOfficialConverter(dbInMemory, _adminConfigMock.Object, _downloaderMock.Object);
                await _converter.UpdateCardsFromBetaToOfficial();

                var decklistFromDb = dbInMemory.Decklists.First();

                Assert.Multiple(() =>
                {
                    //tests if a beta card (Cheksum dragon with betaid:100336006) is converted to 94136469
                    Assert.IsNotNull(decklistFromDb.MainDeck.FirstOrDefault(x => x.PassCode == 94136469));
                    Assert.IsNull(decklistFromDb.MainDeck.FirstOrDefault(x => x.PassCode == 100336006));
                });
            }
        }
Exemple #3
0
        private async Task <Dictionary <DateTime, List <DecklistWithName> > > _getUnzippedDecklists()
        {
            var unzippedDecklists = new Dictionary <DateTime, List <DecklistWithName> >();
            var listOfDecklists   = _fTPDownloader
                                    .DownloadListOfFilesFromFTP(_adminConfig.ServerDataEndpointURL + "/decks_saves/");
            AnalysisMetadata metaData = _getMetaData();

            DateTime dateOfNewestDecklistPack = metaData.LastDecklistsPackDate;

            foreach (string decklistZipName in listOfDecklists)
            {
                DateTime dateOfDecklistsPack = DateTime.ParseExact(_extractDate(decklistZipName, "decks_save_"),
                                                                   "dd MM yy",
                                                                   CultureInfo.InvariantCulture);
                if (dateOfDecklistsPack > metaData.LastDecklistsPackDate)
                {
                    string pathToDecklistsZip = await _fTPDownloader
                                                .DownloadDeckFromFTP(_adminConfig.ServerDataEndpointURL + "/decks_saves/" + decklistZipName);

                    if (dateOfDecklistsPack > dateOfNewestDecklistPack)
                    {
                        dateOfNewestDecklistPack = dateOfDecklistsPack;
                    }
                    unzippedDecklists.Add(
                        dateOfDecklistsPack,
                        _unzipper.GetDecksFromZip(pathToDecklistsZip)
                        );
                }
            }

            metaData.LastDecklistsPackDate = dateOfNewestDecklistPack;
            await _db.SaveChangesAsync();

            return(unzippedDecklists);
        }
Exemple #4
0
        public async Task NumberOfGamesFromOneDayAsync_ThereWas5GamesThatDay_WeGet5()
        {
            DateTime date       = new DateTime(1997, 04, 29);
            var      statistics = new ServerActivityStatistics(date)
            {
                NumberOfGames = 5
            };

            _db.ServerActivityStatistics.Add(statistics);
            await _db.SaveChangesAsync();

            Assert.AreEqual(5, await _analyzer.NumberOfGamesFromOneDayAsync(date));
        }
        public async Task UpdateWithoutSavingChanges_ServerActivityStatisticsSuccesfullyUpdated()
        {
            var duelLogs = new List <DuelLog>();

            duelLogs.Add(new DuelLog(new DateTime(2018, 4, 29), new DateTime(2018, 4, 29), 1, 1, "test", "filename.yrp"));
            duelLogs.Add(new DuelLog(new DateTime(2018, 5, 2), new DateTime(2018, 5, 2), 1, 1, "test", "filename.yrp"));
            duelLogs.Add(new DuelLog(new DateTime(2018, 5, 2), new DateTime(2018, 5, 2), 1, 1, "test", "filename.yrp"));
            duelLogs.Add(new DuelLog(new DateTime(2018, 5, 2), new DateTime(2018, 5, 2), 1, 1, "test", "filename.yrp"));
            duelLogs.Add(new DuelLog(new DateTime(2018, 5, 1), new DateTime(2018, 5, 1), 1, 1, "test", "filename.yrp"));
            duelLogs.Add(new DuelLog(new DateTime(2018, 5, 1), new DateTime(2018, 5, 1), 1, 1, "test", "filename.yrp"));

            await _updater.UpdateWithoutSavingChanges(duelLogs);

            await _dbInMemory.SaveChangesAsync();

            var date_2018_5_1_Statistics = _dbInMemory.ServerActivityStatistics
                                           .Where(x => x.FromDate.Date == new DateTime(2018, 5, 1))
                                           .FirstOrDefault();
            var date_2018_5_2_Statistics = _dbInMemory.ServerActivityStatistics
                                           .Where(x => x.FromDate.Date == new DateTime(2018, 5, 2))
                                           .FirstOrDefault();
            var date_2018_4_29_Statistics = _dbInMemory.ServerActivityStatistics
                                            .Where(x => x.FromDate.Date == new DateTime(2018, 4, 29))
                                            .FirstOrDefault();

            Assert.Multiple(() => {
                Assert.IsTrue(date_2018_5_1_Statistics?.NumberOfGames == 2);
                Assert.IsTrue(date_2018_5_2_Statistics?.NumberOfGames == 3);
                Assert.IsTrue(date_2018_4_29_Statistics?.NumberOfGames == 1);
            });
        }
Exemple #6
0
        public async Task GetArchetypeOfDecklist_DeckHasDefaultArchetype_DeckGetsDefaultArchetype()
        {
            using (var dbInMemory = new YgoProAnalyticsDatabase(_getOptionsForSqlInMemoryTesting <YgoProAnalyticsDatabase>()))
            {
                dbInMemory.Database.EnsureCreated();
                var archetype = new Archetype(Archetype.Default, true);
                nekrozOfBrionac = _NekrozofBrionac(archetype);
                nekrozMirror    = _NekrozMirror(archetype);
                herald          = _Herald(archetype);
                bookstone       = _ImpcantationBookStone(archetype);
                dbInMemory.Cards.AddRange(nekrozOfBrionac, nekrozMirror, herald, bookstone);
                await dbInMemory.SaveChangesAsync();

                var decklist = new Decklist(new List <Card> {
                    nekrozOfBrionac, nekrozOfBrionac, nekrozOfBrionac, bookstone, bookstone, bookstone
                }, new List <Card> {
                    herald, herald
                }, new List <Card>());

                _analyzer = new ArchetypeAndDecklistAnalyzer(dbInMemory);
                _analyzer.GetArchetypeOfTheDecklistWithStatistics(decklist, DateTime.Now);

                Assert.AreEqual(Archetype.Default, decklist.Archetype.Name);
            }
        }
Exemple #7
0
        public async Task GetArchetypeOfDecklist_DeckHas1MainArchetype_DeckGets1Archetype()
        {
            using (var dbInMemory = new YgoProAnalyticsDatabase(_getOptionsForSqlInMemoryTesting <YgoProAnalyticsDatabase>()))
            {
                dbInMemory.Database.EnsureCreated();
                var archetypeNekroz = new Archetype("Nekroz", true);
                var archetypeHerald = new Archetype("Herald", true);
                nekrozOfBrionac = _NekrozofBrionac(archetypeNekroz);
                nekrozMirror    = _NekrozMirror(archetypeNekroz);
                herald          = _Herald(archetypeHerald);
                dbInMemory.Cards.AddRange(nekrozOfBrionac, nekrozMirror, herald);
                await dbInMemory.SaveChangesAsync();

                var decklist = new Decklist(new List <Card> {
                    nekrozOfBrionac, nekrozOfBrionac, nekrozOfBrionac, nekrozOfBrionac, nekrozOfBrionac, nekrozOfBrionac, nekrozOfBrionac, nekrozOfBrionac, nekrozOfBrionac, nekrozMirror
                }, new List <Card> {
                    herald, herald
                }, new List <Card> {
                    herald
                });

                _analyzer = new ArchetypeAndDecklistAnalyzer(dbInMemory);
                _analyzer.GetArchetypeOfTheDecklistWithStatistics(decklist, DateTime.Now);

                Assert.AreEqual("Nekroz", decklist.Archetype.Name);
            }
        }
        public async Task UpdateBanlist_SourcesGiveUsValidData_DbContainValidNumberOfForbiddenAndLimitedAndSemiLimitedCards()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();
            var dbContextSqliteOptions =
                new DbContextOptionsBuilder <YgoProAnalyticsDatabase>()
                .UseSqlite(connection)
                .ConfigureWarnings(x => x.Ignore(RelationalEventId.QueryClientEvaluationWarning))
                .Options;

            using (var db = new YgoProAnalyticsDatabase(dbContextSqliteOptions))
            {
                db.Database.EnsureCreated();
                var archetype = new Archetype(Archetype.Default, true);
                _addChangeOfHeart(db, archetype);
                _addChickenGame(db, archetype);
                _addTerraforming(db, archetype);
                await db.SaveChangesAsync();

                var updater = new BanlistDataToBanlistUpdater(db, _banlistDataDownloaderMock.Object);
                await updater.UpdateBanlists("https://raw.githubusercontent.com/szefo09/updateYGOPro2/master/lflist.conf");

                await _verifyForTCGBanlist(db);
                await _verifyForOCGBanlist(db);
            }
        }
        /// <summary>
        /// Handle <see cref="DataFromYgoProServerRetrieved"/> event.
        /// </summary>
        /// <param name="notification">The event data.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        public async Task Handle(DataFromYgoProServerRetrieved notification, CancellationToken cancellationToken)
        {
            var convertedDuelLogsListsDictionary = notification.ConvertedDuelLogs;

            foreach (var duelLogsWithTimeFrom in convertedDuelLogsListsDictionary)
            {
                var duelLogs = duelLogsWithTimeFrom.Value;
                await _updater.UpdateWithoutSavingChanges(duelLogs);
            }

            await _db.SaveChangesAsync();
        }
        private async Task _analyze(
            KeyValuePair <DateTime, List <DuelLog> > duelLogsFromThePack,
            KeyValuePair <DateTime, List <DecklistWithName> > decklistsAsStringsWithFilenames)
        {
            var allDecksWhichWonFromThePack  = new List <Decklist>();
            var allDecksWhichLostFromThePack = new List <Decklist>();

            foreach (var duelLog in duelLogsFromThePack.Value)
            {
                if (!isBanlistOk(duelLog))
                {
                    continue;
                }

                try
                {
                    _handleDuelLogs(decklistsAsStringsWithFilenames,
                                    allDecksWhichWonFromThePack,
                                    allDecksWhichLostFromThePack,
                                    duelLog);
                }
                catch (UnknownBanlistException)
                {
                    continue;
                }
            }

            var decklistsWhichWonWithoutDuplicatesFromThePack = _analyzeDecklistsAndArchetypesAndRemoveDuplicates(
                allDecksWhichWonFromThePack,
                true);
            var decklistsWhichLostWithoutDuplicatesFromThePack = _analyzeDecklistsAndArchetypesAndRemoveDuplicates(
                allDecksWhichLostFromThePack,
                false);
            var allDecklistsFromThePack = _removeDuplicatesAndMerge(
                decklistsWhichWonWithoutDuplicatesFromThePack,
                decklistsWhichLostWithoutDuplicatesFromThePack);

            _updateDecklistsStatisticsAndAddNewDecksToDatabase(allDecklistsFromThePack);
            await _db.SaveChangesAsync();
        }
        /// <inheritdoc />
        public async Task <IEnumerable <Banlist> > UpdateBanlists(string url)
        {
            _banlists = _db.Banlists.ToList();
            List <Card> cards = _db.Cards.ToList();
            string      banlistDataAsString = await _banlistDataDownloader.DownloadBanlistFromWebsite(url);

            var            banlistDatas = banlistDataAsString.Replace("\r", String.Empty).Split("\n");
            List <Banlist> newBanlists  = new List <Banlist>();
            Banlist        banlist      = null;

            for (int i = 1; i < banlistDatas.Length; i++)
            {
                var line = banlistDatas[i];
                if (line.Length == 0)
                {
                    continue;
                }

                if (_isInformationAboutBanlistName(line))
                {
                    _ifThereIsAnyBanlistAddItToDbContext(banlist);
                    string banlistName = line.Substring(1);
                    if (_isBanlistAlreadyInDatabase(banlistName))
                    {
                        continue;
                    }

                    banlist = new Banlist(banlistName, _banlistNumberInLflist);
                    newBanlists.Add(banlist);
                    continue;
                }

                _checkIfWeAnalyzeForbiddenOrLimitedOrSemiLimitedCards(line);
                if (banlist != null && !_isInformationAboutCardCountLimitations(line))
                {
                    int cardPassCode = int.Parse(line.Substring(0, line.IndexOf(' ')));
                    var card         = cards
                                       .FirstOrDefault(x => x.PassCode == cardPassCode);
                    if (card == null)
                    {
                        continue;
                    }

                    _addCardToAppropriateBanlistSection(banlist, card);
                }
            }

            _ifThereIsAnyBanlistAddItToDbContext(banlist);
            await _db.SaveChangesAsync();

            return(newBanlists);
        }
Exemple #12
0
        public async Task GetByIdWithAllDataIncluded_ThereIsDecklistWithGivenId_WeGetDecklistWithAllDatas()
        {
            var banlist   = new Banlist("2019.11 TCG", 1);
            var archetype = new Archetype("Valid archetype", false);

            var bannedPendulumMonster = _helper.GetCard(archetype);

            bannedPendulumMonster.MonsterCard = _helper.GetMonsterCard(bannedPendulumMonster);
            bannedPendulumMonster.MonsterCard.PendulumMonsterCard = _helper.GetPendulumMonsterCard(bannedPendulumMonster.MonsterCard);
            banlist.ForbiddenCards.Add(bannedPendulumMonster);

            var limitedLinkMonster = _helper.GetCard(archetype);

            limitedLinkMonster.MonsterCard = _helper.GetMonsterCard(limitedLinkMonster);
            limitedLinkMonster.MonsterCard.LinkMonsterCard = _helper.GetLinkMonsterCard(limitedLinkMonster.MonsterCard);
            banlist.LimitedCards.Add(limitedLinkMonster);

            var decklist = _helper.GetValidDecklistWithStatistics(archetype);

            decklist.MainDeck.Add(bannedPendulumMonster);
            decklist.ExtraDeck.Add(limitedLinkMonster);

            _db.Archetypes.Add(archetype);
            _db.Banlists.Add(banlist);
            _db.Cards.Add(bannedPendulumMonster);
            _db.Cards.Add(limitedLinkMonster);
            _db.Decklists.Add(decklist);

            await _db.SaveChangesAsync();

            _initService();

            var decklistFromDb = await _decklistService.GetByIdWithAllDataIncluded(1);

            Assert.Multiple(() => {
                Assert.NotZero(decklistFromDb.Archetype.Name.Length);
                Assert.NotZero(decklistFromDb.DecklistStatistics.Count);
                Assert.NotZero(decklistFromDb.DecklistStatistics.Count);
                Assert.NotNull(decklistFromDb.MainDeck.FirstOrDefault()?.Archetype);
                Assert.NotNull(decklistFromDb.MainDeck.FirstOrDefault()?.MonsterCard.PendulumMonsterCard);
                Assert.NotNull(decklistFromDb.ExtraDeck.FirstOrDefault()?.MonsterCard.LinkMonsterCard);
                Assert.NotZero(decklistFromDb.MainDeck.FirstOrDefault().BanlistsWhereThisCardIsForbidden.Count);
                Assert.NotZero(decklistFromDb.ExtraDeck.FirstOrDefault().BanlistsWhereThisCardIsLimited.Count);
            });

            Assert.IsNotNull("");
        }
Exemple #13
0
        public async Task GetPureArchetypeListWithIdsAndNamesAsNoTrackingFromCache_WeHaveOnePureArchetypeInDb_WeGetOneValidDTO()
        {
            var cacheMock  = new Mock <IMemoryCache>();
            var configMock = new Mock <IAdminConfig>();

            using (var db = new YgoProAnalyticsDatabase(SqlInMemoryHelper.SqlLiteOptions <YgoProAnalyticsDatabase>()))
            {
                await db.Database.EnsureCreatedAsync();

                db.Archetypes.Add(new Archetype(Archetype.Default, true));
                await db.SaveChangesAsync();

                var archetypeService = new ArchetypeService(db, cacheMock.Object, configMock.Object);

                var resultDto = (await archetypeService.GetPureArchetypeListWithIdsAndNamesAsNoTrackingFromCache(true))
                                .First();

                Assert.IsTrue(resultDto.Name == Archetype.Default);
            }
        }
Exemple #14
0
        public async Task GetPureArchetypeListWithIdsAndNamesAsNoTrackingFromCache_WeHaveOnePureArchetypeInDbAndTwoNonPure_WeGetOneDto()
        {
            var cacheMock  = new Mock <IMemoryCache>();
            var configMock = new Mock <IAdminConfig>();

            using (var db = new YgoProAnalyticsDatabase(SqlInMemoryHelper.SqlLiteOptions <YgoProAnalyticsDatabase>()))
            {
                await db.Database.EnsureCreatedAsync();

                db.Archetypes.Add(new Archetype(Archetype.Default, true));
                db.Archetypes.Add(new Archetype("NotPure", false));
                db.Archetypes.Add(new Archetype("NotPure2", false));
                await db.SaveChangesAsync();

                var archetypeService = new ArchetypeService(db, cacheMock.Object, configMock.Object);

                var numberOfDtos = (await archetypeService.GetPureArchetypeListWithIdsAndNamesAsNoTrackingFromCache(true))
                                   .Count();

                Assert.AreEqual(1, numberOfDtos);
            }
        }
        /// <summary>
        /// Updates the cards and archetypes.
        /// </summary>
        /// <param name="URL">The URL.</param>
        /// <exception cref="DbUpdateException"></exception>
        /// <exception cref="DbUpdateConcurrencyException"></exception>
        public async Task UpdateCardsAndArchetypes(string URL)
        {
            _archetypes = _db.Archetypes.ToList();
            _cards      = _db.Cards.ToList();
            string cardsData = await _cardsDataDownloader.DownloadCardsFromWebsite(URL);

            JToken cardsDataList = (JsonConvert.DeserializeObject <JArray>(cardsData)).First;

            foreach (JObject item in cardsDataList.Children <JObject>())
            {
                if (_cardAlreadyExistInOurDatabase(item))
                {
                    continue;
                }

                string    type = item.Value <string>("type").ToUpper();
                Archetype archetype;
                if (item.GetValue("archetype").ToString() == string.Empty)
                {
                    archetype = _getArchetype(Archetype.Default);
                }
                else
                {
                    archetype = _getArchetype(item.Value <string>("archetype"));
                }

                if (type.Contains("MONSTER"))
                {
                    _addMonsterProperties(type, archetype, item);
                }

                _addBasicCardProperties(item, archetype);
                _db.Cards.Add(_cardBuilder.Build());
            }

            await _db.SaveChangesAsync();
        }
Exemple #16
0
        /// <inheritdoc />
        public async Task UpdateCardsFromBetaToOfficial()
        {
            await LoadBetaCardsList(_cardsDataDownloader, _adminConfig);

            if (_db.Cards.Count() == 0)
            {
                return;
            }

            foreach (var cardData in betaCardsList)
            {
                int officialPassCode = cardData.OfficialPassCode;
                int betaPassCode     = cardData.BetaPassCode;
                if (betaPassCode != 0 || officialPassCode != 0)
                {
                    Card officialCard = await _GetCardWithSpecificPasscodeWithJoinsIfItExistsInDatabase(officialPassCode);

                    if (officialCard != null)
                    {
                        Card betaCard = await _GetCardWithSpecificPasscodeWithJoinsIfItExistsInDatabase(betaPassCode);

                        if (betaCard != null)
                        {
                            foreach (var banlist in betaCard.BanlistsWhereThisCardIsForbidden)
                            {
                                if (officialCard.BanlistsWhereThisCardIsForbidden.Contains(banlist))
                                {
                                    continue;
                                }
                                officialCard.BanlistsWhereThisCardIsForbidden.Add(banlist);
                            }

                            foreach (var banlist in betaCard.BanlistsWhereThisCardIsLimited)
                            {
                                if (officialCard.BanlistsWhereThisCardIsLimited.Contains(banlist))
                                {
                                    continue;
                                }
                                officialCard.BanlistsWhereThisCardIsLimited.Add(banlist);
                            }

                            foreach (var banlist in betaCard.BanlistsWhereThisCardIsSemiLimited)
                            {
                                if (officialCard.BanlistsWhereThisCardIsSemiLimited.Contains(banlist))
                                {
                                    continue;
                                }
                                officialCard.BanlistsWhereThisCardIsSemiLimited.Add(banlist);
                            }

                            foreach (var decklist in betaCard.DecksWhereThisCardIsInMainDeck)
                            {
                                if (officialCard.DecksWhereThisCardIsInMainDeck.Contains(decklist))
                                {
                                    continue;
                                }
                                officialCard.DecksWhereThisCardIsInMainDeck.Add(decklist);
                            }

                            foreach (var decklist in betaCard.DecksWhereThisCardIsInSideDeck)
                            {
                                if (officialCard.DecksWhereThisCardIsInSideDeck.Contains(decklist))
                                {
                                    continue;
                                }
                                officialCard.DecksWhereThisCardIsInSideDeck.Add(decklist);
                            }

                            foreach (var decklist in betaCard.DecksWhereThisCardIsInExtraDeck)
                            {
                                if (officialCard.DecksWhereThisCardIsInExtraDeck.Contains(decklist))
                                {
                                    continue;
                                }
                                officialCard.DecksWhereThisCardIsInExtraDeck.Add(decklist);
                            }
                            _db.Cards.Remove(betaCard);
                        }
                    }
                    else
                    {
                        Card betaCard = await _GetCardWithSpecificPasscodeWithJoinsIfItExistsInDatabase(betaPassCode);

                        if (betaCard != null)
                        {
                            betaCard.ChangePassCode(officialPassCode);
                        }
                    }
                }
            }

            await _db.SaveChangesAsync();
        }
        /// <inheritdoc />
        public async Task UpdateAndSaveChanges(IEnumerable <DuelLog> duelLogsFromOneFile)
        {
            await UpdateWithoutSavingChanges(duelLogsFromOneFile);

            await _db.SaveChangesAsync();
        }