forked from otland/forgottenserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguild.cpp
87 lines (72 loc) · 2 KB
/
guild.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2023 The Forgotten Server Authors. All rights reserved.
// Use of this source code is governed by the GPL-2.0 License that can be found in the LICENSE file.
#include "otpch.h"
#include "guild.h"
#include "game.h"
extern Game g_game;
void Guild::addMember(Player* player) { membersOnline.push_back(player); }
void Guild::removeMember(Player* player)
{
membersOnline.remove(player);
if (membersOnline.empty()) {
g_game.removeGuild(id);
delete this;
}
}
GuildRank_ptr Guild::getRankById(uint32_t rankId)
{
for (auto rank : ranks) {
if (rank->id == rankId) {
return rank;
}
}
return nullptr;
}
GuildRank_ptr Guild::getRankByName(const std::string& name) const
{
for (auto rank : ranks) {
if (rank->name == name) {
return rank;
}
}
return nullptr;
}
GuildRank_ptr Guild::getRankByLevel(uint8_t level) const
{
for (auto rank : ranks) {
if (rank->level == level) {
return rank;
}
}
return nullptr;
}
void Guild::addRank(uint32_t rankId, std::string_view rankName, uint8_t level)
{
ranks.emplace_back(std::make_shared<GuildRank>(rankId, rankName, level));
}
Guild* IOGuild::loadGuild(uint32_t guildId)
{
Database& db = Database::getInstance();
if (DBResult_ptr result = db.storeQuery(fmt::format("SELECT `name` FROM `guilds` WHERE `id` = {:d}", guildId))) {
Guild* guild = new Guild(guildId, result->getString("name"));
if ((result = db.storeQuery(
fmt::format("SELECT `id`, `name`, `level` FROM `guild_ranks` WHERE `guild_id` = {:d}", guildId)))) {
do {
guild->addRank(result->getNumber<uint32_t>("id"), result->getString("name"),
result->getNumber<uint16_t>("level"));
} while (result->next());
}
return guild;
}
return nullptr;
}
uint32_t IOGuild::getGuildIdByName(const std::string& name)
{
Database& db = Database::getInstance();
DBResult_ptr result =
db.storeQuery(fmt::format("SELECT `id` FROM `guilds` WHERE `name` = {:s}", db.escapeString(name)));
if (!result) {
return 0;
}
return result->getNumber<uint32_t>("id");
}