Exemplo n.º 1
0
void DeckViewScene::rebuildTree()
{
    clearContents();
    
    if (!deck)
        return;
    
    InnerDecklistNode *listRoot = deck->getRoot();
    for (int i = 0; i < listRoot->size(); i++) {
        InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
        
        DeckViewCardContainer *container = cardContainers.value(currentZone->getName(), 0);
        if (!container) {
            container = new DeckViewCardContainer(currentZone->getName());
            cardContainers.insert(currentZone->getName(), container);
            addItem(container);
        }
        
        for (int j = 0; j < currentZone->size(); j++) {
            DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
            if (!currentCard)
                continue;

            for (int k = 0; k < currentCard->getNumber(); ++k) {
                DeckViewCard *newCard = new DeckViewCard(currentCard->getName(), currentZone->getName(), container);
                container->addCard(newCard);
                emit newCardAdded(newCard);
            }
        }
    }
}
Exemplo n.º 2
0
void DeckListModel::rebuildTree()
{
    beginResetModel();

    root->clearTree();
    InnerDecklistNode *listRoot = deckList->getRoot();
    for (int i = 0; i < listRoot->size(); i++) {
        InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
        InnerDecklistNode *node = new InnerDecklistNode(currentZone->getName(), root);
        for (int j = 0; j < currentZone->size(); j++) {
            DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
            // XXX better sanity checking
            if (!currentCard)
                continue;

            CardInfo *info = db->getCard(currentCard->getName());
            QString cardType;
            if (!info)
                cardType = "unknown";
            else
                cardType = info->getMainCardType();
            InnerDecklistNode *cardTypeNode = dynamic_cast<InnerDecklistNode *>(node->findChild(cardType));
            if (!cardTypeNode)
                cardTypeNode = new InnerDecklistNode(cardType, node);

            new DecklistModelCardNode(currentCard, cardTypeNode);
        }
    }

    endResetModel();
}
Exemplo n.º 3
0
QModelIndex DeckListModel::addCard(const QString &cardName, const QString &zoneName)
{
	InnerDecklistNode *zoneNode = createNodeIfNeeded(zoneName, root);

	CardInfo *info = db->getCard(cardName);
	QString cardType = info->getMainCardType();
	InnerDecklistNode *cardTypeNode = createNodeIfNeeded(cardType, zoneNode);

	DecklistModelCardNode *cardNode = dynamic_cast<DecklistModelCardNode *>(cardTypeNode->findChild(cardName));
	if (!cardNode) {
		DecklistCardNode *decklistCard = deckList->addCard(cardName, zoneName);
		QModelIndex parentIndex = nodeToIndex(cardTypeNode);
		beginInsertRows(parentIndex, cardTypeNode->size(), cardTypeNode->size());
		cardNode = new DecklistModelCardNode(decklistCard, cardTypeNode);
		endInsertRows();
		sort(1);
		emitRecursiveUpdates(parentIndex);
		return nodeToIndex(cardNode);
	} else {
		cardNode->setNumber(cardNode->getNumber() + 1);
		QModelIndex ind = nodeToIndex(cardNode);
		emitRecursiveUpdates(ind);
		return ind;
	}
}
Exemplo n.º 4
0
int DeckListModel::rowCount(const QModelIndex &parent) const
{
//    debugIndexInfo("rowCount", parent);
    InnerDecklistNode *node = getNode<InnerDecklistNode *>(parent);
    if (node)
        return node->size();
    else
        return 0;
}
Exemplo n.º 5
0
/**
 * Called when the download of the json file with the prices is finished.
 */
