feat(Core/Command): Use timestring with all commands (#12050)

This commit is contained in:
Kempec Halk
2022-06-13 19:05:26 +02:00
committed by GitHub
parent d236dc9d07
commit 3c434cee12
6 changed files with 285 additions and 51 deletions

View File

@@ -79,7 +79,8 @@ public:
* selected player, with the provided duration in seconds.
*
* @param handler The ChatHandler, passed by the system.
* @param time The provided duration in seconds.
* @param playerName Player by name. Optional, defaults to selected or self.
* @param time The provided duration as TimeString. Optional, defaults to bg/instance default time.
* @param isInstance provided by the relaying functions, so we don't have
* to write that much code :)
*
@@ -87,40 +88,80 @@ public:
*
* Example Usage:
* @code
* .deserter instance add 3600 (one hour)
* .deserter instance add 1h30m
* -or-
* .deserter bg add 3600 (one hour)
* .deserter bg add 1h30m
* @endcode
*/
static bool HandleDeserterAdd(ChatHandler* handler, Optional<PlayerIdentifier> player, uint32 time, bool isInstance)
static bool HandleDeserterAdd(ChatHandler* handler, Optional<std::string> playerName, Optional<std::string> time, bool isInstance)
{
if (!player)
Player* target = handler->getSelectedPlayerOrSelf();
ObjectGuid guid;
if (playerName)
{
player = PlayerIdentifier::FromTargetOrSelf(handler);
if (!normalizePlayerName(*playerName))
{
handler->SendSysMessage(LANG_PLAYER_NOT_FOUND);
handler->SetSentErrorMessage(true);
return false;
}
guid = sCharacterCache->GetCharacterGuidByName(*playerName);
if (guid)
{
target = ObjectAccessor::FindPlayerByName(*playerName);
}
else
{
if (time)
{
handler->SendSysMessage(LANG_PLAYER_NOT_FOUND);
handler->SetSentErrorMessage(true);
return false;
}
time = playerName;
playerName = "";
}
}
if (!player)
if (!playerName || playerName->empty())
{
handler->SendSysMessage(LANG_NO_CHAR_SELECTED);
handler->SetSentErrorMessage(true);
return false;
if (!handler->GetSession())
{
return false;
}
playerName = target->GetName();
guid = target->GetGUID();
}
if (!time)
{
time = isInstance ? "30m" : "15m";
}
int32 duration = TimeStringToSecs(*time);
if (duration == 0)
{
duration = Acore::StringTo<int32>(*time).value_or(0);
}
if (duration == 0)
{
handler->SendSysMessage(LANG_BAD_VALUE);
handler->SetSentErrorMessage(true);
return false;
}
Player* target = player->GetConnectedPlayer();
if (target)
{
Aura* aura = target->GetAura(isInstance ? LFG_SPELL_DUNGEON_DESERTER : BG_SPELL_DESERTER);
if (aura && aura->GetDuration() >= (int32)time * IN_MILLISECONDS)
if (aura && aura->GetDuration() >= duration * IN_MILLISECONDS)
{
handler->PSendSysMessage("Player %s already has a longer %s Deserter active.", handler->playerLink(player->GetName()), isInstance ? "Instance" : "Battleground");
handler->PSendSysMessage("Player %s already has a longer %s Deserter active.", handler->playerLink(*playerName), isInstance ? "Instance" : "Battleground");
return true;
}
@@ -131,29 +172,29 @@ public:
handler->SetSentErrorMessage(true);
return false;
}
aura->SetDuration(time * IN_MILLISECONDS);
aura->SetDuration(duration * IN_MILLISECONDS);
return true;
}
int32 duration = 0;
if (QueryResult result = CharacterDatabase.Query("SELECT remainTime FROM character_aura WHERE guid = {} AND spell = {}", player->GetGUID().GetCounter(), isInstance ? LFG_SPELL_DUNGEON_DESERTER : BG_SPELL_DESERTER))
int32 remainTime = 0;
if (QueryResult result = CharacterDatabase.Query("SELECT remainTime FROM character_aura WHERE guid = {} AND spell = {}", guid.GetCounter(), isInstance ? LFG_SPELL_DUNGEON_DESERTER : BG_SPELL_DESERTER))
{
Field* fields = result->Fetch();
duration = fields[0].Get<int32>();
remainTime = fields[0].Get<int32>();
if (duration < 0 || duration >= (int32)time * IN_MILLISECONDS)
if (remainTime < 0 || remainTime >= duration * IN_MILLISECONDS)
{
handler->PSendSysMessage("Player %s already has a longer %s Deserter active.", handler->playerLink(player->GetName()), isInstance ? "Instance" : "Battleground");
handler->PSendSysMessage("Player %s already has a longer %s Deserter active.", handler->playerLink(*playerName), isInstance ? "Instance" : "Battleground");
return true;
}
CharacterDatabase.Query("DELETE FROM character_aura WHERE guid = {} AND spell = {}", player->GetGUID().GetCounter(), isInstance ? LFG_SPELL_DUNGEON_DESERTER : BG_SPELL_DESERTER);
CharacterDatabase.Query("DELETE FROM character_aura WHERE guid = {} AND spell = {}", guid.GetCounter(), isInstance ? LFG_SPELL_DUNGEON_DESERTER : BG_SPELL_DESERTER);
}
uint8 index = 0;
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_AURA);
stmt->SetData(index++, player->GetGUID().GetCounter());
stmt->SetData(index++, player->GetGUID().GetCounter());
stmt->SetData(index++, guid.GetCounter());
stmt->SetData(index++, guid.GetCounter());
stmt->SetData(index++, 0);
stmt->SetData(index++, isInstance ? LFG_SPELL_DUNGEON_DESERTER : BG_SPELL_DESERTER);
stmt->SetData(index++, 1);
@@ -166,7 +207,7 @@ public:
stmt->SetData(index++, 0);
stmt->SetData(index++, 0);
stmt->SetData(index++, isInstance ? 1800000 : 900000);
stmt->SetData(index++, time * 1000);
stmt->SetData(index++, duration * 1000);
stmt->SetData(index, 0);
CharacterDatabase.Execute(stmt);
@@ -240,15 +281,15 @@ public:
}
/// @sa HandleDeserterAdd()
static bool HandleDeserterInstanceAdd(ChatHandler* handler, Optional<PlayerIdentifier> player, uint32 time)
static bool HandleDeserterInstanceAdd(ChatHandler* handler, Optional<std::string> playerName, Optional<std::string> time)
{
return HandleDeserterAdd(handler, player, time, true);
return HandleDeserterAdd(handler, playerName, time, true);
}
/// @sa HandleDeserterAdd()
static bool HandleDeserterBGAdd(ChatHandler* handler, Optional<PlayerIdentifier> player, uint32 time)
static bool HandleDeserterBGAdd(ChatHandler* handler, Optional<std::string> playerName, Optional<std::string> time)
{
return HandleDeserterAdd(handler, player, time, false);
return HandleDeserterAdd(handler, playerName, time, false);
}
/// @sa HandleDeserterRemove()