bool IOLoginData::saveItems(const Player* player, const ItemBlockList& itemList, DBInsert& query_insert, PropWriteStream& propWriteStream) { std::ostringstream ss; typedef std::pair<Container*, int32_t> containerBlock; std::queue<containerBlock> queue; int32_t parentId = 0; int32_t runningId = 100; Database* db = Database::getInstance(); for (const auto& it : itemList) { int32_t pid = it.first; Item* item = it.second; ++runningId; propWriteStream.clear(); item->serializeAttr(propWriteStream); size_t attributesSize; const char* attributes = propWriteStream.getStream(attributesSize); ss << player->getGUID() << ',' << pid << ',' << runningId << ',' << item->getID() << ',' << item->getSubType() << ',' << db->escapeBlob(attributes, attributesSize); if (!query_insert.addRow(ss)) { return false; } if (Container* container = item->getContainer()) { queue.emplace(container, runningId); } } while (!queue.empty()) { const containerBlock& cb = queue.front(); Container* container = cb.first; parentId = cb.second; queue.pop(); for (Item* item : container->getItemList()) { ++runningId; Container* subContainer = item->getContainer(); if (subContainer) { queue.emplace(subContainer, runningId); } propWriteStream.clear(); item->serializeAttr(propWriteStream); size_t attributesSize; const char* attributes = propWriteStream.getStream(attributesSize); ss << player->getGUID() << ',' << parentId << ',' << runningId << ',' << item->getID() << ',' << item->getSubType() << ',' << db->escapeBlob(attributes, attributesSize); if (!query_insert.addRow(ss)) { return false; } } } return query_insert.execute(); }
bool IOLoginData::saveItems(const Player* player, const ItemBlockList& itemList, DBInsert& query_insert) { std::ostringstream stream; typedef std::pair<Container*, int32_t> containerBlock; std::list<containerBlock> stack; int32_t parentId = 0; int32_t runningId = 100; Database* db = Database::getInstance(); for (ItemBlockList::const_iterator it = itemList.begin(); it != itemList.end(); ++it) { int32_t pid = it->first; Item* item = it->second; ++runningId; uint32_t attributesSize = 0; PropWriteStream propWriteStream; item->serializeAttr(propWriteStream); const char* attributes = propWriteStream.getStream(attributesSize); stream << player->getGUID() << ',' << pid << ',' << runningId << ',' << item->getID() << ',' << (int32_t)item->getSubType() << ',' << db->escapeBlob(attributes, attributesSize); if (!query_insert.addRow(stream)) { return false; } if (Container* container = item->getContainer()) { stack.emplace_back(container, runningId); } } while (!stack.empty()) { const containerBlock& cb = stack.front(); Container* container = cb.first; parentId = cb.second; stack.pop_front(); for (Item* item : container->getItemList()) { ++runningId; Container* subContainer = item->getContainer(); if (subContainer) { stack.emplace_back(subContainer, runningId); } uint32_t attributesSize = 0; PropWriteStream propWriteStream; item->serializeAttr(propWriteStream); const char* attributes = propWriteStream.getStream(attributesSize); stream << player->getGUID() << ',' << parentId << ',' << runningId << ',' << item->getID() << ',' << (int32_t)item->getSubType() << ',' << db->escapeBlob(attributes, attributesSize); if (!query_insert.addRow(stream)) { return false; } } } return query_insert.execute(); }
bool IOMapSerialize::saveMap() { int64_t start = OTSYS_TIME(); Database* db = Database::getInstance(); std::ostringstream query; //Start the transaction DBTransaction transaction; if (!transaction.begin()) { return false; } //clear old tile data if (!db->executeQuery("DELETE FROM `tile_store`")) { return false; } DBInsert stmt; stmt.setQuery("INSERT INTO `tile_store` (`house_id`, `data`) VALUES "); PropWriteStream stream; for (const auto& it : Houses::getInstance().getHouses()) { //save house items House* house = it.second; for (HouseTile* tile : house->getTiles()) { saveTile(stream, tile); size_t attributesSize; const char* attributes = stream.getStream(attributesSize); if (attributesSize > 0) { query << house->getId() << ',' << db->escapeBlob(attributes, attributesSize); if (!stmt.addRow(query)) { return false; } } stream.clear(); } } if (!stmt.execute()) { return false; } //End the transaction bool success = transaction.commit(); std::cout << "> Saved house items in: " << (OTSYS_TIME() - start) / (1000.) << " s" << std::endl; return success; }
bool IOMapSerialize::saveHouseBinary(Database* db, DBInsert& stmt, House* house) { PropWriteStream stream; for(HouseTileList::iterator tit = house->getHouseTileBegin(); tit != house->getHouseTileEnd(); ++tit) { if(!saveTile(stream, *tit)) continue; } uint32_t attributesSize = 0; const char* attributes = stream.getStream(attributesSize); if(!attributesSize) return true; DBQuery query; query << house->getId() << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", " << db->escapeBlob(attributes, attributesSize); return stmt.addRow(query); }
bool IOMapSerialize::saveMapBinary(Map* map) { Database* db = Database::getInstance(); //Start the transaction DBTransaction transaction(db); if(!transaction.begin()) return false; DBQuery query; query << "DELETE FROM `house_data` WHERE `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID); if(!db->executeQuery(query.str())) return false; DBInsert stmt(db); stmt.setQuery("INSERT INTO `house_data` (`house_id`, `world_id`, `data`) VALUES "); for(HouseMap::iterator it = Houses::getInstance()->getHouseBegin(); it != Houses::getInstance()->getHouseEnd(); ++it) { //save house items PropWriteStream stream; for(HouseTileList::iterator tit = it->second->getHouseTileBegin(); tit != it->second->getHouseTileEnd(); ++tit) { if(!saveTile(stream, *tit)) return false; } uint32_t attributesSize = 0; const char* attributes = stream.getStream(attributesSize); query.str(""); query << it->second->getId() << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", " << db->escapeBlob(attributes, attributesSize); if(!stmt.addRow(query)) return false; } query.str(""); if(!stmt.execute()) return false; //End the transaction return transaction.commit(); }
bool IOMapSerialize::saveItems(Database* db, uint32_t& tileId, uint32_t houseId, const Tile* tile) { int32_t thingCount = tile->getThingCount(); if(!thingCount) return true; Item* item = NULL; int32_t runningId = 0, parentId = 0; ContainerStackList containerStackList; bool stored = false; DBInsert stmt(db); stmt.setQuery("INSERT INTO `tile_items` (`tile_id`, `world_id`, `sid`, `pid`, `itemtype`, `count`, `attributes`) VALUES "); DBQuery query; for(int32_t i = 0; i < thingCount; ++i) { if(!(item = tile->__getThing(i)->getItem()) || (!item->isMovable() && !item->forceSerialize())) continue; if(!stored) { Position tilePosition = tile->getPosition(); query << "INSERT INTO `tiles` (`id`, `world_id`, `house_id`, `x`, `y`, `z`) VALUES (" << tileId << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", " << houseId << ", " << tilePosition.x << ", " << tilePosition.y << ", " << tilePosition.z << ")"; if(!db->query(query.str())) return false; stored = true; query.str(""); } PropWriteStream propWriteStream; item->serializeAttr(propWriteStream); uint32_t attributesSize = 0; const char* attributes = propWriteStream.getStream(attributesSize); query << tileId << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", " << ++runningId << ", " << parentId << ", " << item->getID() << ", " << (int32_t)item->getSubType() << ", " << db->escapeBlob(attributes, attributesSize); if(!stmt.addRow(query.str())) return false; query.str(""); if(item->getContainer()) containerStackList.push_back(std::make_pair(item->getContainer(), runningId)); } Container* container = NULL; for(ContainerStackList::iterator cit = containerStackList.begin(); cit != containerStackList.end(); ++cit) { container = cit->first; parentId = cit->second; for(ItemList::const_iterator it = container->getItems(); it != container->getEnd(); ++it) { if(!(item = *it)) continue; PropWriteStream propWriteStream; item->serializeAttr(propWriteStream); uint32_t attributesSize = 0; const char* attributes = propWriteStream.getStream(attributesSize); query << tileId << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", " << ++runningId << ", " << parentId << ", " << item->getID() << ", " << (int32_t)item->getSubType() << ", " << db->escapeBlob(attributes, attributesSize); if(!stmt.addRow(query.str())) return false; query.str(""); if(item->getContainer()) containerStackList.push_back(std::make_pair(item->getContainer(), runningId)); } } if(stored) ++tileId; return stmt.execute(); }
bool IOLoginData::savePlayer(Player* player) { if (player->getHealth() <= 0) { player->changeHealth(1); } Database* db = Database::getInstance(); std::ostringstream query; query << "SELECT `save` FROM `players` WHERE `id` = " << player->getGUID(); DBResult_ptr result = db->storeQuery(query.str()); if (!result) { return false; } if (result->getDataInt("save") == 0) { query.str(""); query << "UPDATE `players` SET `lastlogin` = " << player->lastLoginSaved << ", `lastip` = " << player->lastIP << " WHERE `id` = " << player->getGUID(); return db->executeQuery(query.str()); } //serialize conditions PropWriteStream propWriteStream; for (Condition* condition : player->conditions) { if (condition->isPersistent()) { condition->serialize(propWriteStream); propWriteStream.write<uint8_t>(CONDITIONATTR_END); } } size_t conditionsSize; const char* conditions = propWriteStream.getStream(conditionsSize); //First, an UPDATE query to write the player itself query.str(""); query << "UPDATE `players` SET "; query << "`level` = " << player->level << ','; query << "`group_id` = " << player->group->id << ','; query << "`vocation` = " << player->getVocationId() << ','; query << "`health` = " << player->health << ','; query << "`healthmax` = " << player->healthMax << ','; query << "`experience` = " << player->experience << ','; query << "`lookbody` = " << static_cast<uint32_t>(player->defaultOutfit.lookBody) << ','; query << "`lookfeet` = " << static_cast<uint32_t>(player->defaultOutfit.lookFeet) << ','; query << "`lookhead` = " << static_cast<uint32_t>(player->defaultOutfit.lookHead) << ','; query << "`looklegs` = " << static_cast<uint32_t>(player->defaultOutfit.lookLegs) << ','; query << "`looktype` = " << player->defaultOutfit.lookType << ','; query << "`lookaddons` = " << static_cast<uint32_t>(player->defaultOutfit.lookAddons) << ','; query << "`maglevel` = " << player->magLevel << ','; query << "`mana` = " << player->mana << ','; query << "`manamax` = " << player->manaMax << ','; query << "`manaspent` = " << player->manaSpent << ','; query << "`soul` = " << player->soul << ','; query << "`town_id` = " << player->town->getID() << ','; const Position& loginPosition = player->getLoginPosition(); query << "`posx` = " << loginPosition.getX() << ','; query << "`posy` = " << loginPosition.getY() << ','; query << "`posz` = " << loginPosition.getZ() << ','; query << "`cap` = " << (player->capacity / 100) << ','; query << "`sex` = " << player->sex << ','; if (player->lastLoginSaved != 0) { query << "`lastlogin` = " << player->lastLoginSaved << ','; } if (player->lastIP != 0) { query << "`lastip` = " << player->lastIP << ','; } query << "`conditions` = " << db->escapeBlob(conditions, conditionsSize) << ','; if (g_game.getWorldType() != WORLD_TYPE_PVP_ENFORCED) { int32_t skullTime = 0; if (player->skullTicks > 0) { skullTime = time(nullptr) + player->skullTicks / 1000; } query << "`skulltime` = " << skullTime << ','; Skulls_t skull = SKULL_NONE; if (player->skull == SKULL_RED) { skull = SKULL_RED; } else if (player->skull == SKULL_BLACK) { skull = SKULL_BLACK; } query << "`skull` = " << static_cast<uint32_t>(skull) << ','; } query << "`lastlogout` = " << player->getLastLogout() << ','; query << "`balance` = " << player->bankBalance << ','; query << "`offlinetraining_time` = " << player->getOfflineTrainingTime() / 1000 << ','; query << "`offlinetraining_skill` = " << player->getOfflineTrainingSkill() << ','; query << "`stamina` = " << player->getStaminaMinutes() << ','; query << "`skill_fist` = " << player->skills[SKILL_FIST].level << ','; query << "`skill_fist_tries` = " << player->skills[SKILL_FIST].tries << ','; query << "`skill_club` = " << player->skills[SKILL_CLUB].level << ','; query << "`skill_club_tries` = " << player->skills[SKILL_CLUB].tries << ','; query << "`skill_sword` = " << player->skills[SKILL_SWORD].level << ','; query << "`skill_sword_tries` = " << player->skills[SKILL_SWORD].tries << ','; query << "`skill_axe` = " << player->skills[SKILL_AXE].level << ','; query << "`skill_axe_tries` = " << player->skills[SKILL_AXE].tries << ','; query << "`skill_dist` = " << player->skills[SKILL_DISTANCE].level << ','; query << "`skill_dist_tries` = " << player->skills[SKILL_DISTANCE].tries << ','; query << "`skill_shielding` = " << player->skills[SKILL_SHIELD].level << ','; query << "`skill_shielding_tries` = " << player->skills[SKILL_SHIELD].tries << ','; query << "`skill_fishing` = " << player->skills[SKILL_FISHING].level << ','; query << "`skill_fishing_tries` = " << player->skills[SKILL_FISHING].tries << ','; if (!player->isOffline()) { query << "`onlinetime` = `onlinetime` + " << (time(nullptr) - player->lastLoginSaved) << ','; } query << "`blessings` = " << static_cast<uint32_t>(player->blessings); query << " WHERE `id` = " << player->getGUID(); DBTransaction transaction; if (!transaction.begin()) { return false; } if (!db->executeQuery(query.str())) { return false; } // learned spells query.str(""); query << "DELETE FROM `player_spells` WHERE `player_id` = " << player->getGUID(); if (!db->executeQuery(query.str())) { return false; } query.str(""); DBInsert spellsQuery("INSERT INTO `player_spells` (`player_id`, `name` ) VALUES "); for (const std::string& spellName : player->learnedInstantSpellList) { query << player->getGUID() << ',' << db->escapeString(spellName); if (!spellsQuery.addRow(query)) { return false; } } if (!spellsQuery.execute()) { return false; } //item saving query << "DELETE FROM `player_items` WHERE `player_id` = " << player->getGUID(); if (!db->executeQuery(query.str())) { return false; } DBInsert itemsQuery("INSERT INTO `player_items` (`player_id`, `pid`, `sid`, `itemtype`, `count`, `attributes`) VALUES "); ItemBlockList itemList; for (int32_t slotId = 1; slotId <= 10; ++slotId) { Item* item = player->inventory[slotId]; if (item) { itemList.emplace_back(slotId, item); } } if (!saveItems(player, itemList, itemsQuery, propWriteStream)) { return false; } if (player->lastDepotId != -1) { //save depot items query.str(""); query << "DELETE FROM `player_depotitems` WHERE `player_id` = " << player->getGUID(); if (!db->executeQuery(query.str())) { return false; } DBInsert depotQuery("INSERT INTO `player_depotitems` (`player_id`, `pid`, `sid`, `itemtype`, `count`, `attributes`) VALUES "); itemList.clear(); for (const auto& it : player->depotChests) { DepotChest* depotChest = it.second; for (Item* item : depotChest->getItemList()) { itemList.emplace_back(it.first, item); } } if (!saveItems(player, itemList, depotQuery, propWriteStream)) { return false; } } //save inbox items query.str(""); query << "DELETE FROM `player_inboxitems` WHERE `player_id` = " << player->getGUID(); if (!db->executeQuery(query.str())) { return false; } DBInsert inboxQuery("INSERT INTO `player_inboxitems` (`player_id`, `pid`, `sid`, `itemtype`, `count`, `attributes`) VALUES "); itemList.clear(); for (Item* item : player->getInbox()->getItemList()) { itemList.emplace_back(0, item); } if (!saveItems(player, itemList, inboxQuery, propWriteStream)) { return false; } query.str(""); query << "DELETE FROM `player_storage` WHERE `player_id` = " << player->getGUID(); if (!db->executeQuery(query.str())) { return false; } query.str(""); DBInsert storageQuery("INSERT INTO `player_storage` (`player_id`, `key`, `value`) VALUES "); player->genReservedStorageRange(); for (const auto& it : player->storageMap) { query << player->getGUID() << ',' << it.first << ',' << it.second; if (!storageQuery.addRow(query)) { return false; } } if (!storageQuery.execute()) { return false; } //End the transaction return transaction.commit(); }
bool IOPlayer::savePlayer(Player* player) { player->preSave(); Database* db = Database::instance(); DBQuery query; DBResult* result; //check if the player have to be saved or not query << "SELECT `save` FROM `players` WHERE `id` = " << player->getGUID(); if(!(result = db->storeQuery(query.str()))){ return false; } if(result->getDataInt("save") == 0){ db->freeResult(result); query.str(""); query << "UPDATE `players` SET `lastlogin` = " << player->lastLoginSaved << ", `lastip` = " << player->lastip << " WHERE `id` = " << player->getGUID(); DBTransaction transaction(db); if(!transaction.begin()){ return false; } if(!db->executeQuery(query.str())){ return false; } transaction.commit(); return true; } db->freeResult(result); //serialize conditions PropWriteStream propWriteStream; for(ConditionList::const_iterator it = player->conditions.begin(); it != player->conditions.end(); ++it){ if((*it)->isPersistent()){ if(!(*it)->serialize(propWriteStream)){ return false; } propWriteStream.ADD_UCHAR(CONDITIONATTR_END); } } uint32_t conditionsSize; const char* conditions = propWriteStream.getStream(conditionsSize); //First, an UPDATE query to write the player itself query.str(""); query << "UPDATE `players` SET `level` = " << player->level << ", `vocation` = " << (int)player->getVocationId() << ", `health` = " << player->health << ", `healthmax` = " << player->healthMax << ", `direction` = " << (int)player->getDirection() << ", `experience` = " << player->experience << ", `lookbody` = " << (int)player->defaultOutfit.lookBody << ", `lookfeet` = " << (int)player->defaultOutfit.lookFeet << ", `lookhead` = " << (int)player->defaultOutfit.lookHead << ", `looklegs` = " << (int)player->defaultOutfit.lookLegs << ", `looktype` = " << (int)player->defaultOutfit.lookType << ", `maglevel` = " << player->magLevel << ", `mana` = " << player->mana << ", `manamax` = " << player->manaMax << ", `manaspent` = " << player->manaSpent << ", `soul` = " << player->soul << ", `town_id` = " << player->town << ", `posx` = " << player->getLoginPosition().x << ", `posy` = " << player->getLoginPosition().y << ", `posz` = " << player->getLoginPosition().z << ", `cap` = " << player->getCapacity() << ", `sex` = " << player->sex << ", `lastlogin` = " << player->lastLoginSaved << ", `lastip` = " << player->lastip << ", `conditions` = " << db->escapeBlob(conditions, conditionsSize) << ", `loss_experience` = " << (int)player->getLossPercent(LOSS_EXPERIENCE) << ", `loss_mana` = " << (int)player->getLossPercent(LOSS_MANASPENT) << ", `loss_skills` = " << (int)player->getLossPercent(LOSS_SKILLTRIES) << ", `loss_items` = " << (int)player->getLossPercent(LOSS_ITEMS) << ", `balance` = " << player->balance; #ifdef __SKULLSYSTEM__ int32_t redSkullTime = 0; if(player->redSkullTicks > 0){ redSkullTime = std::time(NULL) + player->redSkullTicks/1000; } query << ", `redskulltime` = " << redSkullTime; int32_t redSkull = 0; if(player->skull == SKULL_RED){ redSkull = 1; } query << ", `redskull` = " << redSkull; #endif query << " WHERE `id` = " << player->getGUID(); DBTransaction transaction(db); if(!transaction.begin()){ return false; } if(!db->executeQuery(query.str())){ return false; } query.str(""); //skills for(int i = 0; i <= 6; i++){ query << "UPDATE `player_skills` SET `value` = " << player->skills[i][SKILL_LEVEL] << ", `count` = " << player->skills[i][SKILL_TRIES] << " WHERE `player_id` = " << player->getGUID() << " AND `skillid` = " << i; if(!db->executeQuery(query.str())){ return false; } query.str(""); } // deletes all player-related stuff query << "DELETE FROM `player_spells` WHERE `player_id` = " << player->getGUID(); if(!db->executeQuery(query.str())){ return false; } query.str(""); query << "DELETE FROM `player_items` WHERE `player_id` = " << player->getGUID(); if(!db->executeQuery(query.str())){ return false; } query.str(""); query << "DELETE FROM `player_depotitems` WHERE `player_id` = " << player->getGUID(); if(!db->executeQuery(query.str())){ return false; } query.str(""); query << "DELETE FROM `player_storage` WHERE `player_id` = " << player->getGUID(); if(!db->executeQuery(query.str())){ return false; } query.str(""); query << "DELETE FROM `player_viplist` WHERE `player_id` = " << player->getGUID(); if(!db->executeQuery(query.str())){ return false; } query.str(""); DBInsert stmt(db); //learned spells stmt.setQuery("INSERT INTO `player_spells` (`player_id`, `name`) VALUES "); for(LearnedInstantSpellList::const_iterator it = player->learnedInstantSpellList.begin(); it != player->learnedInstantSpellList.end(); ++it){ query << player->getGUID() << ", " << db->escapeString(*it); if(!stmt.addRow(query)){ return false; } } if(!stmt.execute()){ return false; } ItemBlockList itemList; Item* item; for(int32_t slotId = 1; slotId <= 10; ++slotId){ if((item = player->inventory[slotId])){ itemList.push_back(itemBlock(slotId, item)); } } //item saving stmt.setQuery("INSERT INTO `player_items` (`player_id` , `pid` , `sid` , `itemtype` , `count` , `attributes` ) VALUES "); if(!(saveItems(player, itemList, stmt) && stmt.execute())){ return false; } itemList.clear(); for(DepotMap::iterator it = player->depots.begin(); it != player->depots.end(); ++it){ itemList.push_back(itemBlock(it->first, it->second)); } //save depot items stmt.setQuery("INSERT INTO `player_depotitems` (`player_id` , `pid` , `sid` , `itemtype` , `count` , `attributes` ) VALUES "); if(!(saveItems(player, itemList, stmt) && stmt.execute())){ return false; } stmt.setQuery("INSERT INTO `player_storage` (`player_id` , `key` , `value` ) VALUES "); for(StorageMap::const_iterator cit = player->getStorageIteratorBegin(); cit != player->getStorageIteratorEnd();cit++){ query << player->getGUID() << ", " << cit->first << ", " << cit->second; if(!stmt.addRow(query)){ return false; } } if(!stmt.execute()){ return false; } //save vip list if(!player->VIPList.empty()){ std::stringstream ss; ss << "INSERT INTO `player_viplist` (`player_id`, `vip_id`) SELECT " << player->getGUID() << ", `id` FROM `players` WHERE `id` IN"; stmt.setQuery(ss.str()); ss.str(""); for(VIPListSet::iterator it = player->VIPList.begin(); it != player->VIPList.end();){ ss << (*it); ++it; if(it != player->VIPList.end()){ ss << ","; } } stmt.addRow(ss); if(!stmt.execute()){ return false; } } //End the transaction return transaction.commit(); }
bool IOPlayer::saveItems(Player* player, const ItemBlockList& itemList, DBInsert& query_insert) { std::list<Container*> listContainer; std::stringstream stream; typedef std::pair<Container*, int32_t> containerBlock; std::list<containerBlock> stack; int32_t parentId = 0; int32_t runningId = 100; Database* db = Database::instance(); Item* item; int32_t pid; for(ItemBlockList::const_iterator it = itemList.begin(); it != itemList.end(); ++it){ pid = it->first; item = it->second; ++runningId; uint32_t attributesSize; PropWriteStream propWriteStream; item->serializeAttr(propWriteStream); const char* attributes = propWriteStream.getStream(attributesSize); stream << player->getGUID() << ", " << pid << ", " << runningId << ", " << item->getID() << ", " << (int32_t)item->getSubType() << ", " << db->escapeBlob(attributes, attributesSize); if(!query_insert.addRow(stream)){ return false; } if(Container* container = item->getContainer()){ stack.push_back(containerBlock(container, runningId)); } } while(stack.size() > 0){ const containerBlock& cb = stack.front(); Container* container = cb.first; parentId = cb.second; stack.pop_front(); for(uint32_t i = 0; i < container->size(); ++i){ ++runningId; item = container->getItem(i); if(Container* sub = item->getContainer()){ stack.push_back(containerBlock(sub, runningId)); } uint32_t attributesSize; PropWriteStream propWriteStream; item->serializeAttr(propWriteStream); const char* attributes = propWriteStream.getStream(attributesSize); stream << player->getGUID() << ", " << parentId << ", " << runningId << ", " << item->getID() << ", " << (int32_t)item->getSubType() << ", " << db->escapeBlob(attributes, attributesSize); if(!query_insert.addRow(stream)) return false; } } return true; }
bool IOLoginData::savePlayer(Player* player) { if (player->getHealth() <= 0) { player->changeHealth(1); } Database* db = Database::getInstance(); std::ostringstream query; query << "SELECT `save` FROM `players` WHERE `id` = " << player->getGUID(); DBResult* result = db->storeQuery(query.str()); if (!result) { return false; } if (result->getDataInt("save") == 0) { db->freeResult(result); query.str(""); query << "UPDATE `players` SET `lastlogin` = " << player->lastLoginSaved << ", `lastip` = " << player->lastIP << " WHERE `id` = " << player->getGUID(); return db->executeQuery(query.str()); } db->freeResult(result); //serialize conditions PropWriteStream propWriteStream; for (ConditionList::const_iterator it = player->conditions.begin(); it != player->conditions.end(); ++it) { Condition* condition = *it; if (condition->isPersistent()) { if (!condition->serialize(propWriteStream)) { return false; } propWriteStream.ADD_UCHAR(CONDITIONATTR_END); } } uint32_t conditionsSize = 0; const char* conditions = propWriteStream.getStream(conditionsSize); //First, an UPDATE query to write the player itself query.str(""); query << "UPDATE `players` SET "; query << "`level` = " << player->level << ", "; query << "`group_id` = " << player->groupId << ", "; query << "`vocation` = " << (int32_t)player->getVocationId() << ", "; query << "`health` = " << player->health << ", "; query << "`healthmax` = " << player->healthMax << ", "; query << "`experience` = " << player->experience << ", "; query << "`lookbody` = " << (int32_t)player->defaultOutfit.lookBody << ", "; query << "`lookfeet` = " << (int32_t)player->defaultOutfit.lookFeet << ", "; query << "`lookhead` = " << (int32_t)player->defaultOutfit.lookHead << ", "; query << "`looklegs` = " << (int32_t)player->defaultOutfit.lookLegs << ", "; query << "`looktype` = " << (int32_t)player->defaultOutfit.lookType << ", "; query << "`lookaddons` = " << (int32_t)player->defaultOutfit.lookAddons << ", "; query << "`maglevel` = " << player->magLevel << ", "; query << "`mana` = " << player->mana << ", "; query << "`manamax` = " << player->manaMax << ", "; query << "`manaspent` = " << player->manaSpent << ", "; query << "`soul` = " << player->soul << ", "; query << "`town_id` = " << player->town << ", "; const Position& loginPosition = player->getLoginPosition(); query << "`posx` = " << loginPosition.x << ", "; query << "`posy` = " << loginPosition.y << ", "; query << "`posz` = " << loginPosition.z << ", "; query << "`cap` = " << player->getCapacity() << ", "; query << "`sex` = " << player->sex << ", "; if (player->lastLoginSaved != 0) { query << "`lastlogin` = " << player->lastLoginSaved << ", "; } if (player->lastIP != 0) { query << "`lastip` = " << player->lastIP << ", "; } query << "`conditions` = " << db->escapeBlob(conditions, conditionsSize) << ", "; if (g_game.getWorldType() != WORLD_TYPE_PVP_ENFORCED) { int32_t skullTime = 0; if (player->skullTicks > 0) { skullTime = time(NULL) + player->skullTicks / 1000; } query << "`skulltime` = " << skullTime << ", "; int32_t skull = 0; if (player->skull == SKULL_RED) { skull = SKULL_RED; } else if (player->skull == SKULL_BLACK) { skull = SKULL_BLACK; } query << "`skull` = " << skull << ", "; } query << "`lastlogout` = " << player->getLastLogout() << ", "; query << "`balance` = " << player->bankBalance << ", "; query << "`offlinetraining_time` = " << player->getOfflineTrainingTime() / 1000 << ", "; query << "`offlinetraining_skill` = " << player->getOfflineTrainingSkill() << ", "; query << "`stamina` = " << player->getStaminaMinutes() << ", "; if (!player->isOffline()) { query << "`onlinetime` = `onlinetime` + " << (time(NULL) - player->lastLoginSaved) << ", "; } query << "`blessings` = " << player->blessings; query << " WHERE `id` = " << player->getGUID(); DBTransaction transaction; if (!transaction.begin()) { return false; } if (!db->executeQuery(query.str())) { return false; } // skills for (int32_t i = SKILL_FIRST; i <= SKILL_LAST; i++) { query.str(""); query << "UPDATE `player_skills` SET `value` = " << player->skills[i][SKILL_LEVEL] << ", `count` = " << player->skills[i][SKILL_TRIES] << " WHERE `player_id` = " << player->getGUID() << " AND `skillid` = " << i; if (!db->executeQuery(query.str())) { return false; } } // learned spells query.str(""); query << "DELETE FROM `player_spells` WHERE `player_id` = " << player->getGUID(); if (!db->executeQuery(query.str())) { return false; } query.str(""); DBInsert stmt(db); stmt.setQuery("INSERT INTO `player_spells` (`player_id`, `name` ) VALUES "); for (LearnedInstantSpellList::const_iterator it = player->learnedInstantSpellList.begin(); it != player->learnedInstantSpellList.end(); ++it) { query << player->getGUID() << "," << db->escapeString(*it); if (!stmt.addRow(query)) { return false; } } if (!stmt.execute()) { return false; } //item saving query << "DELETE FROM `player_items` WHERE `player_id` = " << player->getGUID(); if (!db->executeQuery(query.str())) { return false; } stmt.setQuery("INSERT INTO `player_items` (`player_id`, `pid`, `sid`, `itemtype`, `count`, `attributes`) VALUES "); ItemBlockList itemList; for (int32_t slotId = 1; slotId <= 10; ++slotId) { Item* item = player->inventory[slotId]; if (item) { itemList.push_back(itemBlock(slotId, item)); } } if (!saveItems(player, itemList, stmt)) { return false; } if (player->depotChange) { //save depot items query.str(""); query << "DELETE FROM `player_depotitems` WHERE `player_id` = " << player->getGUID(); if (!db->executeQuery(query.str())) { return false; } stmt.setQuery("INSERT INTO `player_depotitems` (`player_id`, `pid`, `sid`, `itemtype`, `count`, `attributes`) VALUES "); itemList.clear(); for (DepotMap::iterator it = player->depotChests.begin(); it != player->depotChests.end() ; ++it) { DepotChest* depotChest = it->second; for (ItemDeque::const_iterator iit = depotChest->getItems(), end = depotChest->getEnd(); iit != end; ++iit) { itemList.push_back(itemBlock(it->first, *iit)); } } if (!saveItems(player, itemList, stmt)) { return false; } } //save inbox items query.str(""); query << "DELETE FROM `player_inboxitems` WHERE `player_id` = " << player->getGUID(); if (!db->executeQuery(query.str())) { return false; } stmt.setQuery("INSERT INTO `player_inboxitems` (`player_id`, `pid`, `sid`, `itemtype`, `count`, `attributes`) VALUES "); itemList.clear(); for (ItemDeque::const_iterator it = player->getInbox()->getItems(), end = player->getInbox()->getEnd(); it != end; ++it) { itemList.push_back(itemBlock(0, *it)); } if (!saveItems(player, itemList, stmt)) { return false; } query.str(""); query << "DELETE FROM `player_storage` WHERE `player_id` = " << player->getGUID(); if (!db->executeQuery(query.str())) { return false; } query.str(""); stmt.setQuery("INSERT INTO `player_storage` (`player_id`, `key`, `value`) VALUES "); player->genReservedStorageRange(); for (StorageMap::const_iterator cit = player->getStorageIteratorBegin(), end = player->getStorageIteratorEnd(); cit != end; ++cit) { query << player->getGUID() << "," << cit->first << "," << cit->second; if (!stmt.addRow(query)) { return false; } } if (!stmt.execute()) { return false; } //End the transaction return transaction.commit(); }
bool IOPlayer::savePlayer(Player* player, bool shallow) { player->preSave(); DatabaseDriver* db = DatabaseDriver::instance(); DBQuery query; DBResult_ptr result; //check if the player has to be saved or not query << "SELECT `save` FROM `players` WHERE `id` = " << player->getGUID(); if(!(result = db->storeQuery(query))){ return false; } const uint32_t save = result->getDataInt("save"); if(save == 0) return true; //serialize conditions PropWriteStream propWriteStream; for(ConditionList::const_iterator it = player->conditions.begin(); it != player->conditions.end(); ++it){ if((*it)->isPersistent()){ if(!(*it)->serialize(propWriteStream)){ return false; } propWriteStream.ADD_UCHAR(*CONDITIONATTR_END); } } uint32_t conditionsSize; const char* conditions = propWriteStream.getStream(conditionsSize); //First, an UPDATE query to write the player itself query.reset(); query << "UPDATE `players` SET `level` = " << player->level << ", `vocation` = " << (int32_t)player->getVocationId() << ", `health` = " << player->health << ", `healthmax` = " << player->healthMax << ", `direction` = " << player->getDirection().value() << ", `experience` = " << player->experience << ", `lookbody` = " << (int32_t)player->defaultOutfit.lookBody << ", `lookfeet` = " << (int32_t)player->defaultOutfit.lookFeet << ", `lookhead` = " << (int32_t)player->defaultOutfit.lookHead << ", `looklegs` = " << (int32_t)player->defaultOutfit.lookLegs << ", `looktype` = " << (int32_t)player->defaultOutfit.lookType << ", `lookaddons` = " << (int32_t)player->defaultOutfit.lookAddons << ", `maglevel` = " << player->magLevel << ", `mana` = " << player->mana << ", `manamax` = " << player->manaMax << ", `manaspent` = " << player->manaSpent << ", `soul` = " << player->soul << ", `town_id` = " << player->town << ", `posx` = " << player->getLoginPosition().x << ", `posy` = " << player->getLoginPosition().y << ", `posz` = " << player->getLoginPosition().z << ", `cap` = " << player->getCapacity() << ", `sex` = " << player->sex.value() << ", `conditions` = " << db->escapeBlob(conditions, conditionsSize) << ", `loss_experience` = " << (int32_t)player->getLossPercent(LOSS_EXPERIENCE) << ", `loss_mana` = " << (int32_t)player->getLossPercent(LOSS_MANASPENT) << ", `loss_skills` = " << (int32_t)player->getLossPercent(LOSS_SKILLTRIES) << ", `loss_items` = " << (int32_t)player->getLossPercent(LOSS_ITEMS) << ", `loss_containers` = " << (int32_t)player->getLossPercent(LOSS_CONTAINERS) << ", `stamina` = " << player->stamina; #ifdef __SKULLSYSTEM__ query << ", `skull_type` = " << (player->getSkull() == SKULL_RED || player->getSkull() == SKULL_BLACK ? player->getSkull().value() : 0); query << ", `skull_time` = " << player->lastSkullTime; #endif query << " WHERE `id` = " << player->getGUID(); DBTransaction transaction(db); if(!transaction.begin()) return false; if(!db->executeQuery(query)){ return false; } //skills for(int32_t i = 0; i <= 6; i++){ query.reset(); query << "UPDATE `player_skills` SET `value` = " << player->skills[i][SKILL_LEVEL] << ", `count` = " << player->skills[i][SKILL_TRIES] << " WHERE `player_id` = " << player->getGUID() << " AND `skill_id` = " << i; if(!db->executeQuery(query)){ return false; } } if(shallow) return transaction.commit(); // deletes all player-related stuff /* query << "DELETE FROM `player_items` WHERE `player_id` = " << player->getGUID(); if(!db->executeQuery(query.str())){ return false; } query.str(""); query << "DELETE FROM `player_depotitems` WHERE `player_id` = " << player->getGUID(); if(!db->executeQuery(query.str())){ return false; } query.str(""); */ query.reset(); query << "DELETE FROM `player_storage` WHERE `player_id` = " << player->getGUID(); if(!db->executeQuery(query)){ return false; } query.reset(); query << "DELETE FROM `player_viplist` WHERE `player_id` = " << player->getGUID(); if(!db->executeQuery(query.str())){ return false; } // Starti inserting DBInsert insert(db); /* ItemBlockList itemList; Item* item; for(int32_t slotId = 1; slotId <= 10; ++slotId){ if((item = player->inventory[slotId])){ itemList.push_back(itemBlock(slotId, item)); } } //item saving stmt.setQuery("INSERT INTO `player_items` (`player_id` , `pid` , `sid` , `itemtype` , `count` , `attributes` ) VALUES "); if(!(saveItems(player, itemList, stmt) && stmt.execute())){ return false; } itemList.clear(); for(DepotMap::iterator it = player->depots.begin(); it != player->depots.end(); ++it){ itemList.push_back(itemBlock(it->first, it->second)); } //save depot items stmt.setQuery("INSERT INTO `player_depotitems` (`player_id` , `pid` , `sid` , `itemtype` , `count` , `attributes` ) VALUES "); if(!(saveItems(player, itemList, stmt) && stmt.execute())){ return false; } */ insert.setQuery("INSERT INTO `player_storage` (`player_id` , `id` , `value` ) VALUES "); for(StorageMap::const_iterator cit = player->getCustomValueIteratorBegin(); cit != player->getCustomValueIteratorEnd();cit++){ query.reset(); query << player->getGUID() << ", " << db->escapeString(cit->first) << ", " << db->escapeString(cit->second); if(!insert.addRow(query.str())){ return false; } } if(!insert.execute()){ return false; } //save vip list if(!player->VIPList.empty()){ query.reset(); query << "INSERT INTO `player_viplist` (`player_id`, `vip_id`) SELECT " << player->getGUID() << ", `id` FROM `players` WHERE `id` IN ("; for(VIPListSet::iterator it = player->VIPList.begin(); it != player->VIPList.end(); ){ query << (*it); ++it; if(it != player->VIPList.end()){ query << ","; } else{ query << ")"; } } if(!db->executeQuery(query)){ return false; } } //End the transaction return transaction.commit(); }