improvements

pass through added code for optimizations and cleanup as well as reduce the amount of scoping for readability and maintainability
This commit is contained in:
David Markowitz 2024-04-03 03:55:52 -07:00
parent 94ed21204e
commit 01ab936dbb

View File

@ -79,6 +79,7 @@
#include "CDZoneTableTable.h" #include "CDZoneTableTable.h"
#include "ePlayerFlag.h" #include "ePlayerFlag.h"
#include "dNavMesh.h" #include "dNavMesh.h"
#include <ranges>
namespace { namespace {
std::vector<Command> CommandInfos; std::vector<Command> CommandInfos;
@ -86,54 +87,57 @@ namespace {
} }
void SlashCommandHandler::RegisterCommand(Command command) { void SlashCommandHandler::RegisterCommand(Command command) {
if (command.aliases.empty()) return; if (command.aliases.empty()) {
LOG("Command %s has no aliases! Skipping!", command.help.c_str());
return;
}
LOG_DEBUG("Registering SlashCommand: %s", command.aliases[0].c_str()); for (const auto& alias : command.aliases) {
std::vector<std::string> toRemove; LOG_DEBUG("Registering command %s", alias.c_str());
for (auto& alias : command.aliases) { auto [_, success] = RegisteredCommands.try_emplace(alias, command);
if (alias.empty()) continue; // Don't allow duplicate commands
if (RegisteredCommands.contains(alias)){ if (!success) {
LOG_DEBUG("Command alias %s is already registered! Skipping!", alias.c_str()); LOG_DEBUG("Command alias %s is already registered! Skipping!", alias.c_str());
// denote it to be removed
toRemove.push_back(alias);
continue; continue;
} }
RegisteredCommands.emplace(make_pair(alias, command));
}
// Actually remove the duplicate aliases here
for (auto& removing : toRemove) {
command.aliases.erase(std::find(std::cbegin(command.aliases), std::cend(command.aliases), removing));
} }
CommandInfos.push_back(command); CommandInfos.push_back(command);
}; };
void SlashCommandHandler::HandleChatCommand(const std::u16string& chat, Entity* entity, const SystemAddress& sysAddr) { void SlashCommandHandler::HandleChatCommand(const std::u16string& chat, Entity* entity, const SystemAddress& sysAddr) {
auto input = GeneralUtils::UTF16ToWTF8(chat); auto input = GeneralUtils::UTF16ToWTF8(chat);
if (input.empty() || input.front() != u'/') return; if (input.empty() || input.front() != '/') return;
std::string command = input.substr(1, input.find(' ') - 1); const auto pos = input.find(' ');
std::string command = input.substr(1, pos - 1);
std::string args = input.substr(input.find(' ') + 1, std::string::npos); std::string args;
if (args.front() == '/') args.clear(); // make sure the space exists and isn't the last character
LOG("Handling command \"%s\" with args \"%s\"", command.c_str(), args.c_str()); if (pos != std::string::npos && pos != input.size()) args = input.substr(input.find(' ') + 1);
LOG_DEBUG("Handling command \"%s\" with args \"%s\"", command.c_str(), args.c_str());
if (RegisteredCommands.contains(command)) { const auto commandItr = RegisteredCommands.find(command);
if (entity->GetGMLevel() >= RegisteredCommands[command].requiredLevel) { std::string error;
if (commandItr != RegisteredCommands.end()) {
auto& [alias, commandHandle] = *commandItr;
if (entity->GetGMLevel() >= commandHandle.requiredLevel) {
Database::Get()->InsertSlashCommandUsage(entity->GetObjectID(), input); Database::Get()->InsertSlashCommandUsage(entity->GetObjectID(), input);
RegisteredCommands[command].handle(entity, sysAddr, args); commandHandle.handle(entity, sysAddr, args);
return;
} else { } else {
// We don't need to tell normies they aren't high enough level // We don't need to tell civilians they aren't high enough level
if (entity->GetGMLevel() == eGameMasterLevel::CIVILIAN) return; if (entity->GetGMLevel() != eGameMasterLevel::CIVILIAN) {
std::ostringstream feedback; error = "You are not high enough GM level to use \"" + command + "\"";
feedback << "You are not high enough GM level to use " << std::quoted(command) << ""; }
GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedback.str()));
} }
} else { } else {
// We don't need to tell normies commands don't exist // We don't need to tell civilians commands don't exist
if (entity->GetGMLevel() == eGameMasterLevel::CIVILIAN) return; if (entity->GetGMLevel() == eGameMasterLevel::CIVILIAN) {
std::ostringstream feedback; error = "Command " + command + " does not exist!";
feedback << "Command " << std::quoted(command) << " does not exist!"; }
GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedback.str())); }
if (!error.empty()) {
GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(error));
} }
} }
@ -170,7 +174,7 @@ void SlashCommandHandler::Startup() {
Command KillCommand{ Command KillCommand{
.help = "Smash a user", .help = "Smash a user",
.info = "Smashes the character whom the given user is playing", .info = "Smashes the character whom the given user is playing",
.aliases = { "kill" }, .aliases = { "kill", "tsc" },
.handle = DEVGMCommands::Kill, .handle = DEVGMCommands::Kill,
.requiredLevel = eGameMasterLevel::DEVELOPER .requiredLevel = eGameMasterLevel::DEVELOPER
}; };
@ -1002,41 +1006,42 @@ void SlashCommandHandler::Startup() {
} }
namespace GMZeroCommands { namespace GMZeroCommands {
// The star delimiter is to be used for marking the start and end of a localized string.
void Help(Entity* entity, const SystemAddress& sysAddr, const std::string args) { void Help(Entity* entity, const SystemAddress& sysAddr, const std::string args) {
std::ostringstream feedback;
if (args.empty()) { if (args.empty()) {
std::ostringstream helpMessage; feedback << "----- Commands -----\n";
helpMessage << "----- Commands -----\n*"; for (size_t i = 0; i < CommandInfos.size(); i++) {
for (auto& command : CommandInfos) { const auto& command = CommandInfos[i];
// TODO: Limit displaying commands based on GM level they require // TODO: Limit displaying commands based on GM level they require
if (command.requiredLevel > entity->GetGMLevel()) continue; if (command.requiredLevel > entity->GetGMLevel()) continue;
helpMessage << "/" << command.aliases[0] << ": " << command.help << "\n*"; if (i > 0) feedback << '\n';
feedback << "/" << command.aliases[0] << ": *" << command.help << '*';
} }
GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(helpMessage.str().substr(0, helpMessage.str().size() - 2)));
} else { } else {
bool foundCommand = false; bool foundCommand = false;
for (auto& command : CommandInfos) { for (const auto& command : CommandInfos) {
if (std::find(command.aliases.begin(), command.aliases.end(), args) != command.aliases.end()) { if (std::ranges::find(command.aliases, args) == command.aliases.end()) continue;
if (entity->GetGMLevel() < command.requiredLevel) break;
foundCommand = true; foundCommand = true;
if (entity->GetGMLevel() >= command.requiredLevel) { feedback << "----- " << command.aliases.at(0) << " -----\n";
std::ostringstream commandDetails; // info can be a localizable string
commandDetails << "----- " << command.aliases[0] << " -----\n*"; feedback << '*' << command.info << "*";
commandDetails << command.info << "\n*"; if (command.aliases.size() == 1) break;
if (command.aliases.size() > 1) {
commandDetails << "Aliases: "; feedback << "\nAliases: ";
std::copy(command.aliases.begin(), command.aliases.end(), std::ostream_iterator<std::string>(commandDetails, ", ")); for (size_t i = 0; i < command.aliases.size(); i++) {
} if (i > 0) feedback << ", ";
GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(commandDetails.str().substr(0, commandDetails.str().size() - 2))); feedback << command.aliases[i];
}
} }
} }
if (!foundCommand && entity->GetGMLevel() > eGameMasterLevel::CIVILIAN) { // Let GameMasters know if the command doesn't exist
std::ostringstream feedback; if (!foundCommand && entity->GetGMLevel() > eGameMasterLevel::CIVILIAN) feedback << "Command " << std::quoted(args) << " does not exist!";
feedback << "Command " << std::quoted(args) << " does not exist!"; }
GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedback.str())); GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedback.str()));
} }
}
}
void Pvp(Entity* entity, const SystemAddress& sysAddr, const std::string args) { void Pvp(Entity* entity, const SystemAddress& sysAddr, const std::string args) {
auto* character = entity->GetComponent<CharacterComponent>(); auto* character = entity->GetComponent<CharacterComponent>();