Initial commit of mtgonline project
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
#include "arrow_data.h"
|
||||
|
||||
ArrowData ArrowData::fromProto(const ServerInfo_Arrow &arrow, int creatorId, bool isLocalCreator)
|
||||
{
|
||||
ArrowData data;
|
||||
data.creatorId = creatorId;
|
||||
data.isLocalCreator = isLocalCreator;
|
||||
data.id = arrow.id();
|
||||
data.startPlayerId = arrow.start_player_id();
|
||||
data.startZone = QString::fromStdString(arrow.start_zone());
|
||||
data.startCardId = arrow.start_card_id();
|
||||
data.targetPlayerId = arrow.target_player_id();
|
||||
data.color = convertColorToQColor(arrow.arrow_color());
|
||||
|
||||
if (arrow.has_target_zone()) {
|
||||
data.targetZone = QString::fromStdString(arrow.target_zone());
|
||||
data.targetCardId = arrow.target_card_id();
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef COCKATRICE_ARROW_DATA_H
|
||||
#define COCKATRICE_ARROW_DATA_H
|
||||
|
||||
#include <QColor>
|
||||
#include <QString>
|
||||
#include <libcockatrice/protocol/pb/serverinfo_arrow.pb.h>
|
||||
#include <libcockatrice/utility/color.h>
|
||||
|
||||
struct ArrowData
|
||||
{
|
||||
int creatorId = -1;
|
||||
bool isLocalCreator = false;
|
||||
int id = -1;
|
||||
int startPlayerId = -1;
|
||||
QString startZone = "";
|
||||
int startCardId = -1;
|
||||
int targetPlayerId = -1;
|
||||
QString targetZone = "";
|
||||
int targetCardId = -1;
|
||||
QColor color = "";
|
||||
|
||||
static ArrowData fromProto(const ServerInfo_Arrow &arrow, int creatorId, bool isLocalCreator);
|
||||
|
||||
bool isPlayerTargeted() const
|
||||
{
|
||||
return targetZone.isEmpty();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_ARROW_DATA_H
|
||||
@@ -0,0 +1,159 @@
|
||||
#include "card_list.h"
|
||||
|
||||
#include "../../game_graphics/board/card_item.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <algorithm>
|
||||
#include <libcockatrice/card/card_info.h>
|
||||
|
||||
CardList::CardList(bool _contentsKnown) : QList<CardItem *>(), contentsKnown(_contentsKnown)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Finds the CardItem with the given id in the list.
|
||||
* If contentsKnown is false, then this just returns the first element of the list.
|
||||
*
|
||||
* @param cardId The id of the card to find.
|
||||
*
|
||||
* @returns A pointer to the CardItem, or a nullptr if not found.
|
||||
*/
|
||||
CardItem *CardList::findCard(const int cardId) const
|
||||
{
|
||||
if (!contentsKnown && !empty()) {
|
||||
return at(0);
|
||||
} else {
|
||||
for (auto *cardItem : *this) {
|
||||
if (cardItem->getId() == cardId) {
|
||||
return cardItem;
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief sorts the list by using string comparison on properties extracted from the CardItem
|
||||
* The cards are compared using each property in order.
|
||||
* If two cards have the same value for a property, then the next property in the list is used.
|
||||
*
|
||||
* @param option the option to compare the cards by, in order of usage.
|
||||
*/
|
||||
void CardList::sortBy(const QList<SortOption> &option)
|
||||
{
|
||||
// early return if we know we won't be sorting
|
||||
if (option.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto comparator = [&option](CardItem *a, CardItem *b) {
|
||||
for (auto prop : option) {
|
||||
auto extractor = getExtractorFor(prop);
|
||||
QString t1 = extractor(a);
|
||||
QString t2 = extractor(b);
|
||||
if (t1 != t2) {
|
||||
return t1 < t2;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
std::sort(begin(), end(), comparator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a String for the card such that when sorting cards using that string, it will result in the
|
||||
* following sort order:
|
||||
* - Unrecognized colors
|
||||
* - Land cards
|
||||
* - Colorless cards
|
||||
* - Monocolor cards, in wubrg order
|
||||
* - Monocolor cards of any custom colors
|
||||
* - 2C cards (no internal order)
|
||||
* - 3C cards (no internal order)
|
||||
* - 4C cards (no internal order)
|
||||
* - 5C cards (no internal order)
|
||||
*
|
||||
* @param c The card info
|
||||
* @param appendAtEnd For multicolor cards, whether to also append the entire color string at the end.
|
||||
*/
|
||||
static QString getColorSortString(const CardInfo &c, bool appendAtEnd)
|
||||
{
|
||||
QString colors = c.getColors();
|
||||
switch (colors.size()) {
|
||||
case 0: {
|
||||
if (c.getCardType().contains("Land")) {
|
||||
return "a_land";
|
||||
} else {
|
||||
return "b_colorless";
|
||||
}
|
||||
}
|
||||
case 1:
|
||||
// force wubrg order
|
||||
switch (colors.at(0).toLatin1()) {
|
||||
case 'W':
|
||||
return "c_W";
|
||||
case 'U':
|
||||
return "d_U";
|
||||
case 'B':
|
||||
return "e_B";
|
||||
case 'R':
|
||||
return "f_R";
|
||||
case 'G':
|
||||
return "g_G";
|
||||
default:
|
||||
// handle any custom colors
|
||||
return QString("h_%1").arg(colors.at(0));
|
||||
}
|
||||
default:
|
||||
return QString("i%1_%2").arg(colors.size()).arg(appendAtEnd ? colors : "");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief returns the function that extracts the given property from the CardItem.
|
||||
*/
|
||||
std::function<QString(CardItem *)> CardList::getExtractorFor(SortOption option)
|
||||
{
|
||||
switch (option) {
|
||||
case NoSort:
|
||||
return [](CardItem *) { return ""; };
|
||||
case SortByMainType:
|
||||
return [](CardItem *c) { return c->getCardInfo().getMainCardType(); };
|
||||
case SortByManaValue:
|
||||
// getCmc returns the int as a string. We pad with 0's so that string comp also works on it
|
||||
return [](CardItem *c) { return c->getCard() ? c->getCardInfo().getCmc().rightJustified(4, '0') : ""; };
|
||||
case SortByColorGrouping:
|
||||
return [](CardItem *c) { return c->getCard() ? getColorSortString(c->getCardInfo(), false) : ""; };
|
||||
case SortByName:
|
||||
return [](CardItem *c) { return c->getName(); };
|
||||
case SortByType:
|
||||
return [](CardItem *c) { return c->getCardInfo().getCardType(); };
|
||||
case SortByManaCost:
|
||||
return [](CardItem *c) {
|
||||
if (!c->getCard()) {
|
||||
return QString();
|
||||
}
|
||||
|
||||
auto info = c->getCardInfo();
|
||||
|
||||
// calculation copied from CardDatabaseModel.
|
||||
// we pad the cmc and also append the mana cost to the end so same cmc cards still have a sort order
|
||||
return QString("%1%2").arg(info.getCmc(), 4, QChar('0')).arg(info.getManaCost());
|
||||
};
|
||||
case SortByColors:
|
||||
return [](CardItem *c) { return c->getCard() ? getColorSortString(c->getCardInfo(), true) : ""; };
|
||||
case SortByPt:
|
||||
// do the same padding trick as above
|
||||
return
|
||||
[](CardItem *c) { return c->getCard() ? c->getCardInfo().getPowTough().rightJustified(10, '0') : ""; };
|
||||
case SortBySet:
|
||||
return [](CardItem *c) { return c->getCardInfo().getSetsNames(); };
|
||||
case SortByPrinting:
|
||||
return [](CardItem *c) { return c->getProviderId(); };
|
||||
}
|
||||
|
||||
// this line should never be reached
|
||||
qCWarning(CardListLog) << "cardlist.cpp: Could not find extractor for SortOption" << option;
|
||||
return [](CardItem *) { return ""; };
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @file card_list.h
|
||||
* @ingroup GameLogicCards
|
||||
*/
|
||||
//! \todo Document this file.
|
||||
|
||||
#ifndef CARDLIST_H
|
||||
#define CARDLIST_H
|
||||
|
||||
#include <QList>
|
||||
#include <QLoggingCategory>
|
||||
|
||||
inline Q_LOGGING_CATEGORY(CardListLog, "card_list");
|
||||
|
||||
class CardItem;
|
||||
|
||||
class CardList : public QList<CardItem *>
|
||||
{
|
||||
protected:
|
||||
bool contentsKnown;
|
||||
|
||||
public:
|
||||
enum SortOption
|
||||
{
|
||||
NoSort,
|
||||
|
||||
// Options that are used by groupBy
|
||||
// Should partition all cards into a reasonable number of buckets
|
||||
SortByMainType,
|
||||
SortByManaValue,
|
||||
SortByColorGrouping,
|
||||
|
||||
// Options that are used by sortBy
|
||||
// We don't care about buckets; we want as many distinct values as possible.
|
||||
SortByName,
|
||||
SortByType,
|
||||
SortByManaCost,
|
||||
SortByColors,
|
||||
SortByPt,
|
||||
SortBySet,
|
||||
SortByPrinting
|
||||
};
|
||||
explicit CardList(bool _contentsKnown);
|
||||
CardItem *findCard(const int cardId) const;
|
||||
bool getContentsKnown() const
|
||||
{
|
||||
return contentsKnown;
|
||||
}
|
||||
|
||||
void sortBy(const QList<SortOption> &options);
|
||||
|
||||
static std::function<QString(CardItem *)> getExtractorFor(SortOption option);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,111 @@
|
||||
#include "card_state.h"
|
||||
|
||||
void CardState::resetState(bool keepAnnotations)
|
||||
{
|
||||
attacking = false;
|
||||
counters.clear();
|
||||
pt.clear();
|
||||
if (!keepAnnotations) {
|
||||
annotation.clear();
|
||||
}
|
||||
attachedTo = nullptr;
|
||||
}
|
||||
|
||||
void CardState::setZone(CardZoneLogic *_zone)
|
||||
{
|
||||
if (zone == _zone) {
|
||||
return;
|
||||
}
|
||||
|
||||
zone = _zone;
|
||||
emit zoneChanged(this, zone);
|
||||
emit stateChanged();
|
||||
}
|
||||
|
||||
void CardState::setAttacking(bool _attacking)
|
||||
{
|
||||
if (attacking == _attacking) {
|
||||
return;
|
||||
}
|
||||
attacking = _attacking;
|
||||
emit attackingChanged(_attacking);
|
||||
emit stateChanged();
|
||||
}
|
||||
|
||||
void CardState::insertCounter(int id, int value)
|
||||
{
|
||||
counters.insert(id, value);
|
||||
|
||||
emit countersChanged(counters);
|
||||
emit stateChanged();
|
||||
}
|
||||
|
||||
void CardState::setCounter(int id, int value)
|
||||
{
|
||||
if (value) {
|
||||
counters[id] = value;
|
||||
} else {
|
||||
counters.remove(id);
|
||||
}
|
||||
|
||||
emit countersChanged(counters);
|
||||
emit stateChanged();
|
||||
}
|
||||
|
||||
void CardState::clearCounters()
|
||||
{
|
||||
counters.clear();
|
||||
emit countersChanged(counters);
|
||||
emit stateChanged();
|
||||
}
|
||||
|
||||
void CardState::setAnnotation(const QString &_annotation)
|
||||
{
|
||||
if (annotation == _annotation) {
|
||||
return;
|
||||
}
|
||||
annotation = _annotation;
|
||||
emit annotationChanged(annotation);
|
||||
emit stateChanged();
|
||||
}
|
||||
|
||||
void CardState::setPT(const QString &_pt)
|
||||
{
|
||||
if (pt == _pt) {
|
||||
return;
|
||||
}
|
||||
pt = _pt;
|
||||
emit ptChanged(pt);
|
||||
emit stateChanged();
|
||||
}
|
||||
|
||||
void CardState::setDoesntUntap(bool _doesntUntap)
|
||||
{
|
||||
if (doesntUntap == _doesntUntap) {
|
||||
return;
|
||||
}
|
||||
doesntUntap = _doesntUntap;
|
||||
emit doesntUntapChanged(_doesntUntap);
|
||||
emit stateChanged();
|
||||
}
|
||||
|
||||
void CardState::setDestroyOnZoneChange(bool _destroyOnZoneChange)
|
||||
{
|
||||
if (destroyOnZoneChange == _destroyOnZoneChange) {
|
||||
return;
|
||||
}
|
||||
|
||||
destroyOnZoneChange = _destroyOnZoneChange;
|
||||
emit destroyOnZoneChangeChanged(_destroyOnZoneChange);
|
||||
emit stateChanged();
|
||||
}
|
||||
|
||||
void CardState::setAttachedTo(CardItem *_attachedTo)
|
||||
{
|
||||
if (attachedTo == _attachedTo) {
|
||||
return;
|
||||
}
|
||||
attachedTo = _attachedTo;
|
||||
emit attachedToChanged(_attachedTo);
|
||||
emit stateChanged();
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
#ifndef COCKATRICE_CARD_STATE_H
|
||||
#define COCKATRICE_CARD_STATE_H
|
||||
|
||||
#include <QMap>
|
||||
#include <QObject>
|
||||
|
||||
class CardZoneLogic;
|
||||
class CardItem;
|
||||
class CardState : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private:
|
||||
bool attacking = false;
|
||||
QMap<int, int> counters;
|
||||
QString annotation;
|
||||
QString pt;
|
||||
bool doesntUntap = false;
|
||||
bool destroyOnZoneChange = false;
|
||||
|
||||
CardItem *attachedTo = nullptr;
|
||||
CardZoneLogic *zone = nullptr;
|
||||
|
||||
signals:
|
||||
void stateChanged();
|
||||
|
||||
void attackingChanged(bool newValue);
|
||||
void countersChanged(const QMap<int, int> &newCounters);
|
||||
void annotationChanged(const QString &newAnnotation);
|
||||
void ptChanged(const QString &newPt);
|
||||
void doesntUntapChanged(bool newValue);
|
||||
void destroyOnZoneChangeChanged(bool newValue);
|
||||
void attachedToChanged(CardItem *newAttachedTo);
|
||||
void zoneChanged(CardState *changedCard, CardZoneLogic *newZone);
|
||||
|
||||
public:
|
||||
explicit CardState(QObject *parent, CardZoneLogic *_zone) : QObject(parent), zone(_zone)
|
||||
{
|
||||
}
|
||||
|
||||
void resetState(bool keepAnnotations);
|
||||
|
||||
CardZoneLogic *getZone() const
|
||||
{
|
||||
return zone;
|
||||
}
|
||||
|
||||
void setZone(CardZoneLogic *_zone);
|
||||
|
||||
bool getAttacking() const
|
||||
{
|
||||
return attacking;
|
||||
}
|
||||
void setAttacking(bool _attacking);
|
||||
|
||||
const QMap<int, int> &getCounters() const
|
||||
{
|
||||
return counters;
|
||||
}
|
||||
|
||||
void insertCounter(int id, int value);
|
||||
|
||||
void setCounter(int id, int value);
|
||||
|
||||
void clearCounters();
|
||||
|
||||
QString getAnnotation() const
|
||||
{
|
||||
return annotation;
|
||||
}
|
||||
|
||||
void setAnnotation(const QString &_annotation);
|
||||
|
||||
QString getPT() const
|
||||
{
|
||||
return pt;
|
||||
}
|
||||
|
||||
void setPT(const QString &_pt);
|
||||
|
||||
bool getDoesntUntap() const
|
||||
{
|
||||
return doesntUntap;
|
||||
}
|
||||
|
||||
void setDoesntUntap(bool _doesntUntap);
|
||||
|
||||
bool getDestroyOnZoneChange() const
|
||||
{
|
||||
return destroyOnZoneChange;
|
||||
}
|
||||
|
||||
void setDestroyOnZoneChange(bool _destroyOnZoneChange);
|
||||
|
||||
CardItem *getAttachedTo() const
|
||||
{
|
||||
return attachedTo;
|
||||
}
|
||||
|
||||
void setAttachedTo(CardItem *_attachedTo);
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_CARD_STATE_H
|
||||
@@ -0,0 +1,24 @@
|
||||
#include "counter_state.h"
|
||||
|
||||
#include <libcockatrice/utility/color.h>
|
||||
|
||||
CounterState::CounterState(int id, const QString &name, const QColor &color, int radius, int value, QObject *parent)
|
||||
: QObject(parent), id(id), name(name), color(color), radius(radius), value(value)
|
||||
{
|
||||
}
|
||||
|
||||
CounterState *CounterState::fromProto(const ServerInfo_Counter &counter, QObject *parent)
|
||||
{
|
||||
return new CounterState(counter.id(), QString::fromStdString(counter.name()),
|
||||
convertColorToQColor(counter.counter_color()), counter.radius(), counter.count(), parent);
|
||||
}
|
||||
|
||||
void CounterState::setValue(int newValue)
|
||||
{
|
||||
if (newValue == value) {
|
||||
return;
|
||||
}
|
||||
int old = value;
|
||||
value = newValue;
|
||||
emit valueChanged(old, newValue);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#ifndef COCKATRICE_COUNTER_STATE_H
|
||||
#define COCKATRICE_COUNTER_STATE_H
|
||||
|
||||
#include <QColor>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <libcockatrice/protocol/pb/serverinfo_counter.pb.h>
|
||||
|
||||
class CounterState : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
CounterState(int id, const QString &name, const QColor &color, int radius, int value, QObject *parent = nullptr);
|
||||
|
||||
static CounterState *fromProto(const ServerInfo_Counter &counter, QObject *parent = nullptr);
|
||||
|
||||
int getId() const
|
||||
{
|
||||
return id;
|
||||
}
|
||||
QString getName() const
|
||||
{
|
||||
return name;
|
||||
}
|
||||
QColor getColor() const
|
||||
{
|
||||
return color;
|
||||
}
|
||||
int getRadius() const
|
||||
{
|
||||
return radius;
|
||||
}
|
||||
int getValue() const
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
void setValue(int newValue);
|
||||
|
||||
signals:
|
||||
void valueChanged(int oldValue, int newValue);
|
||||
|
||||
private:
|
||||
int id;
|
||||
QString name;
|
||||
QColor color;
|
||||
int radius;
|
||||
int value;
|
||||
};
|
||||
|
||||
#endif // COCKATRICE_COUNTER_STATE_H
|
||||
Reference in New Issue
Block a user