using System; using System.Collections.Generic; using System.Globalization; using System.IO; using UnityEngine; namespace HexaCrazy { [Serializable] public class LedgerEntry { public int amount; public string source, time; } [Serializable] public class ProfileData { public int version=1, coins=1500, avatar=2, tickets, tutorial, checkinDay=1, totalWins, totalClears, best, earned=1500; public string nickname="LuckyPlayer", id="880877", checkinLast="", checkinBonusLast="", lotteryLast=""; public int[] items={1,1,1,0}, cleared={0,0,0}; public bool music=true, sound=true, reduceMotion; public List ledger=new List{new LedgerEntry{amount=1500,source="WELCOME GIFT",time=DateTime.Now.ToString("MM-dd HH:mm")}}; } public sealed class LocalProfile { public ProfileData Data { get; private set; } public bool Available { get; private set; }=true; public string Path { get; } public Func Now = ()=>DateTime.Now; public LocalProfile(string path) { Path=path; Load(); } public void Load() { Data=new ProfileData(); try { if(File.Exists(Path)) JsonUtility.FromJsonOverwrite(File.ReadAllText(Path),Data); Normalize(); } catch { Data=new ProfileData(); } } void Normalize() { if(Data.items==null||Data.items.Length!=4) Data.items=new[]{1,1,1,0}; if(Data.cleared==null||Data.cleared.Length!=3) Data.cleared=new int[3]; for(int i=0;i<4;i++) Data.items[i]=Math.Max(0,Data.items[i]); for(int i=0;i<3;i++) Data.cleared[i]=Math.Max(0,Math.Min(20,Data.cleared[i])); Data.coins=Math.Max(0,Data.coins); Data.tickets=Math.Max(0,Data.tickets); Data.checkinDay=Math.Max(1,Math.Min(7,Data.checkinDay)); if(string.IsNullOrEmpty(Data.nickname)) Data.nickname="LuckyPlayer"; if(Data.ledger==null) Data.ledger=new List(); if(Data.ledger.Count>200) Data.ledger.RemoveRange(200,Data.ledger.Count-200); } public void Save() { try { Directory.CreateDirectory(System.IO.Path.GetDirectoryName(Path)); string temp=Path+".tmp"; File.WriteAllText(temp,JsonUtility.ToJson(Data,true)); if(File.Exists(Path)) File.Replace(temp,Path,null); else File.Move(temp,Path); Available=true; } catch(Exception e) { Available=false; Debug.LogWarning("Local save unavailable: "+e.GetType().Name); } } public void Reset() { Data=new ProfileData(); Save(); } public string Today => Now().ToString("yyyy-MM-dd",CultureInfo.InvariantCulture); int DaysSince(string value) { return DateTime.TryParse(value,CultureInfo.InvariantCulture,DateTimeStyles.None,out var d) ? Math.Max(0,(Now().Date-d.Date).Days) : 1; } public bool CanCheckin => string.IsNullOrEmpty(Data.checkinLast)||DaysSince(Data.checkinLast)>0; public bool FreeDraw => string.IsNullOrEmpty(Data.lotteryLast)||DaysSince(Data.lotteryLast)>0; public int CheckinSlot => string.IsNullOrEmpty(Data.checkinLast)?1:DaysSince(Data.checkinLast)<=0?Data.checkinDay:((Data.checkinDay+DaysSince(Data.checkinLast)-2)%7)+1; // Existing saves store the next slot, including slot 1 after a day-7 claim. public int ClaimedCheckinSlot => (Data.checkinDay + 5) % 7 + 1; public bool CanClaimCheckinBonus => Data.checkinLast == Today && Data.checkinBonusLast != Today; static readonly int[] CheckinCoins = {200, 0, 300, 0, 500, 0, 1000}; public static string CheckinRewardName(int day) => CheckinCoins[day-1] > 0 ? CheckinCoins[day-1].ToString("N0", CultureInfo.InvariantCulture) + " COINS" : LevelDatabase.ItemNames[day/2-1] + " ×1"; void GrantCheckin(int day, string source) { if (CheckinCoins[day-1] > 0) Coins(CheckinCoins[day-1], source + day); else Data.items[day/2-1]++; } public void Coins(int amount,string source) { Data.coins+=amount; if(amount>0) Data.earned+=amount; Data.ledger.Insert(0,new LedgerEntry{amount=amount,source=source,time=Now().ToString("MM-dd HH:mm")}); if(Data.ledger.Count>200) Data.ledger.RemoveRange(200,Data.ledger.Count-200); } public bool Buy(int item) { if(item<0||item>=4||Data.coins0) Coins(DrawCoins[index],"LUCKY DRAW"); else Data.items[DrawItems[index]]++; Save(); return index; } public int Win(LevelSpec spec,int clears,int score,System.Random rng) { bool first=Data.cleared[spec.mode]