Playvoi/Playvoi.Client/Assets/ShiroginSDK/Runtime/Modules/Data/Scripts/RewardData.cs

115 lines
No EOL
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Assets/ShiroginSDK/Runtime/Data/RewardData.cs
using System;
using System.Collections.Generic;
namespace ShiroginSDK.Runtime.Modules.Data.Scripts
{
[Serializable]
public class AdPlacementState
{
public string PlacementId;
public long LastRewardUnix;
public int CooldownSeconds; // her placement için farklı olabilir
public long RemainingCooldown(long nowUnix)
{
var next = LastRewardUnix + CooldownSeconds;
return Math.Max(0, next - nowUnix);
}
}
[Serializable]
public class RewardData : BaseData
{
// Günlük giriş (streak)
public int DailyStreak;
public long LastDailyClaimUnix;
public long NextDailyClaimUnix;
public List<AdPlacementState> AdPlacements = new();
protected override string PrefsKey => "REWARD_DATA";
private static long NowUnix()
{
return DateTimeOffset.UtcNow.ToUnixTimeSeconds();
}
private AdPlacementState FindPlacement(string id)
{
return AdPlacements.Find(p => p.PlacementId == id);
}
public bool CanClaimDaily(long nowUnix)
{
return nowUnix >= NextDailyClaimUnix;
}
public bool ClaimDaily(int resetHours = 24)
{
var now = NowUnix();
if (!CanClaimDaily(now)) return false;
// Same-day claim mi, ardışık mı kontrol
if (LastDailyClaimUnix > 0)
{
// 24 saatten az ama aynı dönemi kaçırmadıysan streak korunur; basit yaklaşım:
var diff = now - LastDailyClaimUnix;
if (diff <= (resetHours + 6) * 3600L) // 6 saat tolerans
DailyStreak++;
else
DailyStreak = 1;
}
else
{
DailyStreak = 1;
}
LastDailyClaimUnix = now;
NextDailyClaimUnix = now + resetHours * 3600L;
Save();
return true;
}
public void UpsertAdPlacement(string placementId, int cooldownSeconds)
{
var p = FindPlacement(placementId);
if (p == null)
AdPlacements.Add(new AdPlacementState
{ PlacementId = placementId, CooldownSeconds = cooldownSeconds, LastRewardUnix = 0 });
else p.CooldownSeconds = cooldownSeconds;
Save();
}
public bool CanClaimAdReward(string placementId)
{
var p = FindPlacement(placementId);
if (p == null) return true; // default izin ver, sonra setle
return p.RemainingCooldown(NowUnix()) == 0;
}
public bool MarkAdRewardClaimed(string placementId)
{
var p = FindPlacement(placementId);
if (p == null)
{
p = new AdPlacementState { PlacementId = placementId, CooldownSeconds = 30, LastRewardUnix = 0 };
AdPlacements.Add(p);
}
var now = NowUnix();
if (p.RemainingCooldown(now) > 0) return false;
p.LastRewardUnix = now;
Save();
return true;
}
public long GetAdRemainingCooldown(string placementId)
{
var p = FindPlacement(placementId);
if (p == null) return 0;
return p.RemainingCooldown(NowUnix());
}
}
}