-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcustom_rates.cpp
More file actions
347 lines (306 loc) · 10.7 KB
/
custom_rates.cpp
File metadata and controls
347 lines (306 loc) · 10.7 KB
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
340
341
342
343
344
345
346
347
#include "ScriptMgr.h"
#include "Chat.h"
#include "Language.h"
#include "CharacterDatabase.h"
#include "DatabaseEnv.h"
#include "World.h"
#include "RBAC.h"
#include "WorldSession.h"
#include "Player.h"
#include "World.h"
class CustomRates
{
private:
static int32 GetRateFromDB(const Player *player, CharacterDatabaseStatements statement)
{
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(statement);
stmt->setUInt32(0, player->GetGUID());
PreparedQueryResult result = CharacterDatabase.Query(stmt);
if (result)
return static_cast<int32>((*result)[0].GetUInt32());
return -1;
}
static void SaveRateToDB(const Player *player, uint32 rate, bool update, CharacterDatabaseStatements uStmt, CharacterDatabaseStatements iStmt)
{
if (update)
{
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(uStmt);
stmt->setUInt32(0, rate);
stmt->setUInt32(1, player->GetGUID());
CharacterDatabase.Execute(stmt);
}
else
{
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(iStmt);
stmt->setUInt32(0, player->GetGUID());
stmt->setUInt32(1, rate);
CharacterDatabase.Execute(stmt);
}
}
public:
static void DeleteRateFromDB(uint64 guid, CharacterDatabaseStatements statement)
{
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(statement);
//stmt->setUInt32(0, GUID_LOPART(guid));
CharacterDatabase.Execute(stmt);
}
static int32 GetXpRateFromDB(const Player *player)
{
return GetRateFromDB(player, CHAR_SEL_INDIVIDUAL_XP_RATE);
}
static int32 GetLootRateFromDB(const Player *player)
{
return GetRateFromDB(player, CHAR_SEL_INDIVIDUAL_LOOT_RATE);
}
static void SaveXpRateToDB(const Player *player, uint32 rate, bool update)
{
SaveRateToDB(player, rate, update, CHAR_UPD_INDIVIDUAL_XP_RATE, CHAR_INS_INDIVIDUAL_XP_RATE);
}
static void SaveLootRateToDB(const Player *player, uint32 rate, bool update)
{
SaveRateToDB(player, rate, update, CHAR_UPD_INDIVIDUAL_LOOT_RATE, CHAR_INS_INDIVIDUAL_LOOT_RATE);
}
};
class add_del_rates : public PlayerScript
{
public:
add_del_rates() : PlayerScript("add_del_rates") { }
void OnDelete(ObjectGuid guid, uint32 /*accountId*/)
{
CustomRates::DeleteRateFromDB(guid, CHAR_DEL_INDIVIDUAL_XP_RATE);
CustomRates::DeleteRateFromDB(guid, CHAR_DEL_INDIVIDUAL_LOOT_RATE);
}
void OnLogin(Player* player, bool firstLogin)
{
// show custom XP rate on login
int32 rate = CustomRates::GetXpRateFromDB(player);
if (rate != -1 && player->GetLevel() != sWorld->getIntConfig(CONFIG_MAX_PLAYER_LEVEL))
{
uint32 uRate = static_cast<uint32>(rate);
player->SetCustomXpRate(uRate);
if (sWorld->getBoolConfig(CONFIG_PLAYER_INDIVIDUAL_XP_RATE_SHOW_ON_LOGIN))
{
if (uRate)
ChatHandler(player->GetSession()).PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: Your XP rate was set to %u.", uRate);
else
ChatHandler(player->GetSession()).SendSysMessage("|CFF7BBEF7[Custom Rates]|r: Your XP rate was set to 0. You won't gain any XP anymore.");
}
}
// show custom loot rate on login
rate = CustomRates::GetLootRateFromDB(player);
if (rate != -1)
{
uint32 uRate = static_cast<uint32>(rate);
player->SetCustomLootRate(uRate);
if (sWorld->getBoolConfig(CONFIG_PLAYER_INDIVIDUAL_LOOT_RATE_SHOW_ON_LOGIN))
{
if (uRate)
ChatHandler(player->GetSession()).PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: Your loot rate was set to %u.", uRate);
else
ChatHandler(player->GetSession()).SendSysMessage("|CFF7BBEF7[Custom Rates]|r: Your loot rate was set to 0. You won't be able to loot anything.");
}
}
}
};
class custom_rate_commands : public CommandScript
{
private:
public:
custom_rate_commands() : CommandScript("custom_rate_commands") {}
std::vector<ChatCommand> GetCommands() const override
{
static std::vector<ChatCommand> rateCommandTable =
{
{ "xp", rbac::RBAC_PERM_COMMAND_XP_RATE, false, &HandleRateXpCommand, "" },
{ "loot", rbac::RBAC_PERM_COMMAND_LOOT_RATE, false, &HandleRateLootCommand, "" },
};
static std::vector<ChatCommand> commandTable =
{
{ "rate", rbac::RBAC_PERM_COMMAND_RATE, false, NULL, "", rateCommandTable },
};
return commandTable;
}
static bool HandleRateXpCommand(ChatHandler *handler, const char *args)
{
// take a pointer to the player who uses the command
Player *me = handler->GetSession() ? handler->GetSession()->GetPlayer() : NULL;
if (!me)
return false;
if (sWorld->getIntConfig(CONFIG_CUSTOM_RATE_LOOT_ENABLED) == 0)
{
handler->PSendSysMessage("Custom Rate xp is disabled");
handler->SetSentErrorMessage(true);
me->SetCustomXpRate(1);
return false;
}
// already at max level, no point in using the command at all
if (me->GetLevel() == sWorld->getIntConfig(CONFIG_CUSTOM_XP_LEVEL))
{
handler->SendSysMessage("|CFF7BBEF7[Custom Rates]|r: You are already at maximum level.");
return true;
}
// no arguments, show current XP rate
if (!*args)
{
handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: Your current XP rate is %u.", me->GetCustomXpRate());
return true;
}
// first, check if I can use the command
if (me->GetSession()->GetSecurity() < (int)sWorld->getIntConfig(CONFIG_PLAYER_INDIVIDUAL_XP_RATE_SECURITY))
{
handler->SendSysMessage(LANG_YOURS_SECURITY_IS_LOW);
handler->SetSentErrorMessage(true);
return false;
}
// take a pointer to player's selection
Player *target = handler->getSelectedPlayer();
if (!target || !target->GetSession())
{
handler->SendSysMessage(LANG_NO_CHAR_SELECTED);
handler->SetSentErrorMessage(true);
return false;
}
// extract value
int rate = atoi((char *)args);
int maxRate = sWorld->getIntConfig(CONFIG_PLAYER_MAXIMUM_INDIVIDUAL_XP_RATE);
if (rate < 0 || rate > maxRate)
{
handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: Invalid rate specified, must be in interval [0,%i].", maxRate);
handler->SetSentErrorMessage(true);
return false;
}
// take a pointer to the player we need to set xp rate to
// can be either player itself, or his selection, if
// selection security is lower than his security
Player *player = NULL;
if (target == me)
player = me;
else
{
if (me->GetSession()->GetSecurity() > target->GetSession()->GetSecurity())
player = target;
else
{
handler->SendSysMessage(LANG_YOURS_SECURITY_IS_LOW);
handler->SetSentErrorMessage(true);
return false;
}
}
// set player custom XP rate and save it in DB for later use
uint32 uRate = static_cast<uint32>(rate);
player->SetCustomXpRate(uRate);
int32 rateFromDB = CustomRates::GetXpRateFromDB(player);
if (rateFromDB == -1)
CustomRates::SaveXpRateToDB(player, uRate, false);
else
CustomRates::SaveXpRateToDB(player, uRate, true);
// show a message indicating custom XP rate change
if (player == me)
{
if (uRate == 0)
handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: You have set your XP rate to 0. You won't gain any XP anymore.");
else
handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: You have set your XP rate to %u.", uRate);
}
else
{
handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: You have set %s's XP rate to %u.", handler->GetNameLink(player).c_str(), uRate);
ChatHandler(player->GetSession()).PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: %s has set your XP rate to %u.", handler->GetNameLink().c_str(), uRate);
}
return true;
}
static bool HandleRateLootCommand(ChatHandler *handler, const char *args)
{
// take a pointer to the player who uses the command
Player *me = handler->GetSession() ? handler->GetSession()->GetPlayer() : NULL;
if (!me)
return false;
// already at max level, no point in using the command at all
if (me->GetLevel() == sWorld->getIntConfig(CONFIG_CUSTOM_LOOT_LEVEL))
{
handler->SendSysMessage("|CFF7BBEF7[Custom Rates]|r: You are already at maximum level.");
return true;
}
if (sWorld->getIntConfig(CONFIG_CUSTOM_RATE_XP_ENABLED) == 0)
{
handler->PSendSysMessage("Custom Rate loot is disabled");
handler->SetSentErrorMessage(true);
me->SetCustomLootRate(1);
return false;
}
// no arguments, show current loot rate
if (!*args)
{
handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: Your current loot rate is %u.", me->GetCustomLootRate());
return true;
}
// first, check if I can use the command
if (me->GetSession()->GetSecurity() < (int)sWorld->getIntConfig(CONFIG_PLAYER_INDIVIDUAL_LOOT_RATE_SECURITY))
{
handler->SendSysMessage(LANG_YOURS_SECURITY_IS_LOW);
handler->SetSentErrorMessage(true);
return false;
}
// take a pointer to player's selection
Player *target = handler->getSelectedPlayer();
if (!target || !target->GetSession())
{
handler->SendSysMessage(LANG_NO_CHAR_SELECTED);
handler->SetSentErrorMessage(true);
return false;
}
// extract value
int rate = atoi((char *)args);
int maxRate = sWorld->getIntConfig(CONFIG_PLAYER_MAXIMUM_INDIVIDUAL_LOOT_RATE);
if (rate < 0 || rate > maxRate)
{
handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: Invalid rate specified, must be in interval [0,%i].", maxRate);
handler->SetSentErrorMessage(true);
return false;
}
// take a pointer to the player we need to set xp rate to
// can be either player itself, or his selection, if
// selection security is lower than his security
Player *player = NULL;
if (target == me)
player = me;
else
{
if (me->GetSession()->GetSecurity() > target->GetSession()->GetSecurity())
player = target;
else
{
handler->SendSysMessage(LANG_YOURS_SECURITY_IS_LOW);
handler->SetSentErrorMessage(true);
return false;
}
}
// set player custom loot rate and save it in DB for later use
uint32 uRate = static_cast<uint32>(rate);
player->SetCustomLootRate(uRate);
int32 rateFromDB = CustomRates::GetLootRateFromDB(player);
if (rateFromDB == -1)
CustomRates::SaveLootRateToDB(player, uRate, false);
else
CustomRates::SaveLootRateToDB(player, uRate, true);
// show a message indicating custom XP rate change
if (player == me)
{
if (uRate == 0)
handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: You have set your loot rate to 0. You won't gain any XP anymore.");
else
handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: You have set your loot rate to %u.", uRate);
}
else
{
handler->PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: You have set %s's loot rate to %u.", handler->GetNameLink(player).c_str(), uRate);
ChatHandler(player->GetSession()).PSendSysMessage("|CFF7BBEF7[Custom Rates]|r: %s has set your loot rate to %u.", handler->GetNameLink().c_str(), uRate);
}
return true;
}
};
void Add_SC_Custom_Rates()
{
new add_del_rates();
new custom_rate_commands();
}