public T Create <T>() where T : class, IUniqueData, new()
        {
            var uniqueData = Activator.CreateInstance <T>();

            uniqueData.InstanceId = _instanceIdGenerator.GenerateId();
            return(uniqueData);
        }
예제 #2
0
 public IMovement Create() => new Movement()
 {
     InstanceId = _instanceIdGenerator.GenerateId()
 };
예제 #3
0
        public WebCharacter PopulateCharacter(WebCharacter character, string sourceBaseUrl)
        {
            var populatedCharacter = new WebCharacter(character.Name, character.EscapedCharacterName, character.UniqueScraperTypes, character.PotentialScrapingNames);

            if (populatedCharacter.OwnerId != CharacterIds.RosalinaLuma || (populatedCharacter.OwnerId == CharacterIds.RosalinaLuma && sourceBaseUrl.Contains("Smash4")))
            {
                populatedCharacter.SourceUrl = sourceBaseUrl + populatedCharacter.EscapedCharacterName;
            }
            else if (populatedCharacter.OwnerId == CharacterIds.RosalinaLuma && sourceBaseUrl.Contains("Ultimate"))
            {
                populatedCharacter.SourceUrl = sourceBaseUrl + "Rosalina"; //hack, but having different names for same chars across games? come on.
            }
            populatedCharacter.CssKey = character.CssKey;

            const string srcAttributeKey  = "src";
            const string characterNameKey = "[charactername]";
            var          htmlParser       = _webServices.CreateParserFromSourceUrl(populatedCharacter.SourceUrl);
            string       displayNameHtml  = htmlParser.GetSingle(ScrapingConstants.XPathFrameDataVersion);

            string displayName = GetCharacterDisplayName(displayNameHtml);

            if (displayName == "Rosalina")
            {
                displayName = "Rosalina & Luma"; //see previously mentioned hack comment
            }

            string thumbnailPreparedUrl = Regex.Replace(populatedCharacter.SourceUrl, "([^/]*)$", string.Empty);
            var    thumbnailHtmlParser  = _webServices.CreateParserFromSourceUrl(thumbnailPreparedUrl);
            string thumbnailHtml        = thumbnailHtmlParser.GetSingle(ScrapingConstants.XPathThumbnailUrl.Replace(characterNameKey, character.DisplayName));

            string thumbnailUrl = string.Empty;

            if (string.IsNullOrEmpty(thumbnailHtml))
            {
                //try potential scraping names if there are any for the character
                foreach (string name in character.PotentialScrapingNames)
                {
                    thumbnailHtml = thumbnailHtmlParser.GetSingle(ScrapingConstants.XPathThumbnailUrl.Replace(characterNameKey, name));

                    if (!string.IsNullOrEmpty(thumbnailHtml))
                    {
                        thumbnailUrl = GetThumbnailUrl(srcAttributeKey, thumbnailHtml, populatedCharacter.SourceUrl);
                        break;
                    }
                }
            }
            else
            {
                thumbnailUrl = GetThumbnailUrl(srcAttributeKey, thumbnailHtml, populatedCharacter.SourceUrl);
            }

            //character details
            string mainImageUrl = htmlParser.GetAttributeFromSingleNavigable(srcAttributeKey, ScrapingConstants.XPathImageUrl);

            var    sourceUri = new Uri(populatedCharacter.SourceUrl);
            string baseUri   = sourceUri.GetLeftPart(UriPartial.Authority);

            if (!mainImageUrl.StartsWith(baseUri))
            {
                mainImageUrl = baseUri + mainImageUrl;
            }

            //color hex
            string colorTheme = string.Empty;

            if (!string.IsNullOrEmpty(populatedCharacter.CssKey))
            {
                colorTheme = _imageScrapingService.GetColorHexValue(populatedCharacter.CssKey).Result;
            }

            //movements
            var movements = _movementScraper.GetMovementsForCharacter(populatedCharacter);

            //moves
            var moves = _characterMoveScraper.ScrapeMoves(populatedCharacter);

            //attributes
            var attributeRows = new List <ICharacterAttributeRow>();

            foreach (var attributeScraper in _attributeScrapers)
            {
                string attributeName = string.Empty;
                if (attributeScraper.AttributeName == "RunSpeed")
                {
                    attributeName = "DashSpeed"; //grrr..
                }
                else
                {
                    attributeName = attributeScraper.AttributeName;
                }

                attributeScraper.SourceUrl = $"{sourceBaseUrl}{attributeName}";

                try
                {
                    var newAttributes = attributeScraper.Scrape(populatedCharacter);
                    attributeRows.AddRange(newAttributes);
                }
                catch (PageNotFoundException ex)
                {
                    Console.WriteLine($"Exception while running scraper: {attributeScraper.AttributeName} for '{character.Name}'{Environment.NewLine}{ex.Message}");
                }
            }

            //unique data
            var uniqueData = new List <object>();
            var uniqueDataScrapersForThisCharacter = GetUniqueDataScrapersForCharacter(populatedCharacter);

            foreach (var uniqueDataScraper in uniqueDataScrapersForThisCharacter)
            {
                uniqueData.AddRange(uniqueDataScraper.Scrape(populatedCharacter));
            }

            populatedCharacter.InstanceId       = _instanceIdGenerator.GenerateId();
            populatedCharacter.FullUrl          = populatedCharacter.SourceUrl;
            populatedCharacter.DisplayName      = displayName;
            populatedCharacter.ThumbnailUrl     = thumbnailUrl;
            populatedCharacter.MainImageUrl     = mainImageUrl;
            populatedCharacter.ColorTheme       = colorTheme;
            populatedCharacter.Movements        = movements;
            populatedCharacter.Moves            = moves;
            populatedCharacter.AttributeRows    = attributeRows;
            populatedCharacter.UniqueProperties = uniqueData;
            populatedCharacter.Game             = populatedCharacter.SourceUrl.Contains("Ultimate") ? Games.Ultimate : Games.Smash4;
            return(populatedCharacter);
        }
 public ICharacterAttributeRow CreateCharacterAttributeRow() => new CharacterAttributeRow()
 {
     InstanceId = _instanceIdGenerator.GenerateId()
 };