Skip to content

Commit

Permalink
fix: player death
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Feb 5, 2025
1 parent fd26baa commit 67d5c92
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 89 deletions.
42 changes: 1 addition & 41 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10581,48 +10581,8 @@ bool Game::addItemStoreInbox(const std::shared_ptr<Player> &player, uint32_t ite
return true;
}

void Game::addPlayerUniqueLogin(const std::shared_ptr<Player> &player) {
if (!player) {
g_logger().error("Attempted to add null player to unique player names list");
return;
}

const std::string &lowercase_name = asLowerCaseString(player->getName());
m_uniqueLoginPlayerNames[lowercase_name] = player;
}

std::shared_ptr<Player> Game::getPlayerUniqueLogin(const std::string &playerName) const {
if (playerName.empty()) {
g_logger().error("Attempted to get player with empty name string");
return nullptr;
}

auto it = m_uniqueLoginPlayerNames.find(asLowerCaseString(playerName));
return (it != m_uniqueLoginPlayerNames.end()) ? it->second.lock() : nullptr;
}

void Game::removePlayerUniqueLogin(const std::string &playerName) {
if (playerName.empty()) {
g_logger().error("Attempted to remove player with empty name string from unique player names list");
return;
}

const std::string &lowercase_name = asLowerCaseString(playerName);
m_uniqueLoginPlayerNames.erase(lowercase_name);
}

void Game::removePlayerUniqueLogin(const std::shared_ptr<Player> &player) {
if (!player) {
g_logger().error("Attempted to remove null player from unique player names list.");
return;
}

const std::string &lowercaseName = asLowerCaseString(player->getName());
m_uniqueLoginPlayerNames.erase(lowercaseName);
}

void Game::playerCheckActivity(const std::string &playerName, int interval) {
const auto &player = getPlayerUniqueLogin(playerName);
const auto &player = getPlayerByName(playerName);
if (!player) {
return;
}
Expand Down
38 changes: 0 additions & 38 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,43 +635,6 @@ class Game {
void sendUpdateCreature(const std::shared_ptr<Creature> &creature);
std::shared_ptr<Item> wrapItem(const std::shared_ptr<Item> &item, const std::shared_ptr<House> &house);

/**
* @brief Adds a player to the unique login map.
* @details The function registers a player in the unique login map to ensure no duplicate logins.
* If the player pointer is null, it logs an error and returns.
*
* @param player A pointer to the Player object to add.
*/
void addPlayerUniqueLogin(const std::shared_ptr<Player> &player);

/**
* @brief Gets a player from the unique login map using their name.
* @details The function attempts to find a player in the unique login map using their name.
* If the player's name is not found, the function returns a null pointer.
* If an empty string is provided, it logs an error and returns a null pointer.
*
* @param playerName The name of the player to search for.
* @return A pointer to the Player object if found, null otherwise.
*/
std::shared_ptr<Player> getPlayerUniqueLogin(const std::string &playerName) const;

/**
* @brief Removes a player from the unique login map using their name.
* @details The function removes a player from the unique login map using their name.
* If an empty string is provided, it logs an error and returns.
*
* @param playerName The name of the player to remove.
*/
void removePlayerUniqueLogin(const std::string &playerName);

/**
* @brief Removes a player from the unique login map.
* @details The function removes a player from the unique login map.
* If the player pointer is null, it logs an error and returns.
*
* @param player A pointer to the Player object to remove.
*/
void removePlayerUniqueLogin(const std::shared_ptr<Player> &player);
void playerCheckActivity(const std::string &playerName, int interval);

/**
Expand Down Expand Up @@ -824,7 +787,6 @@ class Game {
phmap::flat_hash_map<std::string, QueryHighscoreCacheEntry> queryCache;
phmap::flat_hash_map<std::string, HighscoreCacheEntry> highscoreCache;

phmap::flat_hash_map<std::string, std::weak_ptr<Player>> m_uniqueLoginPlayerNames;
phmap::parallel_flat_hash_map<uint32_t, std::shared_ptr<Player>> players;
phmap::flat_hash_map<std::string, std::weak_ptr<Player>> mappedPlayerNames;
phmap::parallel_flat_hash_map<uint32_t, std::shared_ptr<Guild>> guilds;
Expand Down
14 changes: 4 additions & 10 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,10 @@ void ProtocolGame::login(const std::string &name, uint32_t accountId, OperatingS
g_logger().debug("Player logging in in version '{}' and oldProtocol '{}'", getVersion(), oldProtocol);

// dispatcher thread
std::shared_ptr<Player> foundPlayer = g_game().getPlayerUniqueLogin(name);
std::shared_ptr<Player> foundPlayer = g_game().getPlayerByName(name);
if (!foundPlayer) {
if (const auto &onlinePlayer = g_game().getPlayerByName(name)) {
g_game().removePlayer(onlinePlayer);
}

player = std::make_shared<Player>(getThis());
player->setName(name);
g_game().addPlayerUniqueLogin(player);

player->setID();

Expand Down Expand Up @@ -647,7 +642,7 @@ void ProtocolGame::login(const std::string &name, uint32_t accountId, OperatingS
void ProtocolGame::connect(const std::string &playerName, OperatingSystem_t operatingSystem) {
eventConnect = 0;

std::shared_ptr<Player> foundPlayer = g_game().getPlayerUniqueLogin(playerName);
std::shared_ptr<Player> foundPlayer = g_game().getPlayerByName(playerName);
if (!foundPlayer) {
disconnectClient("You are already logged in.");
return;
Expand All @@ -660,7 +655,6 @@ void ProtocolGame::connect(const std::string &playerName, OperatingSystem_t oper
}

player = foundPlayer;
g_game().addPlayerUniqueLogin(player);

g_chat().removeUserFromAllChannels(player);
player->clearModalWindows();
Expand Down Expand Up @@ -797,7 +791,7 @@ void ProtocolGame::onRecvFirstMessage(NetworkMessage &msg) {

std::string characterName = msg.getString();

std::shared_ptr<Player> foundPlayer = g_game().getPlayerUniqueLogin(characterName);
std::shared_ptr<Player> foundPlayer = g_game().getPlayerByName(characterName);
if (foundPlayer && foundPlayer->client) {
if (foundPlayer->getProtocolVersion() != getVersion() && foundPlayer->isOldProtocol() != oldProtocol) {
disconnectClient(fmt::format("You are already logged in using protocol '{}'. Please log out from the other session to connect here.", foundPlayer->getProtocolVersion()));
Expand Down Expand Up @@ -4222,7 +4216,7 @@ void ProtocolGame::sendCyclopediaCharacterBadges() {
msg.addByte(0x00);
msg.addByte(0x01); // ShowAccountInformation, if 0x01 will show IsOnline, IsPremium, character title, badges

const auto loggedPlayer = g_game().getPlayerUniqueLogin(player->getName());
const auto loggedPlayer = g_game().getPlayerByName(player->getName());
msg.addByte(loggedPlayer ? 0x01 : 0x00); // IsOnline
msg.addByte(player->isPremium() ? 0x01 : 0x00); // IsPremium (GOD has always 'Premium')
// Character loyalty title
Expand Down

0 comments on commit 67d5c92

Please sign in to comment.