AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
deserter_commandscript Class Reference
Inheritance diagram for deserter_commandscript:
CommandScript ScriptObject

Public Member Functions

 deserter_commandscript ()
 
ChatCommandTable GetCommands () const override
 Returns the command structure for the system.
 
virtual std::vector< Acore::ChatCommands::ChatCommandBuilderGetCommands () const =0
 
- Public Member Functions inherited from ScriptObject
virtual bool IsDatabaseBound () const
 
virtual bool isAfterLoadScript () const
 
virtual void checkValidity ()
 
const std::string & GetName () const
 

Static Public Member Functions

static bool HandleDeserterAdd (ChatHandler *handler, Optional< std::string > playerName, Optional< std::string > time, bool isInstance)
 Applies the Deserter Debuff to a player.
 
static bool HandleDeserterRemove (ChatHandler *handler, Optional< PlayerIdentifier > player, bool isInstance)
 Removes the Deserter Debuff from a player.
 
static bool HandleDeserterRemoveAll (ChatHandler *handler, bool isInstance, Optional< std::string > maxTime)
 Removes the Deserter Debuff from all players.
 
static bool HandleDeserterInstanceAdd (ChatHandler *handler, Optional< std::string > playerName, Optional< std::string > time)
 
static bool HandleDeserterBGAdd (ChatHandler *handler, Optional< std::string > playerName, Optional< std::string > time)
 
static bool HandleDeserterInstanceRemove (ChatHandler *handler, Optional< PlayerIdentifier > player)
 
static bool HandleDeserterBGRemove (ChatHandler *handler, Optional< PlayerIdentifier > player)
 
static bool HandleDeserterInstanceRemoveAll (ChatHandler *handler, Optional< std::string > maxTime)
 
static bool HandleDeserterBGRemoveAll (ChatHandler *handler, Optional< std::string > maxTime)
 

Additional Inherited Members

- Protected Member Functions inherited from CommandScript
 CommandScript (const char *name)
 
- Protected Member Functions inherited from ScriptObject
 ScriptObject (const char *name)
 
virtual ~ScriptObject ()=default
 

Detailed Description

Constructor & Destructor Documentation

◆ deserter_commandscript()

deserter_commandscript::deserter_commandscript ( )
inline
42: CommandScript("deserter_commandscript") { }
Definition: ScriptMgr.h:850

Member Function Documentation

◆ GetCommands()

ChatCommandTable deserter_commandscript::GetCommands ( ) const
inlineoverridevirtual

Returns the command structure for the system.

Implements CommandScript.

49 {
50 static ChatCommandTable deserterInstanceCommandTable =
51 {
52 { "add", HandleDeserterInstanceAdd, SEC_ADMINISTRATOR, Console::Yes },
53 { "remove all", HandleDeserterInstanceRemoveAll, SEC_ADMINISTRATOR, Console::Yes },
54 { "remove", HandleDeserterInstanceRemove, SEC_ADMINISTRATOR, Console::Yes }
55 };
56 static ChatCommandTable deserterBGCommandTable =
57 {
58 { "add", HandleDeserterBGAdd, SEC_ADMINISTRATOR, Console::Yes },
59 { "remove all", HandleDeserterBGRemoveAll, SEC_ADMINISTRATOR, Console::Yes },
60 { "remove", HandleDeserterBGRemove, SEC_ADMINISTRATOR, Console::Yes }
61 };
62
63 static ChatCommandTable deserterCommandTable =
64 {
65 { "instance", deserterInstanceCommandTable },
66 { "bg", deserterBGCommandTable }
67 };
68 static ChatCommandTable commandTable =
69 {
70 { "deserter", deserterCommandTable }
71 };
72 return commandTable;
73 }
@ SEC_ADMINISTRATOR
Definition: Common.h:69
std::vector< ChatCommandBuilder > ChatCommandTable
Definition: ChatCommand.h:50
static bool HandleDeserterBGAdd(ChatHandler *handler, Optional< std::string > playerName, Optional< std::string > time)
Definition: cs_deserter.cpp:412
static bool HandleDeserterInstanceRemove(ChatHandler *handler, Optional< PlayerIdentifier > player)
Definition: cs_deserter.cpp:418
static bool HandleDeserterBGRemoveAll(ChatHandler *handler, Optional< std::string > maxTime)
Definition: cs_deserter.cpp:434
static bool HandleDeserterBGRemove(ChatHandler *handler, Optional< PlayerIdentifier > player)
Definition: cs_deserter.cpp:424
static bool HandleDeserterInstanceAdd(ChatHandler *handler, Optional< std::string > playerName, Optional< std::string > time)
Definition: cs_deserter.cpp:406
static bool HandleDeserterInstanceRemoveAll(ChatHandler *handler, Optional< std::string > maxTime)
Definition: cs_deserter.cpp:429

References HandleDeserterBGAdd(), HandleDeserterBGRemove(), HandleDeserterBGRemoveAll(), HandleDeserterInstanceAdd(), HandleDeserterInstanceRemove(), HandleDeserterInstanceRemoveAll(), and SEC_ADMINISTRATOR.

◆ HandleDeserterAdd()

static bool deserter_commandscript::HandleDeserterAdd ( ChatHandler handler,
Optional< std::string >  playerName,
Optional< std::string >  time,
bool  isInstance 
)
inlinestatic

Applies the Deserter Debuff to a player.

This function applies a Deserter Debuff of the given type (Instance or BG) to the selected player, with the provided duration in seconds.

Parameters
handlerThe ChatHandler, passed by the system.
playerNamePlayer by name. Optional, defaults to selected or self.
timeThe provided duration as TimeString. Optional, defaults to bg/instance default time.
isInstanceprovided by the relaying functions, so we don't have to write that much code :)
Returns
true if everything was correct, false if an error occured.

Example Usage:

.deserter instance add 1h30m (using player target or self)
-or-
.deserter bg add 1h30m (using player target or self)
-or-
.deserter bg add Tester 1h30m (using player of name 'Tester')
99 {
100 Player* target = handler->getSelectedPlayerOrSelf();
101 ObjectGuid guid;
102
103 if (playerName)
104 {
105 if (!normalizePlayerName(*playerName))
106 {
108 handler->SetSentErrorMessage(true);
109 return false;
110 }
111
112 guid = sCharacterCache->GetCharacterGuidByName(*playerName);
113 if (guid)
114 {
115 target = ObjectAccessor::FindPlayerByName(*playerName);
116 }
117 else
118 {
119 if (time)
120 {
122 handler->SetSentErrorMessage(true);
123 return false;
124 }
125
126 time = playerName;
127 playerName = "";
128 }
129 }
130
131 if (!playerName || playerName->empty())
132 {
133 if (!handler->GetSession())
134 {
135 return false;
136 }
137
138 playerName = target->GetName();
139 guid = target->GetGUID();
140 }
141
142 if (!time)
143 {
144 time = isInstance ? "30m" : "15m";
145 }
146
147 int32 duration = TimeStringToSecs(*time);
148
149 if (duration == 0)
150 {
151 duration = Acore::StringTo<int32>(*time).value_or(0);
152 }
153
154 if (duration == 0)
155 {
157 handler->SetSentErrorMessage(true);
158 return false;
159 }
160
161 uint32 deserterSpell = isInstance ? LFG_SPELL_DUNGEON_DESERTER : BG_SPELL_DESERTER;
162
163 if (target)
164 {
165 Aura* aura = target->GetAura(deserterSpell);
166 if (aura && aura->GetDuration() >= duration * IN_MILLISECONDS)
167 {
168 handler->PSendSysMessage("Player %s already has a longer %s Deserter active.", handler->playerLink(*playerName), isInstance ? "Instance" : "Battleground");
169 return true;
170 }
171
172 aura = target->AddAura(deserterSpell, target);
173 if (!aura)
174 {
176 handler->SetSentErrorMessage(true);
177 return false;
178 }
179 aura->SetDuration(duration * IN_MILLISECONDS);
180 }
181 else
182 {
183 int32 remainTime = 0;
184 if (QueryResult result = CharacterDatabase.Query("SELECT remainTime FROM character_aura WHERE guid = {} AND spell = {}", guid.GetCounter(), deserterSpell))
185 {
186 Field* fields = result->Fetch();
187 remainTime = fields[0].Get<int32>();
188
189 if (remainTime < 0 || remainTime >= duration * IN_MILLISECONDS)
190 {
191 handler->PSendSysMessage("Player %s already has a longer %s Deserter active.", handler->playerLink(*playerName), isInstance ? "Instance" : "Battleground");
192 return true;
193 }
194 CharacterDatabase.Query("DELETE FROM character_aura WHERE guid = {} AND spell = {}", guid.GetCounter(), deserterSpell);
195 }
196
197 uint8 index = 0;
199 stmt->SetData(index++, guid.GetCounter());
200 stmt->SetData(index++, guid.GetCounter());
201 stmt->SetData(index++, 0);
202 stmt->SetData(index++, deserterSpell);
203 stmt->SetData(index++, 1);
204 stmt->SetData(index++, 1);
205 stmt->SetData(index++, 1);
206 stmt->SetData(index++, 0);
207 stmt->SetData(index++, 0);
208 stmt->SetData(index++, 0);
209 stmt->SetData(index++, 0);
210 stmt->SetData(index++, 0);
211 stmt->SetData(index++, 0);
212 stmt->SetData(index++, isInstance ? 1800000 : 900000);
213 stmt->SetData(index++, duration * IN_MILLISECONDS);
214 stmt->SetData(index, 0);
215 CharacterDatabase.Execute(stmt);
216 }
217
218 handler->PSendSysMessage("%s of %s Deserter has been added to player %s.", secsToTimeString(duration), isInstance ? "Instance" : "Battleground", handler->playerLink(*playerName));
219 return true;
220 }
constexpr auto IN_MILLISECONDS
Definition: Common.h:62
std::int32_t int32
Definition: Define.h:104
std::uint8_t uint8
Definition: Define.h:110
std::uint32_t uint32
Definition: Define.h:108
std::string secsToTimeString(uint64 timeInSecs, bool shortText)
Definition: Util.cpp:74
uint32 TimeStringToSecs(const std::string &timestring)
Definition: Util.cpp:164
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition: DatabaseEnv.cpp:21
std::shared_ptr< ResultSet > QueryResult
Definition: DatabaseEnvFwd.h:28
@ CHAR_INS_AURA
Definition: CharacterDatabase.h:196
#define sCharacterCache
Definition: CharacterCache.h:83
bool normalizePlayerName(std::string &name)
Definition: ObjectMgr.cpp:264
@ LANG_PLAYER_NOT_FOUND
Definition: Language.h:522
@ LANG_BAD_VALUE
Definition: Language.h:147
@ LFG_SPELL_DUNGEON_DESERTER
Definition: cs_deserter.cpp:35
@ BG_SPELL_DESERTER
Definition: cs_deserter.cpp:36
Player * FindPlayerByName(std::string const &name, bool checkInWorld=true)
Definition: ObjectAccessor.cpp:276
Definition: PreparedStatement.h:158
Class used to access individual fields of database query result.
Definition: Field.h:99
std::enable_if_t< std::is_arithmetic_v< T >, T > Get() const
Definition: Field.h:113
Acore::Types::is_default< T > SetData(const uint8 index, T value)
Definition: PreparedStatement.h:78
std::string playerLink(std::string const &name) const
Definition: Chat.h:111
WorldSession * GetSession()
Definition: Chat.h:122
void SetSentErrorMessage(bool val)
Definition: Chat.h:118
void PSendSysMessage(char const *fmt, Args &&... args)
Definition: Chat.h:60
Player * getSelectedPlayerOrSelf() const
Definition: Chat.cpp:345
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition: Chat.cpp:103
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:106
std::string const & GetName() const
Definition: Object.h:446
Definition: ObjectGuid.h:120
LowType GetCounter() const
Definition: ObjectGuid.h:147
Definition: Player.h:1046
Aura * GetAura(uint32 spellId, ObjectGuid casterGUID=ObjectGuid::Empty, ObjectGuid itemCasterGUID=ObjectGuid::Empty, uint8 reqEffMask=0) const
Definition: Unit.cpp:5499
Aura * AddAura(uint32 spellId, Unit *target)
Definition: Unit.cpp:19031
Definition: SpellAuras.h:87
int32 GetDuration() const
Definition: SpellAuras.h:133
void SetDuration(int32 duration, bool withMods=false)
Definition: SpellAuras.cpp:882

References Unit::AddAura(), BG_SPELL_DESERTER, CHAR_INS_AURA, CharacterDatabase, ObjectAccessor::FindPlayerByName(), Field::Get(), Unit::GetAura(), ObjectGuid::GetCounter(), Aura::GetDuration(), Object::GetGUID(), WorldObject::GetName(), ChatHandler::getSelectedPlayerOrSelf(), ChatHandler::GetSession(), IN_MILLISECONDS, LANG_BAD_VALUE, LANG_PLAYER_NOT_FOUND, LFG_SPELL_DUNGEON_DESERTER, normalizePlayerName(), ChatHandler::playerLink(), ChatHandler::PSendSysMessage(), sCharacterCache, secsToTimeString(), ChatHandler::SendSysMessage(), PreparedStatementBase::SetData(), Aura::SetDuration(), ChatHandler::SetSentErrorMessage(), and TimeStringToSecs().

Referenced by HandleDeserterBGAdd(), and HandleDeserterInstanceAdd().

◆ HandleDeserterBGAdd()

static bool deserter_commandscript::HandleDeserterBGAdd ( ChatHandler handler,
Optional< std::string >  playerName,
Optional< std::string >  time 
)
inlinestatic
See also
HandleDeserterAdd()
413 {
414 return HandleDeserterAdd(handler, playerName, time, false);
415 }
static bool HandleDeserterAdd(ChatHandler *handler, Optional< std::string > playerName, Optional< std::string > time, bool isInstance)
Applies the Deserter Debuff to a player.
Definition: cs_deserter.cpp:98

