Пример #1
0
 public bool TestRequirements(SQLiteDatabase database, GotchiRequirements requirements)
 {
     return(new GotchiRequirementsChecker(database)
     {
         Requires = requirements
     }.CheckAsync(Gotchi).Result);
 }
        private bool _checkDescription(ISpecies species, GotchiRequirements requirements)
        {
            try {
                if (Regex.IsMatch(species.Description, requirements.DescriptionPattern, RegexOptions.IgnoreCase))
                {
                    return(true);
                }
            }
            catch (Exception) { }

            return(false);
        }
        private async Task <bool> _checkRolesAsync(Gotchi gotchi, GotchiRequirements requirements)
        {
            try {
                IEnumerable <IRole> roles = await database.GetRolesAsync(gotchi.SpeciesId);

                foreach (Role role in roles)
                {
                    if (Regex.IsMatch(role.Name, requirements.RolePattern, RegexOptions.IgnoreCase))
                    {
                        return(true);
                    }
                }
            }
            catch (Exception) { }

            return(false);
        }
        private async Task <bool> _checkTypesAsync(Gotchi gotchi, GotchiRequirements requirements)
        {
            try {
                // Get the types assigned to this gotchi.
                GotchiType[] types = await Global.GotchiContext.TypeRegistry.GetTypesAsync(database, gotchi);

                // Compare each type using the type pattern provided.
                // #todo Types can also have aliases (AliasPattern). For now, we'll just try to match against the type pattern as well.

                foreach (GotchiType type in types)
                {
                    if (Regex.IsMatch(type.Name, requirements.TypePattern, RegexOptions.IgnoreCase) ||
                        Regex.IsMatch(type.AliasPattern, requirements.TypePattern, RegexOptions.IgnoreCase))
                    {
                        return(true);
                    }
                }
            }
            catch (Exception) { }

            return(false);
        }
        public async Task <bool> CheckAsync(Gotchi gotchi, GotchiRequirements requirements)
        {
            if (requirements is null)
            {
                return(true);
            }

            if (requirements.AlwaysFailValue)
            {
                return(false);
            }

            if (!_checkLevels(gotchi, requirements))
            {
                return(false);
            }

            if (!string.IsNullOrEmpty(requirements.RolePattern) && !await _checkRolesAsync(gotchi, requirements))
            {
                return(false);
            }

            if (!string.IsNullOrEmpty(requirements.TypePattern) && !await _checkTypesAsync(gotchi, requirements))
            {
                return(false);
            }

            ISpecies species = await database.GetSpeciesAsync(gotchi.SpeciesId);

            if (!string.IsNullOrEmpty(requirements.DescriptionPattern) && !_checkDescription(species, requirements))
            {
                return(false);
            }

            return(true);
        }
 public bool TestRequirements(GotchiRequirements requirements)
 {
     return(new GotchiRequirementsChecker {
         Requires = requirements
     }.CheckAsync(Gotchi).Result);
 }
        private bool _checkLevels(Gotchi gotchi, GotchiRequirements requirements)
        {
            int level = GotchiExperienceCalculator.GetLevel(ExperienceGroup.Default, gotchi.Experience);

            return(level >= requirements.MinimumLevelValue && level <= requirements.MaximumLevelValue);
        }