Cleande up the deserialization code

This commit is contained in:
Xawotihs
2011-10-02 14:11:30 +00:00
parent 7e1566fe77
commit 86131f7ea0
7 changed files with 22 additions and 220 deletions

View File

@@ -41,6 +41,8 @@ public:
virtual JQuadPtr getIcon(){return JQuadPtr();}
bool parseLine(const string& s);
friend ostream& operator<<(ostream& out, const Damageable& p);
};
class Damage: public Interruptible
@@ -71,8 +73,4 @@ class DamageStack : public GuiLayer, public Interruptible
DamageStack(GameObserver *observer);
};
ostream& operator<<(ostream& out, const Damageable& p);
istream& operator>>(istream& in, Damageable& p);
#endif

View File

@@ -202,8 +202,7 @@ public:
};
ostream& operator<<(ostream&, const MTGGameZone&);
istream& operator>>(istream&, MTGGameZone&);
ostream& operator<<(ostream&, const MTGPlayerCards&);
istream& operator>>(istream&, MTGPlayerCards&);
#endif

View File

@@ -95,8 +95,6 @@ public:
** Returns the path to the stats file of currently selected deck.
*/
std::string GetCurrentDeckStatsFile();
friend istream& operator>>(istream& in, Player& p);
bool parseLine(const string& s);
};

View File

@@ -100,4 +100,19 @@ bool FileExists(const string & filename);
std::string buildFilePath(const vector<string> & folders, const string & filename);
std::string ensureFolder(const string & folderName);
template <class T> istream& operator>>(istream& in, T& p)
{
string s;
while(std::getline(in, s))
{
if(!p.parseLine(s))
{
break;
}
}
return in;
}
#endif

View File

@@ -342,42 +342,3 @@ bool Damageable::parseLine(const string& s)
return true;
}
istream& operator>>(istream& in, Damageable& p)
{
string s;
streampos pos = in.tellg();
while(std::getline(in, s))
{
size_t limiter = s.find("=");
if (limiter == string::npos) limiter = s.find(":");
string areaS;
if (limiter != string::npos)
{
areaS = s.substr(0, limiter);
if (areaS.compare("life") == 0)
{
p.life = atoi((s.substr(limiter + 1)).c_str());
}
else if (areaS.compare("poisoncount") == 0)
{
p.poisonCount = atoi((s.substr(limiter + 1)).c_str());
}
else if (areaS.compare("damagecount") == 0)
{
p.damageCount = atoi((s.substr(limiter + 1)).c_str());
}
else if (areaS.compare("preventable") == 0)
{
p.preventable = atoi((s.substr(limiter + 1)).c_str());
}
else
{
in.seekg(pos);
break;
}
}
pos = in.tellg();
}
return in;
}

View File

