Skip to content

Commit

Permalink
tweak: review changes minus clang format
Browse files Browse the repository at this point in the history
  • Loading branch information
ToeKneeRED committed Aug 21, 2024
1 parent 62e84a5 commit 4b2fc01
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 86 deletions.
10 changes: 8 additions & 2 deletions Code/client/Services/Generic/TransportService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ TransportService::TransportService(World& aWorld, entt::dispatcher& aDispatcher)
{
m_updateConnection = m_dispatcher.sink<UpdateEvent>().connect<&TransportService::HandleUpdate>(this);
m_settingsChangeConnection = m_dispatcher.sink<NotifySettingsChange>().connect<&TransportService::HandleNotifySettingsChange>(this);
m_connectedConnection = m_dispatcher.sink<ConnectedEvent>().connect<&TransportService::HandleConnect>(this);
m_connectedConnection = m_dispatcher.sink<ConnectedEvent>().connect<&TransportService::HandleConnected>(this);
m_disconnectedConnection = m_dispatcher.sink<DisconnectedEvent>().connect<&TransportService::HandleDisconnected>(this);

m_connected = false;

Expand Down Expand Up @@ -182,11 +183,16 @@ void TransportService::HandleUpdate(const UpdateEvent& acEvent) noexcept
Update();
}

void TransportService::HandleConnect(const ConnectedEvent& acEvent) noexcept
void TransportService::HandleConnected(const ConnectedEvent& acEvent) noexcept
{
m_localPlayerId = acEvent.PlayerId;
}

void TransportService::HandleDisconnected(const DisconnectedEvent& acEvent) noexcept
{
m_localPlayerId = NULL;
}

void TransportService::HandleAuthenticationResponse(const AuthenticationResponse& acMessage) noexcept
{
using AR = AuthenticationResponse::ResponseType;
Expand Down
5 changes: 4 additions & 1 deletion Code/client/Services/TransportService.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Events/ConnectedEvent.h"
#include "Events/DisconnectedEvent.h"

#include <atomic>
#include <Client.hpp>
Expand Down Expand Up @@ -39,7 +40,8 @@ struct TransportService : Client
protected:
// Event handlers
void HandleUpdate(const UpdateEvent& acEvent) noexcept;
void HandleConnect(const ConnectedEvent& acEvent) noexcept;
void HandleConnected(const ConnectedEvent& acEvent) noexcept;
void HandleDisconnected(const DisconnectedEvent& acEvent) noexcept;

// Packet handlers
void HandleAuthenticationResponse(const AuthenticationResponse& acMessage) noexcept;
Expand All @@ -56,5 +58,6 @@ struct TransportService : Client
entt::scoped_connection m_sendServerMessageConnection;
entt::scoped_connection m_settingsChangeConnection;
entt::scoped_connection m_connectedConnection;
entt::scoped_connection m_disconnectedConnection;
std::function<void(UniquePtr<ServerMessage>&)> m_messageHandlers[kServerOpcodeMax];
};
25 changes: 25 additions & 0 deletions Code/server/Game/PlayerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,31 @@ Player const* PlayerManager::GetById(uint32_t aId) const noexcept
return nullptr;
}

Player* PlayerManager::GetByUsername(const String& acUsername) const noexcept
{
auto itor = std::begin(m_players);
const auto end = std::end(m_players);

for (; itor != end; ++itor)
if (itor.value()->GetUsername() == acUsername)
return itor.value().get();

return nullptr;
}


Player const* PlayerManager::GetByUsername(const String& acUsername) noexcept
{
auto itor = std::begin(m_players);
const auto end = std::end(m_players);

for (; itor != end; ++itor)
if (itor.value()->GetUsername() == acUsername)
return itor.value().get();

return nullptr;
}

uint32_t PlayerManager::Count() const noexcept
{
return static_cast<uint32_t>(m_players.size());
Expand Down
3 changes: 3 additions & 0 deletions Code/server/Game/PlayerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ struct PlayerManager
Player* GetById(uint32_t aId) noexcept;
Player const* GetById(uint32_t aId) const noexcept;

Player* GetByUsername(const String& acUsername) const noexcept;
Player const* GetByUsername(const String& acUsername) noexcept;

uint32_t Count() const noexcept;

template <class T> void ForEach(const T& acFunctor) noexcept
Expand Down
158 changes: 83 additions & 75 deletions Code/server/GameServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,6 @@ void GameServer::Kill()
m_requestStop = true;
}

void GameServer::RemoveAdminSession(ConnectionId_t acSession) noexcept
{
if (m_adminSessions.contains(acSession))
{
m_adminSessions.erase(acSession);
}
}

bool GameServer::CheckMoPo()
{
if (!bEnableModCheck)
Expand Down Expand Up @@ -393,96 +385,66 @@ void GameServer::BindServerCommands()
});

m_commands.RegisterCommand<std::string>(
"AddAdmin", "Add admin privileges from player", [&](Console::ArgStack& aStack) {
"AddAdmin", "Add admin privileges to player", [&](Console::ArgStack& aStack) {
auto out = spdlog::get("ConOut");

const auto& cUsername = aStack.Pop<String>();
if (GetAdminByUsername(cUsername))
{
out->info("{} is already an admin", cUsername.c_str());
return;
}

bool playerFound = false;
auto* pPlayer = PlayerManager::Get()->GetByUsername(cUsername);
if (pPlayer)
{
AddAdminSession(pPlayer->GetConnectionId());
out->info("{} admin privileges added", cUsername.c_str());
}
else
{
// retry after sanitizing username
String backupUsername = SanitizeUsername(cUsername);
pPlayer = PlayerManager::Get()->GetByUsername(backupUsername);

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

if (!playerFound)
{
// space in username handling
String backupUsername = cUsername;
if (cUsername.find('_') != std::string::npos)
else
{

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

out->warn("{} is not a valid player", backupUsername.c_str());
}
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;
auto* pPlayer = GetAdminByUsername(cUsername);

for (const auto& cAdmin : m_adminSessions)
if (pPlayer)
{
RemoveAdminSession(pPlayer->GetConnectionId());
out->info("{} admin privileges revoked", cUsername.c_str());
}
else
{
Player* pPlayer = PlayerManager::Get()->GetByConnectionId(cAdmin);
if (pPlayer->GetUsername() == cUsername)
// retry after sanitizing username
String backupUsername = SanitizeUsername(cUsername);
pPlayer = GetAdminByUsername(backupUsername);

if (pPlayer)
{
RemoveAdminSession(pPlayer->GetConnectionId());
out->info("{} admin privileges revoked", cUsername.c_str());
playerFound = true;
break;
}
}

if (!playerFound)
{
// space in username handling
String backupUsername = cUsername;
if (cUsername.find('_') != std::string::npos)
else
{

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<>(
Expand All @@ -497,9 +459,17 @@ void GameServer::BindServerCommands()
String output = "Admins: ";
bool _first = true;

for (const auto& cAdmin : m_adminSessions)
for (const auto& cAdminSession : m_adminSessions)
{
const auto& cUsername = PlayerManager::Get()->GetByConnectionId(cAdmin)->GetUsername();
auto* pPlayer = PlayerManager::Get()->GetByConnectionId(cAdminSession);

if (!pPlayer)
{
out->error("Admin session not found: {}", cAdminSession);
continue;
}

const auto& cUsername = pPlayer->GetUsername();

if (_first)
{
Expand Down Expand Up @@ -1070,3 +1040,41 @@ void GameServer::UpdateTitle() const
std::cout << "\033]0;" << title << "\007";
#endif
}

Player* GameServer::GetAdminByUsername(const String& acUsername) const noexcept
{
for (auto session : m_adminSessions)
{
if (auto* pPlayer = PlayerManager::Get()->GetByConnectionId(session))
{
if (pPlayer->GetUsername() == acUsername)
return pPlayer;
}
}

return nullptr;
}

Player const* GameServer::GetAdminByUsername(const String& acUsername) noexcept
{
for (auto session : m_adminSessions)
{
if (auto const* pPlayer = PlayerManager::Get()->GetByConnectionId(session))
{
if (pPlayer->GetUsername() == acUsername)
return pPlayer;
}
}

return nullptr;
}

String GameServer::SanitizeUsername(const String& acUsername) const noexcept
{
String username = acUsername;

// space in username handling | "_" -> space
std::ranges::replace(username, '_', ' ');

return username;
}
15 changes: 9 additions & 6 deletions Code/server/GameServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ struct GameServer final : Server
{
return m_isPasswordProtected;
}
void SetPublic(bool aIsPublic) noexcept
{
m_isPublic = aIsPublic;
}
bool IsPublic() const noexcept
[[nodiscard]] bool IsPublic() const noexcept
{
return m_isPublic;
}
Expand Down Expand Up @@ -117,7 +113,13 @@ struct GameServer final : Server
m_adminSessions.insert(acSession);
}

void RemoveAdminSession(ConnectionId_t acSession) noexcept;
void RemoveAdminSession(ConnectionId_t acSession) noexcept
{
m_adminSessions.erase(acSession);
}

Player* GetAdminByUsername(const String& acUsername) const noexcept;
Player const* GetAdminByUsername(const String& acUsername) noexcept;

protected:
bool ValidateAuthParams(ConnectionId_t aConnectionId, const UniquePtr<AuthenticationRequest>& acRequest);
Expand All @@ -131,6 +133,7 @@ struct GameServer final : Server

private:
void UpdateTitle() const;
String SanitizeUsername(const String& acUsername) const noexcept;

private:
std::chrono::high_resolution_clock::time_point m_startTime;
Expand Down
2 changes: 0 additions & 2 deletions Code/server/Services/ServerListService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ ServerListService::ServerListService(World& aWorld, entt::dispatcher& aDispatche
if (!bAnnounceServer)
spdlog::warn("bAnnounceServer is set to false. The server will not show up as a public server. "
"If you are just playing with friends, this is probably what you want.");

GameServer::Get()->SetPublic(bAnnounceServer);
}

void ServerListService::OnUpdate(const UpdateEvent& acEvent) noexcept
Expand Down

0 comments on commit 4b2fc01

Please sign in to comment.