EndlessRecords.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEngine;
  6. namespace Fateforge
  7. {
  8. // A device can host several Game Center players. Keep an acknowledged watermark for each.
  9. public sealed class EndlessRecords
  10. {
  11. [Serializable] sealed class Entry
  12. {
  13. public string player;
  14. public long best, reported;
  15. // Older builds also counted unfinished runs in best. Keep that local history
  16. // while allowing only a finished or server-confirmed score onto the leaderboard.
  17. public long finishedBest;
  18. public bool hasFinishedScore, hasReportedScore;
  19. public bool HasReportedScore => hasReportedScore || reported > 0;
  20. }
  21. [Serializable] sealed class Ledger
  22. {
  23. public List<Entry> players = new List<Entry>();
  24. }
  25. readonly string path;
  26. readonly Ledger ledger;
  27. bool needsWrite;
  28. public EndlessRecords(string directory)
  29. {
  30. path = Path.Combine(directory, "endless-records.json");
  31. foreach (string file in new[]{path, path+".bak"})
  32. {
  33. if (!File.Exists(file)) continue;
  34. try
  35. {
  36. var data = JsonUtility.FromJson<Ledger>(File.ReadAllText(file));
  37. if (data?.players == null) continue;
  38. ledger = data;
  39. return;
  40. }
  41. catch (ArgumentException) { }
  42. catch (IOException) { }
  43. }
  44. ledger = new Ledger();
  45. }
  46. Entry Find(string player) => ledger.players.Find(entry => entry.player == (player ?? ""));
  47. public long Best(string player) => Find(player)?.best ?? 0;
  48. public long DeviceBest => ledger.players.Count == 0 ? 0 : ledger.players.Max(entry => entry.best);
  49. public long Pending(string player) => TryGetPending(player, out long score) ? score : 0;
  50. public bool TryGetPending(string player, out long score)
  51. {
  52. Flush();
  53. var entry = Find(player);
  54. score = entry?.finishedBest ?? 0;
  55. return !string.IsNullOrEmpty(player) && entry != null && entry.hasFinishedScore
  56. && (!entry.HasReportedScore || score > entry.reported);
  57. }
  58. Entry Get(string player)
  59. {
  60. var entry = Find(player);
  61. if (entry != null) return entry;
  62. entry = new Entry{player=player ?? ""};
  63. ledger.players.Add(entry);
  64. return entry;
  65. }
  66. public void Record(string player,long score)
  67. {
  68. if (score < 0) return;
  69. var entry = Get(player);
  70. if (entry.hasFinishedScore && score <= entry.finishedBest) { Flush(); return; }
  71. entry.hasFinishedScore = true;
  72. entry.finishedBest = score;
  73. entry.best = Math.Max(entry.best, score);
  74. Persist();
  75. }
  76. public void BindGuest(string player)
  77. {
  78. var guest = Find("");
  79. if (string.IsNullOrEmpty(player) || guest == null) return;
  80. var owner = Get(player);
  81. owner.best = Math.Max(owner.best,guest.best);
  82. owner.finishedBest = Math.Max(owner.finishedBest, guest.finishedBest);
  83. owner.hasFinishedScore |= guest.hasFinishedScore;
  84. ledger.players.Remove(guest);
  85. Persist();
  86. }
  87. public void Acknowledge(string player,long submitted)
  88. {
  89. var entry = Find(player);
  90. if (entry == null || !entry.hasFinishedScore || submitted < 0 || submitted > entry.finishedBest) return;
  91. if (entry.HasReportedScore && submitted <= entry.reported) return;
  92. entry.reported = submitted;
  93. entry.hasReportedScore = true;
  94. Persist();
  95. }
  96. void Persist()
  97. {
  98. needsWrite = true;
  99. Flush();
  100. }
  101. void Flush()
  102. {
  103. if (!needsWrite) return;
  104. FileSaveStore.Atomic(path,JsonUtility.ToJson(ledger));
  105. needsWrite = false;
  106. }
  107. }
  108. }