Skip to content

Commit

Permalink
fix agent remove handling
Browse files Browse the repository at this point in the history
  • Loading branch information
knoxfighter committed Mar 27, 2024
1 parent 2cca145 commit f534444
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
20 changes: 14 additions & 6 deletions killproof_me/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ void mod_combat(cbtevent* ev, ag* src, ag* dst, const char* skillname, uint64_t
/* notify tracking change */
if (!src->elite) {
// only run, when names are set and not null
if (src->name != nullptr && src->name[0] != '\0' && dst->name != nullptr && dst->name[0] != '\0') {
if (src->name != nullptr && dst->name != nullptr) {

std::string username(dst->name);

// remove ':' at the beginning of the name.
if (username.at(0) == ':') {
if (!username.empty() && username.at(0) == ':') {
username.erase(0, 1);
}

Expand Down Expand Up @@ -257,12 +257,20 @@ void mod_combat(cbtevent* ev, ag* src, ag* dst, const char* skillname, uint64_t
}
/* remove */
else {
ARC_LOG(std::format("remove event for user: {}", src->id).c_str());
// do NOT remove yourself
if (username != selfAccountName) {
std::scoped_lock<std::mutex, std::mutex> guard(trackedPlayersMutex, instancePlayersMutex);
const auto& playerIt = std::ranges::find_if(cachedPlayers, [pId = src->id](const auto& pPlayer) {
return pPlayer.second.id == pId;
});
if (playerIt != cachedPlayers.end()) {
ARC_LOG(std::format("found player to remove for user [{}]: {} - {}", src->id, playerIt->second.username, playerIt->second.self).c_str());

// remove specific user
removePlayer(username, AddedBy::Arcdps);
if (!playerIt->second.self) {
std::scoped_lock guard(trackedPlayersMutex, instancePlayersMutex);

// remove specific user
removePlayer(playerIt->second, AddedBy::Arcdps);
}
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions killproof_me/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ void loadKillproofs(Player& player) {
}
}

/**
* lock `trackedPlayersMutex` and `instancePlayersMutex` before calling this
*/
void removePlayer(const Player& pPlayer, AddedBy pAddedByToDelete) {
// check if it should be removed
if (pAddedByToDelete == AddedBy::Miscellaneous || pPlayer.addedBy == pAddedByToDelete) {
// actually remove from tracking
const auto& trackedSub = std::ranges::remove(trackedPlayers, pPlayer.username);
trackedPlayers.erase(trackedSub.begin(), trackedSub.end());

const auto& instanceSub = std::ranges::remove(instancePlayers, pPlayer.username);
instancePlayers.erase(instanceSub.begin(), instanceSub.end());
}
}

/**
* lock `trackedPlayersMutex` and `instancePlayersMutex` before calling this
*/
void removePlayer(uintptr_t pId, AddedBy pAddedByToDelete) {
const auto& playerIt = std::ranges::find_if(cachedPlayers, [pId](const auto& pPlayer) {
return pPlayer.second.id == pId;
});

if (playerIt != cachedPlayers.end()) {
removePlayer(playerIt->second, pAddedByToDelete);
}
}

/**
* lock `trackedPlayersMutex` and `instancePlayersMutex` before calling this
*/
Expand Down
2 changes: 2 additions & 0 deletions killproof_me/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ inline bool fileExists(const std::string& filename) {
return (stat(filename.c_str(), &buffer) == 0);
}

void removePlayer(const Player& pPlayer, AddedBy pAddedByToDelete);
void removePlayer(uintptr_t pId, AddedBy pAddedByToDelete);
void removePlayer(const std::string& username, AddedBy addedByToDelete);
// void removePlayerInstance(const std::string& username);
// void removePlayerTracking(const std::string& username);
Expand Down

0 comments on commit f534444

Please sign in to comment.