[Command] #gearup Table Auto-Install (#1402)

* Update syntax for new httplib and run on own thread

* Only log if path is set in request

* Auto install tool table if does not exist locally

* Add lore and has item checks to reduce verbosity and errors

* Formatting

* Remove test code from test command
This commit is contained in:
Chris Miles 2021-06-16 09:31:56 -05:00 committed by GitHub
parent 19b14ea2d4
commit f2ffca1a06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 19 deletions

View File

@ -76,6 +76,9 @@
#include "npc_scale_manager.h"
#include "../common/content/world_content_service.h"
#define CPPHTTPLIB_OPENSSL_SUPPORT
#include "../common/http/httplib.h"
extern QueryServ* QServ;
extern WorldServer worldserver;
extern TaskManager *task_manager;
@ -3101,17 +3104,61 @@ void command_race(Client *c, const Seperator *sep)
void command_gearup(Client *c, const Seperator *sep)
{
std::string tool_table_name = "tool_gearup_armor_sets";
if (!database.DoesTableExist(tool_table_name)) {
c->Message(
Chat::Red,
Chat::Yellow,
fmt::format(
"Table [{}] does not exist, please source in the optional SQL required for this tool",
"Table [{}] does not exist. Downloading from Github and installing...",
tool_table_name
).c_str()
);
// http get request
httplib::Client cli("https://raw.githubusercontent.com");
cli.set_connection_timeout(0, 15000000); // 15 sec
cli.set_read_timeout(15, 0); // 15 seconds
cli.set_write_timeout(15, 0); // 15 seconds
int sourced_queries = 0;
std::string url = "/EQEmu/Server/master/utils/sql/git/optional/2020_07_20_tool_gearup_armor_sets.sql";
if (auto res = cli.Get(url.c_str())) {
if (res->status == 200) {
for (auto &s: SplitString(res->body, ';')) {
if (!trim(s).empty()) {
auto results = database.QueryDatabase(s);
if (!results.ErrorMessage().empty()) {
c->Message(
Chat::Yellow,
fmt::format(
"Error sourcing SQL [{}]", results.ErrorMessage()
).c_str()
);
return;
}
sourced_queries++;
}
}
}
}
else {
c->Message(
Chat::Yellow,
fmt::format(
"Error retrieving URL [{}]",
url
).c_str()
);
}
c->Message(
Chat::Yellow,
fmt::format(
"Table [{}] installed. Sourced [{}] queries",
tool_table_name, sourced_queries
).c_str()
);
}
std::string expansion_arg = sep->arg[1];
std::string expansion_filter;
@ -3140,7 +3187,10 @@ void command_gearup(Client *c, const Seperator *sep)
)
);
int items_equipped = 0;
int items_already_have = 0;
std::set<int> equipped;
for (auto row = results.begin(); row != results.end(); ++row) {
int item_id = atoi(row[0]);
int slot_id = atoi(row[1]);
@ -3158,17 +3208,34 @@ void command_gearup(Client *c, const Seperator *sep)
}
if (equipped.find(slot_id) == equipped.end()) {
if (c->CastToMob()->CanClassEquipItem(item_id)) {
const EQ::ItemData *item = database.GetItem(item_id);
bool has_item = (c->GetInv().HasItem(item_id, 1, invWhereWorn) != INVALID_INDEX);
bool can_wear_item = !c->CheckLoreConflict(item) && !has_item;
if (!can_wear_item) {
items_already_have++;
}
if (c->CastToMob()->CanClassEquipItem(item_id) && can_wear_item) {
equipped.insert(slot_id);
c->SummonItem(
item_id,
0, 0, 0, 0, 0, 0, 0, 0,
slot_id
);
items_equipped++;
}
}
}
c->Message(
Chat::White,
fmt::format(
"Equipped items [{}] already had [{}] items equipped",
items_equipped,
items_already_have
).c_str()
);
if (expansion_arg.empty()) {
results = database.QueryDatabase(
fmt::format(