Files
Hunter/scripts/TrophyWallet.luau
2026-11-29 15:49:17 +03:00

156 lines
4.6 KiB
Luau

--!strict
-- Server-only wallet. Studio uses session balances instead of writing to a live experience.
local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local store = if RunService:IsStudio() then nil else DataStoreService:GetDataStore("HunterTrophies_V1")
local AUTOSAVE_SECONDS = 75
local RECEIPT_SECONDS = 24 * 60 * 60
type Batch = {id: string, amount: number}
type Wallet = {
saved: number,
pending: number,
earned: number,
batch: Batch?,
loading: boolean,
saving: boolean,
active: boolean,
}
local wallets: {[Player]: Wallet} = {}
local TrophyWallet = {}
local function savedBalance(value: unknown): number
if typeof(value) == "table" then
local balance = value.balance
if typeof(balance) == "number" and balance >= 0 and balance < math.huge then return math.floor(balance) end
end
return 0
end
local function publish(player: Player, wallet: Wallet)
if not wallet.active then return end
local total = wallet.saved + wallet.pending + (if wallet.batch then wallet.batch.amount else 0)
player:SetAttribute("TrophyBalance", total)
-- Let the HUD refresh loaded savings independently of the coin animation.
player:SetAttribute("TrophyOriginBalance", total - wallet.earned)
end
local function open(player: Player)
if wallets[player] then return end
local wallet: Wallet = {saved = 0, pending = 0, earned = 0, batch = nil, loading = store ~= nil, saving = false, active = true}
wallets[player] = wallet
publish(player, wallet)
local dataStore = store
if not dataStore then return end
task.spawn(function()
for attempt = 1, 3 do
local ok, data = pcall(function()
return dataStore:GetAsync(tostring(player.UserId))
end)
if ok then
wallet.saved = savedBalance(data)
break
end
if attempt < 3 and wallet.active then task.wait(attempt) else break end
end
wallet.loading = false
publish(player, wallet)
end)
end
local function flush(player: Player, wallet: Wallet): boolean
local dataStore = store
if not dataStore or wallet.loading or wallet.saving then return false end
local batch = wallet.batch
if not batch then
if wallet.pending == 0 then return true end
batch = {id = HttpService:GenerateGUID(false), amount = wallet.pending}
wallet.batch = batch
wallet.pending = 0
end
local receiptId, amount = batch.id, batch.amount
local now = os.time()
wallet.saving = true
local ok, data = pcall(function()
return dataStore:UpdateAsync(tostring(player.UserId), function(old)
local balance = savedBalance(old)
local receipts: {[string]: number} = {}
if typeof(old) == "table" and typeof(old.receipts) == "table" then
for id, timestamp in old.receipts do
if typeof(id) == "string" and typeof(timestamp) == "number" and now - timestamp < RECEIPT_SECONDS then
receipts[id] = timestamp
end
end
end
-- Reuse the same receipt if the request may have been committed before an error.
if not receipts[receiptId] then
balance += amount
receipts[receiptId] = now
end
return {balance = balance, receipts = receipts}
end)
end)
wallet.saving = false
if not ok then return false end
wallet.saved = savedBalance(data)
wallet.batch = nil
publish(player, wallet)
return true
end
local function drain(player: Player, wallet: Wallet)
if not store then return end
local deadline = os.clock() + 18
while (wallet.batch or wallet.pending > 0) and os.clock() < deadline do
if not flush(player, wallet) then task.wait(1) end
end
if wallet.batch or wallet.pending > 0 then
warn("Could not finish saving Hunter Trophies for user " .. player.UserId)
end
end
function TrophyWallet.awardPickup(player: Player, amount: number)
local wallet = wallets[player]
if not wallet or not wallet.active then return end
wallet.pending += amount
wallet.earned += amount
publish(player, wallet)
end
Players.PlayerAdded:Connect(open)
for _, player in Players:GetPlayers() do open(player) end
Players.PlayerRemoving:Connect(function(player)
local wallet = wallets[player]
if not wallet then return end
wallet.active = false
drain(player, wallet)
wallets[player] = nil
end)
if store then
task.spawn(function()
while task.wait(AUTOSAVE_SECONDS) do
for player, wallet in wallets do
if wallet.active then task.spawn(flush, player, wallet) end
end
end
end)
game:BindToClose(function()
local remaining = 0
for player, wallet in wallets do
remaining += 1
task.spawn(function()
drain(player, wallet)
remaining -= 1
end)
end
local deadline = os.clock() + 23
while remaining > 0 and os.clock() < deadline do task.wait(.1) end
end)
end
return TrophyWallet