Initial commit of mtgonline project
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
#include "subtype_tally.h"
|
||||
|
||||
#include "../board/card_item.h"
|
||||
|
||||
#include <QMap>
|
||||
#include <algorithm>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
/** @brief Extracts subtypes from a single card face's type line. */
|
||||
QStringList extractSubtypesFromFace(const QString &faceType)
|
||||
{
|
||||
// Card type format: "Creature — Goblin Warrior" or "Legendary Enchantment — Saga"
|
||||
QStringList parts = faceType.split(QStringLiteral(" — "));
|
||||
if (parts.size() > 1) {
|
||||
return parts[1].split(QStringLiteral(" "), Qt::SkipEmptyParts);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
/** @brief A single subtype (e.g., "Goblin", "Warrior") with its occurrence count. */
|
||||
struct SubtypeEntry
|
||||
{
|
||||
QString name;
|
||||
int count;
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
namespace SubtypeTally
|
||||
{
|
||||
|
||||
QList<TallyRow> countSubtypes(const QList<CardItem *> &cards)
|
||||
{
|
||||
QMap<QString, int> subtypeCounts;
|
||||
|
||||
for (CardItem *card : cards) {
|
||||
if (card->getFaceDown() || card->getCard().isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QString cardType = card->getCardInfo().getCardType();
|
||||
// Handle double-faced cards: "Creature — Human // Creature — Werewolf"
|
||||
QStringList cardFaces = cardType.split(QStringLiteral(" // "));
|
||||
|
||||
for (const QString &face : cardFaces) {
|
||||
QStringList subtypes = extractSubtypesFromFace(face);
|
||||
for (const QString &subtype : subtypes) {
|
||||
subtypeCounts[subtype]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QList<SubtypeEntry> entries;
|
||||
for (auto it = subtypeCounts.constBegin(); it != subtypeCounts.constEnd(); ++it) {
|
||||
entries.append({it.key(), it.value()});
|
||||
}
|
||||
|
||||
// Sort by count ascending, then alphabetically (lowest counts at bottom of display)
|
||||
std::sort(entries.begin(), entries.end(), [](const SubtypeEntry &a, const SubtypeEntry &b) {
|
||||
if (a.count != b.count) {
|
||||
return a.count < b.count;
|
||||
}
|
||||
return a.name < b.name;
|
||||
});
|
||||
|
||||
// convert entries into TallyRows
|
||||
QList<TallyRow> rows;
|
||||
rows.reserve(entries.size()); // for backwards compatibility with Qt5
|
||||
std::transform(entries.begin(), entries.end(), std::back_inserter(rows),
|
||||
[](const SubtypeEntry &e) { return TallyRow{e.name, QString::number(e.count)}; });
|
||||
|
||||
return rows;
|
||||
}
|
||||
|
||||
} // namespace SubtypeTally
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef COCKATRICE_SUBTYPE_TALLY_H
|
||||
#define COCKATRICE_SUBTYPE_TALLY_H
|
||||
|
||||
#include "tally.h"
|
||||
|
||||
#include <QList>
|
||||
#include <QString>
|
||||
|
||||
class CardItem;
|
||||
|
||||
/**
|
||||
* @brief Extracts and tallies subtypes from selected cards.
|
||||
*/
|
||||
namespace SubtypeTally
|
||||
{
|
||||
/**
|
||||
* @brief Parses card type lines and counts each subtype occurrence.
|
||||
*
|
||||
* Skips face-down cards and cards without type info.
|
||||
* @param cards The list of selected card items to analyze.
|
||||
* @return Entries sorted by count ascending, then alphabetically.
|
||||
*/
|
||||
QList<TallyRow> countSubtypes(const QList<CardItem *> &cards);
|
||||
} // namespace SubtypeTally
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
#include "tally.h"
|
||||
|
||||
#include "subtype_tally.h"
|
||||
|
||||
QList<TallyRow> Tally::compute(const QList<CardItem *> &cards, const TallyType type)
|
||||
{
|
||||
switch (type) {
|
||||
case TallyType::None:
|
||||
return {};
|
||||
case TallyType::Subtypes:
|
||||
return SubtypeTally::countSubtypes(cards);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#ifndef COCKATRICE_TALLY_H
|
||||
#define COCKATRICE_TALLY_H
|
||||
#include <QString>
|
||||
|
||||
class CardItem;
|
||||
|
||||
/** @brief A single row of the tally output. */
|
||||
struct TallyRow
|
||||
{
|
||||
QString name; ///< The row name (displayed on the left)
|
||||
QString value; ///< Value for the row (displayed on the right)
|
||||
|
||||
bool operator==(const TallyRow &) const = default;
|
||||
};
|
||||
|
||||
/**
|
||||
* The tally type
|
||||
*/
|
||||
enum class TallyType
|
||||
{
|
||||
None,
|
||||
Subtypes,
|
||||
};
|
||||
|
||||
namespace Tally
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief Analyzes the selected cards according to the tally type and builds the resulting tally rows.
|
||||
* This forwards the cards to the code for that tally type.
|
||||
*
|
||||
* @param cards The list of selected card items to analyze.
|
||||
* @param type The type of tally to do
|
||||
* @return Rows sorted in top-to-bottom display order
|
||||
*/
|
||||
QList<TallyRow> compute(const QList<CardItem *> &cards, TallyType type);
|
||||
|
||||
} // namespace Tally
|
||||
|
||||
#endif // COCKATRICE_TALLY_H
|
||||
Reference in New Issue
Block a user