Skip to content

Commit

Permalink
Help Alphabetization
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiernan-Alderman committed May 13, 2024
1 parent 0de9672 commit 1c38ddd
Showing 1 changed file with 9 additions and 45 deletions.
54 changes: 9 additions & 45 deletions dGame/dUtilities/SlashCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,55 +867,19 @@ 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 descriptions.",
.aliases = { "help", "h"},
.aliases = { "help", "h"},
.handle = [](Entity* entity, const SystemAddress& sysAddr, const std::string& args) {
std::ostringstream feedback;
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;
std::ostringstream feedback;
feedback << "----- Commands -----";

// Loop through CommandInfos and display commands the player can access
for (const auto& [alias, command] : CommandInfos) {
if (command.requiredLevel <= entity->GetGMLevel()) {
LOG("Help command: %s", alias.c_str());
feedback << "\n/" << alias << ": " << command.help;
}
}

// 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;
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

0 comments on commit 1c38ddd

Please sign in to comment.