Skip to content

Commit

Permalink
fix: house transfer owner only in startup
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Sep 19, 2023
1 parent 3fbbd46 commit f8fe145
Show file tree
Hide file tree
Showing 19 changed files with 287 additions and 75 deletions.
2 changes: 1 addition & 1 deletion data-otservbr-global/lib/compat/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ end

function setHouseOwner(id, guid)
local h = House(id)
return h and h:setOwnerGuid(guid) or false
return h and h:setHouseOwner(guid) or false
end

function getHouseRent(id)
Expand Down
4 changes: 3 additions & 1 deletion data-otservbr-global/migrations/39.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function onUpdateDatabase()
return false -- true = There are others migrations file | false = this is the last migration file
logger.info("Updating database to version 39 (house transfer ownership on startup)")
db.query("ALTER TABLE `houses` ADD `new_owner` int(11) NOT NULL DEFAULT '-1';")
return true
end
3 changes: 3 additions & 0 deletions data-otservbr-global/migrations/40.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function onUpdateDatabase()
return false -- true = There are others migrations file | false = this is the last migration file
end
21 changes: 0 additions & 21 deletions data/items/items.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,6 @@
<item id="622" name="water">
<attribute key="type" value="trashholder"/>
<attribute key="effect" value="bluebubble"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item fromid="623" toid="624" article="a" name="beach chair">
<attribute key="primarytype" value="constructions"/>
Expand All @@ -1054,7 +1053,6 @@
<attribute key="type" value="trashholder"/>
<attribute key="fluidsource" value="water"/>
<attribute key="effect" value="bluebubble"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item fromid="635" toid="636" article="a" name="parchment">
<attribute key="primarytype" value="documents and papers"/>
Expand Down Expand Up @@ -1678,7 +1676,6 @@
<attribute key="primarytype" value="natural tiles"/>
<attribute key="type" value="trashholder"/>
<attribute key="effect" value="fire"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item id="731" article="a" name="bed">
<attribute key="primarytype" value="constructions"/>
Expand Down Expand Up @@ -2354,7 +2351,6 @@
<attribute key="primarytype" value="natural tiles"/>
<attribute key="type" value="trashholder"/>
<attribute key="effect" value="poff"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item id="841" article="a" name="peanut" plural="peanuts">
<attribute key="primarytype" value="food"/>
Expand Down Expand Up @@ -2473,7 +2469,6 @@
<item fromid="880" toid="891" name="shallow water">
<attribute key="primarytype" value="natural tiles"/>
<attribute key="fluidsource" value="water"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item id="892" article="a" name="package of potions">
<attribute key="description" value="It contains 5 health and 5 mana potions"/>
Expand Down Expand Up @@ -12140,7 +12135,6 @@
<attribute key="type" value="trashholder"/>
<attribute key="fluidsource" value="water"/>
<attribute key="effect" value="bluebubble"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item fromid="4603" toid="4608" name="grass"/>
<item fromid="4609" toid="4611" name="shallow water">
Expand All @@ -12149,14 +12143,12 @@
<attribute key="description" value="You see the silvery movement of fish."/>
<attribute key="fluidsource" value="water"/>
<attribute key="effect" value="bluebubble"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item id="4612" name="shallow water">
<attribute key="primarytype" value="natural tiles"/>
<attribute key="type" value="trashholder"/>
<attribute key="fluidsource" value="water"/>
<attribute key="effect" value="bluebubble"/>
<attribute key="allowpickUpAble" value="1"/>
<attribute key="duration" value="1200"/>
<attribute key="decayTo" value="4597"/>
</item>
Expand All @@ -12165,7 +12157,6 @@
<attribute key="type" value="trashholder"/>
<attribute key="fluidsource" value="water"/>
<attribute key="effect" value="bluebubble"/>
<attribute key="allowpickUpAble" value="1"/>
<attribute key="duration" value="1200"/>
<attribute key="decayTo" value="4598"/>
</item>
Expand All @@ -12174,7 +12165,6 @@
<attribute key="type" value="trashholder"/>
<attribute key="fluidsource" value="water"/>
<attribute key="effect" value="bluebubble"/>
<attribute key="allowpickUpAble" value="1"/>
<attribute key="duration" value="1200"/>
<attribute key="decayTo" value="4599"/>
</item>
Expand All @@ -12185,7 +12175,6 @@
<item fromid="4633" toid="4652" name="shallow water">
<attribute key="primarytype" value="natural tiles"/>
<attribute key="fluidsource" value="water"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item id="4653" name="RESERVED SPRITE"/>
<item fromid="4654" toid="4655" name="shallow water">
Expand All @@ -12196,7 +12185,6 @@
<attribute key="primarytype" value="natural tiles"/>
<attribute key="type" value="trashholder"/>
<attribute key="effect" value="greenbubble"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item fromid="4702" toid="4737" name="swamp">
<attribute key="primarytype" value="natural tiles"/>
Expand All @@ -12205,7 +12193,6 @@
<attribute key="primarytype" value="natural tiles"/>
<attribute key="type" value="trashholder"/>
<attribute key="effect" value="greenbubble"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item fromid="4745" toid="4746" name="grass"/>
<item fromid="4747" toid="4748" name="earth"/>
Expand All @@ -12218,7 +12205,6 @@
<attribute key="primarytype" value="natural tiles"/>
<attribute key="fluidsource" value="water"/>
<attribute key="effect" value="bluebubble"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item fromid="4815" toid="4816" article="a" name="mountain">
<attribute key="primarytype" value="rocks"/>
Expand All @@ -12228,7 +12214,6 @@
<attribute key="type" value="trashholder"/>
<attribute key="fluidsource" value="water"/>
<attribute key="effect" value="bluebubble"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item fromid="4821" toid="4822" name="dirt"/>
<item fromid="4823" toid="4826" name="stairs">
Expand Down Expand Up @@ -20480,7 +20465,6 @@
<attribute key="primarytype" value="natural tiles"/>
<attribute key="type" value="trashholder"/>
<attribute key="effect" value="greenbubble"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item id="8717" article="the" name="dust of Boreth">
<attribute key="description" value="Julius might be interested in the ashes of this vampire lord"/>
Expand Down Expand Up @@ -31380,7 +31364,6 @@
<attribute key="primarytype" value="natural tiles"/>
<attribute key="type" value="trashholder"/>
<attribute key="effect" value="greenbubble"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item fromid="16311" toid="16332" article="a" name="bog wart"/>
<item fromid="16333" toid="16376" article="a" name="muddy stone wall"/>
Expand Down Expand Up @@ -32745,7 +32728,6 @@
<attribute key="primarytype" value="natural tiles"/>
<attribute key="type" value="trashholder"/>
<attribute key="effect" value="greenbubble"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item fromid="17951" toid="17956" article="a" name="portrait of the merchant"/>
<item fromid="17957" toid="17958" article="a" name="statue of the merchant"/>
Expand Down Expand Up @@ -34725,7 +34707,6 @@
<attribute key="type" value="trashholder"/>
<attribute key="fluidsource" value="blood"/>
<attribute key="effect" value="redspark"/>
<attribute key="allowpickUpAble" value="1"/>
<attribute key="weight" value="180"/>
</item>
<item id="18620" name="blood">
Expand Down Expand Up @@ -38299,7 +38280,6 @@
<attribute key="type" value="trashholder"/>
<attribute key="fluidsource" value="water"/>
<attribute key="effect" value="bluebubble"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item id="21323" article="an" name="imported water pipe">
<attribute key="primarytype" value="utilities"/>
Expand Down Expand Up @@ -38706,7 +38686,6 @@
<attribute key="primarytype" value="natural tiles"/>
<attribute key="type" value="trashholder"/>
<attribute key="effect" value="fire"/>
<attribute key="allowpickUpAble" value="1"/>
</item>
<item fromid="21478" toid="21499" article="a" name="lava">
<attribute key="primarytype" value="natural tiles"/>
Expand Down
4 changes: 2 additions & 2 deletions data/scripts/talkactions/god/house_owner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function houseOwner.onSay(player, words, param)
end

