using System; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEngine; namespace Fateforge { // A device can host several Game Center players. Keep an acknowledged watermark for each. public sealed class EndlessRecords { [Serializable] sealed class Entry { public string player; public long best, reported; // Older builds also counted unfinished runs in best. Keep that local history // while allowing only a finished or server-confirmed score onto the leaderboard. public long finishedBest; public bool hasFinishedScore, hasReportedScore; public bool HasReportedScore => hasReportedScore || reported > 0; } [Serializable] sealed class Ledger { public List players = new List(); } readonly string path; readonly Ledger ledger; bool needsWrite; public EndlessRecords(string directory) { path = Path.Combine(directory, "endless-records.json"); foreach (string file in new[]{path, path+".bak"}) { if (!File.Exists(file)) continue; try { var data = JsonUtility.FromJson(File.ReadAllText(file)); if (data?.players == null) continue; ledger = data; return; } catch (ArgumentException) { } catch (IOException) { } } ledger = new Ledger(); } Entry Find(string player) => ledger.players.Find(entry => entry.player == (player ?? "")); public long Best(string player) => Find(player)?.best ?? 0; public long DeviceBest => ledger.players.Count == 0 ? 0 : ledger.players.Max(entry => entry.best); public long Pending(string player) => TryGetPending(player, out long score) ? score : 0; public bool TryGetPending(string player, out long score) { Flush(); var entry = Find(player); score = entry?.finishedBest ?? 0; return !string.IsNullOrEmpty(player) && entry != null && entry.hasFinishedScore && (!entry.HasReportedScore || score > entry.reported); } Entry Get(string player) { var entry = Find(player); if (entry != null) return entry; entry = new Entry{player=player ?? ""}; ledger.players.Add(entry); return entry; } public void Record(string player,long score) { if (score < 0) return; var entry = Get(player); if (entry.hasFinishedScore && score <= entry.finishedBest) { Flush(); return; } entry.hasFinishedScore = true; entry.finishedBest = score; entry.best = Math.Max(entry.best, score); Persist(); } public void BindGuest(string player) { var guest = Find(""); if (string.IsNullOrEmpty(player) || guest == null) return; var owner = Get(player); owner.best = Math.Max(owner.best,guest.best); owner.finishedBest = Math.Max(owner.finishedBest, guest.finishedBest); owner.hasFinishedScore |= guest.hasFinishedScore; ledger.players.Remove(guest); Persist(); } public void Acknowledge(string player,long submitted) { var entry = Find(player); if (entry == null || !entry.hasFinishedScore || submitted < 0 || submitted > entry.finishedBest) return; if (entry.HasReportedScore && submitted <= entry.reported) return; entry.reported = submitted; entry.hasReportedScore = true; Persist(); } void Persist() { needsWrite = true; Flush(); } void Flush() { if (!needsWrite) return; FileSaveStore.Atomic(path,JsonUtility.ToJson(ledger)); needsWrite = false; } } }