Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Help Command Pagination #1581

Merged
Merged
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 76 additions & 18 deletions dGame/dUtilities/SlashCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,90 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& chat, Entity*

void GMZeroCommands::Help(Entity* entity, const SystemAddress& sysAddr, const std::string args) {
std::ostringstream feedback;
if (args.empty()) {
feedback << "----- Commands -----";
for (const auto& [alias, command] : CommandInfos) {
// TODO: Limit displaying commands based on GM level they require
if (command.requiredLevel > entity->GetGMLevel()) continue;
LOG("Help command: %s", alias.c_str());
constexpr size_t pageSize = 10;

// Trim the args
std::string trimmedArgs = args;
trimmedArgs.erase(trimmedArgs.begin(), std::find_if_not(trimmedArgs.begin(), trimmedArgs.end(), [](unsigned char ch) {
return std::isspace(ch);
}));
trimmedArgs.erase(std::find_if_not(trimmedArgs.rbegin(), trimmedArgs.rend(), [](unsigned char ch) {
return std::isspace(ch);
}).base(), trimmedArgs.end());

// Check if the argument is a number (for pagination)
if (trimmedArgs.empty() || std::all_of(trimmedArgs.begin(), trimmedArgs.end(), ::isdigit)) {
Tiernan-Alderman marked this conversation as resolved.
Show resolved Hide resolved
std::optional<int> parsedPage = trimmedArgs.empty() ? 1 : std::stoi(trimmedArgs);
size_t page = parsedPage.value_or(1);

std::vector<std::pair<std::string, Command>> accessibleCommands;
for (const auto& [commandName, command] : CommandInfos) {
if (command.requiredLevel <= entity->GetGMLevel()) {
accessibleCommands.emplace_back(commandName, command);
}
}
std::sort(accessibleCommands.begin(), accessibleCommands.end(),
[](const auto& a, const auto& b) {
return a.second.aliases.at(0) < b.second.aliases.at(0);
});

size_t totalPages = (accessibleCommands.size() + pageSize - 1) / pageSize;

if (page < 1 || page > totalPages) {
feedback << "Invalid page number. Total pages: " << totalPages;
GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedback.str()));
return;
}
Tiernan-Alderman marked this conversation as resolved.
Show resolved Hide resolved

size_t startIdx = (page - 1) * pageSize;
size_t endIdx = std::min(startIdx + pageSize, accessibleCommands.size());

feedback << "----- Commands (Page " << page << " of " << totalPages << ") -----";
for (size_t i = startIdx; i < endIdx; ++i) {
const auto& [alias, command] = accessibleCommands[i];
feedback << "\n/" << alias << ": " << command.help;
}
} else {
auto it = CommandInfos.find(args);
if (it != CommandInfos.end() && entity->GetGMLevel() >= it->second.requiredLevel) {
feedback << "----- " << args << " -----\n";
feedback << it->second.info;
if (it->second.aliases.size() > 1) {
feedback << "\nAliases: ";
for (size_t i = 0; i < it->second.aliases.size(); i++) {

const auto feedbackStr = feedback.str();
if (!feedbackStr.empty()) {
GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedbackStr));
}
return;
}

// If args is not a number, check if it matches a command alias
std::string commandToFind = trimmedArgs;
bool foundCommand = false;
for (const auto& [commandName, command] : CommandInfos) {
if (commandName == commandToFind || std::find(command.aliases.begin(), command.aliases.end(), commandToFind) != command.aliases.end()) {
if (entity->GetGMLevel() < command.requiredLevel) {
feedback << "You do not have the required level to view this command info.";
foundCommand = true;
break;
}

foundCommand = true;
feedback << "----- " << command.aliases.at(0) << " Info -----\n";
feedback << command.info << "\n";
if (command.aliases.size() > 1) {
feedback << "Aliases: ";
for (size_t i = 0; i < command.aliases.size(); ++i) {
if (i > 0) feedback << ", ";
feedback << it->second.aliases[i];
feedback << command.aliases[i];
}
}
} else if (entity->GetGMLevel() > eGameMasterLevel::CIVILIAN) {
feedback << "Command " << std::quoted(args) << " does not exist!";
break;
}
}

if (!foundCommand) {
feedback << "Command not found.";
}

const auto feedbackStr = feedback.str();
if (!feedbackStr.empty()) GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedbackStr));
if (!feedbackStr.empty()) {
GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedbackStr));
}
}

void SlashCommandHandler::SendAnnouncement(const std::string& title, const std::string& message) {
Expand Down