chore: cleanup usage of pointers in the activity component (#1989)

* chore: cleanup usage of pointers in the activity component

* feedback
This commit is contained in:
David Markowitz
2026-06-11 07:12:43 -07:00
committed by GitHub
parent e5b8e5c6b7
commit 1e9b18fa9d
4 changed files with 167 additions and 230 deletions

View File

@@ -22,6 +22,7 @@
#include "eMatchUpdate.h" #include "eMatchUpdate.h"
#include "ServiceType.h" #include "ServiceType.h"
#include "MessageType/Chat.h" #include "MessageType/Chat.h"
#include "ObjectIDManager.h"
#include "CDCurrencyTableTable.h" #include "CDCurrencyTableTable.h"
#include "CDActivityRewardsTable.h" #include "CDActivityRewardsTable.h"
@@ -29,6 +30,11 @@
#include "LeaderboardManager.h" #include "LeaderboardManager.h"
#include "CharacterComponent.h" #include "CharacterComponent.h"
#include "Amf3.h" #include "Amf3.h"
#include <ranges>
namespace {
const ActivityInstance g_EmptyInstance{ nullptr, CDActivities{} };
}
ActivityComponent::ActivityComponent(Entity* parent, int32_t componentID) : Component(parent, componentID) { ActivityComponent::ActivityComponent(Entity* parent, int32_t componentID) : Component(parent, componentID) {
RegisterMsg(&ActivityComponent::OnGetObjectReportInfo); RegisterMsg(&ActivityComponent::OnGetObjectReportInfo);
@@ -71,9 +77,9 @@ void ActivityComponent::Serialize(RakNet::BitStream& outBitStream, bool bIsIniti
if (m_DirtyActivityInfo) { if (m_DirtyActivityInfo) {
outBitStream.Write<uint32_t>(m_ActivityPlayers.size()); outBitStream.Write<uint32_t>(m_ActivityPlayers.size());
if (!m_ActivityPlayers.empty()) { if (!m_ActivityPlayers.empty()) {
for (const auto& activityPlayer : m_ActivityPlayers) { for (const auto& [playerID, values] : m_ActivityPlayers) {
outBitStream.Write<LWOOBJID>(activityPlayer->playerID); outBitStream.Write<LWOOBJID>(playerID);
for (const auto& activityValue : activityPlayer->values) { for (const auto& activityValue : values) {
outBitStream.Write<float_t>(activityValue); outBitStream.Write<float_t>(activityValue);
} }
} }
@@ -111,78 +117,81 @@ void ActivityComponent::PlayerJoin(Entity* player) {
if (HasLobby()) { if (HasLobby()) {
PlayerJoinLobby(player); PlayerJoinLobby(player);
} else if (!IsPlayedBy(player)) { } else if (!IsPlayedBy(player)) {
auto* instance = NewInstance(); NewInstance().AddParticipant(player);
instance->AddParticipant(player);
} }
} }
void ActivityComponent::PlayerJoinLobby(Entity* player) { void ActivityComponent::PlayerJoinLobby(Entity* player) {
if (!m_Parent->HasComponent(eReplicaComponentType::QUICK_BUILD)) if (!m_Parent->HasComponent(eReplicaComponentType::QUICK_BUILD))
GameMessages::SendMatchResponse(player, player->GetSystemAddress(), 0); // tell the client they joined a lobby GameMessages::SendMatchResponse(player, player->GetSystemAddress(), 0); // tell the client they joined a lobby
LobbyPlayer* newLobbyPlayer = new LobbyPlayer(); LobbyPlayer newLobbyPlayer{};
newLobbyPlayer->entityID = player->GetObjectID(); newLobbyPlayer.entityID = player->GetObjectID();
Lobby* playerLobby = nullptr; LWOOBJID playerLobbyID = LWOOBJID_EMPTY;
auto* character = player->GetCharacter(); auto* character = player->GetCharacter();
if (character != nullptr) if (character != nullptr)
character->SetLastNonInstanceZoneID(Game::zoneManager->GetZone()->GetWorldID()); character->SetLastNonInstanceZoneID(Game::zoneManager->GetZone()->GetWorldID());
for (Lobby* lobby : m_Queue) { for (auto& [lobbyID, lobby] : m_Queue) {
if (lobby->players.size() < m_ActivityInfo.maxTeamSize || m_ActivityInfo.maxTeamSize == 1 && lobby->players.size() < m_ActivityInfo.maxTeams) { if (lobby.players.size() < m_ActivityInfo.maxTeamSize || m_ActivityInfo.maxTeamSize == 1 && lobby.players.size() < m_ActivityInfo.maxTeams) {
// If an empty slot in an existing lobby is found // If an empty slot in an existing lobby is found
lobby->players.push_back(newLobbyPlayer); lobby.players.push_back(newLobbyPlayer);
playerLobby = lobby; playerLobbyID = lobbyID;
// Update the joining player on players already in the lobby, and update players already in the lobby on the joining player // Update the joining player on players already in the lobby, and update players already in the lobby on the joining player
std::string matchUpdateJoined = "player=9:" + std::to_string(player->GetObjectID()) + "\nplayerName=0:" + player->GetCharacter()->GetName(); LDFData<LWOOBJID> playerLDF("player", player->GetObjectID());
for (LobbyPlayer* joinedPlayer : lobby->players) { LDFData<std::string> playerName("playerName", player->GetCharacter()->GetName());
auto* entity = joinedPlayer->GetEntity(); std::string matchUpdateJoined = playerLDF.GetString() + "\n" + playerName.GetString();
for (const auto& joinedPlayer : lobby.players) {
auto* const entity = joinedPlayer.GetEntity();
if (entity == nullptr) { if (entity == nullptr) {
continue; continue;
} }
std::string matchUpdate = "player=9:" + std::to_string(entity->GetObjectID()) + "\nplayerName=0:" + entity->GetCharacter()->GetName(); LDFData<LWOOBJID> entityLDF("player", entity->GetObjectID());
LDFData<std::string> entityName("playerName", entity->GetCharacter()->GetName());
std::string matchUpdate = entityLDF.GetString() + "\n" + entityName.GetString();
GameMessages::SendMatchUpdate(player, player->GetSystemAddress(), matchUpdate, eMatchUpdate::PLAYER_ADDED); GameMessages::SendMatchUpdate(player, player->GetSystemAddress(), matchUpdate, eMatchUpdate::PLAYER_ADDED);
PlayerReady(entity, joinedPlayer->ready); PlayerReady(entity, joinedPlayer.ready);
GameMessages::SendMatchUpdate(entity, entity->GetSystemAddress(), matchUpdateJoined, eMatchUpdate::PLAYER_ADDED); GameMessages::SendMatchUpdate(entity, entity->GetSystemAddress(), matchUpdateJoined, eMatchUpdate::PLAYER_ADDED);
} }
break;
} }
} }
if (!playerLobby) { if (playerLobbyID == LWOOBJID_EMPTY) {
// If all lobbies are full // If all lobbies are full
playerLobby = new Lobby(); playerLobbyID = ObjectIDManager::GenerateObjectID();
playerLobby->players.push_back(newLobbyPlayer); auto& newLobby = m_Queue[playerLobbyID];
playerLobby->timer = m_ActivityInfo.waitTime / 1000; newLobby.players.push_back(newLobbyPlayer);
m_Queue.push_back(playerLobby); newLobby.timer = m_ActivityInfo.waitTime / 1000;
} }
const auto& lobby = m_Queue[playerLobbyID];
if (m_ActivityInfo.maxTeamSize != 1 && playerLobby->players.size() >= m_ActivityInfo.minTeamSize || m_ActivityInfo.maxTeamSize == 1 && playerLobby->players.size() >= m_ActivityInfo.minTeams) { if (m_ActivityInfo.maxTeamSize != 1 && lobby.players.size() >= m_ActivityInfo.minTeamSize || m_ActivityInfo.maxTeamSize == 1 && lobby.players.size() >= m_ActivityInfo.minTeams) {
// Update the joining player on the match timer // Update the joining player on the match timer
std::string matchTimerUpdate = "time=3:" + std::to_string(playerLobby->timer); LDFData<float> matchTimer("time", lobby.timer);
GameMessages::SendMatchUpdate(player, player->GetSystemAddress(), matchTimerUpdate, eMatchUpdate::PHASE_WAIT_READY); GameMessages::SendMatchUpdate(player, player->GetSystemAddress(), matchTimer.GetString(), eMatchUpdate::PHASE_WAIT_READY);
} }
} }
void ActivityComponent::PlayerLeave(LWOOBJID playerID) { void ActivityComponent::PlayerLeave(LWOOBJID playerID) {
// Removes the player from a lobby and notifies the others, not applicable for non-lobby instances // Removes the player from a lobby and notifies the others, not applicable for non-lobby instances
for (Lobby* lobby : m_Queue) { for (auto& lobby : m_Queue | std::views::values) {
for (int i = 0; i < lobby->players.size(); ++i) { for (int i = 0; i < lobby.players.size(); i++) {
if (lobby->players[i]->entityID == playerID) { const auto& player = lobby.players[i];
std::string matchUpdateLeft = "player=9:" + std::to_string(playerID); if (player.entityID == playerID) {
for (LobbyPlayer* lobbyPlayer : lobby->players) { LDFData<LWOOBJID> matchUpdateLeft("player", playerID);
auto* entity = lobbyPlayer->GetEntity(); for (const auto& lobbyPlayer : lobby.players) {
auto* const entity = lobbyPlayer.GetEntity();
if (entity == nullptr) if (entity == nullptr)
continue; continue;
GameMessages::SendMatchUpdate(entity, entity->GetSystemAddress(), matchUpdateLeft, eMatchUpdate::PLAYER_REMOVED); GameMessages::SendMatchUpdate(entity, entity->GetSystemAddress(), matchUpdateLeft.GetString(), eMatchUpdate::PLAYER_REMOVED);
} }
delete lobby->players[i]; lobby.players.erase(lobby.players.begin() + i);
lobby->players[i] = nullptr;
lobby->players.erase(lobby->players.begin() + i);
return; return;
} }
@@ -191,85 +200,79 @@ void ActivityComponent::PlayerLeave(LWOOBJID playerID) {
} }
void ActivityComponent::Update(float deltaTime) { void ActivityComponent::Update(float deltaTime) {
std::vector<Lobby*> lobbiesToRemove{}; std::vector<LWOOBJID> lobbiesToRemove{};
// Ticks all the lobbies, not applicable for non-instance activities // Ticks all the lobbies, not applicable for non-instance activities
for (Lobby* lobby : m_Queue) { for (auto& [lobbyID, lobby] : m_Queue) {
for (LobbyPlayer* player : lobby->players) { for (const auto& player : lobby.players) {
auto* entity = player->GetEntity(); const auto* const entity = player.GetEntity();
if (entity == nullptr) { if (entity == nullptr) {
PlayerLeave(player->entityID); PlayerLeave(player.entityID);
return; return;
} }
} }
if (lobby->players.empty()) { if (lobby.players.empty()) {
lobbiesToRemove.push_back(lobby); lobbiesToRemove.push_back(lobbyID);
continue; continue;
} }
// Update the match time for all players // Update the match time for all players
if (m_ActivityInfo.maxTeamSize != 1 && lobby->players.size() >= m_ActivityInfo.minTeamSize if (m_ActivityInfo.maxTeamSize != 1 && lobby.players.size() >= m_ActivityInfo.minTeamSize
|| m_ActivityInfo.maxTeamSize == 1 && lobby->players.size() >= m_ActivityInfo.minTeams) { || m_ActivityInfo.maxTeamSize == 1 && lobby.players.size() >= m_ActivityInfo.minTeams) {
if (lobby->timer == m_ActivityInfo.waitTime / 1000) { if (lobby.timer == m_ActivityInfo.waitTime / 1000) {
for (LobbyPlayer* joinedPlayer : lobby->players) { for (const auto& joinedPlayer : lobby.players) {
auto* entity = joinedPlayer->GetEntity(); auto* const entity = joinedPlayer.GetEntity();
if (entity == nullptr) if (entity == nullptr)
continue; continue;
std::string matchTimerUpdate = "time=3:" + std::to_string(lobby->timer); LDFData<float> matchTimerUpdate("time", lobby.timer);
GameMessages::SendMatchUpdate(entity, entity->GetSystemAddress(), matchTimerUpdate, eMatchUpdate::PHASE_WAIT_READY); GameMessages::SendMatchUpdate(entity, entity->GetSystemAddress(), matchTimerUpdate.GetString(), eMatchUpdate::PHASE_WAIT_READY);
} }
} }
lobby->timer -= deltaTime; lobby.timer -= deltaTime;
} }
bool lobbyReady = true; bool lobbyReady = true;
for (LobbyPlayer* player : lobby->players) { for (const auto& player : lobby.players) {
if (player->ready) continue; if (player.ready) continue;
lobbyReady = false; lobbyReady = false;
} }
// If everyone's ready, jump the timer // If everyone's ready, jump the timer
if (lobbyReady && lobby->timer > m_ActivityInfo.startDelay / 1000) { if (lobbyReady && lobby.timer > m_ActivityInfo.startDelay / 1000) {
lobby->timer = m_ActivityInfo.startDelay / 1000; lobby.timer = m_ActivityInfo.startDelay / 1000;
// Update players in lobby on switch to start delay // Update players in lobby on switch to start delay
std::string matchTimerUpdate = "time=3:" + std::to_string(lobby->timer); LDFData<float> matchTimerUpdate("time", lobby.timer);
for (LobbyPlayer* player : lobby->players) { for (const auto& player : lobby.players) {
auto* entity = player->GetEntity(); auto* const entity = player.GetEntity();
if (entity == nullptr) if (entity == nullptr)
continue; continue;
GameMessages::SendMatchUpdate(entity, entity->GetSystemAddress(), matchTimerUpdate, eMatchUpdate::PHASE_WAIT_START); GameMessages::SendMatchUpdate(entity, entity->GetSystemAddress(), matchTimerUpdate.GetString(), eMatchUpdate::PHASE_WAIT_START);
} }
} }
// The timer has elapsed, start the instance // The timer has elapsed, start the instance
if (lobby->timer <= 0.0f) { if (lobby.timer <= 0.0f) {
LOG("Setting up instance."); LOG("Setting up instance.");
ActivityInstance* instance = NewInstance(); auto& instance = NewInstance();
LoadPlayersIntoInstance(instance, lobby->players); LoadPlayersIntoInstance(instance, lobby.players);
instance->StartZone(); instance.StartZone();
lobbiesToRemove.push_back(lobby); lobbiesToRemove.push_back(lobbyID);
} }
} }
while (!lobbiesToRemove.empty()) { for (const auto id : lobbiesToRemove) {
RemoveLobby(lobbiesToRemove.front()); RemoveLobby(id);
lobbiesToRemove.erase(lobbiesToRemove.begin());
} }
} }
void ActivityComponent::RemoveLobby(Lobby* lobby) { void ActivityComponent::RemoveLobby(const LWOOBJID lobbyID) {
for (int i = 0; i < m_Queue.size(); ++i) { if (m_Queue.contains(lobbyID)) m_Queue.erase(lobbyID);
if (m_Queue[i] == lobby) {
m_Queue.erase(m_Queue.begin() + i);
return;
}
}
} }
bool ActivityComponent::HasLobby() const { bool ActivityComponent::HasLobby() const {
@@ -278,9 +281,9 @@ bool ActivityComponent::HasLobby() const {
} }
bool ActivityComponent::PlayerIsInQueue(Entity* player) { bool ActivityComponent::PlayerIsInQueue(Entity* player) {
for (Lobby* lobby : m_Queue) { for (const auto& lobby : m_Queue | std::views::values) {
for (LobbyPlayer* lobbyPlayer : lobby->players) { for (const auto& lobbyPlayer : lobby.players) {
if (player->GetObjectID() == lobbyPlayer->entityID) return true; if (player->GetObjectID() == lobbyPlayer.entityID) return true;
} }
} }
@@ -288,8 +291,8 @@ bool ActivityComponent::PlayerIsInQueue(Entity* player) {
} }
bool ActivityComponent::IsPlayedBy(Entity* player) const { bool ActivityComponent::IsPlayedBy(Entity* player) const {
for (const auto* instance : this->m_Instances) { for (const auto& instance : m_Instances) {
for (const auto* instancePlayer : instance->GetParticipants()) { for (const auto* instancePlayer : instance.GetParticipants()) {
if (instancePlayer != nullptr && instancePlayer->GetObjectID() == player->GetObjectID()) if (instancePlayer != nullptr && instancePlayer->GetObjectID() == player->GetObjectID())
return true; return true;
} }
@@ -299,8 +302,8 @@ bool ActivityComponent::IsPlayedBy(Entity* player) const {
} }
bool ActivityComponent::IsPlayedBy(LWOOBJID playerID) const { bool ActivityComponent::IsPlayedBy(LWOOBJID playerID) const {
for (const auto* instance : this->m_Instances) { for (const auto& instance : m_Instances) {
for (const auto* instancePlayer : instance->GetParticipants()) { for (const auto* instancePlayer : instance.GetParticipants()) {
if (instancePlayer != nullptr && instancePlayer->GetObjectID() == playerID) if (instancePlayer != nullptr && instancePlayer->GetObjectID() == playerID)
return true; return true;
} }
@@ -329,136 +332,95 @@ bool ActivityComponent::TakeCost(Entity* player) const {
} }
void ActivityComponent::PlayerReady(Entity* player, bool bReady) { void ActivityComponent::PlayerReady(Entity* player, bool bReady) {
for (Lobby* lobby : m_Queue) { for (auto& lobby : m_Queue | std::views::values) {
for (LobbyPlayer* lobbyPlayer : lobby->players) { for (auto& lobbyPlayer : lobby.players) {
if (lobbyPlayer->entityID == player->GetObjectID()) { if (lobbyPlayer.entityID == player->GetObjectID()) {
lobbyPlayer->ready = bReady; lobbyPlayer.ready = bReady;
// Update players in lobby on player being ready // Update players in lobby on player being ready
std::string matchReadyUpdate = "player=9:" + std::to_string(player->GetObjectID()); LDFData<LWOOBJID> matchReadyUpdate("player", player->GetObjectID());
eMatchUpdate readyStatus = eMatchUpdate::PLAYER_READY; eMatchUpdate readyStatus = eMatchUpdate::PLAYER_READY;
if (!bReady) readyStatus = eMatchUpdate::PLAYER_NOT_READY; if (!bReady) readyStatus = eMatchUpdate::PLAYER_NOT_READY;
for (LobbyPlayer* otherPlayer : lobby->players) { for (const auto& otherPlayer : lobby.players) {
auto* entity = otherPlayer->GetEntity(); auto* const entity = otherPlayer.GetEntity();
if (entity == nullptr) if (entity == nullptr)
continue; continue;
GameMessages::SendMatchUpdate(entity, entity->GetSystemAddress(), matchReadyUpdate, readyStatus); GameMessages::SendMatchUpdate(entity, entity->GetSystemAddress(), matchReadyUpdate.GetString(), readyStatus);
} }
} }
} }
} }
} }
ActivityInstance* ActivityComponent::NewInstance() { ActivityInstance& ActivityComponent::NewInstance() {
auto* instance = new ActivityInstance(m_Parent, m_ActivityInfo); m_Instances.push_back(ActivityInstance(m_Parent, m_ActivityInfo));
m_Instances.push_back(instance); return m_Instances.back();
return instance;
} }
void ActivityComponent::LoadPlayersIntoInstance(ActivityInstance* instance, const std::vector<LobbyPlayer*>& lobby) const { void ActivityComponent::LoadPlayersIntoInstance(ActivityInstance& instance, const std::vector<LobbyPlayer>& lobby) const {
for (LobbyPlayer* player : lobby) { for (const auto& player : lobby) {
auto* entity = player->GetEntity(); auto* const entity = player.GetEntity();
if (entity == nullptr || !CheckCost(entity)) { if (entity == nullptr || !CheckCost(entity)) {
continue; continue;
} }
instance->AddParticipant(entity); instance.AddParticipant(entity);
} }
} }
const std::vector<ActivityInstance*>& ActivityComponent::GetInstances() const { const ActivityInstance& ActivityComponent::GetInstance(const LWOOBJID playerID) const {
return m_Instances; for (const auto& instance : m_Instances) {
} for (const auto* participant : instance.GetParticipants()) {
ActivityInstance* ActivityComponent::GetInstance(const LWOOBJID playerID) {
for (const auto* instance : GetInstances()) {
for (const auto* participant : instance->GetParticipants()) {
if (participant->GetObjectID() == playerID) if (participant->GetObjectID() == playerID)
return const_cast<ActivityInstance*>(instance); return instance;
} }
} }
return nullptr; return g_EmptyInstance;
} }
void ActivityComponent::ClearInstances() { bool ActivityComponent::PlayerHasActivityData(LWOOBJID playerID) const {
for (ActivityInstance* instance : m_Instances) { return m_ActivityPlayers.contains(playerID);
delete instance;
}
m_Instances.clear();
}
ActivityPlayer* ActivityComponent::GetActivityPlayerData(LWOOBJID playerID) {
for (auto* activityData : m_ActivityPlayers) {
if (activityData->playerID == playerID) {
return activityData;
}
}
return nullptr;
} }
void ActivityComponent::RemoveActivityPlayerData(LWOOBJID playerID) { void ActivityComponent::RemoveActivityPlayerData(LWOOBJID playerID) {
for (size_t i = 0; i < m_ActivityPlayers.size(); i++) { m_ActivityPlayers.erase(playerID);
if (m_ActivityPlayers[i]->playerID == playerID) {
delete m_ActivityPlayers[i];
m_ActivityPlayers[i] = nullptr;
m_ActivityPlayers.erase(m_ActivityPlayers.begin() + i);
m_DirtyActivityInfo = true;
Game::entityManager->SerializeEntity(m_Parent);
return;
}
}
}
ActivityPlayer* ActivityComponent::AddActivityPlayerData(LWOOBJID playerID) {
auto* data = GetActivityPlayerData(playerID);
if (data != nullptr)
return data;
m_ActivityPlayers.push_back(new ActivityPlayer{ playerID, {} });
m_DirtyActivityInfo = true; m_DirtyActivityInfo = true;
Game::entityManager->SerializeEntity(m_Parent);
return GetActivityPlayerData(playerID);
} }
float_t ActivityComponent::GetActivityValue(LWOOBJID playerID, uint32_t index) { float_t ActivityComponent::GetActivityValue(LWOOBJID playerID, uint32_t index) const {
auto value = -1.0f; float value = -1.0f;
auto* data = GetActivityPlayerData(playerID); const auto& data = m_ActivityPlayers.find(playerID);
if (data != nullptr) { if (data != m_ActivityPlayers.cend()) {
value = data->values[std::min(index, static_cast<uint32_t>(9))]; value = data->second[std::min(index, static_cast<uint32_t>(9))];
} }
LOG_DEBUG("Player %llu has score %f at index %i", playerID, value, index);
return value; return value;
} }
void ActivityComponent::SetActivityValue(LWOOBJID playerID, uint32_t index, float_t value) { void ActivityComponent::SetActivityValue(LWOOBJID playerID, uint32_t index, float_t value) {
auto* data = AddActivityPlayerData(playerID); auto& data = m_ActivityPlayers[playerID];
if (data != nullptr) { data[std::min(index, static_cast<uint32_t>(9))] = value;
data->values[std::min(index, static_cast<uint32_t>(9))] = value; LOG_DEBUG("%llu index %i has score of %f", playerID, index, value);
}
m_DirtyActivityInfo = true; m_DirtyActivityInfo = true;
Game::entityManager->SerializeEntity(m_Parent); Game::entityManager->SerializeEntity(m_Parent);
} }
void ActivityComponent::PlayerRemove(LWOOBJID playerID) { void ActivityComponent::PlayerRemove(LWOOBJID playerID) {
for (auto* instance : GetInstances()) { for (int i = 0; i < m_Instances.size(); i++) {
auto participants = instance->GetParticipants(); auto& instance = m_Instances[i];
auto participants = instance.GetParticipants();
for (const auto* participant : participants) { for (const auto* participant : participants) {
if (participant != nullptr && participant->GetObjectID() == playerID) { if (participant != nullptr && participant->GetObjectID() == playerID) {
instance->RemoveParticipant(participant); instance.RemoveParticipant(participant);
RemoveActivityPlayerData(playerID); RemoveActivityPlayerData(playerID);
// If the instance is empty after the delete of the participant, delete the instance too // If the instance is empty after the delete of the participant, delete the instance too
if (instance->GetParticipants().empty()) { if (instance.GetParticipants().empty()) {
m_Instances.erase(std::find(m_Instances.begin(), m_Instances.end(), instance)); m_Instances.erase(m_Instances.begin() + i);
delete instance;
} }
return; return;
} }
@@ -595,14 +557,13 @@ bool ActivityComponent::OnGetObjectReportInfo(GameMessages::GetObjectReportInfo&
auto& instances = activityInfo.PushDebug("Instances: " + std::to_string(m_Instances.size())); auto& instances = activityInfo.PushDebug("Instances: " + std::to_string(m_Instances.size()));
size_t i = 0; size_t i = 0;
for (const auto& activityInstance : m_Instances) { for (const auto& activityInstance : m_Instances) {
if (!activityInstance) continue;
auto& instance = instances.PushDebug("Instance " + std::to_string(i++)); auto& instance = instances.PushDebug("Instance " + std::to_string(i++));
instance.PushDebug<AMFIntValue>("Score") = activityInstance->GetScore(); instance.PushDebug<AMFIntValue>("Score") = activityInstance.GetScore();
instance.PushDebug<AMFIntValue>("Next Zone Clone ID") = activityInstance->GetNextZoneCloneID(); instance.PushDebug<AMFIntValue>("Next Zone Clone ID") = activityInstance.GetNextZoneCloneID();
{ {
auto& activityInfo = instance.PushDebug("Activity Info"); auto& activityInfo = instance.PushDebug("Activity Info");
const auto& instanceActInfo = activityInstance->GetActivityInfo(); const auto& instanceActInfo = activityInstance.GetActivityInfo();
activityInfo.PushDebug<AMFIntValue>("ActivityID") = instanceActInfo.ActivityID; activityInfo.PushDebug<AMFIntValue>("ActivityID") = instanceActInfo.ActivityID;
activityInfo.PushDebug<AMFIntValue>("locStatus") = instanceActInfo.locStatus; activityInfo.PushDebug<AMFIntValue>("locStatus") = instanceActInfo.locStatus;
activityInfo.PushDebug<AMFIntValue>("instanceMapID") = instanceActInfo.instanceMapID; activityInfo.PushDebug<AMFIntValue>("instanceMapID") = instanceActInfo.instanceMapID;
@@ -625,7 +586,7 @@ bool ActivityComponent::OnGetObjectReportInfo(GameMessages::GetObjectReportInfo&
} }
auto& participants = instance.PushDebug("Participants"); auto& participants = instance.PushDebug("Participants");
for (const auto* participant : activityInstance->GetParticipants()) { for (const auto* participant : activityInstance.GetParticipants()) {
if (!participant) continue; if (!participant) continue;
auto* character = participant->GetCharacter(); auto* character = participant->GetCharacter();
if (!character) continue; if (!character) continue;
@@ -635,38 +596,36 @@ bool ActivityComponent::OnGetObjectReportInfo(GameMessages::GetObjectReportInfo&
auto& queue = activityInfo.PushDebug("Queue"); auto& queue = activityInfo.PushDebug("Queue");
i = 0; i = 0;
for (const auto& lobbyQueue : m_Queue) { for (const auto& lobbyQueue : m_Queue | std::views::values) {
auto& lobby = queue.PushDebug("Lobby " + std::to_string(i++)); auto& lobby = queue.PushDebug("Lobby " + std::to_string(i++));
lobby.PushDebug<AMFDoubleValue>("Timer") = lobbyQueue->timer; lobby.PushDebug<AMFDoubleValue>("Timer") = lobbyQueue.timer;
auto& players = lobby.PushDebug("Players"); auto& players = lobby.PushDebug("Players");
for (const auto* player : lobbyQueue->players) { for (const auto& player : lobbyQueue.players) {
if (!player) continue; const auto* const playerEntity = player.GetEntity();
auto* playerEntity = player->GetEntity();
if (!playerEntity) continue; if (!playerEntity) continue;
auto* character = playerEntity->GetCharacter(); auto* character = playerEntity->GetCharacter();
if (!character) continue; if (!character) continue;
players.PushDebug<AMFStringValue>(std::to_string(playerEntity->GetObjectID()) + ": " + character->GetName()) = player->ready ? "Ready" : "Not Ready"; players.PushDebug<AMFStringValue>(std::to_string(playerEntity->GetObjectID()) + ": " + character->GetName()) = player.ready ? "Ready" : "Not Ready";
} }
} }
auto& activityPlayers = activityInfo.PushDebug("Activity Players"); auto& activityPlayers = activityInfo.PushDebug("Activity Players");
for (const auto* activityPlayer : m_ActivityPlayers) { for (const auto& [playerID, playerScores] : m_ActivityPlayers) {
if (!activityPlayer) continue; auto* const activityPlayerEntity = Game::entityManager->GetEntity(playerID);
auto* const activityPlayerEntity = Game::entityManager->GetEntity(activityPlayer->playerID);
if (!activityPlayerEntity) continue; if (!activityPlayerEntity) continue;
auto* character = activityPlayerEntity->GetCharacter(); auto* character = activityPlayerEntity->GetCharacter();
if (!character) continue; if (!character) continue;
auto& playerData = activityPlayers.PushDebug(std::to_string(activityPlayer->playerID) + " " + character->GetName()); auto& playerData = activityPlayers.PushDebug(std::to_string(playerID) + " " + character->GetName());
auto& scores = playerData.PushDebug("Scores"); auto& scores = playerData.PushDebug("Scores");
for (size_t i = 0; i < 10; ++i) { for (size_t i = 0; i < 10; ++i) {
scores.PushDebug<AMFDoubleValue>(std::to_string(i)) = activityPlayer->values[i]; scores.PushDebug<AMFDoubleValue>(std::to_string(i)) = playerScores[i];
} }
} }
activityInfo.PushDebug<AMFIntValue>("ActivityID") = m_ActivityID; activityInfo.PushDebug<AMFIntValue>("ActivityID") = m_ActivityID;
return true; return true;
} }

View File

@@ -8,14 +8,15 @@
#include "eReplicaComponentType.h" #include "eReplicaComponentType.h"
#include "CDActivitiesTable.h" #include "CDActivitiesTable.h"
#include <array>
namespace GameMessages { namespace GameMessages {
class GameMsg; class GameMsg;
}; };
/** /**
* Represents an instance of an activity, having participants and score * Represents an instance of an activity, having participants and score
*/ */
class ActivityInstance { class ActivityInstance {
public: public:
ActivityInstance(Entity* parent, CDActivities activityInfo) { m_Parent = parent; m_ActivityInfo = activityInfo; }; ActivityInstance(Entity* parent, CDActivities activityInfo) { m_Parent = parent; m_ActivityInfo = activityInfo; };
@@ -104,7 +105,7 @@ struct LobbyPlayer {
/** /**
* The ID of the entity that is in the lobby * The ID of the entity that is in the lobby
*/ */
LWOOBJID entityID; LWOOBJID entityID = LWOOBJID_EMPTY;
/** /**
* Whether or not the entity is ready * Whether or not the entity is ready
@@ -126,12 +127,12 @@ struct Lobby {
/** /**
* The lobby of players * The lobby of players
*/ */
std::vector<LobbyPlayer*> players; std::vector<LobbyPlayer> players;
/** /**
* The timer that determines when the activity should start * The timer that determines when the activity should start
*/ */
float timer; float timer{};
}; };
/** /**
@@ -142,12 +143,12 @@ struct ActivityPlayer {
/** /**
* The entity that the score is tracked for * The entity that the score is tracked for
*/ */
LWOOBJID playerID; LWOOBJID playerID{};
/** /**
* The list of score for this entity * The list of score for this entity
*/ */
float values[10]; float values[10]{};
}; };
/** /**
@@ -194,13 +195,13 @@ public:
* @param instance the instance to load the players into * @param instance the instance to load the players into
* @param lobby the players to load into the instance * @param lobby the players to load into the instance
*/ */
void LoadPlayersIntoInstance(ActivityInstance* instance, const std::vector<LobbyPlayer*>& lobby) const; void LoadPlayersIntoInstance(ActivityInstance& instance, const std::vector<LobbyPlayer>& lobby) const;
/** /**
* Removes a lobby from the activity manager * Removes a lobby from the activity manager
* @param lobby the lobby to remove * @param lobby the lobby to remove
*/ */
void RemoveLobby(Lobby* lobby); void RemoveLobby(const LWOOBJID lobbyID);
/** /**
* Marks a player as (un)ready in a lobby * Marks a player as (un)ready in a lobby
@@ -246,7 +247,7 @@ public:
*/ */
bool IsPlayedBy(LWOOBJID playerID) const; bool IsPlayedBy(LWOOBJID playerID) const;
/** /**
* Checks if the entity has enough cost to play this activity * Checks if the entity has enough cost to play this activity
* @param player the entity to check * @param player the entity to check
* @return true if the entity has enough cost to play this activity, false otherwise * @return true if the entity has enough cost to play this activity, false otherwise
@@ -271,20 +272,14 @@ public:
* Creates a new instance for this activity * Creates a new instance for this activity
* @return a new instance for this activity * @return a new instance for this activity
*/ */
ActivityInstance* NewInstance(); ActivityInstance& NewInstance();
/**
* Returns all the currently active instances of this activity
* @return all the currently active instances of this activity
*/
const std::vector<ActivityInstance*>& GetInstances() const;
/** /**
* Returns the instance that some entity is currently playing in * Returns the instance that some entity is currently playing in
* @param playerID the entity to check for * @param playerID the entity to check for
* @return if any, the instance that the entity is currently in * @return if any, the instance that the entity is currently in
*/ */
ActivityInstance* GetInstance(const LWOOBJID playerID); const ActivityInstance& GetInstance(const LWOOBJID playerID) const;
/** /**
* @brief Reloads the config settings for this component * @brief Reloads the config settings for this component
@@ -292,23 +287,12 @@ public:
*/ */
void ReloadConfig(); void ReloadConfig();
/**
* Removes all the instances
*/
void ClearInstances();
/**
* Returns all the score for the players that are currently playing this activity
* @return
*/
std::vector<ActivityPlayer*> GetActivityPlayers() { return m_ActivityPlayers; };
/** /**
* Returns activity data for a specific entity (e.g. score and such). * Returns activity data for a specific entity (e.g. score and such).
* @param playerID the entity to get data for * @param playerID the entity to get data for
* @return the activity data (score) for the passed player in this activity, if it exists * @return the activity data (score) for the passed player in this activity, if it exists
*/ */
ActivityPlayer* GetActivityPlayerData(LWOOBJID playerID); bool PlayerHasActivityData(LWOOBJID playerID) const;
/** /**
* Sets some score value for an entity * Sets some score value for an entity
@@ -324,7 +308,7 @@ public:
* @param index the index to get score for * @param index the index to get score for
* @return activity score for the passed parameters * @return activity score for the passed parameters
*/ */
float_t GetActivityValue(LWOOBJID playerID, uint32_t index); float_t GetActivityValue(LWOOBJID playerID, uint32_t index) const;
/** /**
* Removes activity score tracking for some entity * Removes activity score tracking for some entity
@@ -332,13 +316,6 @@ public:
*/ */
void RemoveActivityPlayerData(LWOOBJID playerID); void RemoveActivityPlayerData(LWOOBJID playerID);
/**
* Adds activity score tracking for some entity
* @param playerID the entity to add the activity score for
* @return the created entry
*/
ActivityPlayer* AddActivityPlayerData(LWOOBJID playerID);
/** /**
* Sets the mapID that this activity points to * Sets the mapID that this activity points to
* @param mapID the map ID to set * @param mapID the map ID to set
@@ -346,7 +323,6 @@ public:
void SetInstanceMapID(uint32_t mapID) { m_ActivityInfo.instanceMapID = mapID; }; void SetInstanceMapID(uint32_t mapID) { m_ActivityInfo.instanceMapID = mapID; };
private: private:
bool OnGetObjectReportInfo(GameMessages::GetObjectReportInfo& msg); bool OnGetObjectReportInfo(GameMessages::GetObjectReportInfo& msg);
/** /**
* The database information for this activity * The database information for this activity
@@ -356,17 +332,17 @@ private:
/** /**
* All the active instances of this activity * All the active instances of this activity
*/ */
std::vector<ActivityInstance*> m_Instances; std::vector<ActivityInstance> m_Instances;
/** /**
* The current lobbies for this activity * The current lobbies for this activity
*/ */
std::vector<Lobby*> m_Queue; std::map<LWOOBJID, Lobby> m_Queue;
/** /**
* All the activity score for the players in this activity * All the activity score for the players in this activity
*/ */
std::vector<ActivityPlayer*> m_ActivityPlayers; std::map<LWOOBJID, std::array<float, 10>> m_ActivityPlayers;
/** /**
* The activity id * The activity id

View File

@@ -19,7 +19,7 @@ void NpcAgCourseStarter::OnUse(Entity* self, Entity* user) {
const auto userId = user->GetObjectID(); const auto userId = user->GetObjectID();
const auto& userSysAddr = user->GetSystemAddress(); const auto& userSysAddr = user->GetSystemAddress();
if (scriptedActivityComponent->GetActivityPlayerData(userId) != nullptr) { if (scriptedActivityComponent->PlayerHasActivityData(userId)) {
GameMessages::SendNotifyClientObject(selfId, u"exit", 0, 0, LWOOBJID_EMPTY, "", userSysAddr); GameMessages::SendNotifyClientObject(selfId, u"exit", 0, 0, LWOOBJID_EMPTY, "", userSysAddr);
} else { } else {
GameMessages::SendNotifyClientObject(selfId, u"start", 0, 0, LWOOBJID_EMPTY, "", userSysAddr); GameMessages::SendNotifyClientObject(selfId, u"start", 0, 0, LWOOBJID_EMPTY, "", userSysAddr);
@@ -45,18 +45,18 @@ void NpcAgCourseStarter::OnMessageBoxResponse(Entity* self, Entity* sender, int3
GameMessages::SendNotifyClientObject(selfId, u"start_timer", 0, 0, LWOOBJID_EMPTY, "", senderSysAddr); GameMessages::SendNotifyClientObject(selfId, u"start_timer", 0, 0, LWOOBJID_EMPTY, "", senderSysAddr);
GameMessages::SendActivityStart(selfId, senderSysAddr); GameMessages::SendActivityStart(selfId, senderSysAddr);
auto* const data = scriptedActivityComponent->AddActivityPlayerData(senderId); const auto score = scriptedActivityComponent->GetActivityValue(senderId, 1);
if (data->values[1] != 0) return; if (score != 0 && score != -1.0f) return;
const auto raceStartTime = Game::server->GetUptime() + std::chrono::seconds(4); // Offset for starting timer const auto raceStartTime = Game::server->GetUptime() + std::chrono::seconds(4); // Offset for starting timer
const auto fRaceStartTime = std::chrono::duration<float, std::ratio<1>>(raceStartTime).count(); const auto fRaceStartTime = std::chrono::duration<float, std::ratio<1>>(raceStartTime).count();
data->values[1] = fRaceStartTime; scriptedActivityComponent->SetActivityValue(senderId, 1, fRaceStartTime);
Game::entityManager->SerializeEntity(self); Game::entityManager->SerializeEntity(self);
} else if (identifier == u"FootRaceCancel") { } else if (identifier == u"FootRaceCancel") {
GameMessages::SendNotifyClientObject(selfId, u"stop_timer", 0, 0, LWOOBJID_EMPTY, "", senderSysAddr); GameMessages::SendNotifyClientObject(selfId, u"stop_timer", 0, 0, LWOOBJID_EMPTY, "", senderSysAddr);
if (scriptedActivityComponent->GetActivityPlayerData(senderId) != nullptr) { if (scriptedActivityComponent->PlayerHasActivityData(senderId)) {
GameMessages::SendNotifyClientObject(selfId, u"exit", 0, 0, LWOOBJID_EMPTY, "", senderSysAddr); GameMessages::SendNotifyClientObject(selfId, u"exit", 0, 0, LWOOBJID_EMPTY, "", senderSysAddr);
} else { } else {
GameMessages::SendNotifyClientObject(selfId, u"start", 0, 0, LWOOBJID_EMPTY, "", senderSysAddr); GameMessages::SendNotifyClientObject(selfId, u"start", 0, 0, LWOOBJID_EMPTY, "", senderSysAddr);
@@ -74,8 +74,7 @@ void NpcAgCourseStarter::OnFireEventServerSide(Entity* self, Entity* sender, std
const auto senderId = sender->GetObjectID(); const auto senderId = sender->GetObjectID();
const auto& senderSysAddr = sender->GetSystemAddress(); const auto& senderSysAddr = sender->GetSystemAddress();
auto* const data = scriptedActivityComponent->GetActivityPlayerData(senderId); if (!scriptedActivityComponent->PlayerHasActivityData(senderId)) return;
if (!data) return;
if (args == "course_cancel") { if (args == "course_cancel") {
GameMessages::SendNotifyClientObject(selfId, u"cancel_timer", 0, 0, GameMessages::SendNotifyClientObject(selfId, u"cancel_timer", 0, 0,
@@ -84,8 +83,11 @@ void NpcAgCourseStarter::OnFireEventServerSide(Entity* self, Entity* sender, std
} else if (args == "course_finish") { } else if (args == "course_finish") {
const auto raceEndTime = Game::server->GetUptime(); const auto raceEndTime = Game::server->GetUptime();
const auto fRaceEndTime = std::chrono::duration<float, std::ratio<1>>(raceEndTime).count(); const auto fRaceEndTime = std::chrono::duration<float, std::ratio<1>>(raceEndTime).count();
const auto raceTimeElapsed = fRaceEndTime - data->values[1]; const float startTime = scriptedActivityComponent->GetActivityValue(senderId, 1);
data->values[2] = raceTimeElapsed; if (startTime == 0 || startTime == -1.0f) return;
const auto raceTimeElapsed = fRaceEndTime - startTime;
scriptedActivityComponent->SetActivityValue(senderId, 2, raceTimeElapsed);
auto* const missionComponent = sender->GetComponent<MissionComponent>(); auto* const missionComponent = sender->GetComponent<MissionComponent>();
if (missionComponent != nullptr) { if (missionComponent != nullptr) {

View File

@@ -114,7 +114,7 @@ uint32_t ActivityManager::CalculateActivityRating(Entity* self, const LWOOBJID p
if (sac == nullptr) if (sac == nullptr)
return 0; return 0;
return sac->GetInstance(playerID)->GetParticipants().size(); return sac->GetInstance(playerID).GetParticipants().size();
} }
uint32_t ActivityManager::GetActivityID(const Entity* self) { uint32_t ActivityManager::GetActivityID(const Entity* self) {