void DeckStats::saveStats(Player *player, Player *opponent, GameObserver * game) { int victory = 1; if (!game->didWin()) { if (player->life == opponent->life) return; if (player->life < opponent->life) victory = 0; } else if (!game->didWin(player)) { victory = 0; } load(currentDeck); map<string, DeckStat *> *stats = &masterDeckStats[currentDeck]; map<string, DeckStat *>::iterator it = stats->find(opponent->deckFileSmall); string manaColorIndex = ""; DeckManager *deckManager = DeckManager::GetInstance(); DeckMetaData *aiDeckMeta = deckManager->getDeckMetaDataByFilename( opponent->deckFile, true); StatsWrapper *stw = deckManager->getExtendedDeckStats( aiDeckMeta, MTGAllCards::getInstance(), true); manaColorIndex = stw->getManaColorIndex(); if (it == stats->end()) { stats->insert( make_pair( opponent->deckFileSmall, NEW DeckStat(1, victory, manaColorIndex) )); } else { it->second->victories += victory; it->second->nbgames += 1; if ( it->second->manaColorIndex == "" ) { it->second->manaColorIndex = manaColorIndex; } } save(currentDeck); DeckMetaData* playerMeta = DeckManager::GetInstance()->getDeckMetaDataByFilename(player->deckFile, false); // metadata caches its internal data (number of games, victories, etc) // tell it to refresh when stats are updated if (playerMeta) playerMeta->Invalidate(); DeckMetaData* aiMeta = DeckManager::GetInstance()->getDeckMetaDataByFilename(opponent->deckFile, true); if (aiMeta) aiMeta->Invalidate(); }
void DeckStats::save(const std::string& filename) { std::ofstream file; if (JFileSystem::GetInstance()->openForWrite(file, filename)) { char writer[512]; map<string, DeckStat *> stats = masterDeckStats[currentDeck]; map<string, DeckStat *>::iterator it; string manaColorIndex = ""; int deckId = atoi(filename.substr(filename.find("_deck") + 5, filename.find(".txt")).c_str()); char buffer[512]; sprintf(buffer, "deck%i.txt", deckId); string playerDeckFilePath= options.profileFile( buffer); DeckManager *deckManager = DeckManager::GetInstance(); DeckMetaData *playerDeckMeta = deckManager->getDeckMetaDataByFilename(playerDeckFilePath, false); if (playerDeckMeta && playerDeckMeta->getColorIndex() == "" ) { StatsWrapper *stw = deckManager->getExtendedDeckStats( playerDeckMeta, MTGAllCards::getInstance(), false); manaColorIndex = stw->getManaColorIndex(); playerDeckMeta->setColorIndex( manaColorIndex ); } file << "MANA:" << manaColorIndex << endl; if(file) for (it = stats.begin(); it != stats.end(); it++) { sprintf(writer, "%s\n", it->first.c_str()); file << writer; sprintf(writer, "%i\n", it->second->nbgames); file << writer; sprintf(writer, "%i\n", it->second->victories); file << writer; file << "MANA:" << it->second->manaColorIndex <<endl; } file.close(); if(playerDeckMeta) playerDeckMeta->Invalidate(); } }
vector<DeckMetaData *> GameState::BuildDeckList(const string& path, const string& smallDeckPrefix, Player * statsPlayer, int maxDecks) { vector<DeckMetaData*> retList; int found = 1; int nbDecks = 1; DeckManager *deckManager = DeckManager::GetInstance(); bool isAI = path.find("baka") != string::npos; while (found && (!maxDecks || nbDecks <= maxDecks)) { found = 0; std::ostringstream filename; filename << path << "/deck" << nbDecks << ".txt"; DeckMetaData * meta = deckManager->getDeckMetaDataByFilename(filename.str(), isAI); if (meta) { found = 1; //Check if the deck is unlocked based on sets etc... bool unlocked = true; vector<int> unlockRequirements = meta->getUnlockRequirements(); for (size_t i = 0; i < unlockRequirements.size(); ++i) { if (! options[unlockRequirements[i]].number) { unlocked = false; break; } } if (unlocked) { if (statsPlayer) { std::ostringstream aiStatsDeckName; aiStatsDeckName << smallDeckPrefix << "_deck" << nbDecks; meta->mStatsFilename = aiStatsDeckName.str(); meta->mIsAI = true; if (meta->mPlayerDeck != statsPlayer->GetCurrentDeckStatsFile()) { meta->mPlayerDeck = statsPlayer->GetCurrentDeckStatsFile(); meta->Invalidate(); } } else { std::ostringstream playerStatsDeckName; playerStatsDeckName << "stats/player_deck" << nbDecks << ".txt"; meta->mStatsFilename = options.profileFile(playerStatsDeckName.str()); meta->mIsAI = false; } retList.push_back(meta); } else { //updateMetaDataList in DeckManager.cpp performs some weird magic, swapping data between its cache and the "retList" from this function //Bottom line, we need to guarantee retList contains exactly the same items as (or updated versions of) the items in DeckManager Cache //In other words, any meta data that didn't make it to retList in this function must be erased from the DeckManager cache deckManager->DeleteMetaData(filename.str(), isAI); } nbDecks++; } meta = NULL; } std::sort(retList.begin(), retList.end(), sortByName); return retList; }