mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
Improved token loading
This commit is contained in:
parent
339945e089
commit
36f6907fa3
10 changed files with 79 additions and 53 deletions
|
|
@ -95,11 +95,11 @@ InnerDecklistNode::~InnerDecklistNode()
|
|||
|
||||
QString InnerDecklistNode::visibleNameFromName(const QString &_name)
|
||||
{
|
||||
if (_name == "main")
|
||||
if (_name == DECK_ZONE_MAIN)
|
||||
return QObject::tr("Maindeck");
|
||||
else if (_name == "side")
|
||||
else if (_name == DECK_ZONE_SIDE)
|
||||
return QObject::tr("Sideboard");
|
||||
else if (_name == "tokens")
|
||||
else if (_name == DECK_ZONE_TOKENS)
|
||||
return QObject::tr("Tokens");
|
||||
else
|
||||
return _name;
|
||||
|
|
@ -414,7 +414,7 @@ bool DeckList::readElement(QXmlStreamReader *xml)
|
|||
else if (childName == "comments")
|
||||
comments = xml->readElementText();
|
||||
else if (childName == "zone") {
|
||||
InnerDecklistNode *newZone = new InnerDecklistNode(xml->attributes().value("name").toString(), root);
|
||||
InnerDecklistNode *newZone = getZoneObjFromName(xml->attributes().value("name").toString());
|
||||
newZone->readElement(xml);
|
||||
} else if (childName == "sideboard_plan") {
|
||||
SideboardPlan *newSideboardPlan = new SideboardPlan;
|
||||
|
|
@ -510,13 +510,14 @@ bool DeckList::loadFromStream_Plain(QTextStream &in)
|
|||
{
|
||||
cleanList();
|
||||
|
||||
InnerDecklistNode *main = 0, *side = 0;
|
||||
bool inSideboard = false;
|
||||
bool inSideboard = false, isSideboard = false;
|
||||
|
||||
int okRows = 0;
|
||||
bool titleFound = false;
|
||||
while (!in.atEnd()) {
|
||||
QString line = in.readLine().simplified();
|
||||
|
||||
// skip comments
|
||||
if (line.startsWith("//"))
|
||||
{
|
||||
if(!titleFound)
|
||||
|
|
@ -529,23 +530,17 @@ bool DeckList::loadFromStream_Plain(QTextStream &in)
|
|||
continue;
|
||||
}
|
||||
|
||||
InnerDecklistNode *zone;
|
||||
// check for sideboard prefix
|
||||
if (line.startsWith("Sideboard", Qt::CaseInsensitive)) {
|
||||
inSideboard = true;
|
||||
continue;
|
||||
} else if (line.startsWith("SB:", Qt::CaseInsensitive)) {
|
||||
}
|
||||
|
||||
isSideboard = inSideboard;
|
||||
|
||||
if (line.startsWith("SB:", Qt::CaseInsensitive)) {
|
||||
line = line.mid(3).trimmed();
|
||||
if (!side)
|
||||
side = new InnerDecklistNode("side", root);
|
||||
zone = side;
|
||||
} else if (inSideboard) {
|
||||
if (!side)
|
||||
side = new InnerDecklistNode("side", root);
|
||||
zone = side;
|
||||
} else {
|
||||
if (!main)
|
||||
main = new InnerDecklistNode("main", root);
|
||||
zone = main;
|
||||
isSideboard = true;
|
||||
}
|
||||
|
||||
// Filter out MWS edition symbols and basic land extras
|
||||
|
|
@ -593,13 +588,27 @@ bool DeckList::loadFromStream_Plain(QTextStream &in)
|
|||
cardName.replace(rx, QString("%1 // ").arg(rx.cap(1)));
|
||||
}
|
||||
|
||||
// Look for the correct card zone
|
||||
QString zoneName = getCardZoneFromName(cardName, isSideboard ? DECK_ZONE_SIDE: DECK_ZONE_MAIN);
|
||||
|
||||
++okRows;
|
||||
new DecklistCardNode(cardName, number, 0, zone);
|
||||
new DecklistCardNode(cardName, number, 0, getZoneObjFromName(zoneName));
|
||||
}
|
||||
updateDeckHash();
|
||||
return (okRows > 0);
|
||||
}
|
||||
|
||||
InnerDecklistNode * DeckList::getZoneObjFromName(const QString zoneName)
|
||||
{
|
||||
for (int i = 0; i < root->size(); i++) {
|
||||
InnerDecklistNode *node = dynamic_cast<InnerDecklistNode *>(root->at(i));
|
||||
if(node->getName() == zoneName)
|
||||
return node;
|
||||
}
|
||||
|
||||
return new InnerDecklistNode(zoneName, root);
|
||||
}
|
||||
|
||||
bool DeckList::loadFromFile_Plain(QIODevice *device)
|
||||
{
|
||||
QTextStream in(device);
|
||||
|
|
@ -616,7 +625,7 @@ struct WriteToStream {
|
|||
const InnerDecklistNode *node,
|
||||
const DecklistCardNode *card
|
||||
) {
|
||||
if (prefixSideboardCards && node->getName() == "side") {
|
||||
if (prefixSideboardCards && node->getName() == DECK_ZONE_SIDE) {
|
||||
stream << "SB: ";
|
||||
}
|
||||
stream << QString("%1 %2\n").arg(
|
||||
|
|
@ -680,7 +689,7 @@ int DeckList::getSideboardSize() const
|
|||
int size = 0;
|
||||
for (int i = 0; i < root->size(); ++i) {
|
||||
InnerDecklistNode *node = dynamic_cast<InnerDecklistNode *>(root->at(i));
|
||||
if (node->getName() != "side")
|
||||
if (node->getName() != DECK_ZONE_SIDE)
|
||||
continue;
|
||||
for (int j = 0; j < node->size(); j++) {
|
||||
DecklistCardNode *card = dynamic_cast<DecklistCardNode *>(node->at(j));
|
||||
|
|
@ -738,8 +747,8 @@ void DeckList::updateDeckHash()
|
|||
bool isValidDeckList = true;
|
||||
QSet<QString> hashZones, optionalZones;
|
||||
|
||||
hashZones << "main" << "side"; // Zones in deck to be included in hashing process
|
||||
optionalZones << "tokens"; // Optional zones in deck not included in hashing process
|
||||
hashZones << DECK_ZONE_MAIN << DECK_ZONE_SIDE; // Zones in deck to be included in hashing process
|
||||
optionalZones << DECK_ZONE_TOKENS; // Optional zones in deck not included in hashing process
|
||||
|
||||
for (int i = 0; i < root->size(); i++)
|
||||
{
|
||||
|
|
@ -750,7 +759,7 @@ void DeckList::updateDeckHash()
|
|||
{
|
||||
DecklistCardNode *card = dynamic_cast<DecklistCardNode *>(node->at(j));
|
||||
for (int k = 0; k < card->getNumber(); ++k)
|
||||
cardList.append((node->getName() == "side" ? "SB:" : "") + card->getName().toLower());
|
||||
cardList.append((node->getName() == DECK_ZONE_SIDE ? "SB:" : "") + card->getName().toLower());
|
||||
}
|
||||
else if (!optionalZones.contains(node->getName())) // Not a valid zone -> cheater?
|
||||
{
|
||||
|
|
|
|||
|
|
@ -21,6 +21,10 @@ class QTextStream;
|
|||
|
||||
class InnerDecklistNode;
|
||||
|
||||
#define DECK_ZONE_MAIN "main"
|
||||
#define DECK_ZONE_SIDE "side"
|
||||
#define DECK_ZONE_TOKENS "tokens"
|
||||
|
||||
class SideboardPlan {
|
||||
private:
|
||||
QString name;
|
||||
|
|
@ -107,17 +111,16 @@ class DecklistCardNode : public AbstractDecklistCardNode {
|
|||
private:
|
||||
QString name;
|
||||
int number;
|
||||
float price;
|
||||
float price;
|
||||
public:
|
||||
DecklistCardNode(const QString &_name = QString(), int _number = 1, float _price = 0, InnerDecklistNode *_parent = 0) : AbstractDecklistCardNode(_parent), name(_name), number(_number), price(_price) { }
|
||||
DecklistCardNode(const QString &_name = QString(), int _number = 1, float _price = 0, InnerDecklistNode *_parent = 0) : AbstractDecklistCardNode(_parent), name(_name), number(_number), price(_price) { }
|
||||
DecklistCardNode(DecklistCardNode *other, InnerDecklistNode *_parent);
|
||||
int getNumber() const { return number; }
|
||||
void setNumber(int _number) { number = _number; }
|
||||
QString getName() const { return name; }
|
||||
void setName(const QString &_name) { name = _name; }
|
||||
float getPrice() const { return price; }
|
||||
|
||||
void setPrice(const float _price) { price = _price; }
|
||||
float getPrice() const { return price; }
|
||||
void setPrice(const float _price) { price = _price; }
|
||||
};
|
||||
|
||||
class DeckList : public QObject {
|
||||
|
|
@ -128,6 +131,9 @@ private:
|
|||
QMap<QString, SideboardPlan *> sideboardPlans;
|
||||
InnerDecklistNode *root;
|
||||
void getCardListHelper(InnerDecklistNode *node, QSet<QString> &result) const;
|
||||
InnerDecklistNode *getZoneObjFromName(const QString zoneName);
|
||||
protected:
|
||||
virtual QString getCardZoneFromName(QString /* cardName */, QString currentZoneName) { return currentZoneName; };
|
||||
signals:
|
||||
void deckHashChanged();
|
||||
public slots:
|
||||
|
|
|
|||
|
|
@ -170,9 +170,9 @@ void Server_Player::setupZones()
|
|||
for (int i = 0; i < listRoot->size(); ++i) {
|
||||
InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
|
||||
Server_CardZone *z;
|
||||
if (currentZone->getName() == "main")
|
||||
if (currentZone->getName() == DECK_ZONE_MAIN)
|
||||
z = deckZone;
|
||||
else if (currentZone->getName() == "side")
|
||||
else if (currentZone->getName() == DECK_ZONE_SIDE)
|
||||
z = sbZone;
|
||||
else
|
||||
continue;
|
||||
|
|
@ -193,15 +193,15 @@ void Server_Player::setupZones()
|
|||
const QString targetZone = QString::fromStdString(m.target_zone());
|
||||
|
||||
Server_CardZone *start, *target;
|
||||
if (startZone == "main")
|
||||
if (startZone == DECK_ZONE_MAIN)
|
||||
start = deckZone;
|
||||
else if (startZone == "side")
|
||||
else if (startZone == DECK_ZONE_SIDE)
|
||||
start = sbZone;
|
||||
else
|
||||
continue;
|
||||
if (targetZone == "main")
|
||||
if (targetZone == DECK_ZONE_MAIN)
|
||||
target = deckZone;
|
||||
else if (targetZone == "side")
|
||||
else if (targetZone == DECK_ZONE_SIDE)
|
||||
target = sbZone;
|
||||
else
|
||||
continue;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue