Skip to content

Commit

Permalink
tweak: hacky fix for spaces in usernames for /AddAdmin and /RemoveAdmin
Browse files Browse the repository at this point in the history
also includes a fix for crashing after 10 commands are sent to the server
  • Loading branch information
ToeKneeRED committed Aug 18, 2024
1 parent 57aeaca commit f771f8c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Code/components/console/ConsoleRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void ConsoleRegistry::StoreCommandInHistory(const TiltedPhoques::String& acLine)
// Do some housekeeping.
if (m_commandHistory.size() == 10)
{
m_commandHistory.erase(m_commandHistory.end());
m_commandHistory.pop_back();
}
}

Expand Down
53 changes: 49 additions & 4 deletions Code/server/GameServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,36 @@ void GameServer::BindServerCommands()

if (!playerFound)
{
out->warn("{} is not a valid player", cUsername.c_str());
// space in username handling
String backupUsername = cUsername;
if (cUsername.find('_') != std::string::npos)
{

while (backupUsername.find('_') != std::string::npos)
{
std::ranges::replace(backupUsername, '_', ' ');
}

PlayerManager::Get()->ForEach([&](const Player* apPlayer) {
if (apPlayer->GetUsername() == backupUsername)
{
AddAdminSession(apPlayer->GetConnectionId());
out->info("{} admin privileges added", backupUsername.c_str());
playerFound = true;
return;
}
});

}
if (!playerFound)
out->warn("{} is not an admin", backupUsername.c_str());
}
});
m_commands.RegisterCommand<std::string>(
"RemoveAdmin", "Remove admin privileges from player", [&](Console::ArgStack& aStack) {
auto out = spdlog::get("ConOut");

const auto& cUsername = aStack.Pop<String>();

bool playerFound = false;

for (const auto& cAdmin : m_adminSessions)
Expand All @@ -437,11 +458,35 @@ void GameServer::BindServerCommands()

if (!playerFound)
{
out->warn("{} is not an admin", cUsername.c_str());
// space in username handling
String backupUsername = cUsername;
if (cUsername.find('_') != std::string::npos)
{

while (backupUsername.find('_') != std::string::npos)
{
std::ranges::replace(backupUsername, '_', ' ');
}

for (const auto& cAdmin : m_adminSessions)
{
Player* pPlayer = PlayerManager::Get()->GetByConnectionId(cAdmin);
if (pPlayer->GetUsername() == backupUsername)
{
RemoveAdminSession(pPlayer->GetConnectionId());
out->info("{} admin privileges revoked", backupUsername.c_str());
playerFound = true;
break;
}
}

}
if (!playerFound)
out->warn("{} is not an admin", backupUsername.c_str());
}
});
m_commands.RegisterCommand<>(
"admins", "List all admins", [&](Console::ArgStack& aStack) {
"admins", "List all admins", [&](Console::ArgStack&) {
auto out = spdlog::get("ConOut");
if (m_adminSessions.size() == 0)
{
Expand Down

0 comments on commit f771f8c

Please sign in to comment.