Skip to content
This repository has been archived by the owner on Jul 28, 2022. It is now read-only.

Commit

Permalink
Retrofit StoreService for new Data module
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleitnick committed Dec 29, 2019
1 parent 40f4154 commit 09816c1
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions src/ServerStorage/Aero/Services/StoreService.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ local PROMPT_PURCHASE_FINISHED_EVENT = "PromptPurchaseFinished"
local marketplaceService = game:GetService("MarketplaceService")

local dataStoreScope = "PlayerReceipts"
local services

local Data


local function IncrementPurchase(player, productId)
productId = tostring(productId)
local productPurchases = services.DataService:Get(player, PRODUCT_PURCHASES_KEY)
if (not productPurchases) then
productPurchases = {}
services.DataService:Set(player, PRODUCT_PURCHASES_KEY, productPurchases)
end
local n = productPurchases[productId]
productPurchases[productId] = (n and (n + 1) or 1)
services.DataService:FlushKey(player, PRODUCT_PURCHASES_KEY)
local playerData = Data.ForPlayer(player)
return playerData:Get(PRODUCT_PURCHASES_KEY, {}):Then(function(productPurchases)
local n = productPurchases[productId]
productPurchases[productId] = (n and (n + 1) or 1)
playerData:MarkDirty(PRODUCT_PURCHASES_KEY)
return playerData:Save(PRODUCT_PURCHASES_KEY)
end)
end


Expand All @@ -72,16 +72,27 @@ local function ProcessReceipt(receiptInfo)
local key = tostring(receiptInfo.PurchaseId)

-- Check if unique purchase was already completed:
local alreadyPurchased = services.DataService:GetCustom(dataStoreName, dataStoreScope, key)

local data = Data.new(dataStoreName, dataStoreScope)
local alreadyPurchasedSuccess, alreadyPurchased = data:Get(key):Await()
if (not alreadyPurchasedSuccess) then
return Enum.ProductPurchaseDecision.NotProcessedYet
end

if (not alreadyPurchased) then
-- Mark as purchased and save immediately:
services.DataService:SetCustom(dataStoreName, dataStoreScope, key, true)
services.DataService:FlushCustom(dataStoreName, dataStoreScope, key)
local success = data:Set(key, true):Then(function()
return data:Save(key)
end):Await()
if (not success) then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
end

if (player) then
IncrementPurchase(player, receiptInfo.ProductId)
local incSuccess = IncrementPurchase(player, receiptInfo.ProductId):Await()
if (not incSuccess) then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
StoreService:FireEvent(PROMPT_PURCHASE_FINISHED_EVENT, player, receiptInfo)
StoreService:FireClientEvent(PROMPT_PURCHASE_FINISHED_EVENT, player, receiptInfo)
end
Expand All @@ -92,8 +103,8 @@ end


function StoreService:HasPurchased(player, productId)
local productPurchases = services.DataService:Get(player, PRODUCT_PURCHASES_KEY)
return (productPurchases and productPurchases[tostring(productId)] ~= nil)
local success, productPurchases = Data.ForPlayer(player):Get(PRODUCT_PURCHASES_KEY, {}):Await()
return (success and productPurchases[tostring(productId)] ~= nil)
end


Expand All @@ -108,8 +119,8 @@ end
-- Get the number of productId's purchased:
function StoreService:GetNumberPurchased(player, productId)
local n = 0
local productPurchases = services.DataService:Get(player, PRODUCT_PURCHASES_KEY)
if (productPurchases) then
local success, productPurchases = Data.ForPlayer(player):Get(PRODUCT_PURCHASES_KEY, {}):Await()
if (success) then
n = (productPurchases[tostring(productId)] or 0)
end
return n
Expand Down Expand Up @@ -139,7 +150,7 @@ end


function StoreService:Init()
services = self.Services
Data = self.Modules.Data
self:RegisterEvent(PROMPT_PURCHASE_FINISHED_EVENT)
self:RegisterClientEvent(PROMPT_PURCHASE_FINISHED_EVENT)
end
Expand Down

0 comments on commit 09816c1

Please sign in to comment.