//MTGAllCards int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primitive) { if ('#' == s[0]) return 1; // a comment shouldn't be treated as an error condition size_t i = s.find_first_of('='); if (i == string::npos || 0 == i) return 0; char* key = const_cast<char*> (s.c_str()); // I know what I'm doing, let me do it key[i] = 0; char* val = key + i + 1; switch (key[0]) { case 'a': if (0 == strcmp("auto", key)) { if (!primitive) primitive = NEW CardPrimitive(); primitive->addMagicText(val); } else if (0 == strncmp("auto", key, 4)) { if (!primitive) primitive = NEW CardPrimitive(); primitive->addMagicText(val, key + 4); } else if (0 == strcmp("alias", key)) { if (!primitive) primitive = NEW CardPrimitive(); primitive->alias = atoi(val); } else if (0 == strcmp("abilities", key)) { if (!primitive) primitive = NEW CardPrimitive(); string value = val; //Specific Abilities std::transform(value.begin(), value.end(), value.begin(), ::tolower); vector<string> values = split(value, ','); for (size_t values_i = 0; values_i < values.size(); ++values_i) { for (int j = Constants::NB_BASIC_ABILITIES - 1; j >= 0; --j) { if (values[values_i].find(Constants::MTGBasicAbilities[j]) != string::npos) { primitive->basicAbilities[j] = 1; break; } } } } break; case 'b': //buyback if (!primitive) primitive = NEW CardPrimitive(); if (ManaCost * cost = primitive->getManaCost()) { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); cost->BuyBack = ManaCost::parseManaCost(value); } break; case 'c': //color if (!primitive) primitive = NEW CardPrimitive(); { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); vector<string> values = split(value, ','); int removeAllOthers = 1; for (size_t values_i = 0; values_i < values.size(); ++values_i) { primitive->setColor(values[values_i], removeAllOthers); removeAllOthers = 0; } } break; case 'f': //flashback//morph { if (!primitive) primitive = NEW CardPrimitive(); if(ManaCost * cost = primitive->getManaCost()) { if( s.find("facedown") != string::npos)//morph { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); cost->morph = ManaCost::parseManaCost(value); } else { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); cost->FlashBack = ManaCost::parseManaCost(value); } } break; } case 'g': //grade if (s.size() - i - 1 > 2) currentGrade = getGrade(val[2]); break; case 'i': //id if (!card) card = NEW MTGCard(); card->setMTGId(atoi(val)); break; case 'k': //kicker if (!primitive) primitive = NEW CardPrimitive(); if (ManaCost * cost = primitive->getManaCost()) { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); size_t multikick = value.find("multi"); bool isMultikicker = false; if(multikick != string::npos) { size_t endK = value.find("{",multikick); value.erase(multikick, endK - multikick); isMultikicker = true; } cost->kicker = ManaCost::parseManaCost(value); cost->kicker->isMulti = isMultikicker; } break; case 'm': //mana if (!primitive) primitive = NEW CardPrimitive(); { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); primitive->setManaCost(value); } break; case 'n': //name if (!primitive) primitive = NEW CardPrimitive(); primitive->setName(val); break; case 'o': //othercost/otherrestriction if (!primitive) primitive = NEW CardPrimitive(); if(key[5] == 'r')//otherrestrictions { string value = val; primitive->setOtherRestrictions(value); } else { if (ManaCost * cost = primitive->getManaCost()) { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); size_t name = value.find("name("); string theName = ""; if(name != string::npos) { size_t endName = value.find(")",name); theName = value.substr(name + 5,endName - name - 5); value.erase(name, endName - name + 1); } cost->alternative = ManaCost::parseManaCost(value); if(theName.size()) cost->alternative->alternativeName.append(theName); } } break; case 'p': if ('r' == key[1]) { // primitive if (!card) card = NEW MTGCard(); map<string, CardPrimitive*>::iterator it = primitives.find(val); if (it != primitives.end()) card->setPrimitive(it->second); } else { //power if (!primitive) primitive = NEW CardPrimitive(); primitive->setPower(atoi(val)); } break; case 'r': //retrace/rarity//restrictions if('s' == key[2] && 't' == key[3])//restrictions { if (!primitive) primitive = NEW CardPrimitive(); string value = val; primitive->setRestrictions(value); } else if ('e' == key[1] && 't' == key[2]) { //retrace if (!primitive) primitive = NEW CardPrimitive(); if (ManaCost * cost = primitive->getManaCost()) { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); cost->Retrace = ManaCost::parseManaCost(value); } } else if (s.find("rar") != string::npos) {//rarity if (!card) card = NEW MTGCard(); card->setRarity(val[0]); } break; case 's': //subtype, suspend { if (s.find("suspend") != string::npos) { size_t time = s.find("suspend("); size_t end = s.find(")="); int suspendTime = atoi(s.substr(time + 8,end - 2).c_str()); if (!primitive) primitive = NEW CardPrimitive(); if (ManaCost * cost = primitive->getManaCost()) { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); cost->suspend = ManaCost::parseManaCost(value); primitive->suspendedTime = suspendTime; } } else { if (!primitive) primitive = NEW CardPrimitive(); vector<string> values = split(val, ' '); for (size_t values_i = 0; values_i < values.size(); ++values_i) primitive->setSubtype(values[values_i]); } break; } case 't': if (!primitive) primitive = NEW CardPrimitive(); if (0 == strcmp("target", key)) { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); primitive->spellTargetType = value; } else if (0 == strcmp("text", key)) primitive->setText(val); else if (0 == strcmp("type", key)) { vector<string> values = split(val, ' '); for (size_t values_i = 0; values_i < values.size(); ++values_i) primitive->setType(values[values_i]); } else if (0 == strcmp("toughness", key)) primitive->setToughness(atoi(val)); break; default: if(primitive) { DebugTrace( endl << "MTGDECK Parsing Error: " << " [" << primitive->getName() << "]" << s << std::endl); } else { DebugTrace( endl << "MTGDECK Parsing Generic Error: " << s << std::endl); } break; } tempPrimitive = primitive; tempCard = card; return i; }
//MTGAllCards int MTGAllCards::processConfLine(string &s, MTGCard *card, CardPrimitive * primitive) { if ('#' == s[0]) return 1; // a comment shouldn't be treated as an error condition size_t del_pos = s.find_first_of('='); if (del_pos == string::npos || 0 == del_pos) return 0; s[del_pos] = '\0'; const string key = s.substr(0, del_pos); const string val = s.substr(del_pos + 1); switch (key[0]) { case 'a': if (key == "auto") { if (!primitive) primitive = NEW CardPrimitive(); primitive->addMagicText(val); } else if (StartsWith(key, "auto")) { if (!primitive) primitive = NEW CardPrimitive(); primitive->addMagicText(val, key.substr(4)); } else if (key == "alias") { if (!primitive) primitive = NEW CardPrimitive(); primitive->alias = atoi(val.c_str()); } else if (key == "abilities") { if (!primitive) primitive = NEW CardPrimitive(); string value = val; //Specific Abilities std::transform(value.begin(), value.end(), value.begin(), ::tolower); vector<string> values = split(value, ','); for (size_t values_i = 0; values_i < values.size(); ++values_i) { for (int j = Constants::NB_BASIC_ABILITIES - 1; j >= 0; --j) { if (values[values_i].find(Constants::MTGBasicAbilities[j]) != string::npos) { primitive->basicAbilities[j] = 1; break; } } } } break; case 'b': //buyback if (!primitive) primitive = NEW CardPrimitive(); if (ManaCost * cost = primitive->getManaCost()) { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); cost->setBuyback(ManaCost::parseManaCost(value)); } break; case 'c': //color if (!primitive) primitive = NEW CardPrimitive(); { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); vector<string> values = split(value, ','); int removeAllOthers = 1; for (size_t values_i = 0; values_i < values.size(); ++values_i) { primitive->setColor(values[values_i], removeAllOthers); removeAllOthers = 0; } } break; case 'd'://dredge if (!primitive) primitive = NEW CardPrimitive(); { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); vector<string> values = parseBetween(value,"dredge(",")"); if(values.size()) primitive->dredgeAmount = atoi(values[1].c_str()); break; } case 'f': //flashback//morph { if (!primitive) primitive = NEW CardPrimitive(); if(ManaCost * cost = primitive->getManaCost()) { if( s.find("facedown") != string::npos)//morph { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); cost->setMorph(ManaCost::parseManaCost(value)); } else { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); cost->setFlashback(ManaCost::parseManaCost(value)); } } break; } case 'g': //grade if (s.size() - del_pos - 1 > 2) currentGrade = getGrade(val[2]); break; case 'i': //id if (!card) card = NEW MTGCard(); card->setMTGId(atoi(val.c_str())); break; case 'k': //kicker if (!primitive) primitive = NEW CardPrimitive(); if (ManaCost * cost = primitive->getManaCost()) { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); size_t multikick = value.find("multi"); bool isMultikicker = false; if(multikick != string::npos) { size_t endK = value.find("{",multikick); value.erase(multikick, endK - multikick); isMultikicker = true; } cost->setKicker(ManaCost::parseManaCost(value)); cost->getKicker()->isMulti = isMultikicker; } break; case 'm': //mana if (!primitive) primitive = NEW CardPrimitive(); { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); primitive->setManaCost(value); } break; case 'n': //name if (!primitive) primitive = NEW CardPrimitive(); primitive->setName(val); break; case 'o': //othercost/otherrestriction if (!primitive) primitive = NEW CardPrimitive(); if(key[5] == 'r')//otherrestrictions { string value = val; primitive->setOtherRestrictions(value); } else { if (ManaCost * cost = primitive->getManaCost()) { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); size_t name = value.find("name("); string theName = ""; if(name != string::npos) { size_t endName = value.find(")",name); theName = value.substr(name + 5,endName - name - 5); value.erase(name, endName - name + 1); } cost->setAlternative(ManaCost::parseManaCost(value)); if(theName.size()) cost->getAlternative()->alternativeName.append(theName); } } break; case 'p': if (key[1] == 'r') { // primitive if (!card) card = NEW MTGCard(); map<string, CardPrimitive*>::iterator it = primitives.find(val); if (it != primitives.end()) card->setPrimitive(it->second); } else { //power if (!primitive) primitive = NEW CardPrimitive(); primitive->setPower(atoi(val.c_str())); } break; case 'r': //retrace/rarity//restrictions if(key[2] == 's' && key[3] == 't')//restrictions { if (!primitive) primitive = NEW CardPrimitive(); string value = val; primitive->setRestrictions(value); } else if (key[1] == 'e' && key[2] == 't') { //retrace if (!primitive) primitive = NEW CardPrimitive(); if (ManaCost * cost = primitive->getManaCost()) { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); cost->setRetrace(ManaCost::parseManaCost(value)); } } else if (s.find("rar") != string::npos) { //rarity if (!card) card = NEW MTGCard(); card->setRarity(val[0]); } break; case 's': //subtype, suspend { if (s.find("suspend") != string::npos) { size_t time = s.find("suspend("); size_t end = s.find(")="); int suspendTime = atoi(s.substr(time + 8,end - 2).c_str()); if (!primitive) primitive = NEW CardPrimitive(); if (ManaCost * cost = primitive->getManaCost()) { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); cost->setSuspend(ManaCost::parseManaCost(value)); primitive->suspendedTime = suspendTime; } } else { if (!primitive) primitive = NEW CardPrimitive(); vector<string> values = split(val.c_str(), ' '); for (size_t values_i = 0; values_i < values.size(); ++values_i) primitive->setSubtype(values[values_i]); } break; } case 't': if (!primitive) primitive = NEW CardPrimitive(); if (key == "target") { string value = val; std::transform(value.begin(), value.end(), value.begin(), ::tolower); primitive->spellTargetType = value; } else if (key == "text") primitive->setText(val); else if (key == "type") { vector<string> values = split(val, ' '); for (size_t values_i = 0; values_i < values.size(); ++values_i) primitive->setType(values[values_i]); } else if (key == "toughness") primitive->setToughness(atoi(val.c_str())); break; default: if(primitive) { DebugTrace( endl << "MTGDECK Parsing Error: " << " [" << primitive->getName() << "]" << s << std::endl); } else { DebugTrace( endl << "MTGDECK Parsing Generic Error: " << s << std::endl); } break; } tempPrimitive = primitive; tempCard = card; return del_pos; }