if param == "" or param == "none" then
house:setOwnerGuid(0)
house:setHouseOwner(0)
return true
end

Expand All @@ -22,7 +22,7 @@ function houseOwner.onSay(player, words, param)
return true
end

house:setOwnerGuid(targetPlayer:getGuid())
house:setHouseOwner(targetPlayer:getGuid())
return true
end

Expand Down
12 changes: 11 additions & 1 deletion data/scripts/talkactions/god/remove_thing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ function removeThing.onSay(player, words, param)
player:sendCancelMessage("You may not remove a ground tile.")
return true
end
thing:remove(tonumber(param) or -1)
if param == "all" then
local items = tile:getItems()
if items then
for i = 1, #items do
local item = items[i]
item:remove()
end
end
else
thing:remove(tonumber(param) or -1)
end
end

position:sendMagicEffect(CONST_ME_MAGIC_RED)
Expand Down
7 changes: 6 additions & 1 deletion data/scripts/talkactions/player/buy_house.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ function buyHouse.onSay(player, words, param)
return false
end

if house:hasTileItem() then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot buy this house, as there are items inside it. Please, contact an administrator.")
return false
end

local price = house:getPrice()
if not player:removeMoneyBank(price) then
player:sendCancelMessage("You do not have enough money.")
return false
end

