Scripts/Commands: Convert argument parsing of ahbot commands (PR #25264)

This commit is contained in:
Peter Keresztes Schmidt
2020-08-17 22:04:44 +02:00
committed by GitHub
parent a9a4f05a28
commit 426513ec8e
6 changed files with 167 additions and 109 deletions

View File

@@ -455,10 +455,10 @@ void AuctionHouseBot::SetItemsRatioForHouse(AuctionHouseType house, uint32 val)
_seller->SetItemsRatioForHouse(house, val);
}
void AuctionHouseBot::SetItemsAmount(uint32(&vals)[MAX_AUCTION_QUALITY])
void AuctionHouseBot::SetItemsAmount(std::array<uint32, MAX_AUCTION_QUALITY> const& amounts)
{
if (_seller)
_seller->SetItemsAmount(vals);
_seller->SetItemsAmount(amounts);
}
void AuctionHouseBot::SetItemsAmountForQuality(AuctionQuality quality, uint32 val)
@@ -473,16 +473,16 @@ void AuctionHouseBot::ReloadAllConfig()
InitializeAgents();
}
void AuctionHouseBot::PrepareStatusInfos(AuctionHouseBotStatusInfo& statusInfo)
void AuctionHouseBot::PrepareStatusInfos(std::unordered_map<AuctionHouseType, AuctionHouseBotStatusInfoPerType>& statusInfo)
{
for (uint32 i = 0; i < MAX_AUCTION_HOUSE_TYPE; ++i)
for (AuctionHouseType ahType : EnumUtils::Iterate<AuctionHouseType>())
{
statusInfo[i].ItemsCount = 0;
statusInfo[ahType].ItemsCount = 0;
for (int j = 0; j < MAX_AUCTION_QUALITY; ++j)
statusInfo[i].QualityInfo[j] = 0;
for (AuctionQuality quality : EnumUtils::Iterate<AuctionQuality>())
statusInfo[ahType].QualityInfo[quality] = 0;
AuctionHouseObject* auctionHouse = sAuctionMgr->GetAuctionsMap(AuctionHouseType(i));
AuctionHouseObject* auctionHouse = sAuctionMgr->GetAuctionsMap(ahType);
for (AuctionHouseObject::AuctionEntryMap::const_iterator itr = auctionHouse->GetAuctionsBegin(); itr != auctionHouse->GetAuctionsEnd(); ++itr)
{
AuctionEntry* auctionEntry = itr->second;
@@ -492,9 +492,9 @@ void AuctionHouseBot::PrepareStatusInfos(AuctionHouseBotStatusInfo& statusInfo)
if (!auctionEntry->owner || sAuctionBotConfig->IsBotChar(auctionEntry->owner)) // Add only ahbot items
{
if (prototype->Quality < MAX_AUCTION_QUALITY)
++statusInfo[i].QualityInfo[prototype->Quality];
++statusInfo[ahType].QualityInfo[AuctionQuality(prototype->Quality)];
++statusInfo[i].ItemsCount;
++statusInfo[ahType].ItemsCount;
}
}
}

View File

@@ -21,12 +21,14 @@
#include "Define.h"
#include "SharedDefines.h"
#include <string>
#include <unordered_map>
#include <vector>
class AuctionBotSeller;
class AuctionBotBuyer;
// shadow of ItemQualities with skipped ITEM_QUALITY_HEIRLOOM, anything after ITEM_QUALITY_ARTIFACT(6) in fact
// EnumUtils: DESCRIBE THIS
enum AuctionQuality
{
AUCTION_QUALITY_GRAY = ITEM_QUALITY_POOR,
@@ -40,6 +42,7 @@ enum AuctionQuality
#define MAX_AUCTION_QUALITY 7
// EnumUtils: DESCRIBE THIS
enum AuctionHouseType
{
AUCTION_HOUSE_NEUTRAL = 0,
@@ -274,11 +277,9 @@ public:
struct AuctionHouseBotStatusInfoPerType
{
uint32 ItemsCount;
uint32 QualityInfo[MAX_AUCTION_QUALITY];
std::unordered_map<AuctionQuality, uint32> QualityInfo;
};
typedef AuctionHouseBotStatusInfoPerType AuctionHouseBotStatusInfo[MAX_AUCTION_HOUSE_TYPE];
// This class handle both Selling and Buying method
// (holder of AuctionBotBuyer and AuctionBotSeller objects)
class TC_GAME_API AuctionHouseBot
@@ -298,12 +299,12 @@ public:
// Followed method is mainly used by cs_ahbot.cpp for in-game/console command
void SetItemsRatio(uint32 al, uint32 ho, uint32 ne);
void SetItemsRatioForHouse(AuctionHouseType house, uint32 val);
void SetItemsAmount(uint32(&vals)[MAX_AUCTION_QUALITY]);
void SetItemsAmount(std::array<uint32, MAX_AUCTION_QUALITY> const& amounts);
void SetItemsAmountForQuality(AuctionQuality quality, uint32 val);
void ReloadAllConfig();
void Rebuild(bool all);
void PrepareStatusInfos(AuctionHouseBotStatusInfo& statusInfo);
void PrepareStatusInfos(std::unordered_map<AuctionHouseType, AuctionHouseBotStatusInfoPerType>& statusInfo);
private:
void InitializeAgents();

View File

@@ -778,15 +778,15 @@ void AuctionBotSeller::SetItemsRatioForHouse(AuctionHouseType house, uint32 val)
LoadItemsQuantity(_houseConfig[house]);
}
void AuctionBotSeller::SetItemsAmount(uint32(&vals)[MAX_AUCTION_QUALITY])
void AuctionBotSeller::SetItemsAmount(std::array<uint32, MAX_AUCTION_QUALITY> const& amounts)
{
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_GRAY_AMOUNT, vals[AUCTION_QUALITY_GRAY]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_WHITE_AMOUNT, vals[AUCTION_QUALITY_WHITE]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_GREEN_AMOUNT, vals[AUCTION_QUALITY_GREEN]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_BLUE_AMOUNT, vals[AUCTION_QUALITY_BLUE]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_PURPLE_AMOUNT, vals[AUCTION_QUALITY_PURPLE]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_ORANGE_AMOUNT, vals[AUCTION_QUALITY_ORANGE]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_YELLOW_AMOUNT, vals[AUCTION_QUALITY_YELLOW]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_GRAY_AMOUNT, amounts[AUCTION_QUALITY_GRAY]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_WHITE_AMOUNT, amounts[AUCTION_QUALITY_WHITE]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_GREEN_AMOUNT, amounts[AUCTION_QUALITY_GREEN]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_BLUE_AMOUNT, amounts[AUCTION_QUALITY_BLUE]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_PURPLE_AMOUNT, amounts[AUCTION_QUALITY_PURPLE]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_ORANGE_AMOUNT, amounts[AUCTION_QUALITY_ORANGE]);
sAuctionBotConfig->SetConfig(CONFIG_AHBOT_ITEM_YELLOW_AMOUNT, amounts[AUCTION_QUALITY_YELLOW]);
for (int i = 0; i < MAX_AUCTION_HOUSE_TYPE; ++i)
LoadItemsQuantity(_houseConfig[i]);

View File

@@ -129,7 +129,7 @@ public:
void AddNewAuctions(SellerConfiguration& config);
void SetItemsRatio(uint32 al, uint32 ho, uint32 ne);
void SetItemsRatioForHouse(AuctionHouseType house, uint32 val);
void SetItemsAmount(uint32(&vals)[MAX_AUCTION_QUALITY]);
void SetItemsAmount(std::array<uint32, MAX_AUCTION_QUALITY> const& amounts);
void SetItemsAmountForQuality(AuctionQuality quality, uint32 val);
void LoadConfig();

View File

@@ -0,0 +1,96 @@
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AuctionHouseBot.h"
#include "Define.h"
#include "SmartEnum.h"
#include <stdexcept>
namespace Trinity
{
namespace Impl
{
/************************************************************************\
|* data for enum 'AuctionQuality' in 'AuctionHouseBot.h' auto-generated *|
\************************************************************************/
template <>
TC_API_EXPORT EnumText EnumUtils<AuctionQuality>::ToString(AuctionQuality value)
{
switch (value)
{
case AUCTION_QUALITY_GRAY: return { "AUCTION_QUALITY_GRAY", "AUCTION_QUALITY_GRAY", "" };
case AUCTION_QUALITY_WHITE: return { "AUCTION_QUALITY_WHITE", "AUCTION_QUALITY_WHITE", "" };
case AUCTION_QUALITY_GREEN: return { "AUCTION_QUALITY_GREEN", "AUCTION_QUALITY_GREEN", "" };
case AUCTION_QUALITY_BLUE: return { "AUCTION_QUALITY_BLUE", "AUCTION_QUALITY_BLUE", "" };
case AUCTION_QUALITY_PURPLE: return { "AUCTION_QUALITY_PURPLE", "AUCTION_QUALITY_PURPLE", "" };
case AUCTION_QUALITY_ORANGE: return { "AUCTION_QUALITY_ORANGE", "AUCTION_QUALITY_ORANGE", "" };
case AUCTION_QUALITY_YELLOW: return { "AUCTION_QUALITY_YELLOW", "AUCTION_QUALITY_YELLOW", "" };
default: throw std::out_of_range("value");
}
}
template <>
TC_API_EXPORT size_t EnumUtils<AuctionQuality>::Count() { return 7; }
template <>
TC_API_EXPORT AuctionQuality EnumUtils<AuctionQuality>::FromIndex(size_t index)
{
switch (index)
{
case 0: return AUCTION_QUALITY_GRAY;
case 1: return AUCTION_QUALITY_WHITE;
case 2: return AUCTION_QUALITY_GREEN;
case 3: return AUCTION_QUALITY_BLUE;
case 4: return AUCTION_QUALITY_PURPLE;
case 5: return AUCTION_QUALITY_ORANGE;
case 6: return AUCTION_QUALITY_YELLOW;
default: throw std::out_of_range("index");
}
}
/**************************************************************************\
|* data for enum 'AuctionHouseType' in 'AuctionHouseBot.h' auto-generated *|
\**************************************************************************/
template <>
TC_API_EXPORT EnumText EnumUtils<AuctionHouseType>::ToString(AuctionHouseType value)
{
switch (value)
{
case AUCTION_HOUSE_NEUTRAL: return { "AUCTION_HOUSE_NEUTRAL", "AUCTION_HOUSE_NEUTRAL", "" };
case AUCTION_HOUSE_ALLIANCE: return { "AUCTION_HOUSE_ALLIANCE", "AUCTION_HOUSE_ALLIANCE", "" };
case AUCTION_HOUSE_HORDE: return { "AUCTION_HOUSE_HORDE", "AUCTION_HOUSE_HORDE", "" };
default: throw std::out_of_range("value");
}
}
template <>
TC_API_EXPORT size_t EnumUtils<AuctionHouseType>::Count() { return 3; }
template <>
TC_API_EXPORT AuctionHouseType EnumUtils<AuctionHouseType>::FromIndex(size_t index)
{
switch (index)
{
case 0: return AUCTION_HOUSE_NEUTRAL;
case 1: return AUCTION_HOUSE_ALLIANCE;
case 2: return AUCTION_HOUSE_HORDE;
default: throw std::out_of_range("index");
}
}
}
}

View File

@@ -21,13 +21,18 @@
#include "Language.h"
#include "RBAC.h"
uint32 const ahbotQualityIds[MAX_AUCTION_QUALITY] =
{
LANG_AHBOT_QUALITY_GRAY, LANG_AHBOT_QUALITY_WHITE,
LANG_AHBOT_QUALITY_GREEN, LANG_AHBOT_QUALITY_BLUE,
LANG_AHBOT_QUALITY_PURPLE, LANG_AHBOT_QUALITY_ORANGE,
LANG_AHBOT_QUALITY_YELLOW
};
static std::unordered_map<AuctionQuality, uint32> const ahbotQualityLangIds =
{
{ AUCTION_QUALITY_GRAY, LANG_AHBOT_QUALITY_GRAY },
{ AUCTION_QUALITY_WHITE, LANG_AHBOT_QUALITY_WHITE },
{ AUCTION_QUALITY_GREEN, LANG_AHBOT_QUALITY_GREEN },
{ AUCTION_QUALITY_BLUE, LANG_AHBOT_QUALITY_BLUE },
{ AUCTION_QUALITY_PURPLE, LANG_AHBOT_QUALITY_PURPLE },
{ AUCTION_QUALITY_ORANGE, LANG_AHBOT_QUALITY_ORANGE },
{ AUCTION_QUALITY_YELLOW, LANG_AHBOT_QUALITY_YELLOW }
};
using namespace Trinity::ChatCommands;
class ahbot_commandscript : public CommandScript
{
@@ -73,103 +78,59 @@ public:
return commandTable;
}
static bool HandleAHBotItemsAmountCommand(ChatHandler* handler, char const* args)
static bool HandleAHBotItemsAmountCommand(ChatHandler* handler, std::array<uint32, MAX_AUCTION_QUALITY> items)
{
uint32 qVals[MAX_AUCTION_QUALITY];
char* arg = strtok((char*)args, " ");
for (int i = 0; i < MAX_AUCTION_QUALITY; ++i)
{
if (!arg)
return false;
qVals[i] = atoi(arg);
arg = strtok(nullptr, " ");
}
sAuctionBot->SetItemsAmount(items);
sAuctionBot->SetItemsAmount(qVals);
for (int i = 0; i < MAX_AUCTION_QUALITY; ++i)
handler->PSendSysMessage(LANG_AHBOT_ITEMS_AMOUNT, handler->GetTrinityString(ahbotQualityIds[i]), sAuctionBotConfig->GetConfigItemQualityAmount(AuctionQuality(i)));
for (AuctionQuality quality : EnumUtils::Iterate<AuctionQuality>())
handler->PSendSysMessage(LANG_AHBOT_ITEMS_AMOUNT, handler->GetTrinityString(ahbotQualityLangIds.at(quality)), sAuctionBotConfig->GetConfigItemQualityAmount(quality));
return true;
}
template <AuctionQuality Q>
static bool HandleAHBotItemsAmountQualityCommand(ChatHandler* handler, char const* args)
static bool HandleAHBotItemsAmountQualityCommand(ChatHandler* handler, uint32 amount)
{
char* arg = strtok((char*)args, " ");
if (!arg)
return false;
uint32 qualityVal = atoi(arg);
sAuctionBot->SetItemsAmountForQuality(Q, qualityVal);
handler->PSendSysMessage(LANG_AHBOT_ITEMS_AMOUNT, handler->GetTrinityString(ahbotQualityIds[Q]),
sAuctionBot->SetItemsAmountForQuality(Q, amount);
handler->PSendSysMessage(LANG_AHBOT_ITEMS_AMOUNT, handler->GetTrinityString(ahbotQualityLangIds.at(Q)),
sAuctionBotConfig->GetConfigItemQualityAmount(Q));
return true;
}
static bool HandleAHBotItemsRatioCommand(ChatHandler* handler, char const* args)
static bool HandleAHBotItemsRatioCommand(ChatHandler* handler, uint32 alliance, uint32 horde, uint32 neutral)
{
uint32 rVal[MAX_AUCTION_QUALITY];
char* arg = strtok((char*)args, " ");
for (int i = 0; i < MAX_AUCTION_QUALITY; ++i)
{
if (!arg)
return false;
rVal[i] = atoi(arg);
arg = strtok(nullptr, " ");
}
sAuctionBot->SetItemsRatio(alliance, horde, neutral);
sAuctionBot->SetItemsRatio(rVal[0], rVal[1], rVal[2]);
for (int i = 0; i < MAX_AUCTION_HOUSE_TYPE; ++i)
handler->PSendSysMessage(LANG_AHBOT_ITEMS_RATIO, AuctionBotConfig::GetHouseTypeName(AuctionHouseType(i)), sAuctionBotConfig->GetConfigItemAmountRatio(AuctionHouseType(i)));
for (AuctionHouseType type : EnumUtils::Iterate<AuctionHouseType>())
handler->PSendSysMessage(LANG_AHBOT_ITEMS_RATIO, AuctionBotConfig::GetHouseTypeName(type), sAuctionBotConfig->GetConfigItemAmountRatio(type));
return true;
}
template<AuctionHouseType H>
static bool HandleAHBotItemsRatioHouseCommand(ChatHandler* handler, char const* args)
static bool HandleAHBotItemsRatioHouseCommand(ChatHandler* handler, uint32 ratio)
{
char* arg = strtok((char*)args, " ");
if (!arg)
return false;
uint32 ratioVal = atoi(arg);
sAuctionBot->SetItemsRatioForHouse(H, ratioVal);
sAuctionBot->SetItemsRatioForHouse(H, ratio);
handler->PSendSysMessage(LANG_AHBOT_ITEMS_RATIO, AuctionBotConfig::GetHouseTypeName(H), sAuctionBotConfig->GetConfigItemAmountRatio(H));
return true;
}
static bool HandleAHBotRebuildCommand(ChatHandler* /*handler*/, char const* args)
static bool HandleAHBotRebuildCommand(ChatHandler* /*handler*/, Optional<ExactSequence<'a', 'l', 'l'>> all)
{
char* arg = strtok((char*)args, " ");
bool all = false;
if (arg && strcmp(arg, "all") == 0)
all = true;
sAuctionBot->Rebuild(all);
sAuctionBot->Rebuild(all.has_value());
return true;
}
static bool HandleAHBotReloadCommand(ChatHandler* handler, char const* /*args*/)
static bool HandleAHBotReloadCommand(ChatHandler* handler)
{
sAuctionBot->ReloadAllConfig();
handler->SendSysMessage(LANG_AHBOT_RELOAD_OK);
return true;
}
static bool HandleAHBotStatusCommand(ChatHandler* handler, char const* args)
static bool HandleAHBotStatusCommand(ChatHandler* handler, Optional<ExactSequence<'a', 'l', 'l'>> all)
{
char* arg = strtok((char*)args, " ");
if (!arg)
return false;
bool all = false;
if (strcmp(arg, "all") == 0)
all = true;
AuctionHouseBotStatusInfo statusInfo;
std::unordered_map<AuctionHouseType, AuctionHouseBotStatusInfoPerType> statusInfo;
sAuctionBot->PrepareStatusInfos(statusInfo);
WorldSession* session = handler->GetSession();
@@ -212,12 +173,12 @@ public:
else
handler->SendSysMessage(LANG_AHBOT_STATUS_TITLE2_CHAT);
for (int i = 0; i < MAX_AUCTION_QUALITY; ++i)
handler->PSendSysMessage(fmtId, handler->GetTrinityString(ahbotQualityIds[i]),
statusInfo[AUCTION_HOUSE_ALLIANCE].QualityInfo[i],
statusInfo[AUCTION_HOUSE_HORDE].QualityInfo[i],
statusInfo[AUCTION_HOUSE_NEUTRAL].QualityInfo[i],
sAuctionBotConfig->GetConfigItemQualityAmount(AuctionQuality(i)));
for (AuctionQuality quality : EnumUtils::Iterate<AuctionQuality>())
handler->PSendSysMessage(fmtId, handler->GetTrinityString(ahbotQualityLangIds.at(quality)),
statusInfo[AUCTION_HOUSE_ALLIANCE].QualityInfo.at(quality),
statusInfo[AUCTION_HOUSE_HORDE].QualityInfo.at(quality),
statusInfo[AUCTION_HOUSE_NEUTRAL].QualityInfo.at(quality),
sAuctionBotConfig->GetConfigItemQualityAmount(quality));
}
if (!session)
@@ -228,17 +189,17 @@ public:
};
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_GRAY>(ChatHandler* handler, char const*);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_WHITE>(ChatHandler* handler, char const*);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_GREEN>(ChatHandler* handler, char const*);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_BLUE>(ChatHandler* handler, char const*);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_PURPLE>(ChatHandler* handler, char const*);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_ORANGE>(ChatHandler* handler, char const*);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_YELLOW>(ChatHandler* handler, char const*);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_GRAY>(ChatHandler* handler, uint32 amount);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_WHITE>(ChatHandler* handler, uint32 amount);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_GREEN>(ChatHandler* handler, uint32 amount);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_BLUE>(ChatHandler* handler, uint32 amount);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_PURPLE>(ChatHandler* handler, uint32 amount);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_ORANGE>(ChatHandler* handler, uint32 amount);
template bool ahbot_commandscript::HandleAHBotItemsAmountQualityCommand<AUCTION_QUALITY_YELLOW>(ChatHandler* handler, uint32 amount);
template bool ahbot_commandscript::HandleAHBotItemsRatioHouseCommand<AUCTION_HOUSE_ALLIANCE>(ChatHandler* handler, char const*);
template bool ahbot_commandscript::HandleAHBotItemsRatioHouseCommand<AUCTION_HOUSE_HORDE>(ChatHandler* handler, char const*);
template bool ahbot_commandscript::HandleAHBotItemsRatioHouseCommand<AUCTION_HOUSE_NEUTRAL>(ChatHandler* handler, char const*);
template bool ahbot_commandscript::HandleAHBotItemsRatioHouseCommand<AUCTION_HOUSE_ALLIANCE>(ChatHandler* handler, uint32 ratio);
template bool ahbot_commandscript::HandleAHBotItemsRatioHouseCommand<AUCTION_HOUSE_HORDE>(ChatHandler* handler, uint32 ratio);
template bool ahbot_commandscript::HandleAHBotItemsRatioHouseCommand<AUCTION_HOUSE_NEUTRAL>(ChatHandler* handler, uint32 ratio);
void AddSC_ahbot_commandscript()
{