1/**
2 * The Forgotten Server - a free and open-source MMORPG server emulator
3 * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "otpch.h"
21
22#include "database.h"
23#include "guild.h"
24#include "ioguild.h"
25
26Guild* IOGuild::loadGuild(uint32_t guildId)
27{
28 Database& db = Database::getInstance();
29 std::ostringstream query;
30 query << "SELECT `name` FROM `guilds` WHERE `id` = " << guildId;
31 if (DBResult_ptr result = db.storeQuery(query.str())) {
32 Guild* guild = new Guild(guildId, result->getString("name"));
33
34 query.str(std::string());
35 query << "SELECT `id`, `name`, `level` FROM `guild_ranks` WHERE `guild_id` = " << guildId;
36
37 if ((result = db.storeQuery(query.str()))) {
38 do {
39 guild->addRank(result->getNumber<uint32_t>("id"), result->getString("name"), result->getNumber<uint16_t>("level"));
40 } while (result->next());
41 }
42 return guild;
43 }
44 return nullptr;
45}
46
47uint32_t IOGuild::getGuildIdByName(const std::string& name)
48{
49 Database& db = Database::getInstance();
50
51 std::ostringstream query;
52 query << "SELECT `id` FROM `guilds` WHERE `name` = " << db.escapeString(name);
53
54 DBResult_ptr result = db.storeQuery(query.str());
55 if (!result) {
56 return 0;
57 }
58 return result->getNumber<uint32_t>("id");
59}
60
61void IOGuild::getWarList(uint32_t guildId, GuildWarVector& guildWarVector)
62{
63 std::ostringstream query;
64 query << "SELECT `guild1`, `guild2` FROM `guild_wars` WHERE (`guild1` = " << guildId << " OR `guild2` = " << guildId << ") AND `ended` = 0 AND `status` = 1";
65
66 DBResult_ptr result = Database::getInstance().storeQuery(query.str());
67 if (!result) {
68 return;
69 }
70
71 do {
72 uint32_t guild1 = result->getNumber<uint32_t>("guild1");
73 if (guildId != guild1) {
74 guildWarVector.push_back(guild1);
75 } else {
76 guildWarVector.push_back(result->getNumber<uint32_t>("guild2"));
77 }
78 } while (result->next());
79}
80