diff --git a/oracle/CMakeLists.txt b/oracle/CMakeLists.txt index addfecd78..43b2b9b8d 100644 --- a/oracle/CMakeLists.txt +++ b/oracle/CMakeLists.txt @@ -15,6 +15,7 @@ set(oracle_SOURCES src/oraclewizard.cpp src/oracleimporter.cpp src/pagetemplates.cpp + src/parsehelpers.cpp src/qt-json/json.cpp ../cockatrice/src/game/cards/card_database.cpp ../cockatrice/src/game/cards/card_database_manager.cpp diff --git a/oracle/src/oracleimporter.cpp b/oracle/src/oracleimporter.cpp index 518d42967..31cf0e9a5 100644 --- a/oracle/src/oracleimporter.cpp +++ b/oracle/src/oracleimporter.cpp @@ -1,6 +1,7 @@ #include "oracleimporter.h" #include "game/cards/card_database_parser/cockatrice_xml_4.h" +#include "parsehelpers.h" #include "qt-json/json.h" #include @@ -107,19 +108,6 @@ QString OracleImporter::getMainCardType(const QStringList &typeList) return typeList.first(); } -/** - * Parses the card text to determine if the card enters the battlefield tapped. - * - * @param name The name of the card - * @param text The full oracle text of the card - */ -static bool parseCipt(const QString &name, const QString &text) -{ - QRegularExpression ciptRegex("( it|" + QRegularExpression::escape(name) + - ") enters( the battlefield)? tapped(?! unless)"); - return ciptRegex.match(text).hasMatch(); -} - CardInfoPtr OracleImporter::addCard(QString name, QString text, bool isToken, diff --git a/oracle/src/parsehelpers.cpp b/oracle/src/parsehelpers.cpp new file mode 100644 index 000000000..779ba956f --- /dev/null +++ b/oracle/src/parsehelpers.cpp @@ -0,0 +1,32 @@ +#include "parsehelpers.h" + +#include + +/** + * Parses the card text to determine if the card enters the battlefield tapped. + * + * The parsing logic will be able to handle the following cases: + * - " enters tapped..." + * - shortname, if it appears at the start of the name. + * - "This enters tapped..." + * - Any naming scheme that appends a non-alphanumeric character plus extra text to the end of the name. + * (e.g. name is "Card Name_SET" or "Card Name (Set)" and text contains "Card Name enters tapped...") + * + * However, it will still miss certain cases: + * - shortnames that aren't the first word in the card name + * + * @param name The name of the card + * @param text The oracle text of the card + */ +bool parseCipt(const QString &name, const QString &text) +{ + static auto WHITESPACE_REGEX = QRegularExpression("\\s+"); + QString shortname = name.split(WHITESPACE_REGEX, Qt::SkipEmptyParts).first(); + + QString namePattern = QString("( it|this [^ ]* |%1|%2.*)") + .arg(QRegularExpression::escape(name), QRegularExpression::escape(shortname)); + + QRegularExpression ciptRegex(namePattern + " enters( the battlefield)? tapped(?! unless)"); + + return ciptRegex.match(text).hasMatch(); +} diff --git a/oracle/src/parsehelpers.h b/oracle/src/parsehelpers.h new file mode 100644 index 000000000..40a36c753 --- /dev/null +++ b/oracle/src/parsehelpers.h @@ -0,0 +1,8 @@ +#ifndef PARSEHELPERS_H +#define PARSEHELPERS_H + +#include + +bool parseCipt(const QString &name, const QString &text); + +#endif // PARSEHELPERS_H