[Commands] Cleanup #setfaction Command. (#1792)

- Cleanup message and logic.
- Doesn't allow you to use invalid faction IDs anymore.
This commit is contained in:
Kinglykrab
2021-11-21 10:11:03 -05:00
committed by GitHub
parent d73194c1f6
commit 51fb46556d
2 changed files with 42 additions and 11 deletions
+41 -10
View File
@@ -2,19 +2,50 @@
void command_setfaction(Client *c, const Seperator *sep)
{
if ((sep->arg[1][0] == 0 || strcasecmp(sep->arg[1], "*") == 0) ||
((c->GetTarget() == 0) || (c->GetTarget()->IsClient()))) {
c->Message(Chat::White, "Usage: #setfaction [faction number]");
int arguments = sep->argnum;
if (!arguments || !sep->IsNumber(1)) {
c->Message(Chat::White, "Usage: #setfaction [Faction ID]");
return;
}
if (
!c->GetTarget() ||
(
c->GetTarget() &&
c->GetTarget()->IsClient()
)
) {
c->Message(Chat::White, "You must target an NPC to use this command.");
return;
}
auto npcTypeID = c->GetTarget()->CastToNPC()->GetNPCTypeID();
c->Message(Chat::Yellow, "Setting NPC %u to faction %i", npcTypeID, atoi(sep->argplus[1]));
NPC* target = c->GetTarget()->CastToNPC();
auto npc_id = target->GetNPCTypeID();
auto faction_id = std::stoi(sep->arg[1]);
auto faction_name = content_db.GetFactionName(faction_id);
if (!faction_name.empty()) {
c->Message(
Chat::White,
fmt::format(
"Faction Changed | Name: {} ({}) Faction: {} ({}).",
target->GetCleanName(),
npc_id,
content_db.GetFactionName(faction_id),
faction_id
).c_str()
);
std::string query = StringFormat(
"UPDATE npc_types SET npc_faction_id = %i WHERE id = %i",
atoi(sep->argplus[1]), npcTypeID
);
content_db.QueryDatabase(query);
std::string query = fmt::format(
"UPDATE npc_types SET npc_faction_id = {} WHERE id = {}",
faction_id,
npc_id
);
content_db.QueryDatabase(query);
} else {
c->Message(
Chat::White,
"Invalid Faction ID, please specify a valid Faction ID."
);
}
}