Skip to content

Commit

Permalink
fix(gamestate/server): deferred creation of player state bags
Browse files Browse the repository at this point in the history
  • Loading branch information
blattersturm committed Jan 5, 2021
1 parent e37db34 commit 3c3dcf9
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions code/components/citizen-server-impl/src/state/ServerGameState.cpp
Expand Up @@ -261,14 +261,38 @@ inline std::shared_ptr<GameStateClientData> GetClientDataUnlocked(ServerGameStat

data = std::make_shared<GameStateClientData>();
data->client = weakClient;
data->playerBag = state->GetStateBags()->RegisterStateBag(fmt::sprintf("player:%d", client->GetNetId()));

if (fx::IsBigMode())
std::weak_ptr<GameStateClientData> weakData;

auto setupBag = [weakClient, weakData, state]()
{
data->playerBag->AddRoutingTarget(client->GetSlotId());
}
auto client = weakClient.lock();
auto data = weakData.lock();

if (client && data)
{
data->playerBag = state->GetStateBags()->RegisterStateBag(fmt::sprintf("player:%d", client->GetNetId()));

if (fx::IsBigMode())
{
data->playerBag->AddRoutingTarget(client->GetSlotId());
}

data->playerBag->SetOwningPeer(client->GetSlotId());
}
};

data->playerBag->SetOwningPeer(client->GetSlotId());
if (client->GetNetId() < 0xFFFF)
{
setupBag();
}
else
{
client->OnAssignNetId.Connect([setupBag]()
{
setupBag();
}, INT32_MAX);
}

client->SetSyncData(data);
client->OnDrop.Connect([weakClient, state]()
Expand Down Expand Up @@ -1224,7 +1248,11 @@ void ServerGameState::Tick(fx::ServerInstanceBase* instance)
evMan->QueueEvent2("playerLeftScope", {}, std::map<std::string, std::string>{ { "player", fmt::sprintf("%d", ownerNetId) }, { "for", fmt::sprintf("%d", client->GetNetId()) } });

auto oldClientData = GetClientDataUnlocked(this, ownerRef);
oldClientData->playerBag->RemoveRoutingTarget(client->GetSlotId());

if (oldClientData->playerBag)
{
oldClientData->playerBag->RemoveRoutingTarget(client->GetSlotId());
}

clientData->playersInScope.reset(otherSlot);
clientData->playersToSlots.erase(ownerNetId);
Expand Down Expand Up @@ -1328,7 +1356,11 @@ void ServerGameState::Tick(fx::ServerInstanceBase* instance)
sec->TriggerClientEventReplayed("onPlayerJoining", fmt::sprintf("%d", client->GetNetId()), entityClient->GetNetId(), entityClient->GetName(), slotId);

auto ecData = GetClientDataUnlocked(this, entityClient);
ecData->playerBag->AddRoutingTarget(client->GetSlotId());

if (ecData->playerBag)
{
ecData->playerBag->AddRoutingTarget(client->GetSlotId());
}

/*NETEV playerEnteredScope SERVER
/#*
Expand Down

0 comments on commit 3c3dcf9

Please sign in to comment.