bool IOPlayer::getGuidByName(uint32_t &guid, std::string& name) { GuidCacheMap::iterator it = guidCacheMap.find(name); if(it != guidCacheMap.end()){ name = it->first; guid = it->second; return true; } DatabaseDriver* db = DatabaseDriver::instance(); DBResult_ptr result; DBQuery query; query << "SELECT `name`, `id` " "FROM `players` " "WHERE `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << " AND `name` = " << db->escapeString(name); if(!(result = db->storeQuery(query))) return false; name = result->getDataString("name"); guid = result->getDataInt("id"); guidCacheMap[name] = guid; return true; }
bool IOMapSerialize::loadHouseInfo() { Database& db = Database::getInstance(); DBResult_ptr result = db.storeQuery("SELECT `id`, `owner`, `paid`, `warnings` FROM `houses`"); if (!result) { return false; } do { House* house = g_game.map.houses.getHouse(result->getNumber<uint32_t>("id")); if (house) { house->setOwner(result->getNumber<uint32_t>("owner"), false); house->setPaidUntil(result->getNumber<time_t>("paid")); house->setPayRentWarnings(result->getNumber<uint32_t>("warnings")); } } while (result->next()); result = db.storeQuery("SELECT `house_id`, `listid`, `list` FROM `house_lists`"); if (result) { do { House* house = g_game.map.houses.getHouse(result->getNumber<uint32_t>("house_id")); if (house) { house->setAccessList(result->getNumber<uint32_t>("listid"), result->getString("list")); } } while (result->next()); } return true; }
HistoryMarketOfferList IOMarket::getOwnHistory(MarketAction_t action, uint32_t playerId) { HistoryMarketOfferList offerList; std::ostringstream query; query << "SELECT `itemtype`, `amount`, `price`, `expires_at`, `state` FROM `market_history` WHERE `player_id` = " << playerId << " AND `sale` = " << action; DBResult_ptr result = Database::getInstance().storeQuery(query.str()); if (!result) { return offerList; } do { HistoryMarketOffer offer; offer.itemId = result->getNumber<uint16_t>("itemtype"); offer.amount = result->getNumber<uint16_t>("amount"); offer.price = result->getNumber<uint32_t>("price"); offer.timestamp = result->getNumber<uint32_t>("expires_at"); MarketOfferState_t offerState = static_cast<MarketOfferState_t>(result->getNumber<uint16_t>("state")); if (offerState == OFFERSTATE_ACCEPTEDEX) { offerState = OFFERSTATE_ACCEPTED; } offer.state = offerState; offerList.push_back(offer); } while (result->next()); return offerList; }
MarketOfferEx IOMarket::getOfferByCounter(uint32_t timestamp, uint16_t counter) { MarketOfferEx offer; const int32_t created = timestamp - g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION); std::ostringstream query; query << "SELECT `id`, `sale`, `itemtype`, `amount`, `created`, `price`, `player_id`, `anonymous`, (SELECT `name` FROM `players` WHERE `id` = `player_id`) AS `player_name` FROM `market_offers` WHERE `created` = " << created << " AND (`id` & 65535) = " << counter << " LIMIT 1"; DBResult_ptr result = Database::getInstance().storeQuery(query.str()); if (!result) { offer.id = 0; return offer; } offer.id = result->getNumber<uint32_t>("id"); offer.type = static_cast<MarketAction_t>(result->getNumber<uint16_t>("sale")); offer.amount = result->getNumber<uint16_t>("amount"); offer.counter = result->getNumber<uint32_t>("id") & 0xFFFF; offer.timestamp = result->getNumber<uint32_t>("created"); offer.price = result->getNumber<uint32_t>("price"); offer.itemId = result->getNumber<uint16_t>("itemtype"); offer.playerId = result->getNumber<uint32_t>("player_id"); if (result->getNumber<uint16_t>("anonymous") == 0) { offer.playerName = result->getString("player_name"); } else { offer.playerName = "Anonymous"; } return offer; }
bool IOBan::isAccountBanned(uint32_t accountId, BanInfo& banInfo) { Database& db = Database::getInstance(); std::ostringstream query; query << "SELECT `reason`, `expires_at`, `banned_at`, `banned_by`, (SELECT `name` FROM `players` WHERE `id` = `banned_by`) AS `name` FROM `account_bans` WHERE `account_id` = " << accountId; DBResult_ptr result = db.storeQuery(query.str()); if (!result) { return false; } int64_t expiresAt = result->getNumber<int64_t>("expires_at"); if (expiresAt != 0 && time(nullptr) > expiresAt) { // Move the ban to history if it has expired query.str(std::string()); query << "INSERT INTO `account_ban_history` (`account_id`, `reason`, `banned_at`, `expired_at`, `banned_by`) VALUES (" << accountId << ',' << db.escapeString(result->getString("reason")) << ',' << result->getNumber<time_t>("banned_at") << ',' << expiresAt << ',' << result->getNumber<uint32_t>("banned_by") << ')'; g_databaseTasks.addTask(query.str()); query.str(std::string()); query << "DELETE FROM `account_bans` WHERE `account_id` = " << accountId; g_databaseTasks.addTask(query.str()); return false; } banInfo.expiresAt = expiresAt; banInfo.reason = result->getString("reason"); banInfo.bannedBy = result->getString("name"); return true; }
bool IOLoginData::getGuidByNameEx(uint32_t& guid, bool& specialVip, std::string& name) { Database* db = Database::getInstance(); std::ostringstream query; query << "SELECT `name`, `id`, `group_id`, `account_id` FROM `players` WHERE `name` = " << db->escapeString(name); DBResult_ptr result = db->storeQuery(query.str()); if (!result) { return false; } name = result->getDataString("name"); guid = result->getDataInt("id"); Group* group = g_game.getGroup(result->getDataInt("group_id")); uint64_t flags; if (group) { flags = group->flags; } else { flags = 0; } specialVip = (flags & PlayerFlag_SpecialVIP) != 0; return true; }
bool IOLoginData::loginserverAuthentication(const std::string& name, const std::string& password, Account& account) { Database* db = Database::getInstance(); std::ostringstream query; query << "SELECT `id`, `name`, `password`, `type`, `premdays`, `lastday` FROM `accounts` WHERE `name` = " << db->escapeString(name); DBResult_ptr result = db->storeQuery(query.str()); if (!result) { return false; } if (transformToSHA1(password) != result->getDataString("password")) { return false; } account.id = result->getDataInt("id"); account.name = result->getDataString("name"); account.accountType = static_cast<AccountType_t>(result->getDataInt("type")); account.premiumDays = result->getDataInt("premdays"); account.lastDay = result->getDataInt("lastday"); query.str(""); query << "SELECT `name`, `deletion` FROM `players` WHERE `account_id` = " << account.id; result = db->storeQuery(query.str()); if (result) { do { if (result->getDataInt("deletion") == 0) { account.charList.push_back(result->getDataString("name")); } } while (result->next()); account.charList.sort(); } return true; }
MarketOfferList IOMarket::getActiveOffers(MarketAction_t action, uint16_t itemId) { MarketOfferList offerList; std::ostringstream query; query << "SELECT `id`, `amount`, `price`, `created`, `anonymous`, (SELECT `name` FROM `players` WHERE `id` = `player_id`) AS `player_name` FROM `market_offers` WHERE `sale` = " << action << " AND `itemtype` = " << itemId; DBResult_ptr result = Database::getInstance().storeQuery(query.str()); if (!result) { return offerList; } const int32_t marketOfferDuration = g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION); do { MarketOffer offer; offer.amount = result->getNumber<uint16_t>("amount"); offer.price = result->getNumber<uint32_t>("price"); offer.timestamp = result->getNumber<uint32_t>("created") + marketOfferDuration; offer.counter = result->getNumber<uint32_t>("id") & 0xFFFF; if (result->getNumber<uint16_t>("anonymous") == 0) { offer.playerName = result->getString("player_name"); } else { offer.playerName = "Anonymous"; } offerList.push_back(offer); } while (result->next()); return offerList; }
void IOLoginData::loadItems(ItemMap& itemMap, DBResult_ptr result) { do { int32_t sid = result->getDataInt("sid"); int32_t pid = result->getDataInt("pid"); int32_t type = result->getDataInt("itemtype"); int32_t count = result->getDataInt("count"); unsigned long attrSize; const char* attr = result->getDataStream("attributes", attrSize); PropStream propStream; propStream.init(attr, attrSize); Item* item = Item::CreateItem(type, count); if (item) { if (!item->unserializeAttr(propStream)) { std::cout << "WARNING: Serialize error in IOLoginData::loadItems" << std::endl; } std::pair<Item*, int32_t> pair(item, pid); itemMap[sid] = pair; } } while (result->next()); }
MarketOfferList IOMarket::getOwnOffers(MarketAction_t action, uint32_t playerId) { MarketOfferList offerList; const int32_t marketOfferDuration = g_config.getNumber(ConfigManager::MARKET_OFFER_DURATION); std::ostringstream query; query << "SELECT `id`, `amount`, `price`, `created`, `itemtype` FROM `market_offers` WHERE `player_id` = " << playerId << " AND `sale` = " << action; DBResult_ptr result = Database::getInstance().storeQuery(query.str()); if (!result) { return offerList; } do { MarketOffer offer; offer.amount = result->getNumber<uint16_t>("amount"); offer.price = result->getNumber<uint32_t>("price"); offer.timestamp = result->getNumber<uint32_t>("created") + marketOfferDuration; offer.counter = result->getNumber<uint32_t>("id") & 0xFFFF; offer.itemId = result->getNumber<uint16_t>("itemtype"); offerList.push_back(offer); } while (result->next()); return offerList; }
uint32_t IOLoginData::gameworldAuthentication(const std::string& accountName, const std::string& password, std::string& characterName) { Database* db = Database::getInstance(); std::ostringstream query; query << "SELECT `id`, `password` FROM `accounts` WHERE `name` = " << db->escapeString(accountName); DBResult_ptr result = db->storeQuery(query.str()); if (!result) { return 0; } if(password != result->getString("password")){ if (transformToSHA1(password) != result->getString("password")) { return 0; } } uint32_t accountId = result->getNumber<uint32_t>("id"); query.str(std::string()); query << "SELECT `account_id`, `name`, `deletion` FROM `players` WHERE `name` = " << db->escapeString(characterName); result = db->storeQuery(query.str()); if (!result) { return 0; } if (result->getNumber<uint32_t>("account_id") != accountId || result->getNumber<uint64_t>("deletion") != 0) { return 0; } characterName = result->getString("name"); return accountId; }
bool IOBan::isIpBanned(uint32_t clientip, BanInfo& banInfo) { if (clientip == 0) { return false; } Database& db = Database::getInstance(); std::ostringstream query; query << "SELECT `reason`, `expires_at`, (SELECT `name` FROM `players` WHERE `id` = `banned_by`) AS `name` FROM `ip_bans` WHERE `ip` = " << clientip; DBResult_ptr result = db.storeQuery(query.str()); if (!result) { return false; } int64_t expiresAt = result->getNumber<int64_t>("expires_at"); if (expiresAt != 0 && time(nullptr) > expiresAt) { query.str(std::string()); query << "DELETE FROM `ip_bans` WHERE `ip` = " << clientip; g_databaseTasks.addTask(query.str()); return false; } banInfo.expiresAt = expiresAt; banInfo.reason = result->getString("reason"); banInfo.bannedBy = result->getString("name"); return true; }
AccountType_t IOLoginData::getAccountType(uint32_t accountId) { std::ostringstream query; query << "SELECT `type` FROM `accounts` WHERE `id` = " << accountId; DBResult_ptr result = Database::getInstance()->storeQuery(query.str()); if (!result) { return ACCOUNT_TYPE_NORMAL; } return static_cast<AccountType_t>(result->getDataInt("type")); }
std::string IOLoginData::getNameByGuid(uint32_t guid) { std::ostringstream query; query << "SELECT `name` FROM `players` WHERE `id` = " << guid; DBResult_ptr result = Database::getInstance()->storeQuery(query.str()); if (!result) { return std::string(); } return result->getDataString("name"); }
int LuaBinder::luaResultNext(lua_State* L) { DBResult_ptr res = LuaBinder::getDBResult(L); if (!res) { pushBoolean(L, false); return 1; } pushBoolean(L, res->next()); return 1; }
int LuaBinder::luaResultGetString(lua_State* L) { DBResult_ptr res = LuaBinder::getDBResult(L); if (!res) { lua_pushnil(L); return 1; } String rowName = LuaBinder::getString(L, 2); LuaBinder::pushString(L, res->getString(rowName)); return 1; }
int LuaBinder::luaResultGetStream(lua_State* L) { DBResult_ptr res = LuaBinder::getDBResult(L); if (!res) { pushBoolean(L, false); return 1; } unsigned long length; const char* stream = res->getStream(getString(L, 2), length); lua_pushlstring(L, stream, length); lua_pushnumber(L, length); return 2; }
void Store::getTransactionHistory(uint32_t accountId, uint16_t page, uint32_t entriesPerPage, std::vector<StoreTransaction>& out) const { Database* db = Database::getInstance(); std::ostringstream query; query << "SELECT `id`, `coins`, `description`, `timestamp` FROM `store_history` WHERE `account_id` = " << accountId << " ORDER BY `timestamp` DESC LIMIT " << entriesPerPage + 1 << " OFFSET " << (page * entriesPerPage); DBResult_ptr result = db->storeQuery(query.str()); if (result) { do { out.emplace_back(result->getNumber<uint32_t>("id"), result->getNumber<int32_t>("coins"), result->getString("description"), result->getNumber<time_t>("timestamp")); } while (result->next()); } }
bool IOLoginData::formatPlayerName(std::string& name) { Database* db = Database::getInstance(); std::ostringstream query; query << "SELECT `name` FROM `players` WHERE `name` = " << db->escapeString(name); DBResult_ptr result = db->storeQuery(query.str()); if (!result) { return false; } name = result->getDataString("name"); return true; }
bool IOGuild::getGuildIdByName(uint32_t& guildId, const std::string& guildName) { Database* db = Database::getInstance(); std::ostringstream query; query << "SELECT `id` FROM `guilds` WHERE `name` = " << db->escapeString(guildName); DBResult_ptr result = db->storeQuery(query.str()); if (!result) { return false; } guildId = result->getDataInt("id"); return true; }
bool IOLoginData::getGuidByName(uint32_t& guid, std::string& name) { Database* db = Database::getInstance(); std::ostringstream query; query << "SELECT `id`, `name` FROM `players` WHERE `name` = " << db->escapeString(name); DBResult_ptr result = db->storeQuery(query.str()); if (!result) { return false; } name = result->getDataString("name"); guid = result->getDataInt("id"); return true; }
bool IOPlayer::getAccountByName(std::string& account, const std::string& player_name) { DatabaseDriver* db = DatabaseDriver::instance(); DBResult_ptr result; DBQuery query; query << "SELECT `a`.`name` FROM `players` p LEFT JOIN `accounts` a ON `p`.`account_id` = `a`.`id` " "WHERE `p`.`world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << " AND `p`.`name` = " << db->escapeString(player_name); if(!(result = db->storeQuery(query))) return false; account = result->getDataString("name"); return true; }
bool IOPlayer::getDefaultTown(std::string& name, uint32_t& depotId) { DatabaseDriver* db = DatabaseDriver::instance(); DBResult_ptr result; DBQuery query; query << "SELECT `town_id` " "FROM `players` " "WHERE `players`.`world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << " AND `name`= " << db->escapeString(name); if(!(result = db->storeQuery(query))) return false; depotId = result->getDataInt("town_id"); return true; }
bool IOAccount::getPassword(const std::string& accountName, const std::string& playerName, std::string& password) { DatabaseDriver* db = DatabaseDriver::instance(); DBQuery query; DBResult_ptr result; query << "SELECT `accounts`.`password` AS `password` FROM `accounts`, `players` " << "WHERE `accounts`.`name` = " << db->escapeString(accountName) << " AND `accounts`.`id` = `players`.`account_id` AND `players`.`name` = " << db->escapeString(playerName); if((result = db->storeQuery(query.str()))){ password = result->getDataString("password"); return true; } return false; }
DBResult_ptr Database::verifyResult(DBResult_ptr result) { if (!result->next()) { return nullptr; } return result; }
bool IOPlayer::getLastIP(uint32_t& ip, uint32_t guid) { DatabaseDriver* db = DatabaseDriver::instance(); DBResult_ptr result; DBQuery query; query << "SELECT `lastip` " "FROM `players` " "WHERE `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << " AND `id` = " << guid << ";"; if(!(result = db->storeQuery(query))) return false; ip = result->getDataInt("lastip"); return true; }
bool IOPlayer::getAccountByName(uint32_t& account, const std::string& name) { DatabaseDriver* db = DatabaseDriver::instance(); DBResult_ptr result; DBQuery query; query << "SELECT `account_id` " "FROM `players` " "WHERE `world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << " AND `name` = " << db->escapeString(name); if(!(result = db->storeQuery(query))) return false; account = result->getDataInt("account_id"); return true; }
bool IOPlayer::getGuildIdByName(uint32_t& guildId, const std::string& guildName) { DatabaseDriver* db = DatabaseDriver::instance(); DBResult_ptr result; DBQuery query; query << "SELECT `guilds`.`id` " "FROM `guilds` " "LEFT JOIN `players` ON `players`.`id` = `guilds`.`owner_id` " "WHERE `players`.`world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << " AND `guilds`.`name` = " << db->escapeString(guildName); if(!(result = db->storeQuery(query))) return false; guildId = result->getDataInt("id"); return true; }
bool IOPlayer::getGuidByNameEx(uint32_t& guid, bool& specialVip, const std::string& player_name) { DatabaseDriver* db = DatabaseDriver::instance(); DBResult_ptr result; DBQuery query; query << "SELECT `players`.`name`, `players`.`id`, `groups`.`flags` AS `flags` " "FROM `players` LEFT JOIN `groups` ON `groups`.`id` = `players`.`group_id` " "WHERE `players`.`world_id` = " << g_config.getNumber(ConfigManager::WORLD_ID) << " AND `players`.`name`= " << db->escapeString(player_name); if(!(result = db->storeQuery(query.str()))) return false; guid = result->getDataInt("id"); specialVip = (result->getDataLong("flags") & (1ull << PlayerFlag_SpecialVIP)) != 0; return true; }
bool IOPlayer::storeNameByGuid(DatabaseDriver &db, uint32_t guid) { DBQuery query; DBResult_ptr result; NameCacheMap::iterator it = nameCacheMap.find(guid); if(it != nameCacheMap.end()) return true; query << "SELECT `name` FROM `players` WHERE `id` = " << guid; if(!(result = db.storeQuery(query))) return false; nameCacheMap[guid] = result->getDataString("name"); return true; }