-
Notifications
You must be signed in to change notification settings - Fork 31
/
guildmaster.cpp
339 lines (284 loc) · 8.44 KB
/
guildmaster.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "ScriptPCH.h"
//extern DatabaseMysql SD2Database;
extern WorldDatabaseWorkerPool WorldDatabase;
#define MSG_GOSSIP_TELE "Teleport to GuildHouse"
#define MSG_GOSSIP_BUY "Buy GuildHouse (1000 gold)"
#define MSG_GOSSIP_SELL "Sell GuildHouse (500 gold)"
#define MSG_GOSSIP_NEXTPAGE "Next -->"
#define MSG_INCOMBAT "You are in combat and cannot be teleported to your GuildHouse."
#define MSG_NOGUILDHOUSE "Your guild currently does not own a GuildHouse."
#define MSG_NOFREEGH "Unfortunately, all GuildHouses are in use."
#define MSG_ALREADYHAVEGH "Sorry, but you already own a GuildHouse (%s)."
#define MSG_NOTENOUGHMONEY "You do not have the %u gold required to purchase a GuildHouse."
#define MSG_GHOCCUPIED "This GuildHouse is unavailable for purchase as it is currently in use."
#define MSG_CONGRATULATIONS "Congratulations! You have successfully purchased a GuildHouse."
#define MSG_SOLD "You have sold your GuildHouse and have received %u gold."
#define MSG_NOTINGUILD "You need to be in a guild before you can use a GuildHouse."
#define CODE_SELL "SELL"
#define MSG_CODEBOX_SELL "Type \"" CODE_SELL "\" into the field to confirm that you want to sell your GuildHouse."
#define OFFSET_GH_ID_TO_ACTION 1500
#define OFFSET_SHOWBUY_FROM 10000
#define ACTION_TELE 1001
#define ACTION_SHOW_BUYLIST 1002 //deprecated. Use (OFFSET_SHOWBUY_FROM + 0) instead
#define ACTION_SELL_GUILDHOUSE 1003
#define ICON_GOSSIP_BALOON 0
#define ICON_GOSSIP_WING 2
#define ICON_GOSSIP_BOOK 3
#define ICON_GOSSIP_WHEEL1 4
#define ICON_GOSSIP_WHEEL2 5
#define ICON_GOSSIP_GOLD 6
#define ICON_GOSSIP_BALOONDOTS 7
#define ICON_GOSSIP_TABARD 8
#define ICON_GOSSIP_XSWORDS 9
#define COST_GH_BUY 10000000 //1000 g.
#define COST_GH_SELL 5000000 //500 g.
#define GOSSIP_COUNT_MAX 10
bool isPlayerGuildLeader(Player *player)
{
return (player->GetRank() == 0) && (player->GetGuildId() != 0);
}
bool getGuildHouseCoords(uint32 guildId, float &x, float &y, float &z, uint32 &map)
{
if (guildId == 0)
{
//if player has no guild
return false;
}
QueryResult result;
result = WorldDatabase.PQuery("SELECT `x`, `y`, `z`, `map` FROM `guildhouses` WHERE `guildId` = %u", guildId);
if (result)
{
Field *fields = result->Fetch();
x = fields[0].GetFloat();
y = fields[1].GetFloat();
z = fields[2].GetFloat();
map = fields[3].GetUInt32();
return true;
}
return false;
}
void teleportPlayerToGuildHouse(Player *player, Creature *_creature)
{
if (player->GetGuildId() == 0)
{
//if player has no guild
_creature->MonsterWhisper(MSG_NOTINGUILD, player->GetGUID());
return;
}
if (!player->getAttackers().empty())
{
//if player in combat
_creature->MonsterSay(MSG_INCOMBAT, LANG_UNIVERSAL, player->GetGUID());
return;
}
float x, y, z;
uint32 map;
if (getGuildHouseCoords(player->GetGuildId(), x, y, z, map))
{
//teleport player to the specified location
player->TeleportTo(map, x, y, z, 0.0f);
}
else
_creature->MonsterWhisper(MSG_NOGUILDHOUSE, player->GetGUID());
}
bool showBuyList(Player *player, Creature *_creature, uint32 showFromId = 0)
{
//show not occupied guildhouses
QueryResult result;
result = WorldDatabase.PQuery("SELECT `id`, `comment` FROM `guildhouses` WHERE `guildId` = 0 AND `id` > %u ORDER BY `id` ASC LIMIT %u",
showFromId, GOSSIP_COUNT_MAX);
if (result)
{
uint32 guildhouseId = 0;
std::string comment = "";
do
{
Field *fields = result->Fetch();
guildhouseId = fields[0].GetInt32();
comment = fields[1].GetString();
//send comment as a gossip item
//transmit guildhouseId in Action variable
player->ADD_GOSSIP_ITEM(ICON_GOSSIP_TABARD, comment, GOSSIP_SENDER_MAIN,
guildhouseId + OFFSET_GH_ID_TO_ACTION);
}
while (result->NextRow());
if (result->GetRowCount() == GOSSIP_COUNT_MAX)
{
//assume that we have additional page
//add link to next GOSSIP_COUNT_MAX items
player->ADD_GOSSIP_ITEM(ICON_GOSSIP_BALOONDOTS, MSG_GOSSIP_NEXTPAGE, GOSSIP_SENDER_MAIN,
guildhouseId + OFFSET_SHOWBUY_FROM);
}
player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, _creature->GetGUID());
return true;
}
else
{
if (showFromId = 0)
{
//all guildhouses are occupied
_creature->MonsterWhisper(MSG_NOFREEGH, player->GetGUID());
player->CLOSE_GOSSIP_MENU();
}
else
{
//this condition occurs when COUNT(guildhouses) % GOSSIP_COUNT_MAX == 0
//just show GHs from beginning
showBuyList(player, _creature, 0);
}
}
return false;
}
bool isPlayerHasGuildhouse(Player *player, Creature *_creature, bool whisper = false)
{
QueryResult result;
result = WorldDatabase.PQuery("SELECT `comment` FROM `guildhouses` WHERE `guildId` = %u",
player->GetGuildId());
if (result)
{
if (whisper)
{
//whisper to player "already have etc..."
Field *fields = result->Fetch();
char msg[100];
sprintf(msg, MSG_ALREADYHAVEGH, fields[0].GetString());
_creature->MonsterWhisper(msg, player->GetGUID());
}
return true;
}
return false;
}
void buyGuildhouse(Player *player, Creature *_creature, uint32 guildhouseId)
{
if (player->GetMoney() < COST_GH_BUY)
{
//show how much money player need to buy GH (in gold)
char msg[100];
sprintf(msg, MSG_NOTENOUGHMONEY, COST_GH_BUY / 10000);
_creature->MonsterWhisper(msg, player->GetGUID());
return;
}
if (isPlayerHasGuildhouse(player, _creature, true))
{
//player already have GH
return;
}
QueryResult result;
//check if somebody already occupied this GH
result = WorldDatabase.PQuery("SELECT `id` FROM `guildhouses` WHERE `id` = %u AND `guildId` <> 0", guildhouseId);
if (result)
{
_creature->MonsterWhisper(MSG_GHOCCUPIED, player->GetGUID());
return;
}
//update DB
result = WorldDatabase.PQuery("UPDATE `guildhouses` SET `guildId` = %u WHERE `id` = %u",
player->GetGuildId(), guildhouseId);
if (result)
player->ModifyMoney(-COST_GH_BUY);
_creature->MonsterSay(MSG_CONGRATULATIONS, LANG_UNIVERSAL, player->GetGUID());
}
void sellGuildhouse(Player *player, Creature *_creature)
{
if (isPlayerHasGuildhouse(player, _creature))
{
QueryResult result;
result = WorldDatabase.PQuery("UPDATE `guildhouses` SET `guildId` = 0 WHERE `guildId` = %u",
player->GetGuildId());
if (result)
player->ModifyMoney(COST_GH_SELL);
//display message e.g. "here your money etc."
char msg[100];
sprintf(msg, MSG_SOLD, COST_GH_SELL / 10000);
_creature->MonsterWhisper(msg, player->GetGUID());
}
}
class guildmaster : public CreatureScript
{
public:
guildmaster() : CreatureScript("guildmaster") { }
bool GossipSelectWithCode(Player *player, Creature *_creature, uint32 sender, uint32 action, const char* sCode)
{
if (sender == GOSSIP_SENDER_MAIN)
{
if (action == ACTION_SELL_GUILDHOUSE)
{
int i = -1;
try
{
//compare code
if (strlen(sCode) + 1 == sizeof CODE_SELL)
i = strcmp(CODE_SELL, sCode);
}
catch(char *str) {sLog->outErrorDb(str);
}
if (i == 0)
{
//right code
sellGuildhouse(player, _creature);
}
player->CLOSE_GOSSIP_MENU();
return true;
}
}
return false;
}
bool OnGossipSelect(Player *player, Creature *_creature, uint32 sender, uint32 action)
{
player->PlayerTalkClass->ClearMenus();
if (sender != GOSSIP_SENDER_MAIN)
return false;
switch (action)
{
case ACTION_TELE:
//teleport player to GH
player->CLOSE_GOSSIP_MENU();
teleportPlayerToGuildHouse(player, _creature);
break;
case ACTION_SHOW_BUYLIST:
//show list of GHs which currently not occupied
showBuyList(player, _creature);
break;
default:
if (action > OFFSET_SHOWBUY_FROM)
{
showBuyList(player, _creature, action - OFFSET_SHOWBUY_FROM);
}
else if (action > OFFSET_GH_ID_TO_ACTION)
{
//player clicked on buy list
player->CLOSE_GOSSIP_MENU();
//get guildhouseId from action
//guildhouseId = action - OFFSET_GH_ID_TO_ACTION
buyGuildhouse(player, _creature, action - OFFSET_GH_ID_TO_ACTION);
}
break;
}
return true;
}
bool OnGossipHello(Player *player, Creature *_creature)
{
player->ADD_GOSSIP_ITEM(ICON_GOSSIP_BALOON, MSG_GOSSIP_TELE,
GOSSIP_SENDER_MAIN, ACTION_TELE);
if (isPlayerGuildLeader(player))
{
if (isPlayerHasGuildhouse(player, _creature))
{
//and additional for guildhouse owner (Removed :
player->PlayerTalkClass->GetGossipMenu().AddMenuItem(ICON_GOSSIP_GOLD, 0, MSG_GOSSIP_SELL, GOSSIP_SENDER_MAIN, ACTION_SELL_GUILDHOUSE, MSG_CODEBOX_SELL, 0, true);
}
else
{
//show additional menu for guild leader
player->ADD_GOSSIP_ITEM(ICON_GOSSIP_GOLD, MSG_GOSSIP_BUY,
GOSSIP_SENDER_MAIN, ACTION_SHOW_BUYLIST);
}
}
player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, _creature->GetGUID());
return true;
}
};
void AddSC_guildmaster()
{
new guildmaster();
}