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