void PriceUpdater::downloadFinished()
{
    QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
    bool ok;
    QVariantMap resultMap = QtJson::Json::parse(QString(reply->readAll()), ok).toMap();
    if (!ok) {
        reply->deleteLater();
        deleteLater();
        return;
    }
    
    QMap<QString, float> cardsPrice;
    
    QListIterator<QVariant> it(resultMap.value("cards").toList());
    while (it.hasNext()) {
        QVariantMap map = it.next().toMap();
        QString name = map.value("name").toString().toLower();
        float price = map.value("price").toString().toFloat();
        QString set = map.value("set_code").toString();

        /**
        * Make sure Masters Edition (MED) isn't the set, as it doesn't
        * physically exist. Also check the price to see that the cheapest set
        * ends up as the final price.
        */
        if (set != "MED" && (!cardsPrice.contains(name) || cardsPrice.value(name) > price))
            cardsPrice.insert(name, price);
    }
    
    InnerDecklistNode *listRoot = deck->getRoot();
    for (int i = 0; i < listRoot->size(); i++) {
        InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
        for (int j = 0; j < currentZone->size(); j++) {
            DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
            if (!currentCard)
                continue;
            currentCard->setPrice(cardsPrice[currentCard->getName().toLower()]);
        }
    }
    
    reply->deleteLater();
    deleteLater();
    emit finishedUpdate();
}
Exemplo n.º 6
0
QModelIndex DeckListModel::index(int row, int column, const QModelIndex &parent) const
{
//    debugIndexInfo("index", parent);
    if (!hasIndex(row, column, parent))
        return QModelIndex();

    InnerDecklistNode *parentNode = getNode<InnerDecklistNode *>(parent);
    if (row >= parentNode->size())
        return QModelIndex();

    return createIndex(row, column, parentNode->at(row));
}
Exemplo n.º 7
0
bool DeckList::saveToStream_Plain(QTextStream &out)
{
	// Support for this is only possible if the internal structure doesn't get more complicated.
	for (int i = 0; i < root->size(); i++) {
		InnerDecklistNode *node = dynamic_cast<InnerDecklistNode *>(root->at(i));
		for (int j = 0; j < node->size(); j++) {
			DecklistCardNode *card = dynamic_cast<DecklistCardNode *>(node->at(j));
			out << QString("%1%2 %3\n").arg(node->getName() == "side" ? "SB: " : "").arg(card->getNumber()).arg(card->getName());
		}
	}
	return true;
}
Exemplo n.º 8
0
/**
 * Called when the download of the json file with the prices is finished.
 */
