Esempio n. 1
0
        /// <summary>Get the subkey/value pairs used in the given name and values.</summary>
        /// <param name="name">The token name to validate.</param>
        /// <param name="values">The values to validate.</param>
        /// <returns>Returns the subkey/value pairs found. If the <paramref name="name"/> includes a subkey, the <paramref name="values"/> are treated as values of that subkey. Otherwise if <see cref="CanHaveSubkeys"/> is true, then each value is treated as <c>subkey:value</c> (if they contain a colon) or <c>value</c> (with a null subkey).</returns>
        protected IEnumerable <KeyValuePair <TokenName, string> > GetSubkeyValuePairsFor(TokenName name, InvariantHashSet values)
        {
            // no subkeys in values
            if (!this.CanHaveSubkeys || name.HasSubkey())
            {
                foreach (string value in values)
                {
                    yield return(new KeyValuePair <TokenName, string>(name, value));
                }
            }

            // possible subkeys in values
            else
            {
                foreach (string value in values)
                {
                    string[] parts = value.Split(new[] { ':' }, 2);
                    if (parts.Length < 2)
                    {
                        yield return(new KeyValuePair <TokenName, string>(name, parts[0]));
                    }
                    else
                    {
                        yield return(new KeyValuePair <TokenName, string>(new TokenName(name.Key, parts[0]), parts[1]));
                    }
                }
            }
        }
Esempio n. 2
0
 /// <summary>Get the allowed values for a token name (or <c>null</c> if any value is allowed).</summary>
 /// <exception cref="InvalidOperationException">The key doesn't match this token, or the key does not respect <see cref="IToken.CanHaveSubkeys"/> or <see cref="IToken.RequiresSubkeys"/>.</exception>
 public override InvariantHashSet GetAllowedValues(TokenName name)
 {
     if (name.HasSubkey())
     {
         return new InvariantHashSet {
                    true.ToString(), false.ToString()
         }
     }
     ;
     return(this.AllowedRootValues);
 }
        /// <summary>Get the current token values.</summary>
        /// <param name="name">The token name to check.</param>
        /// <exception cref="InvalidOperationException">The key doesn't match this token, or the key does not respect <see cref="IToken.CanHaveSubkeys"/> or <see cref="IToken.RequiresSubkeys"/>.</exception>
        public override IEnumerable <string> GetValues(TokenName name)
        {
            this.AssertTokenName(name);

            if (name.HasSubkey())
            {
                return new[] { this.Values.Contains(name.Subkey).ToString() }
            }
            ;
            return(this.Values);
        }
    }
Esempio n. 4
0
        /// <summary>Get the current token values.</summary>
        /// <param name="name">The token name to check.</param>
        /// <exception cref="InvalidOperationException">The key doesn't match this token, or the key does not respect <see cref="IToken.CanHaveSubkeys"/> or <see cref="IToken.RequiresSubkeys"/>.</exception>
        public override IEnumerable <string> GetValues(TokenName name)
        {
            this.AssertTokenName(name);

            if (name.HasSubkey())
            {
                bool hasProfession = this.TryParseEnum(name.Subkey, out Profession profession, mustBeNamed: false) && this.Professions.Contains(profession);
                yield return(hasProfession.ToString());
            }
            else
            {
                foreach (Profession profession in this.Professions)
                {
                    yield return(profession.ToString());
                }
            }
        }
Esempio n. 5
0
        /// <summary>Perform custom validation on a subkey/value pair.</summary>
        /// <param name="name">The token name to validate.</param>
        /// <param name="value">The value to validate.</param>
        /// <param name="error">The validation error, if any.</param>
        /// <returns>Returns whether validation succeeded.</returns>
        public override bool TryValidate(TokenName name, string value, out string error)
        {
            if (!base.TryValidate(name, value, out error))
            {
                return(false);
            }

            // validate profession IDs
            string profession = name.HasSubkey() ? name.Subkey : value;

            if (!this.TryParseEnum(profession, out Profession _, mustBeNamed: false))
            {
                error = $"can't parse '{profession}' as a profession ID; must be one of [{string.Join(", ", Enum.GetNames(typeof(Profession)).OrderByIgnoreCase(p => p))}] or an integer ID.";
                return(false);
            }

            error = null;
            return(true);
        }
        /// <summary>Get the current token values.</summary>
        /// <param name="name">The token name to check.</param>
        /// <exception cref="InvalidOperationException">The key doesn't match this token, or the key does not respect <see cref="IToken.CanHaveSubkeys"/> or <see cref="IToken.RequiresSubkeys"/>.</exception>
        public override IEnumerable <string> GetValues(TokenName name)
        {
            this.AssertTokenName(name);

            if (name.HasSubkey())
            {
                if (this.TryParseEnum(name.Subkey, out Skill skill) && this.SkillLevels.TryGetValue(skill, out int level))
                {
                    yield return(level.ToString());
                }
            }
            else
            {
                foreach (var pair in this.SkillLevels)
                {
                    yield return($"{pair.Key}:{pair.Value}");
                }
            }
        }
Esempio n. 7
0
        /// <summary>Get the current token values.</summary>
        /// <param name="name">The token name to check.</param>
        /// <exception cref="InvalidOperationException">The key doesn't match this token, or the key does not respect <see cref="IToken.CanHaveSubkeys"/> or <see cref="IToken.RequiresSubkeys"/>.</exception>
        public override IEnumerable <string> GetValues(TokenName name)
        {
            this.AssertTokenName(name);

            if (name.HasSubkey())
            {
                if (this.Values.TryGetValue(name.Subkey, out string value))
                {
                    yield return(value);
                }
            }
            else
            {
                foreach (var pair in this.Values)
                {
                    yield return($"{pair.Key}:{pair.Value}");
                }
            }
        }
        /// <summary>Get the current token values.</summary>
        /// <param name="name">The token name to check.</param>
        /// <exception cref="InvalidOperationException">The key doesn't match this token, or the key does not respect <see cref="IToken.CanHaveSubkeys"/> or <see cref="IToken.RequiresSubkeys"/>.</exception>
        public override IEnumerable <string> GetValues(TokenName name)
        {
            this.AssertTokenName(name);

            if (name.HasSubkey())
            {
                bool hasItem = this.TryParseEnum(name.Subkey, out WalletItem item) && this.WalletItems[item]();
                yield return(hasItem.ToString());
            }
            else
            {
                foreach (KeyValuePair <WalletItem, Func <bool> > pair in this.WalletItems)
                {
                    if (pair.Value())
                    {
                        yield return(pair.Key.ToString());
                    }
                }
            }
        }
 /// <summary>Get the allowed values for a token name (or <c>null</c> if any value is allowed).</summary>
 /// <exception cref="InvalidOperationException">The key doesn't match this token, or the key does not respect <see cref="IToken.CanHaveSubkeys"/> or <see cref="IToken.RequiresSubkeys"/>.</exception>
 public override InvariantHashSet GetAllowedValues(TokenName name)
 {
     return(name.HasSubkey()
         ? InvariantHashSet.Boolean()
         : this.AllowedRootValues);
 }
Esempio n. 10
0
 /// <summary>Whether the token may return multiple values for the given name.</summary>
 /// <param name="name">The token name.</param>
 public bool CanHaveMultipleValues(TokenName name)
 {
     return(name.HasSubkey()
         ? this.CanHaveMultipleSubkeyValues
         : this.CanHaveMultipleRootValues);
 }