bool IOMapSerialize::updateHouses()
{
	Database* db = Database::getInstance();
	DBQuery query;

	House* house = NULL;
	for(HouseMap::iterator it = Houses::getInstance()->getHouseBegin(); it != Houses::getInstance()->getHouseEnd(); ++it)
	{
		if(!(house = it->second))
			continue;

		query << "SELECT `price` FROM `houses` WHERE `id` = " << house->getId() << " AND `world_id` = "
			<< g_config.getNumber(ConfigManager::WORLD_ID) << " LIMIT 1";
		if(DBResult* result = db->storeQuery(query.str()))
		{
			if((uint32_t)result->getDataInt("price") != house->getPrice())
				house->setSyncFlag(House::HOUSE_SYNC_UPDATE);

			result->free();
			query.str("");

			query << "UPDATE `houses` SET ";
			if(house->hasSyncFlag(House::HOUSE_SYNC_NAME))
				query << "`name` = " << db->escapeString(house->getName()) << ", ";

			if(house->hasSyncFlag(House::HOUSE_SYNC_TOWN))
				query << "`town` = " << house->getTownId() << ", ";

			if(house->hasSyncFlag(House::HOUSE_SYNC_SIZE))
				query << "`size` = " << house->getSize() << ", ";

			if(house->hasSyncFlag(House::HOUSE_SYNC_PRICE))
				query << "`price` = " << house->getPrice() << ", ";

			if(house->hasSyncFlag(House::HOUSE_SYNC_RENT))
				query << "`rent` = " << house->getRent() << ", ";

			query << "`doors` = " << house->getDoorsCount() << ", `beds` = "
				<< house->getBedsCount() << ", `tiles` = " << house->getTilesCount();
			if(house->hasSyncFlag(House::HOUSE_SYNC_GUILD))
				query << ", `guild` = " << house->isGuild();

			query << " WHERE `id` = " << house->getId() << " AND `world_id` = "
				<< g_config.getNumber(ConfigManager::WORLD_ID) << db->getUpdateLimiter();
		}
		else
		{
			query.str("");
			query << "INSERT INTO `houses` (`id`, `world_id`, `owner`, `name`, `town`, `size`, `price`, `rent`, `doors`, `beds`, `tiles`, `guild`) VALUES ("
				<< house->getId() << ", " << g_config.getNumber(ConfigManager::WORLD_ID) << ", 0, "
				//we need owner for compatibility reasons (field doesn't have a default value)
				<< db->escapeString(house->getName()) << ", " << house->getTownId() << ", "
				<< house->getSize() << ", " << house->getPrice() << ", " << house->getRent() << ", "
				<< house->getDoorsCount() << ", " << house->getBedsCount() << ", "
				<< house->getTilesCount() << ", " << house->isGuild() << ")";
		}

		if(!db->query(query.str()))
			return false;

		query.str("");
	}

	return true;
}
Example #2
0
bool Houses::payHouses() const
{
	if (rentPeriod == RENTPERIOD_NEVER) {
		return true;
	}

	time_t currentTime = time(nullptr);
	for (const auto& it : houseMap) {
		House* house = it.second;
		if (house->getOwner() == 0) {
			continue;
		}

		const uint32_t rent = house->getRent();
		if (rent == 0 || house->getPaidUntil() > currentTime) {
			continue;
		}

		const uint32_t ownerid = house->getOwner();
		Town* town = Towns::getInstance().getTown(house->getTownId());
		if (!town) {
			continue;
		}

		Player player(nullptr);
		if (!IOLoginData::loadPlayerById(&player, ownerid)) {
			//player doesnt exist, reset house owner
			house->setOwner(0);
			continue;
		}

		if (player.getBankBalance() >= rent) {
			player.setBankBalance(player.getBankBalance() - rent);

			time_t paidUntil = currentTime;
			switch (rentPeriod) {
				case RENTPERIOD_DAILY:
					paidUntil += 24 * 60 * 60;
					break;
				case RENTPERIOD_WEEKLY:
					paidUntil += 24 * 60 * 60 * 7;
					break;
				case RENTPERIOD_MONTHLY:
					paidUntil += 24 * 60 * 60 * 30;
					break;
				case RENTPERIOD_YEARLY:
					paidUntil += 24 * 60 * 60 * 365;
					break;
				default:
					break;
			}

			house->setPaidUntil(paidUntil);
		} else {
			if (house->getPayRentWarnings() < 7) {
				int32_t daysLeft = 7 - house->getPayRentWarnings();

				Item* letter = Item::CreateItem(ITEM_LETTER_STAMPED);
				std::string period;

				switch (rentPeriod) {
					case RENTPERIOD_DAILY:
						period = "daily";
						break;

					case RENTPERIOD_WEEKLY:
						period = "weekly";
						break;

					case RENTPERIOD_MONTHLY:
						period = "monthly";
						break;

					case RENTPERIOD_YEARLY:
						period = "annual";
						break;

					default:
						break;
				}

				std::ostringstream ss;
				ss << "Warning! \nThe " << period << " rent of " << house->getRent() << " gold for your house \"" << house->getName() << "\" is payable. Have it within " << daysLeft << " days or you will lose this house.";
				letter->setText(ss.str());
				g_game.internalAddItem(player.getInbox(), letter, INDEX_WHEREEVER, FLAG_NOLIMIT);
				house->setPayRentWarnings(house->getPayRentWarnings() + 1);
			} else {
				house->setOwner(0, true, &player);
			}
		}

		IOLoginData::savePlayer(&player);
	}
	return true;
}
Example #3
0
bool Houses::payHouses()
{
	if(rentPeriod == RENTPERIOD_NEVER) {
		return true;
	}
	uint32_t currentTime = std::time(NULL);

	for(HouseMap::iterator it = houseMap.begin(); it != houseMap.end(); ++it){
		House* house = it->second;

		if(house->getHouseOwner() != 0 && house->getPaidUntil() < currentTime &&
			 house->getRent() != 0){
			uint32_t ownerid = house->getHouseOwner();
			Town* town = Towns::getInstance().getTown(house->getTownId());
			if(!town){
				#ifdef __DEBUG_HOUSES__
				std::cout << "Warning: [Houses::payHouses] town = NULL, townid = " <<
					house->getTownId() << ", houseid = " << house->getHouseId() << std::endl;
				#endif
				continue;
			}

			std::string name;
			if(!IOPlayer::instance()->getNameByGuid(ownerid, name)){
				//player doesnt exist, remove it as house owner?
				//house->setHouseOwner(0);
				continue;
			}

			Player* player = g_game.getPlayerByName(name);

			if(!player){
				player = new Player(name, NULL);

				if(!IOPlayer::instance()->loadPlayer(player, name)){
		#ifdef __DEBUG__
					std::cout << "Failure: [Houses::payHouses], can not load player: " << name << std::endl;
		#endif
					delete player;
					continue;
				}
			}

			Depot* depot = player->getDepot(town->getTownID(), true);

			// savePlayerHere is an ugly hack
			// to avoid saving 2 times a not online player
			// when items are transferred to his depot
			bool savePlayerHere = true;
			if(depot){
				//get money from depot
				bool useAccBalance = g_config.getBool(ConfigManager::USE_ACCBALANCE) != 0;
				bool hasEnoughMoney = false;

				if(useAccBalance){
					if(player->balance >= house->getRent()){
						player->balance -= house->getRent();
						hasEnoughMoney = true;
					}
				}
				else{
					hasEnoughMoney = g_game.removeMoney(depot, house->getRent(), FLAG_NOLIMIT);
				}

				if(hasEnoughMoney){
					uint32_t paidUntil = currentTime;
					switch(rentPeriod){
					case RENTPERIOD_DAILY:
						paidUntil += 24 * 60 * 60;
						break;
					case RENTPERIOD_WEEKLY:
						paidUntil += 24 * 60 * 60 * 7;
						break;
					case RENTPERIOD_MONTHLY:
						paidUntil += 24 * 60 * 60 * 30;
						break;
					case RENTPERIOD_YEARLY:
						paidUntil += 24 * 60 * 60 * 365;
					case RENTPERIOD_NEVER:
						break;
					}

					house->setPaidUntil(paidUntil);
				}
				else if(currentTime >= house->getLastWarning() + 24 * 60 * 60){
					if(house->getPayRentWarnings() >= 7){
						house->setHouseOwner(0);
						// setHouseOwner will load the player,
						// transfer house items to his depot and then
						// will save it, so here should not be saved
						// again
						savePlayerHere = false;
					}
					else{
						int daysLeft = 7 - house->getPayRentWarnings();

						Item* letter = Item::CreateItem(ITEM_LETTER_STAMPED);
						std::string period = "";

						switch(rentPeriod){
							case RENTPERIOD_DAILY:
								period = "daily";
							break;

							case RENTPERIOD_WEEKLY:
								period = "weekly";
							break;

							case RENTPERIOD_MONTHLY:
								period = "monthly";
							break;

							case RENTPERIOD_YEARLY:
								period = "annual";
							break;

							case RENTPERIOD_NEVER:
								//
							break;
						}

						std::stringstream warningText;
						warningText << "Warning! \n" <<
							"The " << period << " rent of " << house->getRent() << " gold for your house \""
							<< house->getName() << "\" is payable. Have it available within " << daysLeft <<
							" days, or you will lose this house.";

						letter->setText(warningText.str());
						g_game.internalAddItem(depot, letter, INDEX_WHEREEVER, FLAG_NOLIMIT);

						house->setPayRentWarnings(house->getPayRentWarnings() + 1);
						house->setLastWarning(currentTime);
					}
				}
			}

			if(player->isOffline()){
				if(savePlayerHere){
					IOPlayer::instance()->savePlayer(player);
				}
				delete player;
			}
		}
	}

	return true;
}
bool IOMapSerialize::saveHouseInfo()
{
	Database& db = Database::getInstance();

	DBTransaction transaction;
	if (!transaction.begin()) {
		return false;
	}

	if (!db.executeQuery("DELETE FROM `house_lists`")) {
		return false;
	}

	std::ostringstream query;
	for (const auto& it : g_game.map.houses.getHouses()) {
		House* house = it.second;
		query << "SELECT `id` FROM `houses` WHERE `id` = " << house->getId();
		DBResult_ptr result = db.storeQuery(query.str());
		if (result) {
			query.str(std::string());
			query << "UPDATE `houses` SET `owner` = " << house->getOwner() << ", `paid` = " << house->getPaidUntil() << ", `warnings` = " << house->getPayRentWarnings() << ", `name` = " << db.escapeString(house->getName()) << ", `town_id` = " << house->getTownId() << ", `rent` = " << house->getRent() << ", `size` = " << house->getTiles().size() << ", `beds` = " << house->getBedCount() << " WHERE `id` = " << house->getId();
		} else {
			query.str(std::string());
			query << "INSERT INTO `houses` (`id`, `owner`, `paid`, `warnings`, `name`, `town_id`, `rent`, `size`, `beds`) VALUES (" << house->getId() << ',' << house->getOwner() << ',' << house->getPaidUntil() << ',' << house->getPayRentWarnings() << ',' << db.escapeString(house->getName()) << ',' << house->getTownId() << ',' << house->getRent() << ',' << house->getTiles().size() << ',' << house->getBedCount() << ')';
		}

		db.executeQuery(query.str());
		query.str(std::string());
	}

	DBInsert stmt("INSERT INTO `house_lists` (`house_id` , `listid` , `list`) VALUES ");

	for (const auto& it : g_game.map.houses.getHouses()) {
		House* house = it.second;

		std::string listText;
		if (house->getAccessList(GUEST_LIST, listText) && !listText.empty()) {
			query << house->getId() << ',' << GUEST_LIST << ',' << db.escapeString(listText);
			if (!stmt.addRow(query)) {
				return false;
			}

			listText.clear();
		}

		if (house->getAccessList(SUBOWNER_LIST, listText) && !listText.empty()) {
			query << house->getId() << ',' << SUBOWNER_LIST << ',' << db.escapeString(listText);
			if (!stmt.addRow(query)) {
				return false;
			}

			listText.clear();
		}

		for (Door* door : house->getDoors()) {
			if (door->getAccessList(listText) && !listText.empty()) {
				query << house->getId() << ',' << door->getDoorId() << ',' << db.escapeString(listText);
				if (!stmt.addRow(query)) {
					return false;
				}

				listText.clear();
			}
		}
	}

	if (!stmt.execute()) {
		return false;
	}

	return transaction.commit();
}
Example #5
0
bool Houses::payHouses()
{
	if (rentPeriod == RENTPERIOD_NEVER) {
		return true;
	}

	time_t currentTime = time(NULL);

	for (HouseMap::iterator it = houseMap.begin(); it != houseMap.end(); ++it) {
		House* house = it->second;

		if (house->getHouseOwner() != 0) {
			uint32_t ownerid = house->getHouseOwner();
			Town* town = Towns::getInstance().getTown(house->getTownId());

			if (!town) {
#ifdef __DEBUG_HOUSES__
				std::cout << "Warning: [Houses::payHouses] town = NULL, townid = " <<
				          house->getTownId() << ", houseid = " << house->getHouseId() << std::endl;
#endif
				continue;
			}

			std::string name;

			if (!IOLoginData::getInstance()->getNameByGuid(ownerid, name)) {
				//player doesnt exist, remove it as house owner?
				house->setHouseOwner(0);
				continue;
			}

			Player* player = g_game.getPlayerByName(name);

			if (!player) {
				player = new Player(name, NULL);

				if (!IOLoginData::getInstance()->loadPlayer(player, name)) {
#ifdef __DEBUG__
					std::cout << "Failure: [Houses::payHouses], can not load player: " << name << std::endl;
#endif
					delete player;
					continue;
				}
			}

			bool paid = false;
			if (player->getBankBalance() >= house->getRent()) {
				player->setBankBalance(player->getBankBalance() - house->getRent());
				paid = true;
			}

			if (paid) {
				time_t paidUntil = currentTime;

				switch (rentPeriod) {
					case RENTPERIOD_DAILY:
						paidUntil += 24 * 60 * 60;
						break;
					case RENTPERIOD_WEEKLY:
						paidUntil += 24 * 60 * 60 * 7;
						break;
					case RENTPERIOD_MONTHLY:
						paidUntil += 24 * 60 * 60 * 30;
						break;
					case RENTPERIOD_YEARLY:
						paidUntil += 24 * 60 * 60 * 365;
						break;
					default:
						break;
				}

				house->setPaidUntil(paidUntil);
			} else {
				if (house->getPayRentWarnings() < 7) {
					int32_t daysLeft = 7 - house->getPayRentWarnings();

					Item* letter = Item::CreateItem(ITEM_LETTER_STAMPED);
					std::string period = "";

					switch (rentPeriod) {
						case RENTPERIOD_DAILY:
							period = "daily";
							break;

						case RENTPERIOD_WEEKLY:
							period = "weekly";
							break;

						case RENTPERIOD_MONTHLY:
							period = "monthly";
							break;

						case RENTPERIOD_YEARLY:
							period = "annual";
							break;

						default:
							break;
					}

					std::ostringstream ss;
					ss << "Warning! \nThe " << period << " rent of " << house->getRent() << " gold for your house \"" << house->getName() << "\" is payable. Have it within " << daysLeft << " days or you will lose this house.";
					letter->setText(ss.str());
					g_game.internalAddItem(player->getInbox(), letter, INDEX_WHEREEVER, FLAG_NOLIMIT);
					house->setPayRentWarnings(house->getPayRentWarnings() + 1);
				} else {
					house->setHouseOwner(0, true, player);
				}
			}

			if (player->isOffline()) {
				IOLoginData::getInstance()->savePlayer(player);
				delete player;
			}
		}
	}
	return true;
}