Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keybaord shortcut for creating related cards #2426

Conversation

MarkyMarkMcDonald
Copy link
Contributor

Finishes #2425.

create all related cards example

Once again, c++ isn't my forte, so let me know if I can clean anything up.

@tooomm
Copy link
Member

tooomm commented Feb 26, 2017

I'm not sure if this is the best/right approach.
I'll leave a more detailed comment for discussion in the issue soon.


QStringList relatedCards = * new QStringList();
relatedCards.append(sourceCard->getInfo()->getRelatedCards());
relatedCards.append(sourceCard->getInfo()->getReverseRelatedCards2Me());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While creating a new QStringList makes the code more readable, the second "append" forces Qt to make a deep copy of the list, with all the overhead involved in memory allocation, copy, etc..
This is not a big deal since the "related cards" list would only contain a few hundreds of bytes at max..

relatedCards.append(sourceCard->getInfo()->getRelatedCards());
relatedCards.append(sourceCard->getInfo()->getReverseRelatedCards2Me());

for (int i = 0; i < relatedCards.size(); i++)
Copy link
Contributor

@ctrlaltca ctrlaltca Feb 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use foreach with QStringList:

foreach (const QString &tokenName, relatedCards)
{
  createCard(sourceCard, dbNameFromTokenDisplayName(tokenName));
}

Copy link
Contributor

@ctrlaltca ctrlaltca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks fine except minor places where i'm being picky ;)

// token names take the form of "<Descriptors> <Power>/<Toughness> <Card Name> " or "<Card Name> ".
// dbName for tokens should take the form of "<Card Name> ".
// trailing whitespace is significant; it is hacked on at the end as an additional identifier in our single key database
QString Player::dbNameFromTokenDisplayName(const QString &tokenName) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice refactoring, it makes clear the meaning of this code.

@@ -2425,6 +2424,44 @@ void Player::updateCardMenu(CardItem *card)
}
}

void Player::addRelatedCardActions(const CardItem *card, QMenu *cardMenu) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Another nice refactoring, less duplicated code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be safe I think we should have nullptr checks on both arguments.

@@ -4,6 +4,7 @@
#include <QInputDialog>
#include <QPoint>
#include <QMap>
#include <QRegExp>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this into the .cpp file, since it's not used inside the .h code.
It will avoid the inclusion of <QRegExp> in all the other files #including player.h but not using it.

Copy link
Member

@Daenyth Daenyth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach and refactoring look great.

Thanks for the contribution!

@ctrlaltca go ahead and merge when you feel it's ready

setShortcutsActive();

for (int i = 0; i < table->getCards().size(); ++i) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer using foreach here also as ctrl said

@@ -2425,6 +2424,44 @@ void Player::updateCardMenu(CardItem *card)
}
}

void Player::addRelatedCardActions(const CardItem *card, QMenu *cardMenu) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be safe I think we should have nullptr checks on both arguments.

@MarkyMarkMcDonald
Copy link
Contributor Author

Thanks for the feedback - I think I handled everything except for the deep copy part. To me the deeper issue is that the this is the only place in the code base outside of importing from oracle that we refer to either of reverseRelatedCards2Me or relatedCards, and we combine those lists together in both cases. I'm not sure what the history is behind adding getReverseRelatedCards2Me - I'd rather clean it up at some other point and move reverseRelatedCards2Me into relatedCards at the data import level.

@ZeldaZach
Copy link
Member

I'm happy with the change. nice job!

@ctrlaltca ctrlaltca merged commit b9cd942 into Cockatrice:master Feb 27, 2017
@ctrlaltca
Copy link
Contributor

Merged, thank you!

@Daenyth
Copy link
Member

Daenyth commented Feb 27, 2017

I'd rather clean it up at some other point and move reverseRelatedCards2Me into relatedCards at the data import level.

You're right - only the card db should care about the difference. The rest of the code would rather just one list. I'd happily accept a PR with such a refactor. Thanks for helping improve the health of the codebase! It's grown really organically over time and needs some aggressive pruning and decoupling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants