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.
 
- Public Member Functions inherited from ScriptObject
virtual bool IsDatabaseBound () const
 
virtual bool isAfterLoadScript () const
 
virtual void checkValidity ()
 
const std::string & GetName () const
 
uint16 GetTotalAvailableHooks ()
 

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, uint16 totalAvailableHooks=0)
 
virtual ~ScriptObject ()=default
 

Detailed Description

Constructor & Destructor Documentation

◆ deserter_commandscript()

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

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:60
static bool HandleDeserterBGAdd(ChatHandler *handler, Optional< std::string > playerName, Optional< std::string > time)
Definition cs_deserter.cpp:404
static bool HandleDeserterInstanceRemove(ChatHandler *handler, Optional< PlayerIdentifier > player)
Definition cs_deserter.cpp:410
static bool HandleDeserterBGRemoveAll(ChatHandler *handler, Optional< std::string > maxTime)
Definition cs_deserter.cpp:426
static bool HandleDeserterBGRemove(ChatHandler *handler, Optional< PlayerIdentifier > player)
Definition cs_deserter.cpp:416
static bool HandleDeserterInstanceAdd(ChatHandler *handler, Optional< std::string > playerName, Optional< std::string > time)
Definition cs_deserter.cpp:398
static bool HandleDeserterInstanceRemoveAll(ChatHandler *handler, Optional< std::string > maxTime)
Definition cs_deserter.cpp:421
std::vector< ChatCommandBuilder > ChatCommandTable
Definition ChatCommand.h:46

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 return false;
109 }
110
111 guid = sCharacterCache->GetCharacterGuidByName(*playerName);
112 if (guid)
113 {
114 target = ObjectAccessor::FindPlayerByName(*playerName);
115 }
116 else
117 {
118 if (time)
119 {
121 return false;
122 }
123
124 time = playerName;
125 playerName = "";
126 }
127 }
128
129 if (!playerName || playerName->empty())
130 {
131 if (!handler->GetSession())
132 {
133 return false;
134 }
135
136 playerName = target->GetName();
137 guid = target->GetGUID();
138 }
139
140 if (!time)
141 {
142 time = isInstance ? "30m" : "15m";
143 }
144
145 int32 duration = TimeStringToSecs(*time);
146
147 if (duration == 0)
148 {
149 duration = Acore::StringTo<int32>(*time).value_or(0);
150 }
151
152 if (duration == 0)
153 {
155 return false;
156 }
157
158 uint32 deserterSpell = isInstance ? LFG_SPELL_DUNGEON_DESERTER : BG_SPELL_DESERTER;
159
160 if (target)
161 {
162 Aura* aura = target->GetAura(deserterSpell);
163 if (aura && aura->GetDuration() >= duration * IN_MILLISECONDS)
164 {
165 handler->PSendSysMessage("Player {} already has a longer {} Deserter active.", handler->playerLink(*playerName), isInstance ? "Instance" : "Battleground");
166 return true;
167 }
168
169 aura = target->AddAura(deserterSpell, target);
170 if (!aura)
171 {
173 return false;
174 }
175 aura->SetDuration(duration * IN_MILLISECONDS);
176 }
177 else
178 {
179 int32 remainTime = 0;
180 if (QueryResult result = CharacterDatabase.Query("SELECT remainTime FROM character_aura WHERE guid = {} AND spell = {}", guid.GetCounter(), deserterSpell))
181 {
182 Field* fields = result->Fetch();
183 remainTime = fields[0].Get<int32>();
184
185 if (remainTime < 0 || remainTime >= duration * IN_MILLISECONDS)
186 {
187 handler->PSendSysMessage("Player {} already has a longer {} Deserter active.", handler->playerLink(*playerName), isInstance ? "Instance" : "Battleground");
188 return true;
189 }
190 CharacterDatabase.Query("DELETE FROM character_aura WHERE guid = {} AND spell = {}", guid.GetCounter(), deserterSpell);
191 }
192
193 uint8 index = 0;
195 stmt->SetData(index++, guid.GetCounter());
196 stmt->SetData(index++, guid.GetCounter());
197 stmt->SetData(index++, 0);
198 stmt->SetData(index++, deserterSpell);
199 stmt->SetData(index++, 1);
200 stmt->SetData(index++, 1);
201 stmt->SetData(index++, 1);
202 stmt->SetData(index++, 0);
203 stmt->SetData(index++, 0);
204 stmt->SetData(index++, 0);
205 stmt->SetData(index++, 0);
206 stmt->SetData(index++, 0);
207 stmt->SetData(index++, 0);
208 stmt->SetData(index++, isInstance ? 1800000 : 900000);
209 stmt->SetData(index++, duration * IN_MILLISECONDS);
210 stmt->SetData(index, 0);
211 CharacterDatabase.Execute(stmt);
212 }
213
214 handler->PSendSysMessage("{} of {} Deserter has been added to player {}.", secsToTimeString(duration), isInstance ? "Instance" : "Battleground", handler->playerLink(*playerName));
215 return true;
216 }
#define sCharacterCache
Definition CharacterCache.h:83
@ CHAR_INS_AURA
Definition CharacterDatabase.h:196
constexpr auto IN_MILLISECONDS
Definition Common.h:53
std::shared_ptr< ResultSet > QueryResult
Definition DatabaseEnvFwd.h:27
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition DatabaseEnv.cpp:21
std::int32_t int32
Definition Define.h:103
std::uint8_t uint8
Definition Define.h:109
std::uint32_t uint32
Definition Define.h:107
@ LANG_PLAYER_NOT_FOUND
Definition Language.h:540
@ LANG_BAD_VALUE
Definition Language.h:148
bool normalizePlayerName(std::string &name)
Definition ObjectMgr.cpp:208
std::string secsToTimeString(uint64 timeInSecs, bool shortText)
Definition Util.cpp:73
uint32 TimeStringToSecs(const std::string &timestring)
Definition Util.cpp:163
Definition SpellAuras.h:87
int32 GetDuration() const
Definition SpellAuras.h:133
void SetDuration(int32 duration, bool withMods=false)
Definition SpellAuras.cpp:868
std::string playerLink(std::string const &name) const
Definition Chat.h:231
void PSendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition Chat.cpp:211
WorldSession * GetSession()
Definition Chat.h:242
void SendErrorMessage(uint32 entry)
Definition Chat.cpp:216
Player * getSelectedPlayerOrSelf() const
Definition Chat.cpp:418
Class used to access individual fields of database query result.
Definition Field.h:98
std::enable_if_t< std::is_arithmetic_v< T >, T > Get() const
Definition Field.h:112
Definition ObjectGuid.h:118
LowType GetCounter() const
Definition ObjectGuid.h:145
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:112
Definition Player.h:1081
Acore::Types::is_default< T > SetData(const uint8 index, T value)
Definition PreparedStatement.h:77
Definition PreparedStatement.h:157
Aura * GetAura(uint32 spellId, ObjectGuid casterGUID=ObjectGuid::Empty, ObjectGuid itemCasterGUID=ObjectGuid::Empty, uint8 reqEffMask=0) const
Definition Unit.cpp:5613
Aura * AddAura(uint32 spellId, Unit *target)
Definition Unit.cpp:18915
std::string const & GetName() const
Definition Object.h:463
@ 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:271

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::SendErrorMessage(), PreparedStatementBase::SetData(), Aura::SetDuration(), 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()
405 {
406 return HandleDeserterAdd(handler, playerName, time, false);
407 }
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()
417 {
418 return HandleDeserterRemove(handler, player, false);
419 }
static bool HandleDeserterRemove(ChatHandler *handler, Optional< PlayerIdentifier > player, bool isInstance)
Removes the Deserter Debuff from a player.
Definition cs_deserter.cpp:240

References HandleDeserterRemove().

Referenced by GetCommands().

◆ HandleDeserterBGRemoveAll()

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

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()
399 {
400 return HandleDeserterAdd(handler, playerName, time, true);
401 }

References HandleDeserterAdd().

Referenced by GetCommands().

◆ HandleDeserterInstanceRemove()

static bool deserter_commandscript::HandleDeserterInstanceRemove ( ChatHandler handler,
Optional< PlayerIdentifier player 
)
inlinestatic
See also
HandleDeserterRemove()
411 {
412 return HandleDeserterRemove(handler, player, true);
413 }

References HandleDeserterRemove().

Referenced by GetCommands().

◆ HandleDeserterInstanceRemoveAll()

static bool deserter_commandscript::HandleDeserterInstanceRemoveAll ( ChatHandler handler,
Optional< std::string >  maxTime 
)
inlinestatic
422 {
423 return HandleDeserterRemoveAll(handler, true, maxTime);
424 }

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')
241 {
242 if (!player)
243 {
244 player = PlayerIdentifier::FromTargetOrSelf(handler);
245 }
246
247 if (!player)
248 {
250 return false;
251 }
252
253 Player* target = player->GetConnectedPlayer();
254 uint32 deserterSpell = isInstance ? LFG_SPELL_DUNGEON_DESERTER : BG_SPELL_DESERTER;
255 int32 duration = 0;
256
257 if (target)
258 {
259 if (Aura* aura = target->GetAura(deserterSpell))
260 {
261 duration = aura->GetDuration();
262 target->RemoveAura(deserterSpell);
263 }
264 }
265 else
266 {
267 if (QueryResult result = CharacterDatabase.Query("SELECT remainTime FROM character_aura WHERE guid = {} AND spell = {}", player->GetGUID().GetCounter(), deserterSpell))
268 {
269 Field* fields = result->Fetch();
270 duration = fields[0].Get<int32>();
271 CharacterDatabase.Execute("DELETE FROM character_aura WHERE guid = {} AND spell = {}", player->GetGUID().GetCounter(), deserterSpell);
272 }
273 }
274
275 if (duration == 0)
276 {
277 handler->SendErrorMessage("Player {} does not have {} Deserter.", handler->playerLink(player->GetName()), isInstance ? "Instance" : "Battleground");
278 return true;
279 }
280
281 if (duration < 0)
282 {
283 handler->SendErrorMessage("Permanent {} Deserter has been removed from player {} (GUID {}).", isInstance ? "Instance" : "Battleground", handler->playerLink(player->GetName()), player->GetGUID().ToString());
284 return true;
285 }
286
287 handler->PSendSysMessage("{} of {} Deserter has been removed from player {} (GUID {}).", secsToTimeString(duration / IN_MILLISECONDS), isInstance ? "Instance" : "Battleground", handler->playerLink(player->GetName()), player->GetGUID().ToString());
288 return true;
289 }
@ LANG_NO_CHAR_SELECTED
Definition Language.h:149
const std::string & GetName() const
Definition ScriptObject.h:53
void RemoveAura(AuraApplicationMap::iterator &i, AuraRemoveMode mode=AURA_REMOVE_BY_DEFAULT)
Definition Unit.cpp:4808
std::string ToString(Type &&val, Params &&... params)
Definition StringConvert.h:250
static Optional< PlayerIdentifier > FromTargetOrSelf(ChatHandler *handler)
Definition ChatCommandTags.h:184

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(), and ChatHandler::SendErrorMessage().

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

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

Referenced by HandleDeserterBGRemoveAll(), and HandleDeserterInstanceRemoveAll().


The documentation for this class was generated from the following file: