| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- namespace Olympus
- {
- [Serializable] public class LedgerEntry { public long time; public string label; public int delta, balance; }
- [Serializable] public class RealmProgress { public string id; public int best, firsts; }
- [Serializable] public class PlayerData
- {
- public int version = 3;
- public string name = "Rustic Lyra";
- public int avatar, coins = 2000;
- public bool profileSet, sound = true, music = true, haptics, autoReveal = true, letterPool = true;
- public int[] items = {1, 1, 1, 1};
- public List<string> found = new List<string>();
- public List<RealmProgress> progress = new List<RealmProgress>();
- public List<LedgerEntry> ledger = new List<LedgerEntry>();
- public string dailyKey = "", weeklyKey = "", lastCheckin = "";
- public int dailyScore, weeklyScore, checkinDay = 1, totalCheckins;
- public string lastSettledMatch = "";
- }
- public interface ISaveCodec { string Encode(PlayerData data); PlayerData Decode(string json); }
- public sealed class PlayerStore
- {
- public PlayerData Data { get; private set; }
- public string LastError { get; private set; }
- readonly string path;
- readonly ISaveCodec codec;
- readonly Func<DateTime> now;
- public PlayerStore(string path, ISaveCodec codec, Func<DateTime> clock = null)
- {
- this.path = path; this.codec = codec; now = clock ?? (() => DateTime.Now);
- Data = Read(path) ?? Read(path + ".bak");
- if (Data == null) Reset();
- EnsurePeriods();
- }
- PlayerData Read(string file)
- {
- try
- {
- if (!File.Exists(file)) return null;
- var d = codec.Decode(File.ReadAllText(file));
- if (d == null || d.version < 1 || d.version > 3 || d.coins < 0 || d.items == null || d.items.Length != 4 || d.items.Any(v => v < 0)
- || d.progress == null || d.found == null || d.ledger == null || d.checkinDay < 1 || d.checkinDay > 7) return null;
- d.avatar = Math.Max(0, Math.Min(9, d.avatar));
- // Legacy saves may hide the pool; tile selection now always requires it.
- d.letterPool = true;
- // Old v2 unfinished-trial fields are ignored; completed progress and economy survive.
- d.version = 3;
- if (string.IsNullOrWhiteSpace(d.name)) d.name = "Rustic Lyra";
- return d;
- }
- catch (Exception e) { LastError = e.Message; return null; }
- }
- public bool Save()
- {
- try
- {
- Directory.CreateDirectory(Path.GetDirectoryName(path));
- string tmp = path + ".tmp";
- File.WriteAllText(tmp, codec.Encode(Data));
- // Never replace a valid backup with a corrupt primary.
- if (File.Exists(path) && Read(path) != null) File.Copy(path, path + ".bak", true);
- if (File.Exists(path)) File.Delete(path);
- File.Move(tmp, path);
- LastError = null; return true;
- }
- catch (Exception e) { LastError = e.Message; return false; }
- }
- public void Reset()
- {
- Data = new PlayerData();
- Data.ledger.Add(new LedgerEntry {time = Timestamp(), label = "Starter grant", delta = 2000, balance = 2000});
- EnsurePeriods(); Save();
- // Reset also replaces the recovery snapshot so erased progress cannot return.
- try { if (File.Exists(path)) File.Copy(path, path + ".bak", true); } catch (IOException) { }
- }
- long Timestamp() => new DateTimeOffset(now()).ToUnixTimeMilliseconds();
- public string Today => now().ToString("yyyy-MM-dd");
- public bool CanCheckin => Data.lastCheckin != Today;
- public void EnsurePeriods()
- {
- var date = now().Date;
- string week = date.AddDays(-(((int)date.DayOfWeek + 6) % 7)).ToString("yyyy-MM-dd");
- if (Data.dailyKey != Today) { Data.dailyKey = Today; Data.dailyScore = 0; }
- if (Data.weeklyKey != week) { Data.weeklyKey = week; Data.weeklyScore = 0; }
- }
- public RealmProgress Progress(string id)
- {
- var p = Data.progress.Find(x => x.id == id);
- if (p == null) { p = new RealmProgress {id = id}; Data.progress.Add(p); }
- return p;
- }
- void ChangeCoins(int delta, string label)
- {
- Data.coins += delta;
- Data.ledger.Insert(0, new LedgerEntry {time = Timestamp(), label = label, delta = delta, balance = Data.coins});
- if (Data.ledger.Count > 200) Data.ledger.RemoveRange(200, Data.ledger.Count - 200);
- }
- public bool Enter(Realm realm)
- {
- if (Data.coins < realm.fee) return false;
- ChangeCoins(-realm.fee, "Entry · " + realm.name); Save(); return true;
- }
- public bool Buy(Item item, GameConfig config)
- {
- int i = (int)item, price = config.itemPrices[i];
- if (Data.coins < price) return false;
- ChangeCoins(-price, "Shop · " + Rules.ItemNames[i]); Data.items[i]++; Save(); return true;
- }
- public bool Consume(Item item)
- {
- int i = (int)item; if (Data.items[i] <= 0) return false;
- Data.items[i]--; Save(); return true;
- }
- public bool Claim(GameConfig config)
- {
- if (!CanCheckin) return false;
- int d = Data.checkinDay - 1;
- int coins = config.checkinCoins[d], item = config.checkinItems[d];
- if (coins > 0) ChangeCoins(coins, "Check-in · Day " + (d + 1));
- if (item >= 0) Data.items[item]++;
- Data.lastCheckin = Today; Data.checkinDay = (d + 1) % 7 + 1; Data.totalCheckins++; Save(); return true;
- }
- public void Found(string id) { if (!Data.found.Contains(id)) { Data.found.Add(id); Save(); } }
- public bool Settle(string matchId, Realm realm, int score, int rank, GameConfig config)
- {
- if (Data.lastSettledMatch == matchId) return false;
- EnsurePeriods(); Data.lastSettledMatch = matchId;
- var p = Progress(realm.id); p.best = Math.Max(p.best, score);
- if (score > 0 && rank == 1) p.firsts++;
- Data.dailyScore += score; Data.weeklyScore += score;
- if (score > 0 && rank <= 3) ChangeCoins(config.prizes[rank - 1], "Place " + rank + " · " + realm.name);
- Save(); return true;
- }
- }
- }
|