using System; using System.Collections.Generic; using System.Globalization; using UnityEngine; namespace ShiroginSDK.Runtime.Modules.Data.Scripts { [Serializable] public class RuntimeData : BaseData { public List FloatValues = new(); public List StringValues = new(); protected override string PrefsKey => "RUNTIME_DATA"; private float GetFloat(string key, float def = 0) { var item = FloatValues.Find(i => i.Key == key); return item != null ? item.Value : def; } private void SetFloat(string key, float val) { FloatValues.RemoveAll(i => i.Key == key); FloatValues.Add(new StringFloatPair { Key = key, Value = val }); Save(); } private string GetString(string key) { var item = StringValues.Find(i => i.Key == key); return item != null ? item.Value : null; } private void SetString(string key, string val) { StringValues.RemoveAll(i => i.Key == key); StringValues.Add(new StringStringPair { Key = key, Value = val }); Save(); } // ---------------------------- Ad Cooldown ---------------------------- public void SetAdCooldown(string productId, float seconds) { // Debug.Log($"[RuntimeData] SetAdCooldown({productId}) = {seconds}"); SetFloat($"AD_COOLDOWN_{productId}", seconds); } public float GetAdCooldown(string productId, float def = 600f) { var val = GetFloat($"AD_COOLDOWN_{productId}", def); // Debug.Log($"[RuntimeData] GetAdCooldown({productId}) -> {val}"); return val; } public void SetAdLastWatch(string productId, DateTime time) { var formatted = time.ToString("o"); // Debug.Log($"[RuntimeData] SetAdLastWatch({productId}) = {formatted}"); SetString($"AD_LAST_{productId}", formatted); } public DateTime GetAdLastWatch(string productId) { var val = GetString($"AD_LAST_{productId}"); if (string.IsNullOrEmpty(val)) return DateTime.MinValue; try { if (DateTime.TryParse(val, null, DateTimeStyles.RoundtripKind, out var parsed)) return parsed; } catch (Exception ex) { Debug.LogWarning($"[RuntimeData] Invalid date for {productId}: {val} ({ex.Message})"); } return DateTime.MinValue; } public bool CanWatchAd(string productId) { var last = GetAdLastWatch(productId); var cd = GetAdCooldown(productId); if (last == DateTime.MinValue) return true; return (DateTime.UtcNow - last).TotalSeconds >= cd; } public float GetAdRemaining(string productId) { var last = GetAdLastWatch(productId); var cd = GetAdCooldown(productId); if (last == DateTime.MinValue) return 0f; var elapsed = (DateTime.UtcNow - last).TotalSeconds; var remaining = Mathf.Max(0, cd - (float)elapsed); return remaining; } public void ClearAdData(string productId) { ClearKey($"AD_COOLDOWN_{productId}"); ClearKey($"AD_LAST_{productId}"); } public void ClearKey(string key) { FloatValues.RemoveAll(i => i.Key == key); StringValues.RemoveAll(i => i.Key == key); Save(); } [Serializable] public class StringFloatPair { public string Key; public float Value; } [Serializable] public class StringStringPair { public string Key; public string Value; } } }