void PriceUpdater::downloadFinished()
{
    QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
    bool ok;
    QVariantMap resultMap = QtJson::Json::parse(QString(reply->readAll()), ok).toMap();
    if (!ok) {
        reply->deleteLater();
        deleteLater();
        return;
    }
    
    QMap<QString, float> cardsPrice;
    
    QListIterator<QVariant> it(resultMap.value("cards").toList());
    while (it.hasNext()) {
        QVariantMap map = it.next().toMap();
        QString name = map.value("name").toString().toLower();
        float price = map.value("average").toString().toFloat();
        if ((cardsPrice.find(name) == cardsPrice.end()) || (price < cardsPrice[name])) {
            cardsPrice.insert(name, price);
        }
    }
    
    InnerDecklistNode *listRoot = deck->getRoot();
    for (int i = 0; i < listRoot->size(); i++) {
        InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
        for (int j = 0; j < currentZone->size(); j++) {
            DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
            if (!currentCard)
                continue;
            currentCard->setPrice(cardsPrice[currentCard->getName().toLower()]);
        }
    }
    
    reply->deleteLater();
    deleteLater();
    emit finishedUpdate();
}
Exemplo n.º 9
0
bool DeckListModel::removeRows(int row, int count, const QModelIndex &parent)
{
    InnerDecklistNode *node = getNode<InnerDecklistNode *>(parent);
    if (!node)
        return false;
    if (row + count > node->size())
        return false;

    beginRemoveRows(parent, row, row + count - 1);
    for (int i = 0; i < count; i++) {
        AbstractDecklistNode *toDelete = node->takeAt(row);
        if (DecklistModelCardNode *temp = dynamic_cast<DecklistModelCardNode *>(toDelete))
            deckList->deleteNode(temp->getDataNode());
        delete toDelete;
    }
    endRemoveRows();

    if (!node->size() && (node != root))
        removeRows(parent.row(), 1, parent.parent());
    else
        emitRecursiveUpdates(parent);

    return true;
}
Exemplo n.º 10
0
void DeckList::updateDeckHash()
{
	QStringList cardList;
	for (int i = 0; i < root->size(); i++) {
		InnerDecklistNode *node = dynamic_cast<InnerDecklistNode *>(root->at(i));
		for (int j = 0; j < node->size(); j++) {
			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.sort();
	QByteArray deckHashArray = QCryptographicHash::hash(cardList.join(";").toUtf8(), QCryptographicHash::Sha1);
	quint64 number = (((quint64) (unsigned char) deckHashArray[0]) << 32)
	                + (((quint64) (unsigned char) deckHashArray[1]) << 24)
	                + (((quint64) (unsigned char) deckHashArray[2] << 16))
	                + (((quint64) (unsigned char) deckHashArray[3]) << 8)
	                + (quint64) (unsigned char) deckHashArray[4];
	deckHash = QString::number(number, 32).rightJustified(8, '0');
	
	emit deckHashChanged();
}
Exemplo n.º 11
0
void Server_Player::setupZones()
{
	// This may need to be customized according to the game rules.
	// ------------------------------------------------------------------

	// Create zones
	Server_CardZone *deckZone = new Server_CardZone(this, "deck", false, HiddenZone);
	addZone(deckZone);
	Server_CardZone *sbZone = new Server_CardZone(this, "sb", false, HiddenZone);
	addZone(sbZone);
	addZone(new Server_CardZone(this, "table", true, PublicZone));
	addZone(new Server_CardZone(this, "hand", false, PrivateZone));
	addZone(new Server_CardZone(this, "stack", false, PublicZone));
	addZone(new Server_CardZone(this, "grave", false, PublicZone));
	addZone(new Server_CardZone(this, "rfg", false, PublicZone));

	addCounter(new Server_Counter(0, "life", Color(255, 255, 255), 25, 20));
	addCounter(new Server_Counter(1, "w", Color(255, 255, 150), 20, 0));
	addCounter(new Server_Counter(2, "u", Color(150, 150, 255), 20, 0));
	addCounter(new Server_Counter(3, "b", Color(150, 150, 150), 20, 0));
	addCounter(new Server_Counter(4, "r", Color(250, 150, 150), 20, 0));
	addCounter(new Server_Counter(5, "g", Color(150, 255, 150), 20, 0));
	addCounter(new Server_Counter(6, "x", Color(255, 255, 255), 20, 0));
	addCounter(new Server_Counter(7, "storm", Color(255, 255, 255), 20, 0));

	initialCards = 7;

	// ------------------------------------------------------------------

	// Assign card ids and create deck from decklist
	InnerDecklistNode *listRoot = deck->getRoot();
	nextCardId = 0;
	for (int i = 0; i < listRoot->size(); ++i) {
		InnerDecklistNode *currentZone = dynamic_cast<InnerDecklistNode *>(listRoot->at(i));
		Server_CardZone *z;
		if (currentZone->getName() == "main")
			z = deckZone;
		else if (currentZone->getName() == "side")
			z = sbZone;
		else
			continue;
		
		for (int j = 0; j < currentZone->size(); ++j) {
			DecklistCardNode *currentCard = dynamic_cast<DecklistCardNode *>(currentZone->at(j));
			if (!currentCard)
				continue;
			for (int k = 0; k < currentCard->getNumber(); ++k)
				z->cards.append(new Server_Card(currentCard->getName(), nextCardId++, 0, 0));
		}
	}
	
	const QList<MoveCardToZone *> &sideboardPlan = deck->getCurrentSideboardPlan();
	for (int i = 0; i < sideboardPlan.size(); ++i) {
		MoveCardToZone *m = sideboardPlan[i];
		
		Server_CardZone *start, *target;
		if (m->getStartZone() == "main")
			start = deckZone;
		else if (m->getStartZone() == "side")
			start = sbZone;
		else
			continue;
		if (m->getTargetZone() == "main")
			target = deckZone;
		else if (m->getTargetZone() == "side")
			target = sbZone;
		else
			continue;
		
		for (int j = 0; j < start->cards.size(); ++j)
			if (start->cards[j]->getName() == m->getCardName()) {
				Server_Card *card = start->cards[j];
				start->cards.removeAt(j);
				target->cards.append(card);
				break;
			}
	}
	
	deckZone->shuffle();
}