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 8 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
81 changes: 48 additions & 33 deletions dGame/dUtilities/SlashCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "dServer.h"

namespace {
std::vector<Command> CommandInfos;
std::map<std::string, Command> CommandInfos;
std::map<std::string, Command> RegisteredCommands;
}

Expand All @@ -38,9 +38,8 @@ void SlashCommandHandler::RegisterCommand(Command command) {
continue;
}
}

CommandInfos.push_back(command);
};
CommandInfos[command.aliases[0]] = command;
}

void SlashCommandHandler::HandleChatCommand(const std::u16string& chat, Entity* entity, const SystemAddress& sysAddr) {
auto input = GeneralUtils::UTF16ToWTF8(chat);
Expand Down Expand Up @@ -74,39 +73,55 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& chat, Entity*
}
}

// This commands in here so we can access the CommandInfos to display info
void GMZeroCommands::Help(Entity* entity, const SystemAddress& sysAddr, const std::string args) {
std::ostringstream feedback;
if (args.empty()) {
feedback << "----- Commands -----";
for (const auto& command : CommandInfos) {
// TODO: Limit displaying commands based on GM level they require
if (command.requiredLevel > entity->GetGMLevel()) continue;
LOG("Help command: %s", command.aliases[0].c_str());
feedback << "\n/" << command.aliases[0] << ": " << command.help;
}
} else {
bool foundCommand = false;
for (const auto& command : CommandInfos) {
if (std::ranges::find(command.aliases, args) == command.aliases.end()) continue;

if (entity->GetGMLevel() < command.requiredLevel) break;
foundCommand = true;
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];
}
constexpr size_t pageSize = 10; // Number of commands per page

// Filter CommandInfos based on player's GM level
std::vector<std::pair<std::string, Command>> accessibleCommands;
std::copy_if(CommandInfos.begin(), CommandInfos.end(), std::back_inserter(accessibleCommands),
[&](const auto& pair) {
return pair.second.requiredLevel <= entity->GetGMLevel();
});

// Calculate total number of pages based on accessible commands
size_t totalPages = (accessibleCommands.size() + pageSize - 1) / pageSize;

size_t page = 1; // Default to first page

// Check if page number is provided
if (!args.empty()) {
try {
page = std::stoi(args);
} catch (const std::exception&) {
feedback << "Invalid page number.";
GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedback.str()));
return;
}
Tiernan-Alderman marked this conversation as resolved.
Show resolved Hide resolved
}

// Let GameMasters know if the command doesn't exist
if (!foundCommand && entity->GetGMLevel() > eGameMasterLevel::CIVILIAN) feedback << "Command " << std::quoted(args) << " does not exist!";
// Check if requested page number is valid
if (page < 1 || page > totalPages) {
feedback << "Invalid page number. Total pages: " << totalPages;
GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedback.str()));
return;
}

// Calculate starting and ending index for commands on the current page
size_t startIdx = (page - 1) * pageSize;
size_t endIdx = std::min(startIdx + pageSize, accessibleCommands.size());

// Display commands for the current page
feedback << "----- Commands (Page " << page << ") -----";
size_t count = 0;
Tiernan-Alderman marked this conversation as resolved.
Show resolved Hide resolved
for (size_t i = startIdx; i < endIdx; ++i) {
const auto& [alias, command] = accessibleCommands[i];
LOG("Help command: %s", alias.c_str());
feedback << "\n/" << alias << ": " << command.help;
++count;
}

// Send feedback text
const auto feedbackStr = feedback.str();
if (!feedbackStr.empty()) GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedbackStr));
}
Expand Down Expand Up @@ -900,7 +915,7 @@ void SlashCommandHandler::Startup() {

Command HelpCommand{
.help = "Display command info",
.info = "If a command is given, display detailed info on that command. Otherwise display a list of commands with short desctiptions.",
.info = "If a command is given, display detailed info on that command. Otherwise display a list of commands with short descriptions.",
.aliases = { "help", "h"},
.handle = GMZeroCommands::Help,
.requiredLevel = eGameMasterLevel::CIVILIAN
Expand Down