[Commands] Cleanup #findnpctype Command. (#1714)

* [Commands] Cleanup #findnpctype Command.
- Cleanup message and logic.

* Logic cleanup, found_count is always greater than 0.

* Fix order.

* Add return.
This commit is contained in:
Kinglykrab 2021-11-12 09:19:43 -05:00 committed by GitHub
parent f591378ed3
commit 908c6c18af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5144,47 +5144,75 @@ void command_invsnapshot(Client *c, const Seperator *sep)
void command_findnpctype(Client *c, const Seperator *sep) void command_findnpctype(Client *c, const Seperator *sep)
{ {
if(sep->arg[1][0] == 0) { int arguments = sep->argnum;
c->Message(Chat::White, "Usage: #findnpctype [search criteria]"); if (!arguments) {
c->Message(Chat::White, "Usage: #findnpctype [Search Criteria]");
return; return;
} }
std::string query; std::string query;
std::string search_criteria = sep->arg[1];
int id = atoi((const char *)sep->arg[1]); if (sep->IsNumber(1)) {
if (id == 0) // If id evaluates to 0, then search as if user entered a string. query = fmt::format(
query = StringFormat("SELECT id, name FROM npc_types WHERE name LIKE '%%%s%%'", sep->arg[1]); "SELECT id, name FROM npc_types WHERE id = {}",
else // Otherwise, look for just that npc id. search_criteria
query = StringFormat("SELECT id, name FROM npc_types WHERE id = %i", id); );
} else {
query = fmt::format(
"SELECT id, name FROM npc_types WHERE name LIKE '%%{}%%'",
search_criteria
);
}
auto results = content_db.QueryDatabase(query); auto results = content_db.QueryDatabase(query);
if (!results.Success()) { if (!results.Success() || !results.RowCount()) {
c->Message (0, "Error querying database."); c->Message(
c->Message (0, query.c_str()); Chat::White,
} fmt::format(
"No matches found for '{}'.",
search_criteria
).c_str()
);
return;
}
if (results.RowCount() == 0) // No matches found. int found_count = 0;
c->Message (0, "No matches found for %s.", sep->arg[1]);
// If query runs successfully. for (auto row : results) {
int count = 0; int found_number = (found_count + 1);
const int maxrows = 20; if (found_count == 20) {
break;
}
// Process each row returned. c->Message(
for (auto row = results.begin(); row != results.end(); ++row) { Chat::White,
// Limit to returning maxrows rows. fmt::format(
if (++count > maxrows) { "NPC {} | {} ({})",
c->Message (0, "%i npc types shown. Too many results.", maxrows); found_number,
break; row[1],
} row[0]
).c_str()
);
found_count++;
}
c->Message (0, " %s: %s", row[0], row[1]); if (found_count == 20) {
} c->Message(Chat::White, "20 NPCs were found, max reached.");
} else {
// If we did not hit the maxrows limit. auto npc_message = (
if (count <= maxrows) found_count == 1 ?
c->Message (0, "Query complete. %i rows shown.", count); "An NPC was" :
fmt::format("{} NPCs were", found_count)
);
c->Message(
Chat::White,
fmt::format(
"{} found.",
npc_message
).c_str()
);
}
} }
void command_faction(Client *c, const Seperator *sep) void command_faction(Client *c, const Seperator *sep)