Update Rarity Filters & Refactoring (#2962)

This commit is contained in:
Zach H 2017-12-17 19:42:54 -05:00 committed by GitHub
parent 014b9947fe
commit 2abfd3b4a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,7 +7,7 @@
template <class T> template <class T>
FilterTreeNode *FilterTreeBranch<T>::nodeAt(int i) const FilterTreeNode *FilterTreeBranch<T>::nodeAt(int i) const
{ {
return ((childNodes.size() > i)? childNodes.at(i) : NULL); return (childNodes.size() > i) ? childNodes.at(i) : nullptr;
} }
template <class T> template <class T>
@ -22,21 +22,18 @@ void FilterTreeBranch<T>::deleteAt(int i)
template <class T> template <class T>
int FilterTreeBranch<T>::childIndex(const FilterTreeNode *node) const int FilterTreeBranch<T>::childIndex(const FilterTreeNode *node) const
{ {
FilterTreeNode *unconst; FilterTreeNode *unconst = const_cast<FilterTreeNode *>(node);
T downcasted; T downcasted = dynamic_cast<T>(unconst);
return (downcasted) ? childNodes.indexOf(downcasted) : -1;
unconst = const_cast<FilterTreeNode *>(node);
downcasted = dynamic_cast<T>(unconst);
if (downcasted == NULL)
return -1;
return childNodes.indexOf(downcasted);
} }
template <class T> template <class T>
FilterTreeBranch<T>::~FilterTreeBranch() FilterTreeBranch<T>::~FilterTreeBranch()
{ {
while (!childNodes.isEmpty()) while (!childNodes.isEmpty())
{
delete childNodes.takeFirst(); delete childNodes.takeFirst();
}
} }
const FilterItemList *LogicMap::findTypeList(CardFilter::Type type) const const FilterItemList *LogicMap::findTypeList(CardFilter::Type type) const
@ -44,24 +41,32 @@ const FilterItemList *LogicMap::findTypeList(CardFilter::Type type) const
QList<FilterItemList *>::const_iterator i; QList<FilterItemList *>::const_iterator i;
for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++) for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++)
{
if ((*i)->type == type) if ((*i)->type == type)
{
return *i; return *i;
}
}
return NULL; return nullptr;
} }
FilterItemList *LogicMap::typeList(CardFilter::Type type) FilterItemList *LogicMap::typeList(CardFilter::Type type)
{ {
QList<FilterItemList *>::iterator i; QList<FilterItemList *>::iterator i;
int count; int count = 0;
count = 0; for (i = childNodes.begin(); i != childNodes.end(); i++)
for (i = childNodes.begin(); i != childNodes.end(); i++) { {
if ((*i)->type == type) if ((*i)->type == type)
{
break; break;
}
count++; count++;
} }
if (i == childNodes.end()) {
if (i == childNodes.end())
{
preInsertChild(this, count); preInsertChild(this, count);
i = childNodes.insert(i, new FilterItemList(type, this)); i = childNodes.insert(i, new FilterItemList(type, this));
postInsertChild(this, count); postInsertChild(this, count);
@ -78,28 +83,30 @@ FilterTreeNode *LogicMap::parent() const
int FilterItemList::termIndex(const QString &term) const int FilterItemList::termIndex(const QString &term) const
{ {
int i; for (int i = 0; i < childNodes.count(); i++)
{
for (i = 0; i < childNodes.count(); i++)
if ((childNodes.at(i))->term == term) if ((childNodes.at(i))->term == term)
{
return i; return i;
}
}
return -1; return -1;
} }
FilterTreeNode *FilterItemList::termNode(const QString &term) FilterTreeNode *FilterItemList::termNode(const QString &term)
{ {
int i, count; int i = termIndex(term);
FilterItem *fi; if (i < 0)
{
FilterItem *fi = new FilterItem(term, this);
int count = childNodes.count();
i = termIndex(term);
if (i < 0) {
fi = new FilterItem(term, this);
count = childNodes.count();
preInsertChild(this, count); preInsertChild(this, count);
childNodes.append(fi); childNodes.append(fi);
postInsertChild(this, count); postInsertChild(this, count);
nodeChanged(); nodeChanged();
return fi; return fi;
} }
@ -108,13 +115,17 @@ FilterTreeNode *FilterItemList::termNode(const QString &term)
bool FilterItemList::testTypeAnd(const CardInfo *info, CardFilter::Attr attr) const bool FilterItemList::testTypeAnd(const CardInfo *info, CardFilter::Attr attr) const
{ {
QList<FilterItem *>::const_iterator i; for (auto i = childNodes.constBegin(); i != childNodes.constEnd(); i++)
{
for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++) { if (! (*i)->isEnabled())
if (!(*i)->isEnabled()) {
continue; continue;
if (!(*i)->acceptCardAttr(info, attr)) }
if (! (*i)->acceptCardAttr(info, attr))
{
return false; return false;
}
} }
return true; return true;
@ -128,16 +139,24 @@ bool FilterItemList::testTypeAndNot(const CardInfo *info, CardFilter::Attr attr)
bool FilterItemList::testTypeOr(const CardInfo *info, CardFilter::Attr attr) const bool FilterItemList::testTypeOr(const CardInfo *info, CardFilter::Attr attr) const
{ {
QList<FilterItem *>::const_iterator i;
bool noChildEnabledChild = true; bool noChildEnabledChild = true;
for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++) { for (auto i = childNodes.constBegin(); i != childNodes.constEnd(); i++)
if (!(*i)->isEnabled()) {
if (! (*i)->isEnabled())
{
continue; continue;
if(noChildEnabledChild) }
noChildEnabledChild=false;
if (noChildEnabledChild)
{
noChildEnabledChild = false;
}
if ((*i)->acceptCardAttr(info, attr)) if ((*i)->acceptCardAttr(info, attr))
{
return true; return true;
}
} }
return noChildEnabledChild; return noChildEnabledChild;
@ -161,38 +180,43 @@ bool FilterItem::acceptType(const CardInfo *info) const
bool FilterItem::acceptColor(const CardInfo *info) const bool FilterItem::acceptColor(const CardInfo *info) const
{ {
QStringList::const_iterator i; QString converted_term = term.trimmed();
QString converted_term;
QString::const_iterator it; converted_term.replace("green", "g", Qt::CaseInsensitive);
int match_count; converted_term.replace("grn", "g", Qt::CaseInsensitive);
converted_term.replace("blue", "u", Qt::CaseInsensitive);
converted_term.replace("blu", "u", Qt::CaseInsensitive);
converted_term.replace("black", "b", Qt::CaseInsensitive);
converted_term.replace("blk", "b", Qt::CaseInsensitive);
converted_term.replace("red", "r", Qt::CaseInsensitive);
converted_term.replace("white", "w", Qt::CaseInsensitive);
converted_term.replace("wht", "w", Qt::CaseInsensitive);
converted_term.replace("colorless", "c", Qt::CaseInsensitive);
converted_term.replace("colourless", "c", Qt::CaseInsensitive);
converted_term.replace("none", "c", Qt::CaseInsensitive);
converted_term = term;
converted_term.replace(QString("green"), QString("g"), Qt::CaseInsensitive);
converted_term.replace(QString("grn"), QString("g"), Qt::CaseInsensitive);
converted_term.replace(QString("blue"), QString("u"), Qt::CaseInsensitive);
converted_term.replace(QString("blu"), QString("u"), Qt::CaseInsensitive);
converted_term.replace(QString("black"), QString("b"), Qt::CaseInsensitive);
converted_term.replace(QString("blk"), QString("b"), Qt::CaseInsensitive);
converted_term.replace(QString("red"), QString("r"), Qt::CaseInsensitive);
converted_term.replace(QString("white"), QString("w"), Qt::CaseInsensitive);
converted_term.replace(QString("wht"), QString("w"), Qt::CaseInsensitive);
converted_term.replace(QString(" "), QString(""), Qt::CaseInsensitive); converted_term.replace(QString(" "), QString(""), Qt::CaseInsensitive);
if (converted_term.toLower() == "none" || converted_term.toLower() == "colorless" || converted_term.toLower() == "c" || converted_term.toLower() == "colourless") { // Colorless card filter
if (info->getColors().length() < 1) { if (converted_term.toLower() == "c" && info->getColors().length() < 1)
return true; {
} return true;
} }
/* This is a tricky part, if the filter has multiple colors in it, like UGW, /*
then we should match all of them to the card's colors */ * This is a tricky part, if the filter has multiple colors in it, like UGW,
* then we should match all of them to the card's colors
match_count = 0; */
for (it = converted_term.begin(); it != converted_term.end(); it++) { int match_count = 0;
for (i = info->getColors().constBegin(); i != info->getColors().constEnd(); i++) for (auto it = converted_term.begin(); it != converted_term.end(); it++)
if ((*i).contains((*it), Qt::CaseInsensitive)) { {
for (auto i = info->getColors().constBegin(); i != info->getColors().constEnd(); i++)
{
if ((*i).contains((*it), Qt::CaseInsensitive))
{
match_count++; match_count++;
} }
}
} }
return match_count == converted_term.length(); return match_count == converted_term.length();
@ -205,16 +229,16 @@ bool FilterItem::acceptText(const CardInfo *info) const
bool FilterItem::acceptSet(const CardInfo *info) const bool FilterItem::acceptSet(const CardInfo *info) const
{ {
SetList::const_iterator i; bool status = false;
bool status; for (auto i = info->getSets().constBegin(); i != info->getSets().constEnd(); i++)
{
status = false; if ((*i)->getShortName().compare(term, Qt::CaseInsensitive) == 0
for (i = info->getSets().constBegin(); i != info->getSets().constEnd(); i++) || (*i)->getLongName().compare(term, Qt::CaseInsensitive) == 0)
if ((*i)->getShortName().compare(term, Qt::CaseInsensitive) == 0 {
|| (*i)->getLongName().compare(term, Qt::CaseInsensitive) == 0) {
status = true; status = true;
break; break;
} }
}
return status; return status;
} }
@ -232,59 +256,73 @@ bool FilterItem::acceptCmc(const CardInfo *info) const
bool FilterItem::acceptPower(const CardInfo *info) const bool FilterItem::acceptPower(const CardInfo *info) const
{ {
int slash = info->getPowTough().indexOf("/"); int slash = info->getPowTough().indexOf("/");
if (slash != -1) return (slash != -1) ? (info->getPowTough().mid(0,slash) == term) : false;
return (info->getPowTough().mid(0,slash) == term);
return false;
} }
bool FilterItem::acceptToughness(const CardInfo *info) const bool FilterItem::acceptToughness(const CardInfo *info) const
{ {
int slash = info->getPowTough().indexOf("/"); int slash = info->getPowTough().indexOf("/");
if (slash != -1) return (slash != -1) ? (info->getPowTough().mid(slash+1) == term) : false;
return (info->getPowTough().mid(slash+1) == term);
return false;
} }
bool FilterItem::acceptRarity(const CardInfo *info) const bool FilterItem::acceptRarity(const CardInfo *info) const
{ {
foreach (QString rareLevel, info->getRarities()) QString converted_term = term.trimmed();
if (rareLevel.compare(term, Qt::CaseInsensitive) == 0)
return true;
/*
* The purpose of this loop is to only apply one of the replacement
* policies and then escape. If we attempt to layer them ontop of
* each other, we will get awkward results (i.e. comythic rare mythic rareon)
* Conditional statement will exit once a case is successful in
* replacement OR we go through all possible cases.
* Will also need to replace just "mythic"
*/
converted_term.replace("mythic", "mythic rare", Qt::CaseInsensitive);
for (int i = 0; converted_term.length() <= 3 && i <= 6; i++)
{
switch (i)
{
case 0: converted_term.replace("mr", "mythic rare", Qt::CaseInsensitive); break;
case 1: converted_term.replace("m r", "mythic rare", Qt::CaseInsensitive); break;
case 2: converted_term.replace("m", "mythic rare", Qt::CaseInsensitive); break;
case 3: converted_term.replace("c", "common", Qt::CaseInsensitive); break;
case 4: converted_term.replace("u", "uncommon", Qt::CaseInsensitive); break;
case 5: converted_term.replace("r", "rare", Qt::CaseInsensitive); break;
case 6: converted_term.replace("s", "special", Qt::CaseInsensitive); break;
default: break;
}
}
foreach (QString rareLevel, info->getRarities())
{
if (rareLevel.compare(converted_term, Qt::CaseInsensitive) == 0)
{
return true;
}
}
return false; return false;
} }
bool FilterItem::acceptCardAttr(const CardInfo *info, CardFilter::Attr attr) const bool FilterItem::acceptCardAttr(const CardInfo *info, CardFilter::Attr attr) const
{ {
switch (attr) { switch (attr)
case CardFilter::AttrName: {
return acceptName(info); case CardFilter::AttrName: return acceptName(info);
case CardFilter::AttrType: case CardFilter::AttrType: return acceptType(info);
return acceptType(info); case CardFilter::AttrColor: return acceptColor(info);
case CardFilter::AttrColor: case CardFilter::AttrText: return acceptText(info);
return acceptColor(info); case CardFilter::AttrSet: return acceptSet(info);
case CardFilter::AttrText: case CardFilter::AttrManaCost: return acceptManaCost(info);
return acceptText(info); case CardFilter::AttrCmc: return acceptCmc(info);
case CardFilter::AttrSet: case CardFilter::AttrRarity: return acceptRarity(info);
return acceptSet(info); case CardFilter::AttrPow: return acceptPower(info);
case CardFilter::AttrManaCost: case CardFilter::AttrTough: return acceptToughness(info);
return acceptManaCost(info); default: return true; /* ignore this attribute */
case CardFilter::AttrCmc:
return acceptCmc(info);
case CardFilter::AttrRarity:
return acceptRarity(info);
case CardFilter::AttrPow:
return acceptPower(info);
case CardFilter::AttrTough:
return acceptToughness(info);
default:
return true; /* ignore this attribute */
} }
} }
/* need to define these here to make QT happy, otherwise /*
* Need to define these here to make QT happy, otherwise
* moc doesnt find some of the FilterTreeBranch symbols. * moc doesnt find some of the FilterTreeBranch symbols.
*/ */
FilterTree::FilterTree() {} FilterTree::FilterTree() {}
@ -293,16 +331,19 @@ FilterTree::~FilterTree() {}
LogicMap *FilterTree::attrLogicMap(CardFilter::Attr attr) LogicMap *FilterTree::attrLogicMap(CardFilter::Attr attr)
{ {
QList<LogicMap *>::iterator i; QList<LogicMap *>::iterator i;
int count;
count = 0; int count = 0;
for (i = childNodes.begin(); i != childNodes.end(); i++) { for (i = childNodes.begin(); i != childNodes.end(); i++)
{
if ((*i)->attr == attr) if ((*i)->attr == attr)
{
break; break;
}
count++; count++;
} }
if (i == childNodes.end()) { if (i == childNodes.end())
{
preInsertChild(this, count); preInsertChild(this, count);
i = childNodes.insert(i, new LogicMap(attr, this)); i = childNodes.insert(i, new LogicMap(attr, this));
postInsertChild(this, count); postInsertChild(this, count);
@ -312,14 +353,12 @@ LogicMap *FilterTree::attrLogicMap(CardFilter::Attr attr)
return *i; return *i;
} }
FilterItemList *FilterTree::attrTypeList(CardFilter::Attr attr, FilterItemList *FilterTree::attrTypeList(CardFilter::Attr attr, CardFilter::Type type)
CardFilter::Type type)
{ {
return attrLogicMap(attr)->typeList(type); return attrLogicMap(attr)->typeList(type);
} }
int FilterTree::findTermIndex(CardFilter::Attr attr, CardFilter::Type type, int FilterTree::findTermIndex(CardFilter::Attr attr, CardFilter::Type type, const QString &term)
const QString &term)
{ {
return attrTypeList(attr, type)->termIndex(term); return attrTypeList(attr, type)->termIndex(term);
} }
@ -329,8 +368,7 @@ int FilterTree::findTermIndex(const CardFilter *f)
return findTermIndex(f->attr(), f->type(), f->term()); return findTermIndex(f->attr(), f->type(), f->term());
} }
FilterTreeNode *FilterTree::termNode(CardFilter::Attr attr, CardFilter::Type type, FilterTreeNode *FilterTree::termNode(CardFilter::Attr attr, CardFilter::Type type, const QString &term)
const QString &term)
{ {
return attrTypeList(attr, type)->termNode(term); return attrTypeList(attr, type)->termNode(term);
} }
@ -340,8 +378,7 @@ FilterTreeNode *FilterTree::termNode(const CardFilter *f)
return termNode(f->attr(), f->type(), f->term()); return termNode(f->attr(), f->type(), f->term());
} }
FilterTreeNode *FilterTree::attrTypeNode(CardFilter::Attr attr, FilterTreeNode *FilterTree::attrTypeNode(CardFilter::Attr attr, CardFilter::Type type)
CardFilter::Type type)
{ {
return attrTypeList(attr, type); return attrTypeList(attr, type);
} }
@ -349,46 +386,58 @@ FilterTreeNode *FilterTree::attrTypeNode(CardFilter::Attr attr,
bool FilterTree::testAttr(const CardInfo *info, const LogicMap *lm) const bool FilterTree::testAttr(const CardInfo *info, const LogicMap *lm) const
{ {
const FilterItemList *fil; const FilterItemList *fil;
bool status; bool status = true;
status = true;
fil = lm->findTypeList(CardFilter::TypeAnd); fil = lm->findTypeList(CardFilter::TypeAnd);
if (fil != NULL && fil->isEnabled() && !fil->testTypeAnd(info, lm->attr)) if (fil && fil->isEnabled() && !fil->testTypeAnd(info, lm->attr))
{
return false; return false;
}
fil = lm->findTypeList(CardFilter::TypeAndNot); fil = lm->findTypeList(CardFilter::TypeAndNot);
if (fil != NULL && fil->isEnabled() && !fil->testTypeAndNot(info, lm->attr)) if (fil && fil->isEnabled() && !fil->testTypeAndNot(info, lm->attr))
{
return false; return false;
}
fil = lm->findTypeList(CardFilter::TypeOr); fil = lm->findTypeList(CardFilter::TypeOr);
if (fil != NULL && fil->isEnabled()) { if (fil && fil->isEnabled())
{
status = false; status = false;
// if this is true we can return because it is OR'd with the OrNot list // if this is true we can return because it is OR'd with the OrNot list
if (fil->testTypeOr(info, lm->attr)) if (fil->testTypeOr(info, lm->attr))
{
return true; return true;
}
} }
fil = lm->findTypeList(CardFilter::TypeOrNot); fil = lm->findTypeList(CardFilter::TypeOrNot);
if (fil != NULL && fil->isEnabled() && fil->testTypeOrNot(info, lm->attr)) if (fil && fil->isEnabled() && fil->testTypeOrNot(info, lm->attr))
{
return true; return true;
}
return status; return status;
} }
bool FilterTree::acceptsCard(const CardInfo *info) const bool FilterTree::acceptsCard(const CardInfo *info) const
{ {
QList<LogicMap *>::const_iterator i; for (auto i = childNodes.constBegin(); i != childNodes.constEnd(); i++)
{
for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++)
if ((*i)->isEnabled() && !testAttr(info, *i)) if ((*i)->isEnabled() && !testAttr(info, *i))
{
return false; return false;
}
}
return true; return true;
} }
void FilterTree::clear() void FilterTree::clear()
{ {
while(childCount() > 0) while (childCount() > 0)
{
deleteAt(0); deleteAt(0);
} }
}