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.:)
This commit is contained in:
techdragon.nguyen@gmail.com
2011-02-10 17:19:11 +00:00
parent e3ac27bc30
commit 93c63cef3d
9 changed files with 70 additions and 80 deletions
-2
View File
@@ -27,8 +27,6 @@ public:
vector<DeckMetaData*> * getPlayerDeckOrderList(); vector<DeckMetaData*> * getPlayerDeckOrderList();
vector<DeckMetaData*> * getAIDeckOrderList(); vector<DeckMetaData*> * getAIDeckOrderList();
void saveDeck ( const string& deckFilename, MTGAllCards *collection );
void saveDeck ( MTGDeck *deck, MTGAllCards *collection );
void AddMetaData( const std::string& filename, bool isAI); void AddMetaData( const std::string& filename, bool isAI);
DeckMetaData* getDeckMetaDataById(int deckId, bool isAI); DeckMetaData* getDeckMetaDataById(int deckId, bool isAI);
DeckMetaData* getDeckMetaDataByFilename(const std::string& filename, bool isAI); DeckMetaData* getDeckMetaDataByFilename(const std::string& filename, bool isAI);
+5 -4
View File
@@ -15,10 +15,11 @@ class GameObserver;
class DeckStat class DeckStat
{ {
public: public:
DeckStat(int _nbgames = 0, int _victories = 0); DeckStat(int _nbgames = 0, int _victories = 0, string manaColorIndex = "");
int nbgames; int nbgames;
int victories; int victories;
string manaColorIndex;
int percentVictories(); int percentVictories();
}; };
@@ -27,9 +28,8 @@ class DeckStats
protected: protected:
static DeckStats * mInstance; static DeckStats * mInstance;
public: public:
//map<string, DeckStat *> stats; // current set of statistics
string currentDeck; string currentDeck;
map<string, map<string,DeckStat*> > masterDeckStats; map<string, map<string, DeckStat*> > masterDeckStats;
static DeckStats * GetInstance(); static DeckStats * GetInstance();
static void EndInstance(); static void EndInstance();
@@ -94,7 +94,8 @@ public:
int countNonLandProducersPerColor[Constants::MTG_NB_COLORS + 1]; int countNonLandProducersPerColor[Constants::MTG_NB_COLORS + 1];
int totalCostPerColor[Constants::MTG_NB_COLORS + 1]; int totalCostPerColor[Constants::MTG_NB_COLORS + 1];
int totalColoredSymbols; int totalColoredSymbols;
string getManaColorIndex();
void updateStats(string filename, MTGAllCards * collection); void updateStats(string filename, MTGAllCards * collection);
void updateStats(DeckDataWrapper *mtgDeck); void updateStats(DeckDataWrapper *mtgDeck);
int countCardsByType(const char * _type, DeckDataWrapper * myDeck); int countCardsByType(const char * _type, DeckDataWrapper * myDeck);
-1
View File
@@ -157,7 +157,6 @@ public:
string meta_desc; string meta_desc;
string meta_name; string meta_name;
int meta_id; int meta_id;
string meta_deck_colors;
int totalCards(); int totalCards();
int totalPrice(); int totalPrice();
MTGDeck(MTGAllCards * _allcards); MTGDeck(MTGAllCards * _allcards);
-36
View File
@@ -105,42 +105,6 @@ DeckMetaData* DeckManager::getDeckMetaDataByFilename(const string& filename, boo
return deck; 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 ) void DeckManager::AddMetaData( const string& filename, bool isAI )
{ {
if (isAI) if (isAI)
-1
View File
@@ -224,7 +224,6 @@ void DeckMenu::Render()
{ {
mSelectedDeckId = i; mSelectedDeckId = i;
mSelectedDeck = currentMenuItem->meta; mSelectedDeck = currentMenuItem->meta;
WFont *mainFont = WResourceManager::Instance()->GetWFont(Fonts::MAIN_FONT); WFont *mainFont = WResourceManager::Instance()->GetWFont(Fonts::MAIN_FONT);
// display the "more info" button if special condition is met // display the "more info" button if special condition is met
+2 -2
View File
@@ -34,6 +34,7 @@ void DeckMetaData::LoadStats()
mPercentVictories = stats->percentVictories(mStatsFilename); mPercentVictories = stats->percentVictories(mStatsFilename);
mVictories = opponentDeckStats->victories; mVictories = opponentDeckStats->victories;
mGamesPlayed = opponentDeckStats->nbgames; mGamesPlayed = opponentDeckStats->nbgames;
mColorIndex = opponentDeckStats->manaColorIndex;
ostringstream oss; ostringstream oss;
int deckFilenameOffset = mStatsFilename.find("deck") + 4; int deckFilenameOffset = mStatsFilename.find("deck") + 4;
int oppDeckId = atoi(mStatsFilename.substr(deckFilenameOffset, mStatsFilename.find_last_of(".")).c_str()); int oppDeckId = atoi(mStatsFilename.substr(deckFilenameOffset, mStatsFilename.find_last_of(".")).c_str());
@@ -65,7 +66,7 @@ void DeckMetaData::LoadStats()
if (FileExists(mStatsFilename)) if (FileExists(mStatsFilename))
{ {
stats->load(mStatsFilename); stats->load(mStatsFilename);
mGamesPlayed = stats->nbGames(); mGamesPlayed = stats->nbGames();
mPercentVictories = stats->percentVictories(); mPercentVictories = stats->percentVictories();
mVictories = static_cast<int>(mGamesPlayed * (mPercentVictories / 100.0f)); mVictories = static_cast<int>(mGamesPlayed * (mPercentVictories / 100.0f));
} }
@@ -94,7 +95,6 @@ void DeckMetaData::LoadDeck()
mName = trim(deck.meta_name); mName = trim(deck.meta_name);
mDescription = trim(deck.meta_desc); mDescription = trim(deck.meta_desc);
mDeckId = atoi((mFilename.substr(mFilename.find("deck") + 4, mFilename.find(".txt"))).c_str()); mDeckId = atoi((mFilename.substr(mFilename.find("deck") + 4, mFilename.find(".txt"))).c_str());
mColorIndex = deck.meta_deck_colors;
mDeckLoaded = true; mDeckLoaded = true;
} }
+62 -3
View File
@@ -9,7 +9,7 @@
DeckStats * DeckStats::mInstance = NULL; 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) 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)) while (std::getline(file, s))
{ {
string deckfile = s; string deckfile = s;
@@ -126,11 +142,18 @@ void DeckStats::load(const std::string& filename)
int games = atoi(s.c_str()); int games = atoi(s.c_str());
std::getline(file, s); std::getline(file, s);
int victories = atoi(s.c_str()); 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()) if ( masterDeckStats[filename].find(deckfile) != masterDeckStats[filename].end())
{ {
SAFE_DELETE( masterDeckStats[filename][deckfile] ); SAFE_DELETE( masterDeckStats[filename][deckfile] );
} }
DeckStat * newDeckStat = NEW DeckStat(games, victories); DeckStat * newDeckStat = NEW DeckStat(games, victories, manaColorIndex);
(masterDeckStats[filename])[deckfile] = newDeckStat; (masterDeckStats[filename])[deckfile] = newDeckStat;
} }
file.close(); file.close();
@@ -145,6 +168,20 @@ void DeckStats::save(const std::string& filename)
{ {
map<string, DeckStat *> stats = masterDeckStats[currentDeck]; map<string, DeckStat *> stats = masterDeckStats[currentDeck];
map<string, DeckStat *>::iterator it; 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->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++) for (it = stats.begin(); it != stats.end(); it++)
{ {
sprintf(writer, "%s\n", it->first.c_str()); sprintf(writer, "%s\n", it->first.c_str());
@@ -153,6 +190,7 @@ void DeckStats::save(const std::string& filename)
file << writer; file << writer;
sprintf(writer, "%i\n", it->second->victories); sprintf(writer, "%i\n", it->second->victories);
file << writer; file << writer;
file << "MANA:" << it->second->manaColorIndex <<endl;
} }
file.close(); file.close();
} }
@@ -173,14 +211,23 @@ void DeckStats::saveStats(Player *player, Player *opponent, GameObserver * game)
load(currentDeck); load(currentDeck);
map<string, DeckStat *> *stats = &masterDeckStats[currentDeck]; map<string, DeckStat *> *stats = &masterDeckStats[currentDeck];
map<string, DeckStat *>::iterator it = stats->find(opponent->deckFileSmall); 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()) 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 else
{ {
it->second->victories += victory; it->second->victories += victory;
it->second->nbgames += 1; it->second->nbgames += 1;
if ( it->second->manaColorIndex == "" )
{
it->second->manaColorIndex = manaColorIndex;
}
} }
save(currentDeck); 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) void StatsWrapper::updateStats(string filename, MTGAllCards *collection)
{ {
if (FileExists(filename)) if (FileExists(filename))
-5
View File
@@ -226,11 +226,6 @@ void GameStateDuel::End()
if (!premadeDeck && mPlayers[0] && mPlayers[1]) if (!premadeDeck && mPlayers[0] && mPlayers[1])
{ // save the stats for the game { // save the stats for the game
mPlayers[0]->End(); 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] ) else if ( !mPlayers[1] && mPlayers[0] )
// clean up player object // clean up player object
+1 -26
View File
@@ -775,12 +775,6 @@ MTGDeck::MTGDeck(const char * config_file, MTGAllCards * _allcards, int meta_onl
meta_desc.append(s.substr(found + 5)); meta_desc.append(s.substr(found + 5));
continue; continue;
} }
found = s.find("MANA:");
if ( found != string::npos )
{
string colorIndex = s.substr(found + 5);
meta_deck_colors = trim(colorIndex);
}
continue; continue;
} }
if (meta_only) break; 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; bool saveDetailedDeckInfo = options.get( Options::SAVEDETAILEDDECKINFO )->number == 1;
if ( filename.find("collection.dat") == string::npos ) 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
saveDetailedDeckInfo = false; saveDetailedDeckInfo = false;
if (useExpandedDescriptions || saveDetailedDeckInfo) if (useExpandedDescriptions || saveDetailedDeckInfo)