mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-18 08:22:15 -07:00
get thing to work
This commit is contained in:
parent
26d8452492
commit
f50d2acf8a
1 changed files with 35 additions and 14 deletions
|
|
@ -3,30 +3,51 @@
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the card text to determine if the card enters the battlefield tapped.
|
* Parses the card text to determine if the card should have the cipt tag
|
||||||
*
|
*
|
||||||
* The parsing logic will be able to handle the following cases:
|
* The parsing logic is able to handle the following cases:
|
||||||
* - "<name> enters tapped..."
|
* - "<name> enters tapped"
|
||||||
* - shortname, if it appears at the start of the name.
|
* - "<shortname> enters tapped", if the card name starts with the shortname
|
||||||
* - "This <type> enters tapped..."
|
* - "This <type> enters tapped"
|
||||||
|
* - "..., it enters tapped"
|
||||||
* - Any naming scheme that appends a non-alphanumeric character plus extra text to the end of the name.
|
* - 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...")
|
* (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:
|
* However, it will still miss on certain cases:
|
||||||
* - shortnames that aren't the first word in the card name
|
* - shortnames that aren't the at the beginning of the card name
|
||||||
|
*
|
||||||
|
* Note that "...enters tapped unless..." returns false.
|
||||||
*
|
*
|
||||||
* @param name The name of the card
|
* @param name The name of the card
|
||||||
* @param text The oracle text of the card
|
* @param text The oracle text of the card
|
||||||
*/
|
*/
|
||||||
bool parseCipt(const QString &name, const QString &text)
|
bool parseCipt(const QString &name, const QString &text)
|
||||||
{
|
{
|
||||||
static auto WHITESPACE_REGEX = QRegularExpression("\\s+");
|
// try to split shortname on all non-alphanumeric characters (including _)
|
||||||
QString shortname = name.split(WHITESPACE_REGEX, Qt::SkipEmptyParts).first();
|
static auto SHORTNAME_DIVIDERS = QRegularExpression("[^\\w]|_");
|
||||||
|
|
||||||
QString namePattern = QString("( it|this [^ ]* |%1|%2.*)")
|
// Try all possible shortnames.
|
||||||
.arg(QRegularExpression::escape(name), QRegularExpression::escape(shortname));
|
// This also handles the case of extra text appended at end.
|
||||||
|
QStringList possibleNames;
|
||||||
|
qsizetype i = 0;
|
||||||
|
while ((i = name.indexOf(SHORTNAME_DIVIDERS, i)) != -1) {
|
||||||
|
possibleNames.append(QRegularExpression::escape(name.first(i)));
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
QRegularExpression ciptRegex(namePattern + " enters( the battlefield)? tapped(?! unless)");
|
// and the full name
|
||||||
|
possibleNames.append(QRegularExpression::escape(name));
|
||||||
|
|
||||||
return ciptRegex.match(text).hasMatch();
|
QString subject = "(it|" // "..., it enters tapped"
|
||||||
|
"(T|t)his [^ ]+|" // "This <type> enters tapped"
|
||||||
|
+ possibleNames.join("|") + ")";
|
||||||
|
|
||||||
|
auto ciptPattern = QRegularExpression(
|
||||||
|
// cipt phrase is either first sentence of line, or is after a punctuation mark
|
||||||
|
"(^|(, |\\. ))" + subject +
|
||||||
|
// support old wording, and exclude the "unless" case
|
||||||
|
" enters( the battlefield)? tapped(?! unless)",
|
||||||
|
QRegularExpression::MultilineOption);
|
||||||
|
|
||||||
|
return ciptPattern.match(text).hasMatch();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue