mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-05 21:13:55 -07:00
create TallyManager
This commit is contained in:
parent
86256602ff
commit
f63279e12f
3 changed files with 129 additions and 0 deletions
|
|
@ -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
|
||||
|
|
|
|||
74
cockatrice/src/game/tally/tally_manager.cpp
Normal file
74
cockatrice/src/game/tally/tally_manager.cpp
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
#include "tally_manager.h"
|
||||
|
||||
#include "../board/card_item.h"
|
||||
|
||||
const QList<TallyManager::EntryType> 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::EntryType, TallyResult> &TallyManager::getAllResults() const
|
||||
{
|
||||
return resultsMap;
|
||||
}
|
||||
|
||||
void TallyManager::updateSelection(const QList<CardItem *> &cards)
|
||||
{
|
||||
updateResultsMap(cards);
|
||||
emit tallyChanged(resultsMap);
|
||||
}
|
||||
|
||||
void TallyManager::updateResultsMap(const QList<CardItem *> &cards)
|
||||
{
|
||||
for (auto i = resultsMap.begin(); i != resultsMap.end(); ++i) {
|
||||
i.value() = computeTallyResult(i.key(), cards);
|
||||
}
|
||||
}
|
||||
|
||||
static int totalCount(const QList<CardItem *> &cards)
|
||||
{
|
||||
return cards.count();
|
||||
}
|
||||
|
||||
static int totalPower(const QList<CardItem *> &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<CardItem *> &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<CardItem *> &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 {};
|
||||
}
|
||||
53
cockatrice/src/game/tally/tally_manager.h
Normal file
53
cockatrice/src/game/tally/tally_manager.h
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#ifndef COCKATRICE_TALLY_MANAGER_H
|
||||
#define COCKATRICE_TALLY_MANAGER_H
|
||||
|
||||
#include <QMap>
|
||||
#include <QObject>
|
||||
|
||||
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<EntryType> ENTRY_TYPES;
|
||||
|
||||
explicit TallyManager(QObject *parent = nullptr);
|
||||
|
||||
const QMap<EntryType, TallyResult> &getAllResults() const;
|
||||
|
||||
public slots:
|
||||
void updateSelection(const QList<CardItem *> &cards);
|
||||
|
||||
signals:
|
||||
void tallyChanged(const QMap<EntryType, TallyResult> &results);
|
||||
|
||||
private:
|
||||
QMap<EntryType, TallyResult> resultsMap;
|
||||
|
||||
static TallyResult computeTallyResult(EntryType type, const QList<CardItem *> &cards);
|
||||
|
||||
void updateResultsMap(const QList<CardItem *> &cards);
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_TALLY_MANAGER_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue