Skip to content

Commit

Permalink
feat: enable/disable transfer/leave on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Sep 23, 2023
1 parent db789f8 commit af193d6
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 26 deletions.
6 changes: 6 additions & 0 deletions config.lua.dist
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ deathLosePercent = -1
-- Houses
-- NOTE: set housePriceEachSQM to -1 to disable the ingame buy house functionality
-- NOTE: set houseBuyLevel to 0 to disable the min level purchase functionality.
--[[ NOTE: The togglehouseTransferOnRestart setting controls whether house transfers/leave are true/false upon server restart.
• When set to true, the transfers will only occur during a server restart.
Setting this to false may pose risks; if a house is abandoned and contains a large number of items on the floor, those items will be transferred to the player's depot instantly.
• This could potentially freeze the server due to the heavy operation. It's advised to keep this setting enabled (true) to minimize risks.
]]
-- Periods: daily/weekly/monthly/yearly/never
-- Base: sqm,rent,sqm+rent
housePriceRentMultiplier = 0.0
Expand All @@ -263,6 +268,7 @@ houseOwnedByAccount = false
houseBuyLevel = 100
housePurchasedShowPrice = false
onlyInvitedCanMoveHouseItems = true
togglehouseTransferOnRestart = true

-- Item Usage
timeBetweenActions = 200
Expand Down
1 change: 1 addition & 0 deletions src/config/config_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ enum booleanConfig_t {

REWARD_CHEST_COLLECT_ENABLED,
TOGGLE_MOUNT_IN_PZ,
TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART,

LAST_BOOLEAN_CONFIG
};
Expand Down
2 changes: 2 additions & 0 deletions src/config/configmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ bool ConfigManager::load() {

boolean[TOGGLE_MOUNT_IN_PZ] = getGlobalBoolean(L, "toggleMountInProtectionZone", false);

boolean[TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART] = getGlobalBoolean(L, "togglehouseTransferOnRestart", false);

loaded = true;
lua_close(L);
return true;
Expand Down
4 changes: 4 additions & 0 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9848,6 +9848,10 @@ const std::unique_ptr<IOWheel> &Game::getIOWheel() const {
}

void Game::transferHouseItemsToDepot() {
if (!g_configManager().getBoolean(TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART)) {
return;
}

if (!transferHouseItemsToPlayer.empty()) {
g_logger().info("Initializing house transfer items");
}
Expand Down
3 changes: 2 additions & 1 deletion src/io/iomapserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ bool IOMapSerialize::loadHouseInfo() {
uint32_t owner = result->getNumber<uint32_t>("owner");
int32_t newOwner = result->getNumber<int32_t>("new_owner");
// Transfer house owner
if (newOwner >= 0) {
auto isTransferOnRestart = g_configManager().getBoolean(TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART);
if (isTransferOnRestart && newOwner >= 0) {
g_game().setTransferPlayerHouseItems(houseId, owner);
if (newOwner == 0) {
g_logger().debug("Removing house id '{}' owner", houseId);
Expand Down
2 changes: 2 additions & 0 deletions src/lua/functions/core/game/config_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ void ConfigFunctions::init(lua_State* L) {
registerEnumIn(L, "configKeys", VIP_AUTOLOOT_VIP_ONLY);
registerEnumIn(L, "configKeys", VIP_STAY_ONLINE);
registerEnumIn(L, "configKeys", VIP_FAMILIAR_TIME_COOLDOWN_REDUCTION);

registerEnumIn(L, "configKeys", TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART);
#undef registerEnumIn
}

Expand Down
24 changes: 16 additions & 8 deletions src/lua/functions/map/house_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ int HouseFunctions::luaHouseSetHouseOwner(lua_State* L) {
}

int HouseFunctions::luaHouseSetNewOwnerGuid(lua_State* L) {
// house:setNewOwnerGuid(guid)
auto house = getUserdata<House>(L, 1);
// house:setNewOwnerGuid(guid[, updateDatabase = true])
auto house = getUserdataShared<House>(L, 1);
if (house) {
if (house->hasNewOwnership()) {
auto isTransferOnRestart = g_configManager().getBoolean(TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART);
if (isTransferOnRestart && house->hasNewOwnership()) {
const auto player = g_game().getPlayerByGUID(house->getOwner());
if (player) {
player->sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot leave this house. Ownership is already scheduled to be transferred upon the next server restart.");
Expand All @@ -137,7 +138,12 @@ int HouseFunctions::luaHouseSetNewOwnerGuid(lua_State* L) {
}

uint32_t guid = getNumber<uint32_t>(L, 2, 0);
house->setNewOwnerGuid(guid, false);
if (isTransferOnRestart) {
house->setNewOwnerGuid(guid, false);
} else {
bool updateDatabase = getBoolean(L, 3, true);
house->setOwner(guid, updateDatabase);
}
pushBoolean(L, true);
} else {
lua_pushnil(L);
Expand All @@ -147,7 +153,7 @@ int HouseFunctions::luaHouseSetNewOwnerGuid(lua_State* L) {

int HouseFunctions::luaHouseHasItemOnTile(lua_State* L) {
// house:hasItemOnTile()
auto house = getUserdata<House>(L, 1);
auto house = getUserdataShared<House>(L, 1);
if (!house) {
reportErrorFunc("House not found");
lua_pushnil(L);
Expand All @@ -160,14 +166,15 @@ int HouseFunctions::luaHouseHasItemOnTile(lua_State* L) {

int HouseFunctions::luaHouseHasNewOwnership(lua_State* L) {
// house:hasNewOwnership(guid)
auto house = getUserdata<House>(L, 1);
auto house = getUserdataShared<House>(L, 1);
if (!house) {
reportErrorFunc("House not found");
lua_pushnil(L);
return 1;
}

pushBoolean(L, house->hasNewOwnership());
auto isTransferOnRestart = g_configManager().getBoolean(TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART);
pushBoolean(L, isTransferOnRestart && house->hasNewOwnership());
return 1;
}

Expand Down Expand Up @@ -208,7 +215,8 @@ int HouseFunctions::luaHouseStartTrade(lua_State* L) {
return 1;
}

if (house->hasNewOwnership()) {
auto isTransferOnRestart = g_configManager().getBoolean(TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART);
if (isTransferOnRestart && house->hasNewOwnership()) {
tradePartner->sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot buy this house. Ownership is already scheduled to be transferred upon the next server restart.");
player->sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot sell this house. Ownership is already scheduled to be transferred upon the next server restart.");
lua_pushnumber(L, RETURNVALUE_YOUCANNOTTRADETHISHOUSE);
Expand Down
51 changes: 34 additions & 17 deletions src/map/house/house.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ void House::addTile(std::shared_ptr<HouseTile> tile) {
}

void House::setNewOwnerGuid(int32_t newOwnerGuid, bool serverStartup) {
auto isTransferOnRestart = g_configManager().getBoolean(TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART);
if (!isTransferOnRestart) {
setOwner(newOwnerGuid, true);
return;
}

std::ostringstream query;
query << "UPDATE `houses` SET `new_owner` = " << newOwnerGuid << " WHERE `id` = " << id;

Expand All @@ -36,6 +42,13 @@ void House::setNewOwnerGuid(int32_t newOwnerGuid, bool serverStartup) {
}

bool House::tryTransferOwnership(std::shared_ptr<Player> player, bool serverStartup) {
bool transferSuccess = false;
if (player) {
transferSuccess = transferToDepot(player);
} else {
transferSuccess = transferToDepot();
}

for (auto tile : houseTiles) {
if (const CreatureVector* creatures = tile->getCreatures()) {
for (int32_t i = creatures->size(); --i >= 0;) {
Expand Down Expand Up @@ -64,13 +77,6 @@ bool House::tryTransferOwnership(std::shared_ptr<Player> player, bool serverStar
door->setAccessList("");
}

bool transferSuccess = false;
if (player) {
transferSuccess = transferToDepot(player);
} else {
transferSuccess = transferToDepot();
}

return transferSuccess;
}

Expand Down Expand Up @@ -259,6 +265,7 @@ bool House::transferToDepot(std::shared_ptr<Player> player) const {
if (townId == 0 || !player) {
return false;
}

ItemList moveItemList;
for (std::shared_ptr<HouseTile> tile : houseTiles) {
if (const TileItemVector* items = tile->getItemList()) {
Expand All @@ -284,9 +291,9 @@ bool House::transferToDepot(std::shared_ptr<Player> player) const {

bool House::hasItemOnTile() const {
bool foundItem = false;
for (const std::shared_ptr<HouseTile>& tile : houseTiles) {
for (const std::shared_ptr<HouseTile> &tile : houseTiles) {
if (const auto &items = tile->getItemList()) {
for (const std::shared_ptr<Item>& item : *items) {
for (const std::shared_ptr<Item> &item : *items) {
if (!item) {
continue;
}
Expand All @@ -300,7 +307,7 @@ bool House::hasItemOnTile() const {
g_logger().error("It is not possible to purchase a house with pickupable item inside: id '{}', name '{}'", item->getID(), item->getName());
break;
} else {
if (item->getContainer() && (!item->isPickupable() || !item->isWrapable())) {
if (item->getContainer() && (item->isPickupable() || item->isWrapable())) {
foundItem = true;
g_logger().error("It is not possible to purchase a house with container item inside: id '{}', name '{}'", item->getID(), item->getName());
break;
Expand Down Expand Up @@ -436,7 +443,6 @@ void House::resetTransferItem() {
transferItem = nullptr;
transfer_container->resetParent();
transfer_container->removeThing(tmpItem, tmpItem->getItemCount());
transfer_container = nullptr;
}
}

Expand All @@ -453,10 +459,16 @@ std::shared_ptr<HouseTransferItem> HouseTransferItem::createHouseTransferItem(st
void HouseTransferItem::onTradeEvent(TradeEvents_t event, std::shared_ptr<Player> owner) {
if (event == ON_TRADE_TRANSFER) {
if (house) {
owner->sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have successfully bought the house. The ownership will be transferred upon server restart.");
auto isTransferOnRestart = g_configManager().getBoolean(TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART);
auto ownershipTransferMessage = " The ownership will be transferred upon server restart.";
auto boughtMessage = fmt::format("You have successfully bought the house.{}", isTransferOnRestart ? ownershipTransferMessage : "");
auto soldMessage = fmt::format("You have successfully sold your house.{}", isTransferOnRestart ? ownershipTransferMessage : "");

owner->sendTextMessage(MESSAGE_EVENT_ADVANCE, boughtMessage);

auto oldOwner = g_game().getPlayerByGUID(house->getOwner());
if (oldOwner) {
oldOwner->sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have successfully sold your house. The ownership will be transferred upon server restart.");
oldOwner->sendTextMessage(MESSAGE_EVENT_ADVANCE, soldMessage);
}
house->executeTransfer(static_self_cast<HouseTransferItem>(), owner);
}
Expand All @@ -474,11 +486,16 @@ bool House::executeTransfer(std::shared_ptr<HouseTransferItem> item, std::shared
return false;
}

if (hasNewOwnerOnStartup) {
return false;
}
auto isTransferOnRestart = g_configManager().getBoolean(TOGGLE_HOUSE_TRANSFER_ON_SERVER_RESTART);
if (isTransferOnRestart) {
if (hasNewOwnerOnStartup) {
return false;
}

setNewOwnerGuid(newOwner->getGUID(), false);
setNewOwnerGuid(newOwner->getGUID(), false);
} else {
setOwner(newOwner->getGUID());
}
transferItem = nullptr;
return true;
}
Expand Down

0 comments on commit af193d6

Please sign in to comment.