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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Chat.h"
#include "CommandScript.h"
#include "Guild.h"
#include "GuildMgr.h"
using namespace Acore::ChatCommands;
class guild_commandscript : public CommandScript
{
public:
guild_commandscript() : CommandScript("guild_commandscript") { }
ChatCommandTable GetCommands() const override
{
static ChatCommandTable guildCommandTable =
{
{ "create", HandleGuildCreateCommand, SEC_GAMEMASTER, Console::Yes },
{ "delete", HandleGuildDeleteCommand, SEC_GAMEMASTER, Console::Yes },
{ "invite", HandleGuildInviteCommand, SEC_GAMEMASTER, Console::Yes },
{ "uninvite", HandleGuildUninviteCommand, SEC_GAMEMASTER, Console::Yes },
{ "rank", HandleGuildRankCommand, SEC_GAMEMASTER, Console::Yes },
{ "rename", HandleGuildRenameCommand, SEC_GAMEMASTER, Console::Yes },
{ "info", HandleGuildInfoCommand, SEC_GAMEMASTER, Console::Yes }
};
static ChatCommandTable commandTable =
{
{ "guild", guildCommandTable }
};
return commandTable;
}
static bool HandleGuildCreateCommand(ChatHandler* handler, Optional<PlayerIdentifier> target, QuotedString guildName)
{
if (!target)
{
target = PlayerIdentifier::FromTargetOrSelf(handler);
}
if (!target || !target->IsConnected())
{
handler->SendErrorMessage(LANG_PLAYER_NOT_FOUND);
return false;
}
if (guildName.empty())
{
return false;
}
Player* playerTarget = target->GetConnectedPlayer();
if (playerTarget->GetGuildId())
{
handler->SendErrorMessage(LANG_PLAYER_IN_GUILD);
return false;
}
if (sGuildMgr->GetGuildByName(guildName))
{
handler->SendErrorMessage(LANG_GUILD_RENAME_ALREADY_EXISTS, guildName);
return false;
}
if (!sObjectMgr->IsValidCharterName(guildName))
{
handler->SendErrorMessage(LANG_BAD_VALUE);
return false;
}
Guild* guild = new Guild;
if (!guild->Create(playerTarget, guildName))
{
delete guild;
handler->SendErrorMessage(LANG_GUILD_NOT_CREATED);
return false;
}
sGuildMgr->AddGuild(guild);
return true;
}
static bool HandleGuildDeleteCommand(ChatHandler*, QuotedString guildName)
{
if (guildName.empty())
{
return false;
}
Guild* targetGuild = sGuildMgr->GetGuildByName(guildName);
if (!targetGuild)
return false;
targetGuild->Disband();
delete targetGuild;
return true;
}
static bool HandleGuildInviteCommand(ChatHandler* handler, Optional<PlayerIdentifier> target, QuotedString guildName)
{
if (!target)
{
target = PlayerIdentifier::FromTargetOrSelf(handler);
}
if (!target)
{
return false;
}
if (guildName.empty())
{
return false;
}
Guild* targetGuild = sGuildMgr->GetGuildByName(guildName);
if (!targetGuild)
return false;
// player's guild membership checked in AddMember before add
return targetGuild->AddMember(target->GetGUID());
}
static bool HandleGuildUninviteCommand(ChatHandler* handler, Optional<PlayerIdentifier> target)
{
if (!target)
{
target = PlayerIdentifier::FromTargetOrSelf(handler);
}
if (!target)
{
return false;
}
Player* playerTarget = target->GetConnectedPlayer();
uint32 guildId = playerTarget ? playerTarget->GetGuildId() : sCharacterCache->GetCharacterGuildIdByGuid(target->GetGUID());
if (!guildId)
return false;
Guild* targetGuild = sGuildMgr->GetGuildById(guildId);
if (!targetGuild)
return false;
targetGuild->DeleteMember(target->GetGUID(), false, true, true);
return true;
}
static bool HandleGuildRankCommand(ChatHandler* handler, Optional<PlayerIdentifier> player, uint8 rank)
{
if (!player)
player = PlayerIdentifier::FromTargetOrSelf(handler);
if (!player)
return false;
uint32 guildId = player->IsConnected() ? player->GetConnectedPlayer()->GetGuildId() : sCharacterCache->GetCharacterGuildIdByGuid(player->GetGUID());
if (!guildId)
return false;
Guild* targetGuild = sGuildMgr->GetGuildById(guildId);
if (!targetGuild)
return false;
return targetGuild->ChangeMemberRank(player->GetGUID(), rank);
}
static bool HandleGuildRenameCommand(ChatHandler* handler, QuotedString oldGuildStr, QuotedString newGuildStr)
{
if (oldGuildStr.empty() || newGuildStr.empty())
{
return false;
}
Guild* guild = sGuildMgr->GetGuildByName(oldGuildStr);
if (!guild)
{
handler->SendErrorMessage(LANG_COMMAND_COULDNOTFIND, oldGuildStr);
return false;
}
if (sGuildMgr->GetGuildByName(newGuildStr))
{
handler->SendErrorMessage(LANG_GUILD_RENAME_ALREADY_EXISTS, newGuildStr);
return false;
}
if (!guild->SetName(newGuildStr))
{
handler->SendErrorMessage(LANG_BAD_VALUE);
return false;
}
handler->PSendSysMessage(LANG_GUILD_RENAME_DONE, oldGuildStr, newGuildStr);
return true;
}
static bool HandleGuildInfoCommand(ChatHandler* handler, Optional<Variant<ObjectGuid::LowType, QuotedString>> const& guildIdentifier)
{
Guild* guild = nullptr;
if (guildIdentifier)
{
if (ObjectGuid::LowType const* guid = std::get_if<ObjectGuid::LowType>(&*guildIdentifier))
guild = sGuildMgr->GetGuildById(*guid);
else
guild = sGuildMgr->GetGuildByName(guildIdentifier->get<QuotedString>());
}
else if (Optional<PlayerIdentifier> target = PlayerIdentifier::FromTargetOrSelf(handler); target && target->IsConnected())
guild = target->GetConnectedPlayer()->GetGuild();
if (!guild)
return false;
// Display Guild Information
handler->PSendSysMessage(LANG_GUILD_INFO_NAME, guild->GetName(), guild->GetId()); // Guild Id + Name
std::string guildMasterName;
if (sCharacterCache->GetCharacterNameByGuid(guild->GetLeaderGUID(), guildMasterName))
handler->PSendSysMessage(LANG_GUILD_INFO_GUILD_MASTER, guildMasterName, guild->GetLeaderGUID().ToString()); // Guild Master
// Format creation date
char createdDateStr[20];
time_t createdDate = guild->GetCreatedDate();
tm localTm;
strftime(createdDateStr, 20, "%Y-%m-%d %H:%M:%S", localtime_r(&createdDate, &localTm));
handler->PSendSysMessage(LANG_GUILD_INFO_CREATION_DATE, createdDateStr); // Creation Date
handler->PSendSysMessage(LANG_GUILD_INFO_MEMBER_COUNT, guild->GetMemberCount()); // Number of Members
handler->PSendSysMessage(LANG_GUILD_INFO_BANK_GOLD, guild->GetTotalBankMoney() / 100 / 100); // Bank Gold (in gold coins)
handler->PSendSysMessage(LANG_GUILD_INFO_MOTD, guild->GetMOTD()); // Message of the Day
handler->PSendSysMessage(LANG_GUILD_INFO_EXTRA_INFO, guild->GetInfo()); // Extra Information
QueryResult result = CharacterDatabase.Query("SELECT rid, rname FROM guild_rank WHERE guildid = {}", guild->GetId());
if (result)
{
handler->PSendSysMessage(LANG_GUILD_INFO_RANKS);
do
{
Field* fields = result->Fetch();
uint32 rid = fields[0].Get<uint32>();
std::string rname = fields[1].Get<std::string>();
handler->PSendSysMessage(LANG_GUILD_INFO_RANKS_LIST, rid, rname);
} while (result->NextRow());
}
return true;
}
};
void AddSC_guild_commandscript()
{
new guild_commandscript();
}
|