Improved token loading

This commit is contained in:
Fabio Bas
2017-04-29 20:01:57 +02:00
committed by Zach H
parent 339945e089
commit 36f6907fa3
10 changed files with 79 additions and 53 deletions

View File

@@ -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?
{