void DeckStats::load(const std::string& filename) { currentDeck = filename; if ( masterDeckStats.find(filename) != masterDeckStats.end() ) { return; } std::string contents; if (JFileSystem::GetInstance()->readIntoString(filename, contents)) { std::stringstream stream(contents); std::string s; // get the associated player deck file: 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); DeckMetaData *playerDeckMetaData = DeckManager::GetInstance()->getDeckMetaDataByFilename( playerDeckFilePath, false); if (!playerDeckMetaData) { DebugTrace("DeckStats.cpp:CONSISTENCY ERROR: DeckStats are set, but no deck meta data"); return; } // check if this player deck has already been profiled for manacolors char next = stream.peek(); string manaColorIndex = ""; if ( next == 'M') { std::getline(stream, s ); manaColorIndex = s.substr( s.find(":") + 1); playerDeckMetaData->setColorIndex( manaColorIndex ); } while (std::getline(stream, s)) { string deckfile = s; std::getline(stream, s); int games = atoi(s.c_str()); std::getline(stream, s); int victories = atoi(s.c_str()); next = stream.peek(); if ( next == 'M') { std::getline(stream, s ); manaColorIndex = s.substr( s.find(":") + 1); } if ( masterDeckStats[filename].find(deckfile) != masterDeckStats[filename].end()) { SAFE_DELETE( masterDeckStats[filename][deckfile] ); } DeckStat * newDeckStat = NEW DeckStat(games, victories, manaColorIndex); (masterDeckStats[filename])[deckfile] = newDeckStat; } } }
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(); } }