mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-10 20:13:24 -08:00
[Refactor] Extract the per-mode checks from deriveFilterMode
This commit is contained in:
parent
62cb7016f7
commit
e0a6d3d2d5
1 changed files with 40 additions and 24 deletions
|
|
@ -27,38 +27,54 @@ FilterModeInfo getFilterModeInfo(FilterMode filterMode)
|
||||||
return {CardFilter::Type::TypeEnd, CardFilter::Type::TypeEnd, CardFilter::Attr::AttrEnd};
|
return {CardFilter::Type::TypeEnd, CardFilter::Type::TypeEnd, CardFilter::Attr::AttrEnd};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
/// Whether every filter in the set could have been created by this mode.
|
||||||
|
bool canCreateEveryFilter(const FilterModeInfo &modeInfo, const QList<const CardFilter *> &filters)
|
||||||
|
{
|
||||||
|
for (const CardFilter *filter : filters) {
|
||||||
|
const bool createdByMode = filter->attr() == modeInfo.attr &&
|
||||||
|
(filter->type() == modeInfo.includeType || filter->type() == modeInfo.excludeType);
|
||||||
|
if (!createdByMode) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Modes with both an include and an exclude filter need exactly one filter per color.
|
||||||
|
bool hasOneFilterPerColor(const FilterModeInfo &modeInfo, const QList<const CardFilter *> &filters)
|
||||||
|
{
|
||||||
|
if (modeInfo.includeType == CardFilter::Type::TypeEnd || modeInfo.excludeType == CardFilter::Type::TypeEnd) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const QChar &color : QStringLiteral("WUBRG")) {
|
||||||
|
int count = 0;
|
||||||
|
for (const CardFilter *filter : filters) {
|
||||||
|
if (filter->term() == color) {
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (count != 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
FilterMode deriveFilterMode(const QList<const CardFilter *> &filters, FilterMode currentMode)
|
FilterMode deriveFilterMode(const QList<const CardFilter *> &filters, FilterMode currentMode)
|
||||||
{
|
{
|
||||||
constexpr int modeCount = static_cast<int>(FilterMode::End);
|
constexpr int modeCount = static_cast<int>(FilterMode::End);
|
||||||
bool modeImpossible[modeCount] = {};
|
bool modeImpossible[modeCount] = {};
|
||||||
const QString colors = "WUBRG";
|
|
||||||
|
|
||||||
for (int i = 0; i < modeCount; ++i) {
|
for (int i = 0; i < modeCount; ++i) {
|
||||||
const FilterModeInfo modeInfo = getFilterModeInfo(static_cast<FilterMode>(i));
|
const FilterModeInfo modeInfo = getFilterModeInfo(static_cast<FilterMode>(i));
|
||||||
|
|
||||||
// Remove the modes that cannot create one of the filters.
|
// A mode is impossible when it cannot create every filter, or when it has both
|
||||||
for (const CardFilter *filter : filters) {
|
// an include and an exclude filter and a color is not covered exactly once.
|
||||||
if (filter->attr() != modeInfo.attr ||
|
modeImpossible[i] = !canCreateEveryFilter(modeInfo, filters) || !hasOneFilterPerColor(modeInfo, filters);
|
||||||
(filter->type() != modeInfo.includeType && filter->type() != modeInfo.excludeType)) {
|
|
||||||
modeImpossible[i] = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Modes that have both an include and an exclude filter should always have a filter for each color.
|
|
||||||
if (modeInfo.includeType != CardFilter::Type::TypeEnd && modeInfo.excludeType != CardFilter::Type::TypeEnd &&
|
|
||||||
!modeImpossible[i]) {
|
|
||||||
for (const QChar &color : colors) {
|
|
||||||
int count = 0;
|
|
||||||
for (const CardFilter *filter : filters) {
|
|
||||||
count += filter->term() == color;
|
|
||||||
}
|
|
||||||
if (count != 1) {
|
|
||||||
modeImpossible[i] = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep the current mode if it can produce the filters, so that a live mode is never swapped for another.
|
// Keep the current mode if it can produce the filters, so that a live mode is never swapped for another.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue