2023-02-18 20:10:02 -03:00
|
|
|
// Copyright 2023 The Forgotten Server Authors. All rights reserved.
|
2022-02-22 15:41:21 -03:00
|
|
|
// Use of this source code is governed by the GPL-2.0 License that can be found in the LICENSE file.
|
2013-07-07 02:30:08 +02:00
|
|
|
|
|
|
|
|
#include "otpch.h"
|
|
|
|
|
|
|
|
|
|
#include "guild.h"
|
|
|
|
|
|
|
|
|
|
#include "game.h"
|
2024-10-30 17:29:47 -03:00
|
|
|
#include "tools.h"
|
2013-07-07 02:30:08 +02:00
|
|
|
|
|
|
|
|
extern Game g_game;
|
|
|
|
|
|
2022-04-13 14:06:08 +00:00
|
|
|
void Guild::addMember(Player* player) { membersOnline.push_back(player); }
|
2013-07-07 02:30:08 +02:00
|
|
|
|
|
|
|
|
void Guild::removeMember(Player* player)
|
|
|
|
|
{
|
2013-09-24 04:21:32 +02:00
|
|
|
membersOnline.remove(player);
|
2015-07-12 22:52:33 +02:00
|
|
|
|
|
|
|
|
if (membersOnline.empty()) {
|
|
|
|
|
g_game.removeGuild(id);
|
|
|
|
|
}
|
2013-07-07 02:30:08 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 06:11:31 -03:00
|
|
|
void Guild::addRank(uint32_t rankId, std::string_view rankName, uint8_t level)
|
|
|
|
|
{
|
|
|
|
|
ranks.emplace_back(std::make_shared<GuildRank>(rankId, rankName, level));
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 02:58:19 +02:00
|
|
|
GuildRank_ptr Guild::getRankById(uint32_t rankId)
|
2013-07-07 02:30:08 +02:00
|
|
|
{
|
2020-06-09 02:58:19 +02:00
|
|
|
for (auto rank : ranks) {
|
|
|
|
|
if (rank->id == rankId) {
|
|
|
|
|
return rank;
|
2013-07-07 02:30:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-09-22 02:02:19 +02:00
|
|
|
return nullptr;
|
2013-07-07 02:30:08 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-09 02:58:19 +02:00
|
|
|
GuildRank_ptr Guild::getRankByName(const std::string& name) const
|
2017-12-18 00:18:31 -02:00
|
|
|
{
|
2020-06-09 02:58:19 +02:00
|
|
|
for (auto rank : ranks) {
|
2024-10-29 22:36:24 -04:00
|
|
|
if (caseInsensitiveEqual(rank->name, name)) {
|
2020-06-09 02:58:19 +02:00
|
|
|
return rank;
|
2017-12-18 00:18:31 -02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 02:58:19 +02:00
|
|
|
GuildRank_ptr Guild::getRankByLevel(uint8_t level) const
|
2013-07-07 02:30:08 +02:00
|
|
|
{
|
2020-06-09 02:58:19 +02:00
|
|
|
for (auto rank : ranks) {
|
|
|
|
|
if (rank->level == level) {
|
|
|
|
|
return rank;
|
2013-07-07 02:30:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-09-22 02:02:19 +02:00
|
|
|
return nullptr;
|
2013-07-07 02:30:08 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 06:11:31 -03:00
|
|
|
Guild_ptr IOGuild::loadGuild(uint32_t guildId)
|
2022-02-17 04:40:43 +01:00
|
|
|
{
|
|
|
|
|
Database& db = Database::getInstance();
|
2024-05-26 06:11:31 -03:00
|
|
|
DBResult_ptr result = db.storeQuery(fmt::format("SELECT `name` FROM `guilds` WHERE `id` = {:d}", guildId));
|
|
|
|
|
if (!result) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2022-02-17 04:40:43 +01:00
|
|
|
|
2024-05-26 06:11:31 -03:00
|
|
|
const auto& guild = std::make_shared<Guild>(guildId, result->getString("name"));
|
2024-11-01 17:09:10 +01:00
|
|
|
if ((result = db.storeQuery(
|
|
|
|
|
fmt::format("SELECT `id`, `name`, `level` FROM `guild_ranks` WHERE `guild_id` = {:d}", guildId)))) {
|
2024-05-26 06:11:31 -03:00
|
|
|
do {
|
|
|
|
|
guild->addRank(result->getNumber<uint32_t>("id"), result->getString("name"),
|
|
|
|
|
result->getNumber<uint16_t>("level"));
|
|
|
|
|
} while (result->next());
|
2022-02-17 04:40:43 +01:00
|
|
|
}
|
2024-05-26 06:11:31 -03:00
|
|
|
return guild;
|
2022-02-17 04:40:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t IOGuild::getGuildIdByName(const std::string& name)
|
|
|
|
|
{
|
|
|
|
|
Database& db = Database::getInstance();
|
|
|
|
|
|
2022-04-13 14:06:08 +00:00
|
|
|
DBResult_ptr result =
|
|
|
|
|
db.storeQuery(fmt::format("SELECT `id` FROM `guilds` WHERE `name` = {:s}", db.escapeString(name)));
|
2022-02-17 04:40:43 +01:00
|
|
|
if (!result) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return result->getNumber<uint32_t>("id");
|
|
|
|
|
}
|