Implemented a lazy load pattern for the deck stats - when the DeckMenu is displaying decks, it calls LoadStats() for only the ones visible in the list. This helps reduces the lag that occurs each time we attempt to load all the AI decks during match selection.
This still could be improved - DeckMetaData's constructor loads an MTGDeck object to parse out the name of a deck from its file. This means that we crack open 106 files on the first attempt to show the list of opponent decks. I started optimizing this, but reverted, as the list itself is sorted alphabetically. Currently, with these mods, it's still taking 4 1/2 seconds on my psp to load the opponent list on the first go around. While at it, did some cleanup - removed the need for passing around a player pointer in some of the DeckStat functions, etc.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
|
||||
#include "DeckManager.h"
|
||||
#include "DeckStats.h"
|
||||
#include "Player.h"
|
||||
#include "GameObserver.h"
|
||||
@@ -27,19 +28,6 @@ DeckStats * DeckStats::GetInstance()
|
||||
return mInstance;
|
||||
}
|
||||
|
||||
void DeckStats::cleanStats()
|
||||
{
|
||||
/* map<string, DeckStat *>::iterator it;
|
||||
for (it = stats.begin(); it != stats.end(); it++)
|
||||
{
|
||||
SAFE_DELETE(it->second);
|
||||
}
|
||||
|
||||
stats.clear();
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
DeckStats::~DeckStats()
|
||||
{
|
||||
map<string, map<string,DeckStat*> > ::iterator it;
|
||||
@@ -118,14 +106,7 @@ int DeckStats::percentVictories()
|
||||
return 50;
|
||||
}
|
||||
|
||||
void DeckStats::load(Player * player)
|
||||
{
|
||||
char filename[512];
|
||||
sprintf(filename, "stats/%s.txt", player->deckFileSmall.c_str());
|
||||
load(options.profileFile(filename).c_str());
|
||||
}
|
||||
|
||||
void DeckStats::load(const char * filename)
|
||||
void DeckStats::load(const std::string& filename)
|
||||
{
|
||||
|
||||
currentDeck = filename;
|
||||
@@ -133,7 +114,7 @@ void DeckStats::load(const char * filename)
|
||||
{
|
||||
return;
|
||||
}
|
||||
wagic::ifstream file(filename);
|
||||
wagic::ifstream file(filename.c_str());
|
||||
std::string s;
|
||||
|
||||
if (file)
|
||||
@@ -156,16 +137,9 @@ void DeckStats::load(const char * filename)
|
||||
}
|
||||
}
|
||||
|
||||
void DeckStats::save(Player * player)
|
||||
void DeckStats::save(const std::string& filename)
|
||||
{
|
||||
char filename[512];
|
||||
sprintf(filename, "stats/%s.txt", player->deckFileSmall.c_str());
|
||||
save(options.profileFile(filename).c_str());
|
||||
}
|
||||
|
||||
void DeckStats::save(const char * filename)
|
||||
{
|
||||
std::ofstream file(filename);
|
||||
std::ofstream file(filename.c_str());
|
||||
char writer[512];
|
||||
if (file)
|
||||
{
|
||||
@@ -196,7 +170,7 @@ void DeckStats::saveStats(Player *player, Player *opponent, GameObserver * game)
|
||||
{
|
||||
victory = 0;
|
||||
}
|
||||
load(player);
|
||||
load(currentDeck);
|
||||
map<string, DeckStat *> *stats = &masterDeckStats[currentDeck];
|
||||
map<string, DeckStat *>::iterator it = stats->find(opponent->deckFileSmall);
|
||||
if (it == stats->end())
|
||||
@@ -208,7 +182,18 @@ void DeckStats::saveStats(Player *player, Player *opponent, GameObserver * game)
|
||||
it->second->victories += victory;
|
||||
it->second->nbgames += 1;
|
||||
}
|
||||
save(player);
|
||||
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::EndInstance()
|
||||
|
||||
Reference in New Issue
Block a user