Minor tweaks to Mike's map of maps cache work on stats. Mostly these changes are stylistic changes that favor more terse code, but there was one bug where a double-delete was happening - GameStateDeckViewer used to allocate its own StatsWrapper, and deleted it as part of teardown - now that the allocation is owned by the deck stats instance, it's not allowed to do the deletion anymore.
I'll also note that the mods I did to getDeckMetaDataById() are completely unnecessary - pragmatically speaking, it's doing the same thing. The only difference is that I'm using std::find_if instead of brute iterator manipulation, and I'm using a predicate function. For a simple check like this, it's kind of pointless, but if you need to do more complex comparisons, predicate operators can become quite powerful.
This commit is contained in:
@@ -24,30 +24,35 @@ vector<DeckMetaData *> * DeckManager::getAIDeckOrderList()
|
|||||||
return &aiDeckOrderList;
|
return &aiDeckOrderList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Predicate helper for getDeckMetadataByID()
|
||||||
|
*/
|
||||||
|
struct DeckIDMatch
|
||||||
|
{
|
||||||
|
DeckIDMatch(int id) : mID(id)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator() (DeckMetaData* inPtr)
|
||||||
|
{
|
||||||
|
return inPtr->getDeckId() == mID;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mID;
|
||||||
|
};
|
||||||
|
|
||||||
DeckMetaData* DeckManager::getDeckMetaDataById( int deckId, bool isAI )
|
DeckMetaData* DeckManager::getDeckMetaDataById( int deckId, bool isAI )
|
||||||
{
|
{
|
||||||
|
DeckMetaData* deck = NULL;
|
||||||
|
std::vector<DeckMetaData *>& deckList = isAI ? aiDeckOrderList : playerDeckOrderList;
|
||||||
|
|
||||||
DeckMetaData* currentDeck = NULL;
|
std::vector<DeckMetaData *>::iterator pos = find_if(deckList.begin(), deckList.end(), DeckIDMatch(deckId));
|
||||||
vector<DeckMetaData *>::iterator currentPos, end;
|
if (pos != deckList.end())
|
||||||
if ( isAI )
|
|
||||||
{
|
{
|
||||||
currentPos = aiDeckOrderList.begin();
|
deck = *pos;
|
||||||
end = aiDeckOrderList.end();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
currentPos = playerDeckOrderList.begin();
|
|
||||||
end = playerDeckOrderList.end();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return deck;
|
||||||
for (; currentPos != end; ++currentPos)
|
|
||||||
{
|
|
||||||
currentDeck = *currentPos;
|
|
||||||
if (currentDeck->getDeckId() == deckId )
|
|
||||||
return currentDeck;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StatsWrapper * DeckManager::getExtendedStatsForDeckId( int deckId, MTGAllCards *collection, bool isAI )
|
StatsWrapper * DeckManager::getExtendedStatsForDeckId( int deckId, MTGAllCards *collection, bool isAI )
|
||||||
@@ -60,23 +65,21 @@ StatsWrapper * DeckManager::getExtendedStatsForDeckId( int deckId, MTGAllCards *
|
|||||||
StatsWrapper * DeckManager::getExtendedDeckStats( DeckMetaData *selectedDeck, MTGAllCards *collection, bool isAI )
|
StatsWrapper * DeckManager::getExtendedDeckStats( DeckMetaData *selectedDeck, MTGAllCards *collection, bool isAI )
|
||||||
{
|
{
|
||||||
StatsWrapper* stats = NULL;
|
StatsWrapper* stats = NULL;
|
||||||
map<string, StatsWrapper *> *statsMap = NULL;
|
|
||||||
|
|
||||||
string deckName = selectedDeck->getFilename();
|
string deckName = selectedDeck->getFilename();
|
||||||
int deckId = selectedDeck->getDeckId();
|
int deckId = selectedDeck->getDeckId();
|
||||||
|
|
||||||
if (isAI)
|
map<string, StatsWrapper*>* statsMap = isAI ? &aiDeckStatsMap : &playerDeckStatsMap;
|
||||||
statsMap = &aiDeckStatsMap;
|
if (statsMap->find(deckName) == statsMap->end())
|
||||||
else
|
|
||||||
statsMap = &playerDeckStatsMap;
|
|
||||||
|
|
||||||
if (statsMap->empty() || (statsMap->find(deckName) == statsMap->end()))
|
|
||||||
{
|
{
|
||||||
stats = NEW StatsWrapper(deckId);
|
stats = NEW StatsWrapper(deckId);
|
||||||
stats->updateStats( deckName, collection);
|
stats->updateStats( deckName, collection);
|
||||||
statsMap->insert( make_pair(deckName, stats));
|
statsMap->insert( make_pair(deckName, stats));
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
stats = statsMap->find(deckName)->second;
|
stats = statsMap->find(deckName)->second;
|
||||||
|
}
|
||||||
|
|
||||||
return stats;
|
return stats;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,6 @@ GameStateDeckViewer::~GameStateDeckViewer()
|
|||||||
SAFE_DELETE(myCollection->parent);
|
SAFE_DELETE(myCollection->parent);
|
||||||
SAFE_DELETE(myCollection);
|
SAFE_DELETE(myCollection);
|
||||||
}
|
}
|
||||||
SAFE_DELETE(stw);
|
|
||||||
SAFE_DELETE(filterMenu);
|
SAFE_DELETE(filterMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -78,8 +78,7 @@ void SimplePopup::Update(DeckMetaData* selectedDeck)
|
|||||||
mDeckInformation = selectedDeck;
|
mDeckInformation = selectedDeck;
|
||||||
|
|
||||||
// get the information from the cache, if it doesn't exist create an entry
|
// get the information from the cache, if it doesn't exist create an entry
|
||||||
DeckManager *deckManager = DeckManager::GetInstance();
|
mStatsWrapper = DeckManager::GetInstance()->getExtendedDeckStats( mDeckInformation, mCollection, (mDeckInformation->getFilename().find("baka") != string::npos) );
|
||||||
mStatsWrapper = deckManager->getExtendedDeckStats( mDeckInformation, mCollection, (mDeckInformation->getFilename().find("baka") != string::npos) );
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user