mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-15 11:38:49 -07:00
warn if "play top card until" filter expression doesn't match any card in database (#5243)
* make FilterString::check const * implement thing
This commit is contained in:
parent
b2ad2acff3
commit
a5de633c64
3 changed files with 52 additions and 5 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
#include "dlg_move_top_cards_until.h"
|
#include "dlg_move_top_cards_until.h"
|
||||||
|
|
||||||
|
#include "../game/cards/card_database.h"
|
||||||
|
#include "../game/cards/card_database_manager.h"
|
||||||
#include "../game/filters/filter_string.h"
|
#include "../game/filters/filter_string.h"
|
||||||
#include "trice_limits.h"
|
#include "trice_limits.h"
|
||||||
|
|
||||||
|
|
@ -45,14 +47,57 @@ DlgMoveTopCardsUntil::DlgMoveTopCardsUntil(QWidget *parent, QString _expr, uint
|
||||||
setWindowTitle(tr("Put top cards on stack until..."));
|
setWindowTitle(tr("Put top cards on stack until..."));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Checks if a card matching the expr exists in the card database.
|
||||||
|
*
|
||||||
|
* @returns true if a card matching the expression exists.
|
||||||
|
*/
|
||||||
|
static bool matchExistsInDb(const FilterString &filterString)
|
||||||
|
{
|
||||||
|
const auto cardDatabase = CardDatabaseManager::getInstance();
|
||||||
|
const auto allCards = cardDatabase->getCardList();
|
||||||
|
|
||||||
|
const auto it = std::find_if(allCards.begin(), allCards.end(),
|
||||||
|
[&filterString](const CardInfoPtr &card) { return filterString.check(card); });
|
||||||
|
|
||||||
|
return it != allCards.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Validates that a card matching the expr exists in the card database.
|
||||||
|
* If no match is found, then pop up a window to warn the user, giving them a chance to back out.
|
||||||
|
*
|
||||||
|
* @returns whether to proceed with the action
|
||||||
|
*/
|
||||||
|
bool DlgMoveTopCardsUntil::validateMatchExists(const FilterString &filterString)
|
||||||
|
{
|
||||||
|
if (matchExistsInDb(filterString)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto msg = tr("No cards matching the search expression exists in the card database. Proceed anyways?");
|
||||||
|
const auto res =
|
||||||
|
QMessageBox::warning(this, tr("Cockatrice"), msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
|
||||||
|
if (res == QMessageBox::No) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void DlgMoveTopCardsUntil::validateAndAccept()
|
void DlgMoveTopCardsUntil::validateAndAccept()
|
||||||
{
|
{
|
||||||
auto movingCardsUntilFilter = FilterString(exprEdit->text());
|
auto movingCardsUntilFilter = FilterString(exprEdit->text());
|
||||||
if (movingCardsUntilFilter.valid()) {
|
if (!movingCardsUntilFilter.valid()) {
|
||||||
accept();
|
|
||||||
} else {
|
|
||||||
QMessageBox::warning(this, tr("Invalid filter"), movingCardsUntilFilter.error(), QMessageBox::Ok);
|
QMessageBox::warning(this, tr("Invalid filter"), movingCardsUntilFilter.error(), QMessageBox::Ok);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!validateMatchExists(movingCardsUntilFilter)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
accept();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString DlgMoveTopCardsUntil::getExpr() const
|
QString DlgMoveTopCardsUntil::getExpr() const
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,8 @@
|
||||||
#include <QLabel>
|
#include <QLabel>
|
||||||
#include <QLineEdit>
|
#include <QLineEdit>
|
||||||
#include <QSpinBox>
|
#include <QSpinBox>
|
||||||
#include <QString>
|
|
||||||
|
class FilterString;
|
||||||
|
|
||||||
class DlgMoveTopCardsUntil : public QDialog
|
class DlgMoveTopCardsUntil : public QDialog
|
||||||
{
|
{
|
||||||
|
|
@ -18,6 +19,7 @@ class DlgMoveTopCardsUntil : public QDialog
|
||||||
QDialogButtonBox *buttonBox;
|
QDialogButtonBox *buttonBox;
|
||||||
|
|
||||||
void validateAndAccept();
|
void validateAndAccept();
|
||||||
|
bool validateMatchExists(const FilterString &filterString);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit DlgMoveTopCardsUntil(QWidget *parent = nullptr, QString expr = QString(), uint numberOfHits = 1);
|
explicit DlgMoveTopCardsUntil(QWidget *parent = nullptr, QString expr = QString(), uint numberOfHits = 1);
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ class FilterString
|
||||||
public:
|
public:
|
||||||
FilterString();
|
FilterString();
|
||||||
explicit FilterString(const QString &exp);
|
explicit FilterString(const QString &exp);
|
||||||
bool check(const CardData &card)
|
bool check(const CardData &card) const
|
||||||
{
|
{
|
||||||
return result(card);
|
return result(card);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue