SaveData.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Collections.Generic;
  3. namespace DevilLink2.Persistence
  4. {
  5. // Only durable player data belongs here. No board or run state.
  6. public sealed class SaveData
  7. {
  8. public const int CurrentVersion = 4;
  9. public string Nickname { get; set; } = Gameplay.PlayerProfile.DefaultNickname;
  10. public string Avatar { get; set; } = Gameplay.PlayerProfile.DefaultAvatar;
  11. public int SchemaVersion { get; set; } = CurrentVersion;
  12. public long Coins { get; set; } = 2000;
  13. public long SpinTickets { get; set; }
  14. public int ClassicProgress { get; set; }
  15. public int TimeAttackProgress { get; set; }
  16. public int ZenProgress { get; set; }
  17. public int HintCount { get; set; } = 1;
  18. public int ShuffleCount { get; set; } = 1;
  19. public int TimeCount { get; set; }
  20. public List<CoinTransactionData> CoinLedger { get; set; } = new List<CoinTransactionData>();
  21. // Compact receipts outlive the 200 visible ledger records, preventing replay after restart.
  22. public List<Guid> ProcessedTransactionIds { get; set; } = new List<Guid>();
  23. public int NextCheckInDay { get; set; } = 1;
  24. public string LastCheckInDateUtc { get; set; }
  25. public string LastFreeSpinDateUtc { get; set; }
  26. public List<RewardOperationData> RewardOperations { get; set; } = new List<RewardOperationData>();
  27. public SaveData Copy()
  28. {
  29. var data = (SaveData)MemberwiseClone();
  30. data.CoinLedger = CoinLedger == null ? new List<CoinTransactionData>() : CoinLedger.ConvertAll(t => t?.Copy());
  31. data.ProcessedTransactionIds = ProcessedTransactionIds == null ? new List<Guid>() : new List<Guid>(ProcessedTransactionIds);
  32. data.RewardOperations = RewardOperations == null ? new List<RewardOperationData>() : RewardOperations.ConvertAll(t => t?.Copy());
  33. return data;
  34. }
  35. }
  36. public sealed class RewardOperationData
  37. {
  38. public Guid Id { get; set; }
  39. public int Kind { get; set; }
  40. public int Number { get; set; }
  41. public DateTimeOffset TimestampUtc { get; set; }
  42. public bool UsedTicket { get; set; }
  43. public long RemainingTicketsAfter { get; set; }
  44. public RewardOperationData Copy() => (RewardOperationData)MemberwiseClone();
  45. }
  46. public sealed class CoinTransactionData
  47. {
  48. public Guid Id { get; set; }
  49. public long Delta { get; set; }
  50. public int Category { get; set; }
  51. public DateTimeOffset TimestampUtc { get; set; }
  52. public string Description { get; set; }
  53. public CoinTransactionData Copy() => (CoinTransactionData)MemberwiseClone();
  54. }
  55. public sealed class UnsupportedSaveVersionException : Exception
  56. {
  57. public UnsupportedSaveVersionException(string version) : base("Unsupported save schema: " + version + ". Files were not overwritten.") { }
  58. }
  59. public sealed class SaveFormatException : Exception
  60. {
  61. public SaveFormatException(string message, Exception inner = null) : base(message, inner) { }
  62. }
  63. public sealed class DecodedSave
  64. {
  65. public SaveData Data { get; }
  66. public bool NeedsRewrite { get; }
  67. public DecodedSave(SaveData data, bool needsRewrite) { Data = data; NeedsRewrite = needsRewrite; }
  68. }
  69. public enum SaveLoadSource { Primary, Backup, Defaults }
  70. public sealed class SaveLoadResult
  71. {
  72. public SaveData Data { get; }
  73. public SaveLoadSource Source { get; }
  74. public bool WasRewritten { get; }
  75. public SaveLoadResult(SaveData data, SaveLoadSource source, bool rewritten)
  76. { Data = data; Source = source; WasRewritten = rewritten; }
  77. }
  78. }