Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guildhalls and guild bank. #2214

Closed
wants to merge 36 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d76dc5a
Schema and Lua migrations for guild halls and guild bank.
Znote Mar 25, 2017
6e6ba52
Better SQL structure for the guild_transactions table.
Znote Mar 26, 2017
c681f48
Resolve conflicts
Znote Nov 3, 2019
16336b6
Merge with master and resolve conflicts
Znote Nov 3, 2019
bba6f97
Revert filename change back to 19.lua
Znote Nov 3, 2019
82a14dc
Correct merge mistakes
Znote Nov 3, 2019
5748c4f
Rename migration to 25.
Znote Nov 3, 2019
b534426
Merge remote-tracking branch 'otland/master' into 2213
Znote Jun 18, 2020
6814711
Database: Changed tinyint to enum and two column names.
Znote Jun 18, 2020
432b273
Add houses.type column after houses.id
Znote Jun 18, 2020
711321b
Marked 4 houses as guildhalls.
Znote Jun 18, 2020
b6372dd
Added house type enum and propagate house type to database
Znote Jun 18, 2020
c52c6ba
MySQL Enum value WITHDRAW instead of WITHDRAWAL
Znote Jun 18, 2020
5873ec4
House::updateDoorDescription() distinguishes houses from guildhalls.
Znote Jun 18, 2020
025048b
Store bank balance and owner guid in guild object
Znote Jun 19, 2020
c54674a
House::setOwner and House::payHouses adjusted to accomodate guildhalls
Znote Jun 19, 2020
8a31f60
Push false if house:setOwnerGuid(guid) fails to find guild_id if hous…
Znote Jun 19, 2020
f9eab57
Updating balance of a guild will also update the database
Znote Jun 19, 2020
c59f671
Guild leaders and vice-leaders now has access to walk into their guil…
Znote Jun 19, 2020
2887b0d
fix house permissions
Znote Jun 19, 2020
f1530b2
Lua functions
Znote Jun 21, 2020
8605666
feedback optimizations: (sql and source)
Znote Jun 22, 2020
33ceac0
Uppercase unsigned in sql statements
Znote Jun 22, 2020
a1f9a72
Guild Bank NPC
Znote Jun 22, 2020
bb7858a
guild_transactions, receipt support
Znote Jun 23, 2020
168a0e0
New column names, new column category, NPC allows transfers between g…
Znote Jun 29, 2020
e8905dd
Swap calc logic for paidUntil to be more human readable
Znote Jul 12, 2020
8d6fa83
Implements houseId for guilds
Znote Jul 12, 2020
3ee2f37
buyhouse.lua support guildhall purchase.
Znote Jul 12, 2020
a355019
Merge branch 'master' into 2213
Znote Jul 12, 2020
852b901
Must be guild leader to buy guildhall.
Znote Jul 12, 2020
1625491
buyhouse: review feedback, eye friendly code
Znote Jul 13, 2020
d11e44d
Sell guildhall support (untested)
Znote Jul 13, 2020
d8bf253
Revert outdated migrations
Znote Nov 15, 2020
216ff40
Merge remote-tracking branch 'otland/master' into 2213
Znote Nov 15, 2020
2dc848f
Updated migrations
Znote Nov 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions data/items/items.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33359,10 +33359,12 @@
<item fromid="24283" toid="24300" article="a" name="hoard of cursed gold" />
<item id="24301" article="a" name="receipt">
<attribute key="weight" value="25" />
<attribute key="readable" value="1" />
<attribute key="description" value="This receipt serves as a reference to a successful guild bank transfer." />
</item>
<item id="24302" article="a" name="receipt">
<attribute key="weight" value="25" />
<attribute key="readable" value="1" />
<attribute key="description" value="This receipt serves as a reference to a unsuccessful guild bank transfer." />
</item>
<item id="24303" name="black crystals" />
Expand Down
21 changes: 20 additions & 1 deletion data/migrations/27.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
function onUpdateDatabase()
return false
print("> Updating database to version 27 (guildhalls, guild banks #2213)")
db.query("ALTER TABLE `houses` ADD `type` ENUM('HOUSE', 'GUILDHALL') NOT NULL DEFAULT 'HOUSE' AFTER `id`")
db.query("ALTER TABLE `guilds` ADD `balance` bigint(20) UNSIGNED NOT NULL DEFAULT '0'")
db.query([[
CREATE TABLE IF NOT EXISTS `guild_transactions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`guild_id` int(11) NOT NULL,
`guild_associated` int(11) DEFAULT NULL,
`player_associated` int(11) DEFAULT NULL,
`type` ENUM('DEPOSIT', 'WITHDRAW') NOT NULL,
`category` ENUM ('OTHER', 'RENT', 'MATERIAL', 'SERVICES', 'REVENUE', 'CONTRIBUTION') NOT NULL DEFAULT 'OTHER',
`balance` bigint(20) UNSIGNED NOT NULL DEFAULT '0',
`time` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`guild_id`) REFERENCES `guilds`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`guild_associated`) REFERENCES `guilds`(`id`) ON DELETE SET NULL,
FOREIGN KEY (`player_associated`) REFERENCES `players`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB;
]])
return true
end
3 changes: 3 additions & 0 deletions data/migrations/28.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function onUpdateDatabase()
return false
end
5 changes: 5 additions & 0 deletions data/npc/GuildBanker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<npc name="Guild Banker" script="guildbank.lua" walkinterval="2000" floorchange="0">
<health now="100" max="100" />
<look type="132" head="38" body="85" legs="78" feet="116" addons="3" />
</npc>
490 changes: 490 additions & 0 deletions data/npc/scripts/guildbank.lua

Large diffs are not rendered by default.

72 changes: 65 additions & 7 deletions data/talkactions/scripts/buyhouse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,81 @@ function onSay(player, words, param)
return false
end

if house:getOwnerGuid() > 0 then
player:sendCancelMessage("This house already has an owner.")
-- HOUSE_TYPE_NORMAL
if house:getType() == HOUSE_TYPE_NORMAL then
if house:getOwnerGuid() > 0 then
player:sendCancelMessage("This house already has an owner.")
return false
end

if player:getHouse() then
player:sendCancelMessage("You are already the owner of a house.")
return false
end

local price = house:getTileCount() * housePrice
if not player:removeMoney(price) then
player:sendCancelMessage("You do not have enough money.")
return false
end

house:setOwnerGuid(player:getGuid())
player:sendTextMessage(MESSAGE_INFO_DESCR, "You have successfully bought this house, be sure to have the money for the rent in the bank.")
return false
end

if player:getHouse() then
player:sendCancelMessage("You are already the owner of a house.")
-- HOUSE_TYPE_GUILDHALL
if house:getOwnerGuild() > 0 then
player:sendCancelMessage("This guildhall already has an owner.")
return false
end

local guild = player:getGuild()
if guild:getHouseId() then
player:sendCancelMessage("Your guild already owns a guildhall.")
return false
end

if guild:getOwnerGuid() ~= player:getGuid() then
player:sendCancelMessage("Only Guild Leaders can buy guildhalls.")
return false
end

local price = house:getTileCount() * housePrice
if not player:removeTotalMoney(price) then
player:sendCancelMessage("You do not have enough money.")
local balance = guild:getBankBalance()
if price > balance then
player:sendCancelMessage("Your guild bank do not have enough money, it is missing ".. price - balance .. " gold.")
return false
end

guild:setBankBalance(balance - price)
local currentTime = os.time()
local insertData = table.concat({
guild:getId(),
player:getGuid(),
"'WITHDRAW'",
"'RENT'",
price,
currentTime
},',')
db.query("INSERT INTO `guild_transactions` (`guild_id`, `player_associated`, `type`, `category`, `balance`, `time`) VALUES ("..insertData..");")

local receipt = Game.createItem(ITEM_RECEIPT_SUCCESS, 1)

receipt:setAttribute(ITEM_ATTRIBUTE_TEXT, table.concat({
"Date: " .. os.date("%d. %b %Y - %H:%M:%S", currentTime),
"Type: Guild Withdraw",
"Category: House rent",
"House: ".. house:getName(),
"Gold Amount: " .. price,
"Receipt Owner: " .. player:getName(),
"Recipient: The " .. guild:getName(),
"\nWe are happy to inform you that your transfer request was successfully carried out."
},"\n"))

player:addItemEx(receipt)

house:setOwnerGuid(player:getGuid())
player:sendTextMessage(MESSAGE_INFO_DESCR, "You have successfully bought this house, be sure to have the money for the rent in the bank.")
player:sendTextMessage(MESSAGE_INFO_DESCR, "You have successfully bought this guildhall, be sure to have the money for the rent in the guild bank.")
return false
end
8 changes: 4 additions & 4 deletions data/world/forgotten-house.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<house name="Hilltop 1" houseid="27" entryx="166" entryy="409" entryz="6" rent="0" townid="2" size="28" />
<house name="Hilltop 2" houseid="28" entryx="177" entryy="400" entryz="6" rent="0" townid="2" size="33" />
<house name="Hilltop 3" houseid="29" entryx="178" entryy="412" entryz="6" rent="0" townid="2" size="42" />
<house name="Hilltop Hall" houseid="30" entryx="193" entryy="405" entryz="5" rent="0" townid="2" size="158" />
<house name="Hilltop Hall" houseid="30" entryx="193" entryy="405" entryz="5" rent="0" guildhall="true" townid="2" size="158" />
<house name="Rhyves Flats 1" houseid="31" entryx="166" entryy="365" entryz="7" rent="0" townid="2" size="10" />
<house name="Rhyves Flats 2" houseid="32" entryx="166" entryy="361" entryz="7" rent="0" townid="2" size="10" />
<house name="Rhyves Flats 3" houseid="33" entryx="166" entryy="358" entryz="7" rent="0" townid="2" size="10" />
Expand Down Expand Up @@ -55,8 +55,8 @@
<house name="Kazgal Close6" houseid="55" entryx="215" entryy="457" entryz="12" rent="0" townid="3" size="30" />
<house name="Kazgal Close 7" houseid="56" entryx="218" entryy="454" entryz="12" rent="0" townid="3" size="30" />
<house name="Kazgal Close 8" houseid="57" entryx="224" entryy="454" entryz="12" rent="0" townid="3" size="30" />
<house name="Hammersmith Hall" houseid="58" entryx="230" entryy="439" entryz="12" rent="0" townid="3" size="164" />
<house name="Varnack's Cavern" houseid="59" entryx="253" entryy="444" entryz="12" rent="0" townid="3" size="185" />
<house name="Hammersmith Hall" houseid="58" entryx="230" entryy="439" entryz="12" rent="0" guildhall="true" townid="3" size="164" />
<house name="Varnack's Cavern" houseid="59" entryx="253" entryy="444" entryz="12" rent="0" guildhall="true" townid="3" size="185" />
<house name="Saund Street 1" houseid="60" entryx="231" entryy="550" entryz="6" rent="0" townid="5" size="10" />
<house name="Saund Street 2" houseid="61" entryx="226" entryy="550" entryz="6" rent="0" townid="5" size="14" />
<house name="Saund Street 3" houseid="62" entryx="241" entryy="554" entryz="6" rent="0" townid="5" size="10" />
Expand Down Expand Up @@ -102,7 +102,7 @@
<house name="Farm Lane 4" houseid="102" entryx="116" entryy="399" entryz="7" rent="0" townid="2" size="30" />
<house name="The Square 1" houseid="103" entryx="127" entryy="406" entryz="7" rent="0" townid="2" size="22" />
<house name="The Square 2 (Shop)" houseid="104" entryx="121" entryy="398" entryz="7" rent="0" townid="2" size="26" />
<house name="Central Hall" houseid="105" entryx="125" entryy="381" entryz="7" rent="0" townid="2" size="163" />
<house name="Central Hall" houseid="105" entryx="125" entryy="381" entryz="7" rent="0" guildhall="true" townid="2" size="163" />
<house name="The Square 3" houseid="106" entryx="136" entryy="380" entryz="7" rent="0" townid="2" size="18" />
<house name="The Square 4 (Shop)" houseid="107" entryx="143" entryy="391" entryz="7" rent="0" townid="2" size="33" />
<house name="Church Avenue 6" houseid="108" entryx="158" entryy="404" entryz="7" rent="0" townid="2" size="20" />
Expand Down
19 changes: 18 additions & 1 deletion schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,29 @@ CREATE TABLE IF NOT EXISTS `guilds` (
`name` varchar(255) NOT NULL,
`ownerid` int(11) NOT NULL,
`creationdata` int(11) NOT NULL,
`balance` bigint(20) unsigned NOT NULL DEFAULT '0',
`motd` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY (`name`),
UNIQUE KEY (`ownerid`),
FOREIGN KEY (`ownerid`) REFERENCES `players`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;

CREATE TABLE IF NOT EXISTS `guild_transactions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`guild_id` int(11) NOT NULL,
`guild_associated` int(11) DEFAULT NULL,
`player_associated` int(11) DEFAULT NULL,
`type` ENUM('DEPOSIT', 'WITHDRAW') NOT NULL,
`category` ENUM ('OTHER', 'RENT', 'MATERIAL', 'SERVICES', 'REVENUE', 'CONTRIBUTION') NOT NULL DEFAULT 'OTHER',
`balance` bigint(20) UNSIGNED NOT NULL DEFAULT '0',
`time` bigint(20) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`guild_id`) REFERENCES `guilds`(`id`) ON DELETE CASCADE,
FOREIGN KEY (`guild_associated`) REFERENCES `guilds`(`id`) ON DELETE SET NULL,
FOREIGN KEY (`player_associated`) REFERENCES `players`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS `guild_invites` (
`player_id` int(11) NOT NULL DEFAULT '0',
`guild_id` int(11) NOT NULL DEFAULT '0',
Expand Down Expand Up @@ -207,6 +223,7 @@ CREATE TABLE IF NOT EXISTS `houses` (
`highest_bidder` int(11) NOT NULL DEFAULT '0',
`size` int(11) NOT NULL DEFAULT '0',
`beds` int(11) NOT NULL DEFAULT '0',
`type` ENUM('HOUSE', 'GUILDHALL') NOT NULL DEFAULT 'HOUSE',
PRIMARY KEY (`id`),
KEY `owner` (`owner`),
KEY `town_id` (`town_id`)
Expand Down Expand Up @@ -349,7 +366,7 @@ CREATE TABLE IF NOT EXISTS `towns` (
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8;

INSERT INTO `server_config` (`config`, `value`) VALUES ('db_version', '26'), ('motd_hash', ''), ('motd_num', '0'), ('players_record', '0');
INSERT INTO `server_config` (`config`, `value`) VALUES ('db_version', '27'), ('motd_hash', ''), ('motd_num', '0'), ('players_record', '0');

DROP TRIGGER IF EXISTS `ondelete_players`;
DROP TRIGGER IF EXISTS `oncreate_guilds`;
Expand Down
2 changes: 2 additions & 0 deletions src/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ enum item_t : uint16_t {
ITEM_LETTER = 2597,
ITEM_LETTER_STAMPED = 2598,
ITEM_LABEL = 2599,
ITEM_RECEIPT_SUCCESS = 24301,
ITEM_RECEIPT_FAIL = 24302,

ITEM_AMULETOFLOSS = 2173,

Expand Down
4 changes: 4 additions & 0 deletions src/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ enum ReturnValue {
RETURNVALUE_TRADEPLAYERHIGHESTBIDDER,
RETURNVALUE_YOUCANNOTTRADETHISHOUSE,
RETURNVALUE_YOUDONTHAVEREQUIREDPROFESSION,
RETURNVALUE_TRADEPLAYERNOTINAGUILD,
RETURNVALUE_TRADEGUILDALREADYOWNSAHOUSE,
RETURNVALUE_TRADEPLAYERNOTGUILDLEADER,
RETURNVALUE_YOUARENOTGUILDLEADER,
RETURNVALUE_CANNOTMOVEITEMISNOTSTOREITEM,
RETURNVALUE_ITEMCANNOTBEMOVEDTHERE,
};
Expand Down
8 changes: 8 additions & 0 deletions src/guild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ void Guild::addRank(uint32_t rankId, const std::string& rankName, uint8_t level)
{
ranks.emplace_back(std::make_shared<GuildRank>(rankId, rankName, level));
}

void Guild::setBankBalance(uint64_t balance) {
bankBalance = balance;
Database& db = Database::getInstance();
std::ostringstream query;
query << "UPDATE `guilds` SET `balance`=" << bankBalance << " WHERE `id`=" << id;
db.executeQuery(query.str());
}
22 changes: 22 additions & 0 deletions src/guild.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ class Guild
memberCount = count;
}

uint64_t getBankBalance() const {
return bankBalance;
}
void setBankBalance(uint64_t balance);

uint32_t getHouseId() const {
return houseId;
}
void setHouseId(uint32_t id) {
houseId = id;
}

uint32_t getOwnerGUID() const {
return ownerGUID;
}
void setOwnerGUID(uint32_t guid) {
ownerGUID = guid;
}

const std::vector<GuildRank_ptr>& getRanks() const {
return ranks;
}
Expand All @@ -79,6 +98,9 @@ class Guild
std::string motd;
uint32_t id;
uint32_t memberCount = 0;
uint64_t bankBalance = 0;
uint32_t ownerGUID = 0;
uint32_t houseId = 0;
};

#endif
Loading