86 lines
2.9 KiB
Luau
86 lines
2.9 KiB
Luau
-- Daily login (7-day cycle) and playtime (9 tiers per day) rewards. Data: PlayerData.Rewards(player).
|
|
-- Attributes for the UI: RW_LoginDay (gifts claimed in total), RW_LoginReady (today's gift not claimed yet),
|
|
-- RW_PlaySec (seconds played today), RW_PlayClaimed (bit i-1 = playtime tier i claimed today).
|
|
-- Day = UTC date (00:00 UTC = 03:00 MSK). A missed day does not reset the cycle.
|
|
local Players = game:GetService("Players")
|
|
local RS = game:GetService("ReplicatedStorage")
|
|
local folder = RS:WaitForChild("OrnamentMachine")
|
|
local Config = require(folder:WaitForChild("Config"))
|
|
local PD = require(script.Parent:WaitForChild("OrnamentPlayerData"))
|
|
local R = Config.REWARDS
|
|
local TICK = 10
|
|
|
|
local remote = folder:FindFirstChild("RewardsRemote") or Instance.new("RemoteFunction")
|
|
remote.Name = "RewardsRemote"
|
|
remote.Parent = folder
|
|
|
|
local function today() return os.date("!%Y-%m-%d") end
|
|
local function state(player)
|
|
local r = PD.Rewards(player)
|
|
if not r then return nil end
|
|
local t = today()
|
|
if r.PlayDate ~= t then r.PlayDate, r.PlaySec, r.PlayClaimed = t, 0, 0 end
|
|
return r
|
|
end
|
|
local function publish(player, r)
|
|
player:SetAttribute("RW_LoginDay", r.LoginDay)
|
|
player:SetAttribute("RW_LoginReady", r.LoginDate ~= today())
|
|
player:SetAttribute("RW_PlaySec", r.PlaySec)
|
|
player:SetAttribute("RW_PlayClaimed", r.PlayClaimed)
|
|
end
|
|
local function grant(player, g)
|
|
if g.Shards then PD.AddShards(player, g.Shards) end
|
|
for id, n in g.Boosters or {} do PD.AddBooster(player, id, n) end
|
|
if g.XP then PD.AddXP(player, g.XP) end
|
|
end
|
|
local function claimed(mask, i) return math.floor(mask / 2 ^ (i - 1)) % 2 == 1 end
|
|
|
|
remote.OnServerInvoke = function(player, action, idx)
|
|
local r = state(player)
|
|
if not r then return false, "Not loaded yet" end
|
|
if action == "Login" then
|
|
if r.LoginDate == today() then return false, "Come back tomorrow" end
|
|
local day = r.LoginDay % #R.LOGIN + 1
|
|
grant(player, R.LOGIN[day])
|
|
r.LoginDay += 1
|
|
r.LoginDate = today()
|
|
elseif action == "Play" then
|
|
idx = tonumber(idx)
|
|
local tier = idx and R.PLAYTIME[idx]
|
|
if not tier then return false end
|
|
if claimed(r.PlayClaimed, idx) then return false, "Already claimed" end
|
|
if r.PlaySec < tier.Minutes * 60 then return false, "Keep playing" end
|
|
grant(player, tier)
|
|
r.PlayClaimed += 2 ^ (idx - 1)
|
|
else
|
|
return false
|
|
end
|
|
PD.MarkDirty(player)
|
|
publish(player, r)
|
|
return true
|
|
end
|
|
|
|
local function onJoin(player)
|
|
local t0 = os.clock()
|
|
repeat task.wait(0.5) until PD.Get(player) or not player.Parent or os.clock() - t0 > 60
|
|
local r = player.Parent and state(player)
|
|
if r then publish(player, r) end
|
|
end
|
|
Players.PlayerAdded:Connect(onJoin)
|
|
for _, p in Players:GetPlayers() do task.spawn(onJoin, p) end
|
|
|
|
-- playtime: +TICK seconds for every player in the game
|
|
task.spawn(function()
|
|
while true do
|
|
task.wait(TICK)
|
|
for _, p in Players:GetPlayers() do
|
|
local r = state(p)
|
|
if r then
|
|
r.PlaySec += TICK
|
|
PD.MarkDirty(p)
|
|
publish(p, r)
|
|
end
|
|
end
|
|
end
|
|
end)
|