References HandleDeserterAdd().

Referenced by GetCommands().

◆ HandleDeserterBGRemove()

static bool deserter_commandscript::HandleDeserterBGRemove ( ChatHandler handler,
Optional< PlayerIdentifier player 
)
inlinestatic
See also
HandleDeserterRemove()
425 {
426 return HandleDeserterRemove(handler, player, false);
427 }
static bool HandleDeserterRemove(ChatHandler *handler, Optional< PlayerIdentifier > player, bool isInstance)
Removes the Deserter Debuff from a player.
Definition: cs_deserter.cpp:244

References HandleDeserterRemove().

Referenced by GetCommands().

◆ HandleDeserterBGRemoveAll()

static bool deserter_commandscript::HandleDeserterBGRemoveAll ( ChatHandler handler,
Optional< std::string >  maxTime 
)
inlinestatic
435 {
436 return HandleDeserterRemoveAll(handler, false, maxTime);
437 }
static bool HandleDeserterRemoveAll(ChatHandler *handler, bool isInstance, Optional< std::string > maxTime)
Removes the Deserter Debuff from all players.
Definition: cs_deserter.cpp:322

References HandleDeserterRemoveAll().

Referenced by GetCommands().

◆ HandleDeserterInstanceAdd()

static bool deserter_commandscript::HandleDeserterInstanceAdd ( ChatHandler handler,
Optional< std::string >  playerName,
Optional< std::string >  time 
)
inlinestatic
See also
HandleDeserterAdd()
407 {
408 return HandleDeserterAdd(handler, playerName, time, true);
409 }

References HandleDeserterAdd().

Referenced by GetCommands().

◆ HandleDeserterInstanceRemove()

static bool deserter_commandscript::HandleDeserterInstanceRemove ( ChatHandler handler,
Optional< PlayerIdentifier player 
)
inlinestatic
See also
HandleDeserterRemove()
419 {
420 return HandleDeserterRemove(handler, player, true);
421 }

References HandleDeserterRemove().

Referenced by GetCommands().

◆ HandleDeserterInstanceRemoveAll()

static bool deserter_commandscript::HandleDeserterInstanceRemoveAll ( ChatHandler handler,
Optional< std::string >  maxTime 
)
inlinestatic
430 {
431 return HandleDeserterRemoveAll(handler, true, maxTime);
432 }

References HandleDeserterRemoveAll().

Referenced by GetCommands().

◆ HandleDeserterRemove()

static bool deserter_commandscript::HandleDeserterRemove ( ChatHandler handler,
Optional< PlayerIdentifier player,
bool  isInstance 
)
inlinestatic

