bool IOLoginData::loadPlayer(Player* player, const std::string& name, bool preLoad /*= false*/)
{
Database* db = Database::getInstance();
DBQuery query;
query << "SELECT `id`, `account_id`, `group_id`, `world_id`, `sex`, `vocation`, `experience`, `level`, "
<< "`maglevel`, `health`, `healthmax`, `blessings`, `mana`, `manamax`, `manaspent`, `soul`, `lookbody`, "
<< "`lookfeet`, `lookhead`, `looklegs`, `looktype`, `lookaddons`, `posx`, `posy`, `posz`, `cap`, "
<< "`lastlogin`, `lastlogout`, `lastip`, `conditions`, `skull`, `skulltime`, `guildnick`, `rank_id`, "
<< "`town_id`, `balance`, `stamina`, `direction`, `loss_experience`, `loss_mana`, `loss_skills`, "
<< "`loss_containers`, `loss_items`, `marriage`, `promotion`, `description`, `special_rate_loot` FROM `players` WHERE "
<< "`name` " << db->getStringComparer() << db->escapeString(name) << " AND `world_id` = "
<< g_config.getNumber(ConfigManager::WORLD_ID) << " AND `deleted` = 0 LIMIT 1";
DBResult* result;
if(!(result = db->storeQuery(query.str())))
return false;
uint32_t accountId = result->getDataInt("account_id");
if(accountId < 1)
{
result->free();
return false;
}
Account account = loadAccount(accountId, true);
player->account = account.name;
player->accountId = accountId;
Group* group = Groups::getInstance()->getGroup(result->getDataInt("group_id"));
player->setGroup(group);
player->setGUID(result->getDataInt("id"));
player->premiumDays = account.premiumDays;
nameCacheMap[player->getGUID()] = name;
guidCacheMap[name] = player->getGUID();
if(preLoad)
{
//only loading basic info
result->free();
return true;
}
player->setHasSpecialLootRate(result->getDataInt("special_rate_loot"));
player->nameDescription += result->getDataString("description");
player->setSex(result->getDataInt("sex"));
if(g_config.getBool(ConfigManager::STORE_DIRECTION))
player->setDirection((Direction)result->getDataInt("direction"));
player->level = std::max((uint32_t)1, (uint32_t)result->getDataInt("level"));
uint64_t currExpCount = Player::getExpForLevel(player->level), nextExpCount = Player::getExpForLevel(
player->level + 1), experience = (uint64_t)result->getDataLong("experience");
if(experience < currExpCount || experience > nextExpCount)
experience = currExpCount;
player->experience = experience;
player->levelPercent = 0;
if(currExpCount < nextExpCount)
player->levelPercent = Player::getPercentLevel(player->experience - currExpCount, nextExpCount - currExpCount);
player->soul = result->getDataInt("soul");
player->capacity = result->getDataInt("cap");
player->setStamina(result->getDataLong("stamina"));
player->marriage = result->getDataInt("marriage");
player->balance = result->getDataLong("balance");
if(g_config.getBool(ConfigManager::BLESSINGS) && (player->isPremium()
|| !g_config.getBool(ConfigManager::BLESSING_ONLY_PREMIUM)))
player->blessings = result->getDataInt("blessings");
uint64_t conditionsSize = 0;
const char* conditions = result->getDataStream("conditions", conditionsSize);
PropStream propStream;
propStream.init(conditions, conditionsSize);
Condition* condition;
while((condition = Condition::createCondition(propStream)))
{
if(condition->unserialize(propStream))
player->storedConditionList.push_back(condition);
else
delete condition;
}
player->vocationId = result->getDataInt("vocation");
player->setPromotionLevel(result->getDataInt("promotion"));
player->health = result->getDataInt("health");
player->healthMax = result->getDataInt("healthmax");
player->mana = result->getDataInt("mana");
player->manaMax = result->getDataInt("manamax");
player->magLevel = result->getDataInt("maglevel");
uint64_t nextManaCount = player->vocation->getReqMana(player->magLevel + 1), manaSpent = result->getDataLong("manaspent");
if(manaSpent > nextManaCount)
manaSpent = 0;
player->manaSpent = manaSpent;
player->magLevelPercent = Player::getPercentLevel(player->manaSpent, nextManaCount);
if(!group || !group->getOutfit())
{
player->defaultOutfit.lookType = result->getDataInt("looktype");
uint32_t outfitId = Outfits::getInstance()->getOutfitId(player->defaultOutfit.lookType);
bool wearable = true;
if(outfitId > 0)
{
Outfit outfit;
wearable = Outfits::getInstance()->getOutfit(outfitId, player->getSex(true), outfit);
if(wearable && player->defaultOutfit.lookType != outfit.lookType)
player->defaultOutfit.lookType = outfit.lookType;
}
if(!wearable) //just pick the first default outfit we can find
{
const OutfitMap& defaultOutfits = Outfits::getInstance()->getOutfits(player->getSex(true));
if(!defaultOutfits.empty())
{
Outfit newOutfit = (*defaultOutfits.begin()).second;
player->defaultOutfit.lookType = newOutfit.lookType;
}
}
}
else
player->defaultOutfit.lookType = group->getOutfit();
player->defaultOutfit.lookHead = result->getDataInt("lookhead");
player->defaultOutfit.lookBody = result->getDataInt("lookbody");
player->defaultOutfit.lookLegs = result->getDataInt("looklegs");
player->defaultOutfit.lookFeet = result->getDataInt("lookfeet");
player->defaultOutfit.lookAddons = result->getDataInt("lookaddons");
player->currentOutfit = player->defaultOutfit;
Skulls_t skull = SKULL_RED;
if(g_config.getBool(ConfigManager::USE_BLACK_SKULL))
skull = (Skulls_t)result->getDataInt("skull");
player->setSkullEnd((time_t)result->getDataInt("skulltime"), true, skull);
player->town = result->getDataInt("town_id");
if(Town* town = Towns::getInstance()->getTown(player->town))
player->setMasterPosition(town->getPosition());
player->setLossPercent(LOSS_EXPERIENCE, result->getDataInt("loss_experience"));
player->setLossPercent(LOSS_MANA, result->getDataInt("loss_mana"));
player->setLossPercent(LOSS_SKILLS, result->getDataInt("loss_skills"));
player->setLossPercent(LOSS_CONTAINERS, result->getDataInt("loss_containers"));
player->setLossPercent(LOSS_ITEMS, result->getDataInt("loss_items"));
player->loginPosition = Position(result->getDataInt("posx"), result->getDataInt("posy"), result->getDataInt("posz"));
player->lastLogin = result->getDataLong("lastlogin");
player->lastLogout = result->getDataLong("lastlogout");
player->lastIP = result->getDataInt("lastip");
if(!player->loginPosition.x || !player->loginPosition.y)
player->loginPosition = player->getMasterPosition();
const uint32_t rankId = result->getDataInt("rank_id");
const std::string nick = result->getDataString("guildnick");
result->free();
if(rankId > 0)
{
query.str("");
query << "SELECT `guild_ranks`.`name` AS `rank`, `guild_ranks`.`guild_id` AS `guildid`, `guild_ranks`.`level` AS `level`, `guilds`.`name` AS `guildname` FROM `guild_ranks`, `guilds` WHERE `guild_ranks`.`id` = " << rankId << " AND `guild_ranks`.`guild_id` = `guilds`.`id` LIMIT 1";
if((result = db->storeQuery(query.str())))
{
player->guildId = result->getDataInt("guildid");
player->guildName = result->getDataString("guildname");
player->guildLevel = (GuildLevel_t)result->getDataInt("level");
player->rankId = rankId;
player->rankName = result->getDataString("rank");
player->guildNick = nick;
result->free();
query.str("");
query << "SELECT `id`, `guild_id`, `enemy_id` FROM `guild_wars` WHERE (`guild_id` = "
<< player->guildId << " OR `enemy_id` = " << player->guildId << ") AND `status` IN (1,4)";
if((result = db->storeQuery(query.str())))
{
War_t war;
do
{
uint32_t guild = result->getDataInt("guild_id");
if(player->guildId == guild)
{
war.type = WAR_ENEMY;
war.war = result->getDataInt("id");
player->addEnemy(result->getDataInt("enemy_id"), war);
}
else
{
war.type = WAR_GUILD;
war.war = result->getDataInt("id");
player->addEnemy(guild, war);
}
}
while(result->next());
result->free();
}
}
}
else if(g_config.getBool(ConfigManager::INGAME_GUILD_MANAGEMENT))
{
query.str("");
query << "SELECT `guild_id` FROM `guild_invites` WHERE `player_id` = " << player->getGUID();
if((result = db->storeQuery(query.str())))
{
do
player->invitedToGuildsList.push_back((uint32_t)result->getDataInt("guild_id"));
while(result->next());
result->free();
}
}
query.str("");
query << "SELECT `password` FROM `accounts` WHERE `id` = " << accountId << " LIMIT 1";
if(!(result = db->storeQuery(query.str())))
return false;
player->password = result->getDataString("password");
result->free();
// we need to find out our skills
// so we query the skill table
query.str("");
query << "SELECT `skillid`, `value`, `count` FROM `player_skills` WHERE `player_id` = " << player->getGUID();
if((result = db->storeQuery(query.str())))
{
//now iterate over the skills
do
{
int16_t skillId = result->getDataInt("skillid");
if(skillId < SKILL_FIRST || skillId > SKILL_LAST)
continue;
uint32_t skillLevel = result->getDataInt("value");
uint64_t nextSkillCount = player->vocation->getReqSkillTries(
skillId, skillLevel + 1), skillCount = result->getDataLong("count");
if(skillCount > nextSkillCount)
skillCount = 0;
player->skills[skillId][SKILL_LEVEL] = skillLevel;
player->skills[skillId][SKILL_TRIES] = skillCount;
player->skills[skillId][SKILL_PERCENT] = Player::getPercentLevel(skillCount, nextSkillCount);
}
while(result->next());
result->free();
}
query.str("");
query << "SELECT `player_id`, `name` FROM `player_spells` WHERE `player_id` = " << player->getGUID();
if((result = db->storeQuery(query.str())))
{
do
player->learnedInstantSpellList.push_back(result->getDataString("name"));
while(result->next());
result->free();
}
ItemMap itemMap;
ItemMap::iterator it;
//load inventory items
query.str("");
query << "SELECT `pid`, `sid`, `itemtype`, `count`, `attributes` FROM `player_items` WHERE `player_id` = " << player->getGUID() << " ORDER BY `sid` DESC";
if((result = db->storeQuery(query.str())))
{
loadItems(itemMap, result);
for(ItemMap::reverse_iterator rit = itemMap.rbegin(); rit != itemMap.rend(); ++rit)
{
Item* item = rit->second.first;
int32_t pid = rit->second.second;
if(pid > 0 && pid < 11)
player->__internalAddThing(pid, item);
else
{
it = itemMap.find(pid);
if(it != itemMap.end())
{
if(Container* container = it->second.first->getContainer())
container->__internalAddThing(item);
}
}
}
result->free();
itemMap.clear();
}
//load depot items
query.str("");
query << "SELECT `pid`, `sid`, `itemtype`, `count`, `attributes` FROM `player_depotitems` WHERE `player_id` = " << player->getGUID() << " ORDER BY `sid` DESC";
if((result = db->storeQuery(query.str())))
{
loadItems(itemMap, result);
for(ItemMap::reverse_iterator rit = itemMap.rbegin(); rit != itemMap.rend(); ++rit)
{
Item* item = rit->second.first;
int32_t pid = rit->second.second;
if(pid >= 0 && pid < 100)
{
if(Container* c = item->getContainer())
{
if(Depot* depot = c->getDepot())
player->addDepot(depot, pid);
else
std::clog << "[Error - IOLoginData::loadPlayer] Cannot load depot " << pid << " for player " << name << std::endl;
}
else
std::clog << "[Error - IOLoginData::loadPlayer] Cannot load depot " << pid << " for player " << name << std::endl;
}
else
{
it = itemMap.find(pid);
if(it != itemMap.end())
{
if(Container* container = it->second.first->getContainer())
container->__internalAddThing(item);
}
}
}
result->free();
itemMap.clear();
}
//load storage map
query.str("");
query << "SELECT `key`, `value` FROM `player_storage` WHERE `player_id` = " << player->getGUID();
if((result = db->storeQuery(query.str())))
{
do
player->setStorage((uint32_t)result->getDataInt("key"), result->getDataString("value"));
while(result->next());
result->free();
}
//load vip
query.str("");
if(!g_config.getBool(ConfigManager::VIPLIST_PER_PLAYER))
query << "SELECT `player_id` AS `vip` FROM `account_viplist` WHERE `account_id` = " << account.number << " AND `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID);
else
query << "SELECT `vip_id` AS `vip` FROM `player_viplist` WHERE `player_id` = " << player->getGUID();
if((result = db->storeQuery(query.str())))
{
std::string dummy;
do
{
uint32_t vid = result->getDataInt("vip");
if(storeNameByGuid(vid))
player->addVIP(vid, dummy, false, true);
}
while(result->next());
result->free();
}
player->updateInventoryWeight();
player->updateItemsLight(true);
player->updateBaseSpeed();
return true;
}