0 values in power/toughness filters won't match non-zero values anymore (#3250)

## Related Ticket(s)
- Fixes #3212 

## What will change with this Pull Request?
- `0` input on power/toughness filters will only return `0` values
- `relationCheck` method will only get called after simple string comparison failed
- due to their similar structure `acceptPower` and `acceptToughness` methods has been merged
This commit is contained in:
David Szabo
2018-05-24 00:18:53 +02:00
committed by tooomm
parent ec4a99e47b
commit b3df71ae41
2 changed files with 25 additions and 14 deletions

View File

@@ -272,16 +272,25 @@ bool FilterItem::acceptLoyalty(const CardInfoPtr info) const
}
}
bool FilterItem::acceptPower(const CardInfoPtr info) const
bool FilterItem::acceptPowerToughness(const CardInfoPtr info, CardFilter::Attr attr) const
{
int slash = info->getPowTough().indexOf("/");
return (slash != -1) ? (relationCheck(info->getPowTough().mid(0, slash).toInt())) : false;
}
bool FilterItem::acceptToughness(const CardInfoPtr info) const
{
int slash = info->getPowTough().indexOf("/");
return (slash != -1) ? (relationCheck(info->getPowTough().mid(slash + 1).toInt())) : false;
if (slash == -1) {
return false;
}
QString valueString;
if (attr == CardFilter::AttrPow) {
valueString = info->getPowTough().mid(0, slash);
} else {
valueString = info->getPowTough().mid(slash + 1);
}
if (term == valueString) {
return true;
}
// advanced filtering should only happen after fast string comparison failed
bool conversion;
int value = valueString.toInt(&conversion);
return conversion ? relationCheck(value) : false;
}
bool FilterItem::acceptRarity(const CardInfoPtr info) const
@@ -337,7 +346,7 @@ bool FilterItem::relationCheck(int cardInfo) const
{
bool result, conversion;
// if int conversion fails, there must be either an operator at the start
// if int conversion fails, there's probably an operator at the start
result = (cardInfo == term.toInt(&conversion));
if (!conversion) {
// leading whitespaces could cause indexing to fail
@@ -358,8 +367,11 @@ bool FilterItem::relationCheck(int cardInfo) const
result = (cardInfo < termInt);
} else if (trimmedTerm.startsWith('>')) {
result = (cardInfo > termInt);
} else {
} else if (trimmedTerm.startsWith("=")) {
result = (cardInfo == termInt);
} else {
// the int conversion hasn't failed due to an operator at the start
result = false;
}
}
}
@@ -386,9 +398,9 @@ bool FilterItem::acceptCardAttr(const CardInfoPtr info, CardFilter::Attr attr) c
case CardFilter::AttrRarity:
return acceptRarity(info);
case CardFilter::AttrPow:
return acceptPower(info);
case CardFilter::AttrTough:
return acceptToughness(info);
// intentional fallthrough
return acceptPowerToughness(info, attr);
case CardFilter::AttrLoyalty:
return acceptLoyalty(info);
default:

View File

@@ -210,8 +210,7 @@ public:
bool acceptSet(const CardInfoPtr info) const;
bool acceptManaCost(const CardInfoPtr info) const;
bool acceptCmc(const CardInfoPtr info) const;
bool acceptPower(const CardInfoPtr info) const;
bool acceptToughness(const CardInfoPtr info) const;
bool acceptPowerToughness(const CardInfoPtr info, CardFilter::Attr attr) const;
bool acceptLoyalty(const CardInfoPtr info) const;
bool acceptRarity(const CardInfoPtr info) const;
bool acceptCardAttr(const CardInfoPtr info, CardFilter::Attr attr) const;