Removes the Deserter Debuff from a player.

This function removes a Deserter Debuff of the given type (Instance or BG) from the selected player.

Parameters
handlerThe ChatHandler, passed by the system.
playerThe target player, either by name, the target or self
isInstanceprovided by the relaying functions, so we don't have to write that much code :)
Returns
true if everything was correct, false if an error occured.

Example Usage:

.deserter instance remove (using player target or self)
-or-
.deserter bg remove (using player target or self)
-or-
.deserter bg remove Tester (using player of name 'Tester')
245 {
246 if (!player)
247 {
248 player = PlayerIdentifier::FromTargetOrSelf(handler);
249 }
250
251 if (!player)
252 {
254 handler->SetSentErrorMessage(true);
255 return false;
256 }
257
258 Player* target = player->GetConnectedPlayer();
259 uint32 deserterSpell = isInstance ? LFG_SPELL_DUNGEON_DESERTER : BG_SPELL_DESERTER;
260 int32 duration = 0;
261
262 if (target)
263 {
264 if (Aura* aura = target->GetAura(deserterSpell))
265 {
266 duration = aura->GetDuration();
267 target->RemoveAura(deserterSpell);
268 }
269 }
270 else
271 {
272 if (QueryResult result = CharacterDatabase.Query("SELECT remainTime FROM character_aura WHERE guid = {} AND spell = {}", player->GetGUID().GetCounter(), deserterSpell))
273 {
274 Field* fields = result->Fetch();
275 duration = fields[0].Get<int32>();
276 CharacterDatabase.Execute("DELETE FROM character_aura WHERE guid = {} AND spell = {}", player->GetGUID().GetCounter(), deserterSpell);
277 }
278 }
279
280 if (duration == 0)
281 {
282 handler->PSendSysMessage("Player %s does not have %s Deserter.", handler->playerLink(player->GetName()), isInstance ? "Instance" : "Battleground");
283 handler->SetSentErrorMessage(true);
284 return true;
285 }
286
287 if (duration < 0)
288 {
289 handler->PSendSysMessage("Permanent %s Deserter has been removed from player %s (GUID %u).", isInstance ? "Instance" : "Battleground", handler->playerLink(player->GetName()), player->GetGUID().GetCounter());
290 handler->SetSentErrorMessage(true);
291 return true;
292 }
293
294 handler->PSendSysMessage("%s of %s Deserter has been removed from player %s (GUID %u).", secsToTimeString(duration / IN_MILLISECONDS), isInstance ? "Instance" : "Battleground", handler->playerLink(player->GetName()), player->GetGUID().GetCounter());
295 return true;
296 }
@ LANG_NO_CHAR_SELECTED
Definition: Language.h:148
static Optional< PlayerIdentifier > FromTargetOrSelf(ChatHandler *handler)
Definition: ChatCommandTags.h:189
void RemoveAura(AuraApplicationMap::iterator &i, AuraRemoveMode mode=AURA_REMOVE_BY_DEFAULT)
Definition: Unit.cpp:4708

References BG_SPELL_DESERTER, CharacterDatabase, Acore::ChatCommands::PlayerIdentifier::FromTargetOrSelf(), Field::Get(), Unit::GetAura(), IN_MILLISECONDS, LANG_NO_CHAR_SELECTED, LFG_SPELL_DUNGEON_DESERTER, ChatHandler::playerLink(), ChatHandler::PSendSysMessage(), Unit::RemoveAura(), secsToTimeString(), ChatHandler::SendSysMessage(), and ChatHandler::SetSentErrorMessage().

Referenced by HandleDeserterBGRemove(), and HandleDeserterInstanceRemove().

◆ HandleDeserterRemoveAll()

static bool deserter_commandscript::HandleDeserterRemoveAll ( ChatHandler handler,
bool  isInstance,
Optional< std::string >  maxTime 
)
inlinestatic

Removes the Deserter Debuff from all players.

This function removes a Deserter Debuff of the given type (Instance or BG) from all players, online or offline.

Parameters
handlerThe ChatHandler, passed by the system.
isInstanceprovided by the relaying functions, so we don't have to write that much code :)
maxTimeOptional: The maximum remaining time of the Debuff on players to be removed. Any Player with a Deserter Debuff of this time or less will get their Debuff removed. Use -1 for any. Default: 15m for BG, 30m for Instance.
Returns
true if everything was correct, false if an error occured.

