Exemple #1
0
 public void EquipModifier(ModifierPattern modifier)
 {
     if (currentModifier != null)
     {
         inventory.Add(currentModifier);
     }
     currentModifier = modifier;
     inventory.Remove(modifier);
 }
        private void ParseMod(List <Modifier> mods, string text, ModifierPattern data, Match result, bool negative = false)
        {
            var modifier = new Modifier()
            {
                Index    = result.Index,
                Id       = data.Metadata.Id,
                Text     = data.Metadata.Text,
                Category = !data.Metadata.Id.StartsWith("explicit") ? data.Metadata.Category : null,
            };

            if (result.Groups.Count > 1)
            {
                for (var index = 1; index < result.Groups.Count; index++)
                {
                    if (double.TryParse(result.Groups[index].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var parsedValue))
                    {
                        var modifierText = modifier.Text;
                        if (negative)
                        {
                            parsedValue *= -1;
                            modifierText = IncreasedPattern.Replace(modifierText, gameLanguageProvider.Language.ModifierReduced);
                        }

                        modifier.Values.Add(parsedValue);
                        modifier.Text = ParseHashPattern.Replace(modifierText, result.Groups[index].Value, 1);
                    }
                }

                if (data.Options?.Any() == true)
                {
                    var optionMod = data.Options.SingleOrDefault(x => x.Pattern.IsMatch(text));
                    if (optionMod == null)
                    {
                        return;
                    }
                    modifier.OptionValue = new ModifierOption()
                    {
                        Text = optionMod.Text,
                    };
                    modifier.Text = ParseHashPattern.Replace(modifier.Text, optionMod.Text, 1);
                }
            }

            modifier.Text = modifier.Text.Replace("+-", "-");

            mods.Add(modifier);
        }
        public bool IsMatch(string id, string text)
        {
            ModifierPattern pattern = null;

            pattern = PseudoPatterns.FirstOrDefault(x => x.Metadata.Id == id);
            if (pattern == null)
            {
                pattern = ImplicitPatterns.FirstOrDefault(x => x.Metadata.Id == id);
            }
            if (pattern == null)
            {
                pattern = ExplicitPatterns.FirstOrDefault(x => x.Metadata.Id == id);
            }
            if (pattern == null)
            {
                pattern = CraftedPatterns.FirstOrDefault(x => x.Metadata.Id == id);
            }
            if (pattern == null)
            {
                pattern = EnchantPatterns.FirstOrDefault(x => x.Metadata.Id == id);
            }
            if (pattern == null)
            {
                pattern = FracturedPatterns.FirstOrDefault(x => x.Metadata.Id == id);
            }

            if (pattern == null)
            {
                return(false);
            }

            if (pattern.Pattern != null && pattern.Pattern.IsMatch(text))
            {
                return(true);
            }

            if (pattern.NegativePattern != null && pattern.NegativePattern.IsMatch(text))
            {
                return(true);
            }

            return(false);
        }
        private void ParseMod(List <Modifier> modifiers, ModifierPatternMetadata data, ModifierPattern pattern, string text)
        {
            var match = pattern.Pattern.Match(text);

            var modifier = new Modifier()
            {
                Index    = match.Index,
                Id       = data.Id,
                Text     = pattern.Text,
                Category = !data.Id.StartsWith("explicit") ? data.Category : null,
            };

            if (data.IsOption)
            {
                modifier.OptionValue = new ModifierOption()
                {
                    Value = pattern.Value,
                };
            }
            else if (pattern.Value.HasValue)
            {
                modifier.Values.Add(pattern.Value.Value);
                modifier.Text = ParseHashPattern.Replace(modifier.Text, match.Groups[1].Value, 1);
            }
            else if (match.Groups.Count > 1)
            {
                for (var index = 1; index < match.Groups.Count; index++)
                {
                    if (double.TryParse(match.Groups[index].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out var parsedValue))
                    {
                        if (pattern.Negative)
                        {
                            parsedValue *= -1;
                        }

                        modifier.Values.Add(parsedValue);
                        modifier.Text = ParseHashPattern.Replace(modifier.Text, match.Groups[index].Value, 1);
                    }
                }

                modifier.Text = modifier.Text.Replace("+-", "-");
            }

            modifiers.Add(modifier);
        }
        public async Task Initialize()
        {
            var result = await cacheRepository.GetOrSet(
                "Sidekick.Infrastructure.PoeApi.Items.Modifiers.ModifierProvider.Initialize",
                () => poeTradeClient.Fetch <ApiCategory>("data/stats"));

            var categories = result.Result;

            PseudoPatterns    = new List <ModifierPattern>();
            ExplicitPatterns  = new List <ModifierPattern>();
            ImplicitPatterns  = new List <ModifierPattern>();
            EnchantPatterns   = new List <ModifierPattern>();
            CraftedPatterns   = new List <ModifierPattern>();
            VeiledPatterns    = new List <ModifierPattern>();
            FracturedPatterns = new List <ModifierPattern>();
            IncreasedPattern  = new Regex(gameLanguageProvider.Language.ModifierIncreased);

            var hashPattern        = new Regex("\\\\#");
            var parenthesesPattern = new Regex("((?:\\\\\\ )*\\\\\\([^\\(\\)]*\\\\\\))");

            var additionalProjectileEscaped = Regex.Escape(gameLanguageProvider.Language.AdditionalProjectile);
            var additionalProjectiles       = hashPattern.Replace(Regex.Escape(gameLanguageProvider.Language.AdditionalProjectiles), "([-+\\d,\\.]+)");

            // We need to ignore the case here, there are some mistakes in the data of the game.
            AdditionalProjectilePattern = new Regex(gameLanguageProvider.Language.AdditionalProjectile, RegexOptions.IgnoreCase);

            foreach (var category in categories)
            {
                var first = category.Entries.FirstOrDefault();
                if (first == null)
                {
                    continue;
                }

                // The notes in parentheses are never translated by the game.
                // We should be fine hardcoding them this way.
                string suffix, pattern;
                List <ModifierPattern> patterns;
                switch (first.Id.Split('.').First())
                {
                default: continue;

                case "pseudo": suffix = "(?:\\n|(?<!(?:\\n.*){2,})$)"; patterns = PseudoPatterns; break;

                case "delve":
                case "monster":
                case "explicit": suffix = "(?:\\n|(?<!(?:\\n.*){2,})$)"; patterns = ExplicitPatterns; break;

                case "implicit": suffix = "(?:\\ \\(implicit\\)\\n|(?<!(?:\\n.*){2,})$)"; patterns = ImplicitPatterns; break;

                case "enchant": suffix = "(?:\\ \\(enchant\\)\\n|(?<!(?:\\n.*){2,})$)"; patterns = EnchantPatterns; break;

                case "crafted": suffix = "(?:\\ \\(crafted\\)\\n|(?<!(?:\\n.*){2,})$)"; patterns = CraftedPatterns; break;

                case "veiled": suffix = "(?:\\ \\(veiled\\)\\n|(?<!(?:\\n.*){2,})$)"; patterns = VeiledPatterns; break;

                case "fractured": suffix = "(?:\\ \\(fractured\\)\\n|(?<!(?:\\n.*){2,})$)"; patterns = FracturedPatterns; break;
                }

                foreach (var entry in category.Entries)
                {
                    var modifier = new ModifierPattern()
                    {
                        Metadata = new ModifierMetadata()
                        {
                            Category = category.Label,
                            Id       = entry.Id,
                            Text     = entry.Text,
                            Type     = entry.Type,
                        },
                    };

                    pattern = Regex.Escape(entry.Text);
                    pattern = parenthesesPattern.Replace(pattern, "(?:$1)?");
                    pattern = NewLinePattern.Replace(pattern, "\\n");

                    if (entry.Option == null || entry.Option.Options == null || entry.Option.Options.Count == 0)
                    {
                        pattern = hashPattern.Replace(pattern, "([-+\\d,\\.]+)");
                    }
                    else
                    {
                        modifier.Options = new List <ModifierOptionParse>();

                        foreach (var entryOption in entry.Option.Options)
                        {
                            var modifierOption = new ModifierOptionParse()
                            {
                                Text = entryOption.Text,
                            };

                            if (NewLinePattern.IsMatch(entryOption.Text))
                            {
                                var lines   = NewLinePattern.Split(entryOption.Text).ToList();
                                var options = lines.ConvertAll(x => hashPattern.Replace(pattern, Regex.Escape(x)));
                                modifierOption.Pattern = new Regex($"(?:^|\\n){string.Join("\\n", options)}{suffix}");
                                modifierOption.Text    = string.Join("\n", lines.Select((x, i) => new
                                {
                                    Text  = x,
                                    Index = i
                                })
                                                                     .ToList()
                                                                     .ConvertAll(x =>
                                {
                                    if (x.Index == 0)
                                    {
                                        return(x.Text);
                                    }

                                    return(entry.Text.Replace("#", x.Text));
                                }));
                            }
                            else
                            {
                                modifierOption.Pattern = new Regex($"(?:^|\\n){hashPattern.Replace(pattern, Regex.Escape(entryOption.Text))}{suffix}", RegexOptions.None);
                            }
                        }

                        pattern = hashPattern.Replace(pattern, "(.*)");
                    }

                    if (IncreasedPattern.IsMatch(pattern))
                    {
                        var negativePattern = IncreasedPattern.Replace(pattern, gameLanguageProvider.Language.ModifierReduced);
                        modifier.NegativePattern = new Regex($"(?:^|\\n){negativePattern}{suffix}", RegexOptions.None);
                    }

                    if (AdditionalProjectilePattern.IsMatch(entry.Text))
                    {
                        var additionalProjectilePattern = pattern.Replace(additionalProjectileEscaped, additionalProjectiles, System.StringComparison.OrdinalIgnoreCase);
                        modifier.AdditionalProjectilePattern = new Regex($"(?:^|\\n){additionalProjectilePattern}{suffix}", RegexOptions.IgnoreCase);
                    }

                    modifier.Pattern = new Regex($"(?:^|\\n){pattern}{suffix}", RegexOptions.None);
                    patterns.Add(modifier);
                }
            }
        }