--!strict
-- // Roblox Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- // Imports
local Net = require(ReplicatedStorage.Shared.Net)
local RateLimiter = require(ReplicatedStorage.Shared.Utilities.RateLimiter)
local ProfileService = require(script.Parent.ProfileService)
local GuildService = require(script.Parent.GuildService)
-- // Types
type DepositRequest = {
GuildId: string,
Amount: number,
}
-- // Constants
local MaxDeposit = 1_000_000
local DepositLimiter = RateLimiter.new(4, 10)
-- // Local Methods
local function ParseRequest(Request: unknown): DepositRequest?
if typeof(Request) ~= "table" then
return nil
end
local GuildId = (Request :: any).GuildId
local Amount = (Request :: any).Amount
if typeof(GuildId) ~= "string" or #GuildId > 36 then
return nil
end
if typeof(Amount) ~= "number" or Amount ~= Amount or Amount % 1 ~= 0 then
return nil
end
if Amount < 1 or Amount > MaxDeposit then
return nil
end
return { GuildId = GuildId, Amount = Amount }
end
local function Deposit(Player: Player, Request: unknown): (boolean, string?)
if not DepositLimiter:Allow(Player.UserId) then
return false, "Slow down"
end
local Parsed = ParseRequest(Request)
if not Parsed then
return false, "Bad request"
end
local Profile = ProfileService.Get(Player)
if not Profile or Profile.Data.GuildId ~= Parsed.GuildId then
return false, "Not in that guild"
end
if Profile.Data.Gold < Parsed.Amount then
return false, "Not enough gold"
end
Profile.Data.Gold -= Parsed.Amount
GuildService.AddToTreasury(Parsed.GuildId, Parsed.Amount, Player.UserId)
return true, nil
end
-- // Main
local GuildTreasuryService = {}
function GuildTreasuryService.Start()
Net.OnInvoke("GuildDeposit", Deposit)
Players.PlayerRemoving:Connect(function(Player: Player)
DepositLimiter:Forget(Player.UserId)
end)
end
return GuildTreasuryService
Server-side, typed, rate limited, and never trusts a number it didn't check.