diff --git a/cockatrice/src/filtertree.cpp b/cockatrice/src/filtertree.cpp index 058cc207..f1d4aabf 100644 --- a/cockatrice/src/filtertree.cpp +++ b/cockatrice/src/filtertree.cpp @@ -7,7 +7,7 @@ template FilterTreeNode *FilterTreeBranch::nodeAt(int i) const { - return ((childNodes.size() > i)? childNodes.at(i) : NULL); + return (childNodes.size() > i) ? childNodes.at(i) : nullptr; } template @@ -22,21 +22,18 @@ void FilterTreeBranch::deleteAt(int i) template int FilterTreeBranch::childIndex(const FilterTreeNode *node) const { - FilterTreeNode *unconst; - T downcasted; - - unconst = const_cast(node); - downcasted = dynamic_cast(unconst); - if (downcasted == NULL) - return -1; - return childNodes.indexOf(downcasted); + FilterTreeNode *unconst = const_cast(node); + T downcasted = dynamic_cast(unconst); + return (downcasted) ? childNodes.indexOf(downcasted) : -1; } template FilterTreeBranch::~FilterTreeBranch() { while (!childNodes.isEmpty()) + { delete childNodes.takeFirst(); + } } const FilterItemList *LogicMap::findTypeList(CardFilter::Type type) const @@ -44,24 +41,32 @@ const FilterItemList *LogicMap::findTypeList(CardFilter::Type type) const QList::const_iterator i; for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++) + { if ((*i)->type == type) + { return *i; + } + } - return NULL; + return nullptr; } FilterItemList *LogicMap::typeList(CardFilter::Type type) { QList::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) + { break; + } count++; } - if (i == childNodes.end()) { + + if (i == childNodes.end()) + { preInsertChild(this, count); i = childNodes.insert(i, new FilterItemList(type, this)); postInsertChild(this, count); @@ -78,28 +83,30 @@ FilterTreeNode *LogicMap::parent() const int FilterItemList::termIndex(const QString &term) const { - int i; - - for (i = 0; i < childNodes.count(); i++) + for (int i = 0; i < childNodes.count(); i++) + { if ((childNodes.at(i))->term == term) + { return i; + } + } return -1; } FilterTreeNode *FilterItemList::termNode(const QString &term) { - int i, count; - FilterItem *fi; + int i = termIndex(term); + 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); childNodes.append(fi); postInsertChild(this, count); nodeChanged(); + return fi; } @@ -108,13 +115,17 @@ FilterTreeNode *FilterItemList::termNode(const QString &term) bool FilterItemList::testTypeAnd(const CardInfo *info, CardFilter::Attr attr) const { - QList::const_iterator i; - - for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++) { - if (!(*i)->isEnabled()) + for (auto i = childNodes.constBegin(); i != childNodes.constEnd(); i++) + { + if (! (*i)->isEnabled()) + { continue; - if (!(*i)->acceptCardAttr(info, attr)) + } + + if (! (*i)->acceptCardAttr(info, attr)) + { return false; + } } 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 { - QList::const_iterator i; bool noChildEnabledChild = true; - for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++) { - if (!(*i)->isEnabled()) + for (auto i = childNodes.constBegin(); i != childNodes.constEnd(); i++) + { + if (! (*i)->isEnabled()) + { continue; - if(noChildEnabledChild) - noChildEnabledChild=false; + } + + if (noChildEnabledChild) + { + noChildEnabledChild = false; + } + if ((*i)->acceptCardAttr(info, attr)) + { return true; + } } return noChildEnabledChild; @@ -161,38 +180,43 @@ bool FilterItem::acceptType(const CardInfo *info) const bool FilterItem::acceptColor(const CardInfo *info) const { - QStringList::const_iterator i; - QString converted_term; - QString::const_iterator it; - int match_count; + QString converted_term = term.trimmed(); + + converted_term.replace("green", "g", Qt::CaseInsensitive); + 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); - - if (converted_term.toLower() == "none" || converted_term.toLower() == "colorless" || converted_term.toLower() == "c" || converted_term.toLower() == "colourless") { - if (info->getColors().length() < 1) { - return true; - } + + // Colorless card filter + if (converted_term.toLower() == "c" && info->getColors().length() < 1) + { + 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 */ - - match_count = 0; - for (it = converted_term.begin(); it != converted_term.end(); it++) { - for (i = info->getColors().constBegin(); i != info->getColors().constEnd(); i++) - if ((*i).contains((*it), Qt::CaseInsensitive)) { + /* + * 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 + */ + int match_count = 0; + for (auto it = converted_term.begin(); it != converted_term.end(); it++) + { + for (auto i = info->getColors().constBegin(); i != info->getColors().constEnd(); i++) + { + if ((*i).contains((*it), Qt::CaseInsensitive)) + { match_count++; } + } } return match_count == converted_term.length(); @@ -205,16 +229,16 @@ bool FilterItem::acceptText(const CardInfo *info) const bool FilterItem::acceptSet(const CardInfo *info) const { - SetList::const_iterator i; - bool status; - - status = false; - for (i = info->getSets().constBegin(); i != info->getSets().constEnd(); i++) - if ((*i)->getShortName().compare(term, Qt::CaseInsensitive) == 0 - || (*i)->getLongName().compare(term, Qt::CaseInsensitive) == 0) { + bool status = false; + for (auto i = info->getSets().constBegin(); i != info->getSets().constEnd(); i++) + { + if ((*i)->getShortName().compare(term, Qt::CaseInsensitive) == 0 + || (*i)->getLongName().compare(term, Qt::CaseInsensitive) == 0) + { status = true; break; } + } return status; } @@ -232,59 +256,73 @@ bool FilterItem::acceptCmc(const CardInfo *info) const bool FilterItem::acceptPower(const CardInfo *info) const { int slash = info->getPowTough().indexOf("/"); - if (slash != -1) - return (info->getPowTough().mid(0,slash) == term); - - return false; + return (slash != -1) ? (info->getPowTough().mid(0,slash) == term) : false; } bool FilterItem::acceptToughness(const CardInfo *info) const { int slash = info->getPowTough().indexOf("/"); - if (slash != -1) - return (info->getPowTough().mid(slash+1) == term); - - return false; + return (slash != -1) ? (info->getPowTough().mid(slash+1) == term) : false; } bool FilterItem::acceptRarity(const CardInfo *info) const { - foreach (QString rareLevel, info->getRarities()) - if (rareLevel.compare(term, Qt::CaseInsensitive) == 0) - return true; + QString converted_term = term.trimmed(); + /* + * 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; } bool FilterItem::acceptCardAttr(const CardInfo *info, CardFilter::Attr attr) const { - switch (attr) { - case CardFilter::AttrName: - return acceptName(info); - case CardFilter::AttrType: - return acceptType(info); - case CardFilter::AttrColor: - return acceptColor(info); - case CardFilter::AttrText: - return acceptText(info); - case CardFilter::AttrSet: - return acceptSet(info); - case CardFilter::AttrManaCost: - return acceptManaCost(info); - 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 */ + switch (attr) + { + case CardFilter::AttrName: return acceptName(info); + case CardFilter::AttrType: return acceptType(info); + case CardFilter::AttrColor: return acceptColor(info); + case CardFilter::AttrText: return acceptText(info); + case CardFilter::AttrSet: return acceptSet(info); + case CardFilter::AttrManaCost: return acceptManaCost(info); + 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. */ FilterTree::FilterTree() {} @@ -293,16 +331,19 @@ FilterTree::~FilterTree() {} LogicMap *FilterTree::attrLogicMap(CardFilter::Attr attr) { QList::iterator i; - int count; - count = 0; - for (i = childNodes.begin(); i != childNodes.end(); i++) { + int count = 0; + for (i = childNodes.begin(); i != childNodes.end(); i++) + { if ((*i)->attr == attr) + { break; + } count++; } - if (i == childNodes.end()) { + if (i == childNodes.end()) + { preInsertChild(this, count); i = childNodes.insert(i, new LogicMap(attr, this)); postInsertChild(this, count); @@ -312,14 +353,12 @@ LogicMap *FilterTree::attrLogicMap(CardFilter::Attr attr) return *i; } -FilterItemList *FilterTree::attrTypeList(CardFilter::Attr attr, - CardFilter::Type type) +FilterItemList *FilterTree::attrTypeList(CardFilter::Attr attr, CardFilter::Type type) { return attrLogicMap(attr)->typeList(type); } -int FilterTree::findTermIndex(CardFilter::Attr attr, CardFilter::Type type, - const QString &term) +int FilterTree::findTermIndex(CardFilter::Attr attr, CardFilter::Type type, const QString &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()); } -FilterTreeNode *FilterTree::termNode(CardFilter::Attr attr, CardFilter::Type type, - const QString &term) +FilterTreeNode *FilterTree::termNode(CardFilter::Attr attr, CardFilter::Type type, const QString &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()); } -FilterTreeNode *FilterTree::attrTypeNode(CardFilter::Attr attr, - CardFilter::Type type) +FilterTreeNode *FilterTree::attrTypeNode(CardFilter::Attr attr, CardFilter::Type 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 { const FilterItemList *fil; - bool status; - - status = true; + bool status = true; 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; + } 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; + } fil = lm->findTypeList(CardFilter::TypeOr); - if (fil != NULL && fil->isEnabled()) { + if (fil && fil->isEnabled()) + { status = false; + // if this is true we can return because it is OR'd with the OrNot list if (fil->testTypeOr(info, lm->attr)) + { return true; + } } 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 status; } bool FilterTree::acceptsCard(const CardInfo *info) const { - QList::const_iterator i; - - for (i = childNodes.constBegin(); i != childNodes.constEnd(); i++) + for (auto i = childNodes.constBegin(); i != childNodes.constEnd(); i++) + { if ((*i)->isEnabled() && !testAttr(info, *i)) + { return false; + } + } return true; } void FilterTree::clear() { - while(childCount() > 0) + while (childCount() > 0) + { deleteAt(0); -} + } +} \ No newline at end of file