Example Usage:

.deserter bg remove all
-or-
.deserter bg remove all 30m
-or-
.deserter bg remove all -1
323 {
324 uint32 deserterSpell = isInstance ? LFG_SPELL_DUNGEON_DESERTER : BG_SPELL_DESERTER;
325 int32 remainTime = isInstance ? 1800 : 1800;
326 uint64 deserterCount = 0;
327 bool countOnline = true;
328
329 if (maxTime)
330 {
331 remainTime = TimeStringToSecs(*maxTime);
332 if (remainTime == 0)
333 {
334 remainTime = Acore::StringTo<int32>(*maxTime).value_or(0);
335 }
336 }
337
338 // Optimization. Do not execute any further functions or Queries if remainTime is 0.
339 if (remainTime == 0)
340 {
342 handler->SetSentErrorMessage(true);
343 return false;
344 }
345
346 QueryResult result;
347 if (remainTime > 0)
348 {
349 result = CharacterDatabase.Query("SELECT COUNT(guid) FROM character_aura WHERE spell = {} AND remainTime <= {}", deserterSpell, remainTime * IN_MILLISECONDS);
350 }
351 else
352 {
353 result = CharacterDatabase.Query("SELECT COUNT(guid) FROM character_aura WHERE spell = {}", deserterSpell);
354 }
355
356 if (result)
357 {
358 deserterCount = (*result)[0].Get<uint64>();
359 }
360
361 // Optimization. Only execute these if there even is a result.
362 if (deserterCount > 0)
363 {
364 countOnline = false;
365 if (remainTime > 0)
366 {
367 CharacterDatabase.Execute("DELETE FROM character_aura WHERE spell = {} AND remainTime <= {}", deserterSpell, remainTime * IN_MILLISECONDS);
368 }
369 else
370 {
371 CharacterDatabase.Execute("DELETE FROM character_aura WHERE spell = {}", deserterSpell);
372 }
373 }
374
375 std::shared_lock<std::shared_mutex> lock(*HashMapHolder<Player>::GetLock());
377 for (HashMapHolder<Player>::MapType::const_iterator itr = onlinePlayerList.begin(); itr != onlinePlayerList.end(); ++itr)
378 {
379 Player* player = itr->second;
380 Aura* aura = player->GetAura(deserterSpell);
381 if (aura && (remainTime < 0 || aura->GetDuration() <= remainTime * IN_MILLISECONDS))
382 {
383 if (countOnline)
384 deserterCount++;
385 player->RemoveAura(deserterSpell);
386 }
387 }
388
389 std::string remainTimeStr = secsToTimeString(remainTime);
390 if (remainTime < 0)
391 {
392 remainTimeStr = "infinity";
393 }
394
395 if (deserterCount == 0)
396 {
397 handler->PSendSysMessage("No player on this realm has %s Deserter with a duration of %s or less.", isInstance ? "Instance" : "Battleground", remainTimeStr);
398 return true;
399 }
400
401 handler->PSendSysMessage("%s Deserter has been removed from %u player(s) with a duration of %s or less.", isInstance ? "Instance" : "Battleground", deserterCount, remainTimeStr);
402 return true;
403 }
std::uint64_t uint64
Definition: Define.h:107
HashMapHolder< Player >::MapType const & GetPlayers()
Definition: ObjectAccessor.cpp:80
Definition: ObjectAccessor.h:45
std::unordered_map< ObjectGuid, T * > MapType
Definition: ObjectAccessor.h:51

References BG_SPELL_DESERTER, CharacterDatabase, Unit::GetAura(), ObjectAccessor::GetPlayers(), IN_MILLISECONDS, LANG_BAD_VALUE, LFG_SPELL_DUNGEON_DESERTER, ChatHandler::PSendSysMessage(), Unit::RemoveAura(), secsToTimeString(), ChatHandler::SendSysMessage(), ChatHandler::SetSentErrorMessage(), and TimeStringToSecs().

Referenced by HandleDeserterBGRemoveAll(), and HandleDeserterInstanceRemoveAll().