mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
* Simplify add card. Took 25 minutes Took 8 minutes # Commit time for manual adjustment: # Took 16 minutes Took 7 seconds * Refactor out db loading from card db. Took 39 minutes Took 9 minutes Took 2 minutes Took 17 seconds * Refactor out db queries from card db. Took 42 minutes * Lint. Took 3 minutes * I guess. Took 7 minutes * Tests. Took 15 minutes * I don't understand this. Took 9 minutes * fix linker errors * Rename to querier and promote to QObject Took 39 minutes * Lint. Took 3 minutes --------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de> Co-authored-by: ebbit1q <ebbit1q@gmail.com>
82 lines
2.5 KiB
C++
82 lines
2.5 KiB
C++
#include "deck_stats_interface.h"
|
|
|
|
#include "deck_list.h"
|
|
#include "deck_list_card_node.h"
|
|
|
|
#include <QDesktopServices>
|
|
#include <QMessageBox>
|
|
#include <QNetworkAccessManager>
|
|
#include <QNetworkReply>
|
|
#include <QNetworkRequest>
|
|
#include <QRegularExpression>
|
|
#include <QUrlQuery>
|
|
|
|
DeckStatsInterface::DeckStatsInterface(CardDatabase &_cardDatabase, QObject *parent)
|
|
: QObject(parent), cardDatabase(_cardDatabase)
|
|
{
|
|
manager = new QNetworkAccessManager(this);
|
|
connect(manager, &QNetworkAccessManager::finished, this, &DeckStatsInterface::queryFinished);
|
|
}
|
|
|
|
void DeckStatsInterface::queryFinished(QNetworkReply *reply)
|
|
{
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
QMessageBox::critical(nullptr, tr("Error"), reply->errorString());
|
|
reply->deleteLater();
|
|
deleteLater();
|
|
return;
|
|
}
|
|
|
|
QString data(reply->readAll());
|
|
reply->deleteLater();
|
|
|
|
static const QRegularExpression rx("<meta property=\"og:url\" content=\"([^\"]+)\"");
|
|
auto match = rx.match(data);
|
|
if (!match.hasMatch()) {
|
|
QMessageBox::critical(nullptr, tr("Error"), tr("The reply from the server could not be parsed."));
|
|
deleteLater();
|
|
return;
|
|
}
|
|
|
|
QString deckUrl = match.captured(1);
|
|
QDesktopServices::openUrl(deckUrl);
|
|
|
|
deleteLater();
|
|
}
|
|
|
|
void DeckStatsInterface::getAnalyzeRequestData(DeckList *deck, QByteArray *data)
|
|
{
|
|
DeckList deckWithoutTokens;
|
|
copyDeckWithoutTokens(*deck, deckWithoutTokens);
|
|
|
|
QUrl params;
|
|
QUrlQuery urlQuery;
|
|
urlQuery.addQueryItem("deck", deckWithoutTokens.writeToString_Plain());
|
|
urlQuery.addQueryItem("decktitle", deck->getName());
|
|
params.setQuery(urlQuery);
|
|
data->append(params.query(QUrl::EncodeReserved).toUtf8());
|
|
}
|
|
|
|
void DeckStatsInterface::analyzeDeck(DeckList *deck)
|
|
{
|
|
QByteArray data;
|
|
getAnalyzeRequestData(deck, &data);
|
|
|
|
QNetworkRequest request(QUrl("https://deckstats.net/index.php"));
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
|
|
|
manager->post(request, data);
|
|
}
|
|
|
|
void DeckStatsInterface::copyDeckWithoutTokens(DeckList &source, DeckList &destination)
|
|
{
|
|
auto copyIfNotAToken = [this, &destination](const auto node, const auto card) {
|
|
CardInfoPtr dbCard = cardDatabase.query()->getCardInfo(card->getName());
|
|
if (dbCard && !dbCard->getIsToken()) {
|
|
DecklistCardNode *addedCard = destination.addCard(card->getName(), node->getName(), -1);
|
|
addedCard->setNumber(card->getNumber());
|
|
}
|
|
};
|
|
|
|
source.forEachCard(copyIfNotAToken);
|
|
}
|