[Commands] Cleanup #spawneditmass Command. (#2229)

* [Commands] Cleanup #spawneditmass Command.
- Cleanup messages and logic.
- Split command into its own file.

* Save querying database if they're not using the only supported option.

* Condense to one message.
This commit is contained in:
Kinglykrab 2022-06-04 13:59:46 -04:00 committed by GitHub
parent be1772d464
commit 17034a6e47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 150 additions and 131 deletions

View File

@ -299,7 +299,7 @@ int command_init(void)
command_add("showzonepoints", "Show zone points for current zone", AccountStatus::Guide, command_showzonepoints) ||
command_add("shutdown", "Shut this zone process down", AccountStatus::GMLeadAdmin, command_shutdown) ||
command_add("spawn", "[name] [race] [level] [material] [hp] [gender] [class] [priweapon] [secweapon] [merchantid] - Spawn an NPC", AccountStatus::Steward, command_spawn) ||
command_add("spawneditmass", "Mass editing spawn command", AccountStatus::GMLeadAdmin, command_spawneditmass) ||
command_add("spawneditmass", "[Search Criteria] [Edit Option] [Edit Value] [Apply] Mass editing spawn command (Apply is optional, 0 = False, 1 = True, default is False)", AccountStatus::GMLeadAdmin, command_spawneditmass) ||
command_add("spawnfix", "Find targeted NPC in database based on its X/Y/heading and update the database to make it spawn at your current location/heading.", AccountStatus::GMAreas, command_spawnfix) ||
command_add("spawnstatus", "[All|Disabled|Enabled|Spawn ID] - Show respawn timer status", AccountStatus::GMAdmin, command_spawnstatus) ||
command_add("spellinfo", "[spellid] - Get detailed info about a spell", AccountStatus::Steward, command_spellinfo) ||
@ -651,136 +651,6 @@ void command_help(Client *c, const Seperator *sep)
);
}
void command_spawneditmass(Client *c, const Seperator *sep)
{
std::string query = fmt::format(
SQL(
SELECT
npc_types.id,
npc_types.name,
spawn2.respawntime,
spawn2.id
FROM
npc_types
JOIN spawnentry ON spawnentry.npcID = npc_types.id
JOIN spawn2 ON spawn2.spawngroupID = spawnentry.spawngroupID
WHERE
spawn2.zone = '{0}' and spawn2.version = {1}
GROUP BY npc_types.id
),
zone->GetShortName(),
zone->GetInstanceVersion()
);
std::string status = "(Searching)";
if (strcasecmp(sep->arg[4], "apply") == 0) {
status = "(Applying)";
}
std::string search_value;
std::string edit_option;
std::string edit_value;
std::string apply_set;
if (sep->arg[1]) {
search_value = sep->arg[1];
}
if (sep->arg[2]) {
edit_option = sep->arg[2];
}
if (sep->arg[3]) {
edit_value = sep->arg[3];
}
if (sep->arg[4]) {
apply_set = sep->arg[4];
}
if (!edit_option.empty() && edit_value.empty()) {
c->Message(Chat::Yellow, "Please specify an edit option value | #npceditmass <search> <option> <value>");
return;
}
std::vector<std::string> npc_ids;
std::vector<std::string> spawn2_ids;
int found_count = 0;
auto results = database.QueryDatabase(query);
for (auto row = results.begin(); row != results.end(); ++row) {
std::string npc_id = row[0];
std::string npc_name = row[1];
std::string respawn_time = row[2];
std::string spawn2_id = row[3];
if (npc_name.find(search_value) == std::string::npos) {
continue;
}
c->Message(
Chat::Yellow,
fmt::format(
"NPC ({0}) [{1}] respawn_time [{2}] {3}",
npc_id,
npc_name,
respawn_time,
status
).c_str()
);
npc_ids.push_back(npc_id);
spawn2_ids.push_back(spawn2_id);
found_count++;
}
c->Message(Chat::Yellow, "Found [%i] NPC Spawn2 entries that match this criteria in this zone", found_count);
if (edit_option.empty()) {
c->Message(Chat::Yellow, "Please specify an edit option | #npceditmass <search> <option>");
c->Message(Chat::Yellow, "Options [respawn_time]");
return;
}
std::string saylink = fmt::format(
"#spawneditmass {} {} {} apply",
search_value,
edit_option,
edit_value
);
if (found_count > 0) {
c->Message(
Chat::Yellow, "To apply these changes, click <%s> or type [%s]",
EQ::SayLinkEngine::GenerateQuestSaylink(saylink, false, "Apply").c_str(),
saylink.c_str()
);
}
if (edit_option == "respawn_time" && apply_set == "apply") {
std::string spawn2_ids_string = implode(",", spawn2_ids);
if (spawn2_ids_string.empty()) {
c->Message(Chat::Red, "Error: Ran into an unknown error compiling Spawn2 IDs");
return;
}
database.QueryDatabase(
fmt::format(
SQL(
UPDATE spawn2 SET respawntime = {} WHERE id IN({})
),
std::stoi(edit_value),
spawn2_ids_string
)
);
c->Message(Chat::Yellow, "Updated [%i] spawns", found_count);
}
}
void command_findaliases(Client *c, const Seperator *sep)
{
int arguments = sep->argnum;
@ -1225,6 +1095,7 @@ void command_bot(Client *c, const Seperator *sep)
#include "gm_commands/showzonepoints.cpp"
#include "gm_commands/shutdown.cpp"
#include "gm_commands/spawn.cpp"
#include "gm_commands/spawneditmass.cpp"
#include "gm_commands/spawnfix.cpp"
#include "gm_commands/spawnstatus.cpp"
#include "gm_commands/spellinfo.cpp"

View File

@ -0,0 +1,148 @@
#include "../client.h"
void command_spawneditmass(Client *c, const Seperator *sep)
{
int arguments = sep->argnum;
if (arguments < 3) {
c->Message(Chat::Yellow, "Usage: #spawneditmass [Search Criteria] [Edit Option] [Edit Value] [Apply]");
return;
}
std::string search_criteria = sep->arg[1];
std::string edit_option = sep->arg[2];
std::string edit_value = sep->arg[3];
bool is_apply = sep->arg[4] ? !strcasecmp(sep->arg[4], "apply") : false;
if (edit_option != "respawn_time") { // Remove this when other options are added, here so people know there's only one option at the moment.
c->Message(Chat::Yellow, "The only edit option currently implemented is \"respawn_time\", more will be added in the future.");
return;
}
auto query = fmt::format(
SQL(
SELECT
npc_types.id,
npc_types.name,
spawn2.respawntime,
spawn2.id
FROM
npc_types
JOIN spawnentry ON spawnentry.npcID = npc_types.id
JOIN spawn2 ON spawn2.spawngroupID = spawnentry.spawngroupID
WHERE
spawn2.zone = '{}' AND spawn2.version = {}
GROUP BY npc_types.id
),
zone->GetShortName(),
zone->GetInstanceVersion()
);
std::vector<std::string> spawn2_ids;
auto results = database.QueryDatabase(query);
c->Message(
Chat::Yellow,
fmt::format(
"Spawn Edit Mass ({})",
is_apply ? "Applying" : "Searching"
).c_str()
);
for (auto row : results) {
std::string npc_id = row[0];
std::string npc_name = row[1];
std::string respawn_time = row[2];
std::string spawn2_id = row[3];
if (npc_name.find(search_criteria) == std::string::npos) {
continue;
}
c->Message(
Chat::Yellow,
fmt::format(
"Spawn2 ID: {} NPC ID: {} Name: {} Respawn Time: {} ({})",
commify(spawn2_id),
commify(npc_id),
npc_name,
ConvertSecondsToTime(std::stoi(respawn_time)),
commify(respawn_time)
).c_str()
);
spawn2_ids.push_back(spawn2_id);
}
if (!spawn2_ids.size()) {
c->Message(
Chat::Yellow,
fmt::format(
"No NPC Spawn2 entries were found matching '{}' in this zone.",
search_criteria
).c_str()
);
return;
}
c->Message(
Chat::Yellow,
fmt::format(
"Found {} NPC Spawn2 entr{} matching '{}' in this zone",
spawn2_ids.size(),
spawn2_ids.size() != 1 ? "ies" : "y",
search_criteria
).c_str()
);
if (!is_apply) {
auto edit_link = fmt::format(
"#spawneditmass {} {} {} apply",
search_criteria,
edit_option,
edit_value
);
c->Message(
Chat::Yellow,
fmt::format(
"To apply these changes, click {} or type \"{}\".",
EQ::SayLinkEngine::GenerateQuestSaylink(edit_link, false, "apply"),
edit_link
).c_str()
);
}
if (edit_option == "respawn_time" && is_apply) {
auto spawn2_ids_string = implode(", ", spawn2_ids);
if (spawn2_ids_string.empty()) {
c->Message(
Chat::Yellow,
fmt::format(
"No NPC Spawn2 entries were found matching '{}' in this zone.",
search_criteria
).c_str()
);
return;
}
database.QueryDatabase(
fmt::format(
SQL(
UPDATE spawn2 SET respawntime = {} WHERE id IN ({})
),
std::stoi(edit_value),
spawn2_ids_string
)
);
c->Message(
Chat::Yellow,
fmt::format(
"Updated {} NPC spawn{} in this zone.",
spawn2_ids.size(),
spawn2_ids.size() != 1 ? "s" : ""
).c_str()
);
}
}