Skip to content

Commit c95b323

Browse files
committed
Merge Allow the server to refresh sku's after initial load (pr-3056)
c3cf69e - fix(commerce): allow the server to refresh sku's after initial load
2 parents 7cc7add + c3cf69e commit c95b323

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

code/components/citizen-server-impl/src/ServerExtCommerce.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,8 @@ class ClientExtCommerceComponent : public fwRefCountable
471471
fx::Client* m_client;
472472

473473
bool m_commerceDataLoaded;
474+
475+
std::chrono::milliseconds m_lastUpdate { 0 };
474476

475477
std::set<int> m_ownedSkus;
476478

@@ -481,11 +483,18 @@ void ClientExtCommerceComponent::LoadCommerceData()
481483
{
482484
auto userId = GetUserId();
483485

484-
if (m_commerceDataLoaded || !userId)
486+
// If we already have our commerce data, only allow the server to refresh it every 10 seconds so we're not spamming requests
487+
if (!userId || (m_commerceDataLoaded && (msec() - m_lastUpdate) < 10s))
485488
{
486489
return;
487490
}
488491

492+
// Reset our commerce data state so we can properly wait for it to load again (in case this is a refresh)
493+
m_commerceDataLoaded = false;
494+
// don't allow the client to re-fetch while we're doing the request, this will be set again further down
495+
// once the request is finished
496+
m_lastUpdate = msec();
497+
489498
fwRefContainer<ClientExtCommerceComponent> thisRef(this);
490499

491500
HttpRequestOptions opts;
@@ -564,6 +573,7 @@ std::optional<int> ClientExtCommerceComponent::GetUserId()
564573

565574
void ClientExtCommerceComponent::SetSkus(std::set<int>&& list)
566575
{
576+
m_lastUpdate = msec();
567577
m_ownedSkus = std::move(list);
568578
m_commerceDataLoaded = true;
569579
}

ext/native-decls/LoadPlayerCommerceDataExt.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,36 @@ apiset: server
88
void LOAD_PLAYER_COMMERCE_DATA_EXT(char* playerSrc);
99
```
1010
11-
Requests the commerce data from Tebex for the specified player, including the owned SKUs. Use `IS_PLAYER_COMMERCE_INFO_LOADED` to check if it has loaded.
11+
Requests the commerce data from Tebex for the specified player, including the owned SKUs.
12+
13+
Use [`IS_PLAYER_COMMERCE_INFO_LOADED_EXT`](#_0x1D14F4FE) to check if it has loaded.
14+
15+
This will not automatically update whenever a client purchases a package, if you want to fetch new purchases you will need to call this native again.
16+
17+
This native will temporarily cache the players commerce data for 10 seconds, a call to this native after 10 seconds will re-fetch the players commerce data.
1218
1319
## Parameters
1420
* **playerSrc**: The player handle
1521
22+
## Examples
23+
```lua
24+
RegisterNetEvent("doesOwnPackage", function(packageIdSku)
25+
-- source isn't valid across waits, so we localize it
26+
local source = source
27+
28+
-- input isn't right
29+
if type(packageIdSku) ~= "number" then
30+
return
31+
end
32+
33+
-- The native will cache the results
34+
LoadPlayerCommerceDataExt(source)
35+
-- Wait for the players data to load
36+
while not IsPlayerCommerceInfoLoadedExt(source) do
37+
Wait(0)
38+
end
39+
40+
-- Tell the client if they own the package or not
41+
TriggerClientEvent("doesOwnPackage", source, DoesPlayerOwnSkuExt(source, packageIdSku))
42+
end)
43+
```

0 commit comments

Comments
 (0)