house:setOwnerGuid(player:getGuid())
house:setHouseOwner(player:getGuid())
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have successfully bought this house, be sure to have the money for the rent in the bank.")
return false
end
Expand Down
8 changes: 7 additions & 1 deletion data/scripts/talkactions/player/leave_house.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ function leaveHouse.onSay(player, words, param)
return false
end

if house:hasNewOwnership() then
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You cannot leave this house. Ownership is already scheduled to be transferred upon the next server restart.")
playerPosition:sendMagicEffect(CONST_ME_POFF)
return false
end

-- Move hireling back to lamp
local tiles = house:getTiles()
if tiles then
Expand All @@ -30,7 +36,7 @@ function leaveHouse.onSay(player, words, param)
end
end

house:setOwnerGuid(0)
house:setNewOwnerGuid(0)
player:sendTextMessage(MESSAGE_EVENT_ADVANCE, "You have successfully left your house.")
playerPosition:sendMagicEffect(CONST_ME_POFF)
return false
Expand Down
1 change: 1 addition & 0 deletions schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ CREATE TABLE IF NOT EXISTS `guild_membership` (
CREATE TABLE IF NOT EXISTS `houses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`owner` int(11) NOT NULL,
`new_owner` int(11) NOT NULL DEFAULT '-1',
`paid` int(10) UNSIGNED NOT NULL DEFAULT '0',
`warnings` int(11) NOT NULL DEFAULT '0',
`name` varchar(255) NOT NULL,
Expand Down
1 change: 1 addition & 0 deletions src/canary_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ int CanaryServer::run() {
g_game().setGameState(GAME_STATE_INIT);

setupHousesRent();
g_game().transferHouseItemsToDepot();

IOMarket::checkExpiredOffers();
IOMarket::getInstance().updateStatistics();
Expand Down
37 changes: 37 additions & 0 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9854,6 +9854,43 @@ const std::unique_ptr<IOWheel> &Game::getIOWheel() const {
return m_IOWheel;
}

void Game::transferHouseItemsToDepot() {
if (!transferHouseItemsToPlayer.empty()) {
g_logger().info("Initializing house transfer items");
}

uint16_t transferSuccess = 0;
for (const auto &[houseId, playerGuid] : transferHouseItemsToPlayer) {
House* house = map.houses.getHouse(houseId);
if (house) {
auto offlinePlayer = new Player(nullptr);
if (!IOLoginData::loadPlayerById(offlinePlayer, playerGuid)) {
delete offlinePlayer;
continue;
}

if (!offlinePlayer) {
continue;
}

g_logger().info("Tranfering items to the inbox from player '{}'", offlinePlayer->getName());
if (house->tryTransferOwnership(offlinePlayer, true)) {
transferSuccess++;
house->setNewOwnerGuid(-1, true);
}
}
}
if (transferSuccess > 0) {
g_logger().info("Finished house transfer items from '{}' players", transferSuccess);
transferHouseItemsToPlayer.clear();
Map::save();
}
}

void Game::setTransferPlayerHouseItems(uint32_t houseId, uint32_t playerId) {
transferHouseItemsToPlayer[houseId] = playerId;
}

template <typename T>
phmap::parallel_flat_hash_set<T> setDifference(const phmap::parallel_flat_hash_set<T> &setA, const phmap::parallel_flat_hash_set<T> &setB) {
phmap::parallel_flat_hash_set<T> setResult;
Expand Down
5 changes: 5 additions & 0 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,9 @@ class Game {
std::unique_ptr<IOWheel> &getIOWheel();
const std::unique_ptr<IOWheel> &getIOWheel() const;

void setTransferPlayerHouseItems(uint32_t houseId, uint32_t playerId);
void transferHouseItemsToDepot();

private:
std::map<uint32_t, int32_t> forgeMonsterEventIds;
std::set<uint32_t> fiendishMonsters;
Expand Down Expand Up @@ -769,6 +772,8 @@ class Game {
std::map<uint32_t, Monster*> monsters;
std::vector<uint32_t> forgeableMonsters;

std::map<uint32_t, uint32_t> transferHouseItemsToPlayer;

std::map<uint32_t, TeamFinder*> teamFinderMap; // [leaderGUID] = TeamFinder*

// list of items that are in trading state, mapped to the player
Expand Down
3 changes: 2 additions & 1 deletion src/io/iologindata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ bool IOLoginData::loadPlayerByName(Player* player, const std::string &name, bool

bool IOLoginData::loadPlayer(Player* player, DBResult_ptr result, bool disable /* = false*/) {
if (!result || !player) {
g_logger().warn("[IOLoginData::loadPlayer] - Player or Resultnullptr: {}", __FUNCTION__);
std::string nullptrType = !result ? "Result" : "Player";
g_logger().warn("[{}] - {} is nullptr", __FUNCTION__, nullptrType);
return false;
}

Expand Down
22 changes: 19 additions & 3 deletions src/io/iomapserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "pch.hpp"

#include "io/iomapserialize.hpp"
#include "io/iologindata.hpp"
#include "game/game.hpp"
#include "items/bed.hpp"

Expand Down Expand Up @@ -259,15 +260,30 @@ void IOMapSerialize::saveTile(PropWriteStream &stream, const Tile* tile) {
bool IOMapSerialize::loadHouseInfo() {
Database &db = Database::getInstance();

DBResult_ptr result = db.storeQuery("SELECT `id`, `owner`, `paid`, `warnings` FROM `houses`");
DBResult_ptr result = db.storeQuery("SELECT `id`, `owner`, `new_owner`, `paid`, `warnings` FROM `houses`");
if (!result) {
return false;
}

do {
House* house = g_game().map.houses.getHouse(result->getNumber<uint32_t>("id"));
auto houseId = result->getNumber<uint32_t>("id");
House* house = g_game().map.houses.getHouse(houseId);
if (house) {
house->setOwner(result->getNumber<uint32_t>("owner"), false);
uint32_t owner = result->getNumber<uint32_t>("owner");
int32_t newOwner = result->getNumber<int32_t>("new_owner");
// Transfer house owner
if (newOwner >= 0) {
g_game().setTransferPlayerHouseItems(houseId, owner);
if (newOwner == 0) {
g_logger().debug("Removing house id '{}' owner", houseId);
house->setOwner(0);
} else {
g_logger().debug("Setting house id '{}' owner to player GUID '{}'", houseId, newOwner);
house->setOwner(newOwner);
}
} else {
house->setOwner(owner, false);
}
house->setPaidUntil(result->getNumber<time_t>("paid"));
house->setPayRentWarnings(result->getNumber<uint32_t>("warnings"));
}
Expand Down
Loading

0 comments on commit f8fe145

Please sign in to comment.