BetterPartyFinder/BetterPartyFinder/Filter.cs

85 lines
2.9 KiB
C#
Raw Normal View History

2021-01-14 04:06:35 +00:00
using System;
2021-01-14 17:00:15 +00:00
using System.Linq;
2021-01-14 04:06:35 +00:00
namespace BetterPartyFinder {
public class Filter : IDisposable {
private Plugin Plugin { get; }
internal Filter(Plugin plugin) {
this.Plugin = plugin;
this.Plugin.Functions.ReceivePartyFinderListing += this.ReceiveListing;
}
private void ReceiveListing(PartyFinderListing listing, PartyFinderListingEventArgs args) {
args.Visible = this.ListingVisible(listing);
}
private bool ListingVisible(PartyFinderListing listing) {
// get the current preset or mark all pfs as visible
var selectedId = this.Plugin.Config.SelectedPreset;
if (selectedId == null || !this.Plugin.Config.Presets.TryGetValue(selectedId.Value, out var filter)) {
return true;
}
// check max item level
if (!filter.AllowHugeItemLevel && Util.MaxItemLevel > 0 && listing.MinimumItemLevel > Util.MaxItemLevel) {
return false;
}
// filter based on duty whitelist/blacklist
if (filter.Duties.Count > 0 && listing.DutyType == DutyType.Normal) {
var inList = filter.Duties.Contains(listing.RawDuty);
// ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
2021-01-14 17:00:15 +00:00
switch (filter.DutiesMode) {
2021-01-14 04:06:35 +00:00
case ListMode.Blacklist when inList:
case ListMode.Whitelist when !inList:
return false;
}
}
// filter based on item level range
if (filter.MinItemLevel != null && listing.MinimumItemLevel < filter.MinItemLevel) {
return false;
}
if (filter.MaxItemLevel != null && listing.MinimumItemLevel > filter.MaxItemLevel) {
return false;
}
2021-01-14 22:12:30 +00:00
// filter based on restrictions
// make sure the listing doesn't contain any of the toggled off search areas
if (((listing.SearchArea ^ filter.SearchArea) & ~filter.SearchArea) > 0) {
return false;
}
if (!listing[filter.LootRule]) {
return false;
}
if (((listing.DutyFinderSettings ^ filter.DutyFinderSettings) & ~filter.DutyFinderSettings) > 0) {
return false;
}
if (!listing[filter.Conditions]) {
return false;
}
if (!listing[filter.Objectives]) {
return false;
}
2021-01-14 17:00:15 +00:00
// filter based on category (slow)
if (!filter.Categories.Any(category => category.ListingMatches(this.Plugin.Interface.Data, listing))) {
return false;
}
2021-01-14 04:06:35 +00:00
return true;
}
public void Dispose() {
this.Plugin.Functions.ReceivePartyFinderListing -= this.ReceiveListing;
}
}
}