diff --git a/cockatrice/CMakeLists.txt b/cockatrice/CMakeLists.txt index 0b2192399..4ffec752f 100644 --- a/cockatrice/CMakeLists.txt +++ b/cockatrice/CMakeLists.txt @@ -107,6 +107,8 @@ set(cockatrice_SOURCES src/game/player/player_manager.cpp src/game/player/player_target.cpp src/game/replay.cpp + src/game/tally/tally_manager.cpp + src/game/tally/tally_manager.h src/game/zones/card_zone_logic.cpp src/game/zones/hand_zone_logic.cpp src/game/zones/pile_zone_logic.cpp diff --git a/cockatrice/src/game/tally/tally_manager.cpp b/cockatrice/src/game/tally/tally_manager.cpp new file mode 100644 index 000000000..0e372241a --- /dev/null +++ b/cockatrice/src/game/tally/tally_manager.cpp @@ -0,0 +1,74 @@ +#include "tally_manager.h" + +#include "../board/card_item.h" + +const QList TallyManager::ENTRY_TYPES = {COUNT, TOTAL_POWER, TOTAL_MANA_VALUE}; + +TallyManager::TallyManager(QObject *parent) : QObject(parent) +{ + for (auto entryType : ENTRY_TYPES) { + resultsMap.insert(entryType, {}); + } + updateResultsMap({}); +} + +const QMap &TallyManager::getAllResults() const +{ + return resultsMap; +} + +void TallyManager::updateSelection(const QList &cards) +{ + updateResultsMap(cards); + emit tallyChanged(resultsMap); +} + +void TallyManager::updateResultsMap(const QList &cards) +{ + for (auto i = resultsMap.begin(); i != resultsMap.end(); ++i) { + i.value() = computeTallyResult(i.key(), cards); + } +} + +static int totalCount(const QList &cards) +{ + return cards.count(); +} + +static int totalPower(const QList &cards) +{ + static const auto powerRegex = QRegularExpression("(\\d+)/(\\d+)"); + int total = 0; + for (auto card : cards) { + QString pt = card->getPT(); + auto match = powerRegex.match(pt); + if (match.hasMatch()) { + int power = match.captured(1).toInt(); // defaults to 0 if string can't be parsed as int + total += power; + } + } + return total; +} + +static int totalManaValue(const QList &cards) +{ + int total = 0; + for (auto card : cards) { + int mv = card->getCardInfo().getCmc().toInt(); + total += mv; + } + return total; +} + +TallyResult TallyManager::computeTallyResult(EntryType type, const QList &cards) +{ + switch (type) { + case COUNT: + return {tr("Count"), totalCount(cards)}; + case TOTAL_POWER: + return {tr("Total Power"), totalPower(cards)}; + case TOTAL_MANA_VALUE: + return {tr("Total Mana Value"), totalManaValue(cards)}; + } + return {}; +} diff --git a/cockatrice/src/game/tally/tally_manager.h b/cockatrice/src/game/tally/tally_manager.h new file mode 100644 index 000000000..da9394957 --- /dev/null +++ b/cockatrice/src/game/tally/tally_manager.h @@ -0,0 +1,53 @@ +#ifndef COCKATRICE_TALLY_MANAGER_H +#define COCKATRICE_TALLY_MANAGER_H + +#include +#include + +class CardItem; + +/** + * The result of a tally + */ +struct TallyResult +{ + QString text; ///< The displayed text. + int value = 0; ///< The computed value +}; + +/** + * Responsible for managing all the tally counts and states + */ +class TallyManager : public QObject +{ + Q_OBJECT + +public: + enum EntryType + { + COUNT, + TOTAL_POWER, + TOTAL_MANA_VALUE + }; + + static const QList ENTRY_TYPES; + + explicit TallyManager(QObject *parent = nullptr); + + const QMap &getAllResults() const; + +public slots: + void updateSelection(const QList &cards); + +signals: + void tallyChanged(const QMap &results); + +private: + QMap resultsMap; + + static TallyResult computeTallyResult(EntryType type, const QList &cards); + + void updateResultsMap(const QList &cards); +}; + +#endif // COCKATRICE_TALLY_MANAGER_H