// Public members public override async Task ApplyAsync(ISearchContext context, ISearchResult result) { IEnumerable <string> roleNames = StringUtilities.ParseDelimitedString(Value, ",").Select(role => role.ToLowerInvariant()); await result.FilterByAsync(async (species) => { return(!(await context.Database.GetRolesAsync(species)).Any(role => roleNames.Any(name => name.Equals(role.Name, StringComparison.OrdinalIgnoreCase)))); }, Invert); }
public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { // Filters out all species that are not in the prey list of the given species. ISpecies predatorSpecies = (await context.Database.GetSpeciesAsync(Value)).FirstOrDefault(); IEnumerable <IPredationInfo> preySpecies = predatorSpecies != null ? await context.Database.GetPreyAsync(predatorSpecies) : Enumerable.Empty <IPredationInfo>(); await result.FilterByAsync(async (species) => await Task.FromResult(!preySpecies.Any(info => info.Species.Id == species.Id)), Invert); }
public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { // Filters out species that don't have the given keyword in the prey notes. await result.FilterByAsync(async (species) => { return(!(await context.Database.GetPreyAsync(species)) .Where(info => info.Notes.ToLowerInvariant().Contains(Value.ToLowerInvariant())) .Any()); }, Invert); }
// Public members public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { switch (ParseStatusType(Value)) { case StatusType.Extant: await result.FilterByAsync(async (species) => await Task.FromResult(species.Status.IsExinct), Invert); break; case StatusType.Extinct: await result.FilterByAsync(async (species) => await Task.FromResult(!species.Status.IsExinct), Invert); break; case StatusType.Endangered: await result.FilterByAsync(async (species) => !await context.Database.IsEndangeredAsync(species), Invert); break; } }
public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { // Filter all species that don't have the given species as a descendant. ISpecies descendantSpecies = (await context.Database.GetSpeciesAsync(Value)).FirstOrDefault(); IEnumerable <long> ancestorIds = await context.Database.GetAncestorIdsAsync(descendantSpecies.Id); await result.FilterByAsync(async (species) => { return(await Task.FromResult(ancestorIds.Count() <= 0 || !ancestorIds.Any(id => id == species.Id))); }, Invert); }
// Public members public override async Task ApplyAsync(ISearchContext context, ISearchResult result) { IEnumerable <string> zoneNames = ZoneUtilities.ParseZoneNameList(Value); IEnumerable <long> ZoneIds = (await context.Database.GetZonesAsync(zoneNames)) .Where(zone => zone.Id.HasValue) .Select(zone => (long)zone.Id); await result.FilterByAsync(async (species) => { return(!(await context.Database.GetZonesAsync(species, GetZoneOptions.IdsOnly)).Any(zone => ZoneIds.Any(id => id == zone.Zone.Id))); }, Invert); }
public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { // Filter all species that aren't in the first n results. if (int.TryParse(Value, out int limit)) { IEnumerable <ISpecies> searchResults = (await result.GetResultsAsync()) .Take(limit); await result.FilterByAsync(async (species) => await Task.FromResult(!searchResults.Any(sp => sp.Id == species.Id)), Invert); } }
public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { // Filter all species that don't have the given species as an ancestor. ISpecies ancestorSpecies = (await context.Database.GetSpeciesAsync(Value)).FirstOrDefault(); Dictionary <long, bool> resultCache = new Dictionary <long, bool>(); await result.FilterByAsync(async (species) => { bool isFiltered = true; if (ancestorSpecies != null && species.Id != ancestorSpecies.Id && !resultCache.TryGetValue(species.Id.Value, out isFiltered)) { // Get all of the ancestor IDs for this species, ordered from the oldest to the latest. // Skip until we find the ancestor ID we're looking for. IEnumerable <long> ancestorIds = await context.Database.GetAncestorIdsAsync(species.Id); // Skip until we find the ID of the ancestor we're looking for. // If we find it, all species beyond this point also have the ancestor. // If we don't find it, none of these species has the ancestor. IEnumerable <long> idsWithAncestor = ancestorIds.SkipWhile(id => id != ancestorSpecies.Id); if (idsWithAncestor.Any()) { // If the species does have the ancestor, do not filter it. isFiltered = false; foreach (long id in idsWithAncestor) { resultCache[id] = false; } } else { // If the species does not have the ancestor, filter it. isFiltered = true; foreach (long id in ancestorIds) { resultCache[id] = true; } } } return(isFiltered); }, Invert); }
// Public members public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { await result.FilterByAsync(async (species) => { // If any of the comma-separated values accept this species, include it in the results. foreach (string value in Values) { if (!await IsFilteredAsync(context, species, value)) { return(false); } } return(true); }, Invert); }
public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { if (int.TryParse(Value, out int count) && count > 0) { IEnumerable <ISpecies> results = await result.GetResultsAsync(); // Take N random IDs from the results. IEnumerable <long> randomIds = results .Where(species => species.Id.HasValue) .OrderBy(species => NumberUtilities.GetRandomInteger(int.MaxValue)) .Take(count) .Select(species => (long)species.Id) .ToArray(); // Filter all but those results. await result.FilterByAsync(async (species) => await Task.FromResult(!randomIds.Any(id => id == species.Id)), Invert); } }
// Public members public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { switch (ParseHasType(Value)) { case HasType.Prey: await result.FilterByAsync(async (species) => (await context.Database.GetPreyAsync(species)).Count() <= 0, Invert); break; case HasType.Predator: await result.FilterByAsync(async (species) => (await context.Database.GetPredatorsAsync(species)).Count() <= 0, Invert); break; case HasType.Ancestor: await result.FilterByAsync(async (species) => await context.Database.GetAncestorAsync(species, GetSpeciesOptions.Fast) is null, Invert); break; case HasType.Descendant: await result.FilterByAsync(async (species) => (await context.Database.GetDirectDescendantsAsync(species)).Count() <= 0, Invert); break; case HasType.Role: await result.FilterByAsync(async (species) => (await context.Database.GetRolesAsync(species)).Count() <= 0, Invert); break; case HasType.Picture: await result.FilterByAsync(async (species) => (await context.Database.GetPicturesAsync(species)).Count() <= 0, Invert); break; case HasType.Size: await result.FilterByAsync(async (species) => await Task.FromResult(!SpeciesSizeMatch.Match(species.Description).Success), Invert); break; } }
public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { await result.FilterByAsync(async (species) => { return(!(await context.Database.GetPicturesAsync(species)).Any(picture => picture.Artist.ToString().Equals(Value, StringComparison.OrdinalIgnoreCase))); }, Invert); }
// Public members public async override Task ApplyAsync(ISearchContext context, ISearchResult result) { /* Filter all species that don't compete with the given species. * * A species is considered a competitor if: * * (1) It shares a zone with the given species, and * (2) It eats the same prey items, and * (3) It is currently extant * * This is very similar to the "status:endangered" filter, without the requirement that one species derive from the other. */ ISpecies species = (await context.Database.GetSpeciesAsync(Value)).FirstOrDefault(); List <ISpecies> competitorSpeciesList = new List <ISpecies>(); if (species.IsValid()) { IEnumerable <ISpecies> preySpecies = (await context.Database.GetPreyAsync(species)).Select(info => info.Species).Where(sp => !sp.IsExtinct()); // Create a list of all species that exist in the same zone as the given species. List <ISpecies> sharedZoneSpeciesList = new List <ISpecies>(); foreach (IZone zone in (await context.Database.GetZonesAsync(species, GetZoneOptions.IdsOnly)).Select(info => info.Zone)) { sharedZoneSpeciesList.AddRange((await context.Database.GetSpeciesAsync(zone)).Where(sp => !sp.IsExtinct())); } if (preySpecies.Any()) { // If the species has prey, find all species that have the same prey. foreach (ISpecies candidateSpecies in sharedZoneSpeciesList) { IEnumerable <ISpecies> candidateSpeciesPreySpecies = (await context.Database.GetPreyAsync(candidateSpecies)).Select(info => info.Species).Where(sp => !sp.IsExtinct()); if (candidateSpeciesPreySpecies.Any() && candidateSpeciesPreySpecies.All(sp1 => preySpecies.Any(sp2 => sp1.Id.Equals(sp2.Id)))) { competitorSpeciesList.Add(candidateSpecies); } } } else { // If the species does not have prey, find all species with the same roles. IEnumerable <IRole> roles = await context.Database.GetRolesAsync(species); if (roles.Any()) { foreach (ISpecies candidateSpecies in sharedZoneSpeciesList) { if ((await context.Database.GetRolesAsync(candidateSpecies)).All(role1 => roles.Any(role2 => role1.Id.Equals(role2.Id)))) { competitorSpeciesList.Add(candidateSpecies); } } } } } // Filter all species that aren't in the competitor species list. await result.FilterByAsync(async (s) => { return(await Task.FromResult(!species.IsValid() || species.IsExtinct() || s.Id.Equals(species.Id) || !competitorSpeciesList.Any(sp => sp.Id.Equals(s.Id)))); }, Invert); }