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 11 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
71 changes: 44 additions & 27 deletions dGame/dUtilities/SlashCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,52 @@ 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());
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++) {
if (i > 0) feedback << ", ";
feedback << it->second.aliases[i];
}
}
} else if (entity->GetGMLevel() > eGameMasterLevel::CIVILIAN) {
feedback << "Command " << std::quoted(args) << " does not exist!";
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()) {
std::optional<int> parsedPage = GeneralUtils::TryParse<int>(args);
if (parsedPage.has_value()) {
page = *parsedPage;
} else {
feedback << "Invalid page number.";
GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedback.str()));
return;
}
Tiernan-Alderman marked this conversation as resolved.
Show resolved Hide resolved
}

// 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 << ") -----";
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;
}

// Send feedback text
const auto feedbackStr = feedback.str();
if (!feedbackStr.empty()) GameMessages::SendSlashCommandFeedbackText(entity, GeneralUtils::ASCIIToUTF16(feedbackStr));
}
Expand All @@ -120,11 +142,6 @@ void SlashCommandHandler::SendAnnouncement(const std::string& title, const std::
bitStream.Write<char>(character);
}

bitStream.Write<uint32_t>(message.size());
Tiernan-Alderman marked this conversation as resolved.
Show resolved Hide resolved
for (auto character : message) {
Tiernan-Alderman marked this conversation as resolved.
Show resolved Hide resolved
bitStream.Write<char>(character);
}

Game::chatServer->Send(&bitStream, SYSTEM_PRIORITY, RELIABLE, 0, Game::chatSysAddr, false);
}

Expand Down