From 93c63cef3d023d5ae694d3c88401665599d4ff71 Mon Sep 17 00:00:00 2001 From: "techdragon.nguyen@gmail.com" Date: Thu, 10 Feb 2011 17:19:11 +0000 Subject: [PATCH] reimplemented mana color display for deck selection. This is now totally controlled by stats generation. No data needs to be written to the deck master files themselves. Now the mana colors will only show if you have battled with a particular deck at least once. This is not retroactive, so you will need to battle the ai again. This can not be edited manually either to prevent tampering with the statistical data. Player deck mana color display is also covered this way. Decks will still be saved in the new layout if a disk write is necessary. So any changes via the deck editor will result in a deck file rewrite is was always the case.:) --- projects/mtg/include/DeckManager.h | 2 - projects/mtg/include/DeckStats.h | 9 +++-- projects/mtg/include/MTGDeck.h | 1 - projects/mtg/src/DeckManager.cpp | 36 ----------------- projects/mtg/src/DeckMenu.cpp | 1 - projects/mtg/src/DeckMetaData.cpp | 4 +- projects/mtg/src/DeckStats.cpp | 65 ++++++++++++++++++++++++++++-- projects/mtg/src/GameStateDuel.cpp | 5 --- projects/mtg/src/MTGDeck.cpp | 27 +------------ 9 files changed, 70 insertions(+), 80 deletions(-) diff --git a/projects/mtg/include/DeckManager.h b/projects/mtg/include/DeckManager.h index 6765105e6..7ca56d9df 100644 --- a/projects/mtg/include/DeckManager.h +++ b/projects/mtg/include/DeckManager.h @@ -27,8 +27,6 @@ public: vector * getPlayerDeckOrderList(); vector * getAIDeckOrderList(); - void saveDeck ( const string& deckFilename, MTGAllCards *collection ); - void saveDeck ( MTGDeck *deck, MTGAllCards *collection ); void AddMetaData( const std::string& filename, bool isAI); DeckMetaData* getDeckMetaDataById(int deckId, bool isAI); DeckMetaData* getDeckMetaDataByFilename(const std::string& filename, bool isAI); diff --git a/projects/mtg/include/DeckStats.h b/projects/mtg/include/DeckStats.h index 8434e9b37..428b538c8 100644 --- a/projects/mtg/include/DeckStats.h +++ b/projects/mtg/include/DeckStats.h @@ -15,10 +15,11 @@ class GameObserver; class DeckStat { public: - DeckStat(int _nbgames = 0, int _victories = 0); + DeckStat(int _nbgames = 0, int _victories = 0, string manaColorIndex = ""); int nbgames; int victories; + string manaColorIndex; int percentVictories(); }; @@ -27,9 +28,8 @@ class DeckStats protected: static DeckStats * mInstance; public: - //map stats; // current set of statistics string currentDeck; - map > masterDeckStats; + map > masterDeckStats; static DeckStats * GetInstance(); static void EndInstance(); @@ -94,7 +94,8 @@ public: int countNonLandProducersPerColor[Constants::MTG_NB_COLORS + 1]; int totalCostPerColor[Constants::MTG_NB_COLORS + 1]; int totalColoredSymbols; - + + string getManaColorIndex(); void updateStats(string filename, MTGAllCards * collection); void updateStats(DeckDataWrapper *mtgDeck); int countCardsByType(const char * _type, DeckDataWrapper * myDeck); diff --git a/projects/mtg/include/MTGDeck.h b/projects/mtg/include/MTGDeck.h index da3ee48d0..c896d7812 100644 --- a/projects/mtg/include/MTGDeck.h +++ b/projects/mtg/include/MTGDeck.h @@ -157,7 +157,6 @@ public: string meta_desc; string meta_name; int meta_id; - string meta_deck_colors; int totalCards(); int totalPrice(); MTGDeck(MTGAllCards * _allcards); diff --git a/projects/mtg/src/DeckManager.cpp b/projects/mtg/src/DeckManager.cpp index adf3570ef..041c4a026 100644 --- a/projects/mtg/src/DeckManager.cpp +++ b/projects/mtg/src/DeckManager.cpp @@ -105,42 +105,6 @@ DeckMetaData* DeckManager::getDeckMetaDataByFilename(const string& filename, boo return deck; } -void DeckManager::saveDeck( MTGDeck *deck, MTGAllCards *collection ) -{ - if ( deck->meta_deck_colors == "" ) - { - bool isAI = deck->getFilename().find("baka") != string::npos; - StatsWrapper *stats = getExtendedStatsForDeckId( deck->meta_id, collection, isAI ); - ostringstream manaColorIndex; - for (int i = Constants::MTG_COLOR_ARTIFACT; i < Constants::MTG_COLOR_LAND; ++i) - { - if (stats->totalCostPerColor[i] != 0) - manaColorIndex << "1"; - else - manaColorIndex <<"0"; - } - deck->meta_deck_colors = manaColorIndex.str(); - // save the deck to disk - deck->save( deck->getFilename(), true, deck->meta_name, deck->meta_desc ); - // update DeckMetaData object - DeckMetaData *meta = getDeckMetaDataByFilename(deck->getFilename(), isAI); - meta->setColorIndex( deck->meta_deck_colors ); - } -} - -void DeckManager::saveDeck( const string& deckFilename, MTGAllCards *collection ) -{ - bool isAI = deckFilename.find("baka") != string::npos; - DeckMetaData *metaData = getDeckMetaDataByFilename( deckFilename, isAI ); - if ( metaData->getColorIndex() == "" ) - { - MTGDeck *tempDeck = NEW MTGDeck( deckFilename.c_str(), collection, 0 ); - saveDeck(tempDeck, collection); - SAFE_DELETE( tempDeck ); - } -} - - void DeckManager::AddMetaData( const string& filename, bool isAI ) { if (isAI) diff --git a/projects/mtg/src/DeckMenu.cpp b/projects/mtg/src/DeckMenu.cpp index 315ad21a5..360d8699b 100644 --- a/projects/mtg/src/DeckMenu.cpp +++ b/projects/mtg/src/DeckMenu.cpp @@ -224,7 +224,6 @@ void DeckMenu::Render() { mSelectedDeckId = i; mSelectedDeck = currentMenuItem->meta; - WFont *mainFont = WResourceManager::Instance()->GetWFont(Fonts::MAIN_FONT); // display the "more info" button if special condition is met diff --git a/projects/mtg/src/DeckMetaData.cpp b/projects/mtg/src/DeckMetaData.cpp index ce29deb15..64c854a88 100644 --- a/projects/mtg/src/DeckMetaData.cpp +++ b/projects/mtg/src/DeckMetaData.cpp @@ -34,6 +34,7 @@ void DeckMetaData::LoadStats() mPercentVictories = stats->percentVictories(mStatsFilename); mVictories = opponentDeckStats->victories; mGamesPlayed = opponentDeckStats->nbgames; + mColorIndex = opponentDeckStats->manaColorIndex; ostringstream oss; int deckFilenameOffset = mStatsFilename.find("deck") + 4; int oppDeckId = atoi(mStatsFilename.substr(deckFilenameOffset, mStatsFilename.find_last_of(".")).c_str()); @@ -65,7 +66,7 @@ void DeckMetaData::LoadStats() if (FileExists(mStatsFilename)) { stats->load(mStatsFilename); - mGamesPlayed = stats->nbGames(); + mGamesPlayed = stats->nbGames(); mPercentVictories = stats->percentVictories(); mVictories = static_cast(mGamesPlayed * (mPercentVictories / 100.0f)); } @@ -94,7 +95,6 @@ void DeckMetaData::LoadDeck() mName = trim(deck.meta_name); mDescription = trim(deck.meta_desc); mDeckId = atoi((mFilename.substr(mFilename.find("deck") + 4, mFilename.find(".txt"))).c_str()); - mColorIndex = deck.meta_deck_colors; mDeckLoaded = true; } diff --git a/projects/mtg/src/DeckStats.cpp b/projects/mtg/src/DeckStats.cpp index cd99c0ceb..8829fa595 100644 --- a/projects/mtg/src/DeckStats.cpp +++ b/projects/mtg/src/DeckStats.cpp @@ -9,7 +9,7 @@ DeckStats * DeckStats::mInstance = NULL; -DeckStat::DeckStat(int _nbgames, int _victories) : nbgames(_nbgames), victories(_victories) +DeckStat::DeckStat(int nbgames, int victories, string manaColorIndex) : nbgames(nbgames), victories(victories), manaColorIndex( manaColorIndex) { } @@ -119,6 +119,22 @@ void DeckStats::load(const std::string& filename) if (file) { + // 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); + // check if this player deck has already been profiled for manacolors + char next = file.peek(); + string manaColorIndex = ""; + if ( next == 'M') + { + std::getline(file, s ); + manaColorIndex = s.substr( s.find(":") + 1); + playerDeckMetaData->setColorIndex( manaColorIndex ); + } + while (std::getline(file, s)) { string deckfile = s; @@ -126,11 +142,18 @@ void DeckStats::load(const std::string& filename) int games = atoi(s.c_str()); std::getline(file, s); int victories = atoi(s.c_str()); + next = file.peek(); + + if ( next == 'M') + { + std::getline(file, 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); + DeckStat * newDeckStat = NEW DeckStat(games, victories, manaColorIndex); (masterDeckStats[filename])[deckfile] = newDeckStat; } file.close(); @@ -145,6 +168,20 @@ void DeckStats::save(const std::string& filename) { map stats = masterDeckStats[currentDeck]; map::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->getColorIndex() == "" ) + { + StatsWrapper *stw = deckManager->getExtendedDeckStats( playerDeckMeta, MTGAllCards::getInstance(), false); + manaColorIndex = stw->getManaColorIndex(); + playerDeckMeta->setColorIndex( manaColorIndex ); + } + file << "MANA:" << manaColorIndex << endl; for (it = stats.begin(); it != stats.end(); it++) { sprintf(writer, "%s\n", it->first.c_str()); @@ -153,6 +190,7 @@ void DeckStats::save(const std::string& filename) file << writer; sprintf(writer, "%i\n", it->second->victories); file << writer; + file << "MANA:" << it->second->manaColorIndex < *stats = &masterDeckStats[currentDeck]; map::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) )); + 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); @@ -300,6 +347,18 @@ void StatsWrapper::initStatistics(string deckstats) } } +string StatsWrapper::getManaColorIndex() +{ + ostringstream oss; + for (int i = Constants::MTG_COLOR_ARTIFACT; i < Constants::MTG_COLOR_LAND; ++i) + if (totalCostPerColor[i] != 0) + oss << "1"; + else + oss <<"0"; + return oss.str(); + +} + void StatsWrapper::updateStats(string filename, MTGAllCards *collection) { if (FileExists(filename)) diff --git a/projects/mtg/src/GameStateDuel.cpp b/projects/mtg/src/GameStateDuel.cpp index 43c029ef2..408f32e9e 100644 --- a/projects/mtg/src/GameStateDuel.cpp +++ b/projects/mtg/src/GameStateDuel.cpp @@ -226,11 +226,6 @@ void GameStateDuel::End() if (!premadeDeck && mPlayers[0] && mPlayers[1]) { // save the stats for the game mPlayers[0]->End(); - if (mParent->players[1] != PLAYER_TYPE_TESTSUITE) - { - DeckManager::GetInstance()->saveDeck( mPlayers[1]->deckFile, MTGCollection()); - DeckManager::GetInstance()->saveDeck( mPlayers[0]->deckFile, MTGCollection()); - } } else if ( !mPlayers[1] && mPlayers[0] ) // clean up player object diff --git a/projects/mtg/src/MTGDeck.cpp b/projects/mtg/src/MTGDeck.cpp index 01071ad90..ff64cd002 100644 --- a/projects/mtg/src/MTGDeck.cpp +++ b/projects/mtg/src/MTGDeck.cpp @@ -775,12 +775,6 @@ MTGDeck::MTGDeck(const char * config_file, MTGAllCards * _allcards, int meta_onl meta_desc.append(s.substr(found + 5)); continue; } - found = s.find("MANA:"); - if ( found != string::npos ) - { - string colorIndex = s.substr(found + 5); - meta_deck_colors = trim(colorIndex); - } continue; } if (meta_only) break; @@ -1042,26 +1036,7 @@ int MTGDeck::save(const string& destFileName, bool useExpandedDescriptions, cons bool saveDetailedDeckInfo = options.get( Options::SAVEDETAILEDDECKINFO )->number == 1; - if ( filename.find("collection.dat") == string::npos ) - { - // add in color information - DeckManager *deckManager = DeckManager::GetInstance(); - file <<"#MANA:"; - - string deckId = filename.substr( filename.find("/deck")+5, filename.find(".txt") - (filename.find("/deck")+5) ); - bool isAI = filename.find("ai/baka") != string::npos; - StatsWrapper *stats = deckManager->getExtendedStatsForDeckId( atoi(deckId.c_str()), this->database, isAI ); - - for (int i = Constants::MTG_COLOR_ARTIFACT; i < Constants::MTG_COLOR_LAND; ++i) - { - if (stats->totalCostPerColor[i] != 0) - file << "1"; - else - file <<"0"; - } - file << endl; - } - else + if ( filename.find("collection.dat") != string::npos ) saveDetailedDeckInfo = false; if (useExpandedDescriptions || saveDetailedDeckInfo)