@@ -124,8 +124,6 @@ void MTGPlayerCards::OptimizedHand(Player * who,int amount, int lands, int creat
{
//give the Ai hand adventage to insure a challanging match.
GameObserver * game = who->getObserver();
game->currentPlayerId = game->currentPlayerId;
game->currentPlayer = game->currentPlayer;
if (!game->players[0]->isAI() && game->players[1]->isAI())
{
@@ -1072,66 +1070,6 @@ bool MTGGameZone::parseLine(const string& ss)
return result;
}
istream& operator>>(istream& in, MTGGameZone& z)
{
for (int i = 0; i < z.nb_cards; i++)
{
SAFE_DELETE( z.cards[i] );
}
z.cards.clear();
z.cardsMap.clear();
string s;
while(std::getline(in, s))
{
while(s.size())
{
size_t limiter = s.find(",");
MTGCard * card = 0;
string toFind;
if (limiter != string::npos)
{
toFind = trim(s.substr(0, limiter));
s = s.substr(limiter + 1);
}
else
{
toFind = trim(s);
s = "";
}
card = MTGCollection()->getCardByName(toFind);
int id = Rules::getMTGId(toFind);
if (card)
{
/* For the moment we add the card directly in the final zone.
This is not the normal way and this prevents to resolve spells.
We'll need a fusion operation afterward to cast relevant spells */
MTGCardInstance * newCard = NEW MTGCardInstance(card, z.owner->game);
z.addCard(newCard);
}
else
{
if(toFind == "*")
z.nb_cards++;
else if ( id < 0 )
{
// For the moment, we create a dummy Token to please the testsuite
Token* myToken = new Token(id);
z.addCard(myToken);
}
else
{
DebugTrace("Card unfound " << toFind << " " << id);
}
}
}
}
return in;
}
bool MTGPlayerCards::parseLine(const string& s)
{
size_t limiter = s.find("=");
@@ -1165,47 +1103,3 @@ bool MTGPlayerCards::parseLine(const string& s)
return false;
}
istream& operator>>(istream& in, MTGPlayerCards& z)
{
string s;
streampos pos = in.tellg();
while(std::getline(in, s))
{
size_t limiter = s.find("=");
if (limiter == string::npos) limiter = s.find(":");
string areaS;
if (limiter != string::npos)
{
areaS = s.substr(0, limiter);
if (areaS.compare("graveyard") == 0)
{
istringstream stream(s.substr(limiter+1));
stream >> (*z.graveyard);
}
else if (areaS.compare("library") == 0)
{
istringstream stream(s.substr(limiter+1));
stream >> (*z.library);
}
else if (areaS.compare("hand") == 0)
{
istringstream stream(s.substr(limiter+1));
stream >> (*z.hand);
}
else if (areaS.compare("inplay") == 0 || areaS.compare("battlefield") == 0)
{
istringstream stream(s.substr(limiter+1));
stream >> (*z.battlefield);
}
else
{
in.seekg(pos);
break;
}
}
pos = in.tellg();
}
return in;
}

View File

@@ -203,16 +203,10 @@ std::string Player::GetCurrentDeckStatsFile()
return options.profileFile(filename.str());
}
ostream& operator<<(ostream& out, const Player& p)
{
return out << *(p.game);
}
bool Player::parseLine(const string& s)
{
if(((Damageable*)this)->parseLine(s))
return true;
if(((Damageable*)this)->parseLine(s))
return true;
size_t limiter = s.find("=");
if (limiter == string::npos) limiter = s.find(":");
@@ -263,64 +257,7 @@ bool Player::parseLine(const string& s)
return false;
}
istream& operator>>(istream& in, Player& p)
ostream& operator<<(ostream& out, const Player& p)
{
string s;
streampos pos = in.tellg();
in >> *((Damageable*)&p);
in.seekg(pos);
while(std::getline(in, s))
{
size_t limiter = s.find("=");
if (limiter == string::npos) limiter = s.find(":");
string areaS;
if (limiter != string::npos)
{
areaS = s.substr(0, limiter);
if (areaS.compare("manapool") == 0)
{
SAFE_DELETE(p.manaPool);
p.manaPool = new ManaPool(&p);
ManaCost::parseManaCost(s.substr(limiter + 1), p.manaPool);
}
else if (areaS.compare("avatar") == 0)
{ // We don't load directly for now
p.mAvatarName = s.substr(limiter + 1);
}
else if (areaS.compare("customphasering") == 0)
{
p.phaseRing = s.substr(limiter + 1);
}
else if (areaS.compare("offerinterruptonphase") == 0)
{
for (int i = 0; i < Constants::NB_MTG_PHASES; i++)
{
string phaseStr = Constants::MTGPhaseCodeNames[i];
if (s.find(phaseStr) != string::npos)
{
p.offerInterruptOnPhase = PhaseRing::phaseStrToInt(phaseStr);
break;
}
}
}
else
{
in.seekg(pos);
break;
}
}
pos = in.tellg();
}
if(!p.game)
{
p.game = new MTGPlayerCards();
p.game->setOwner(&p);
}
in >> *(p.game);
return in;
return out << *(p.game);
}