using System; using System.Collections.Generic; using System.Linq; namespace DevilLink2.Gameplay { public interface IUtcClock { DateTimeOffset UtcNow { get; } } public sealed class SystemUtcClock : IUtcClock { public DateTimeOffset UtcNow => DateTimeOffset.UtcNow; } public enum CoinCategory { LevelVictory, ShopPurchase, Reward, DailyCheckIn, LuckyWheel } public enum PlayerChangeKind { Purchase, ItemUsed, Reward } public sealed class CoinTransaction { public Guid Id { get; } public long Delta { get; } public CoinCategory Category { get; } public DateTimeOffset TimestampUtc { get; } public string Description { get; } public CoinTransaction(Guid id, long delta, CoinCategory category, DateTimeOffset timestampUtc, string description) { if (id == Guid.Empty || delta == 0 || delta == long.MinValue || !Enum.IsDefined(typeof(CoinCategory), category) || (category == CoinCategory.ShopPurchase ? delta >= 0 : delta <= 0)) throw new ArgumentException("Invalid coin transaction."); Id = id; Delta = delta; Category = category; TimestampUtc = timestampUtc.ToUniversalTime(); Description = description ?? ""; } } public sealed class CoinLedger { public const int Capacity = 200; private readonly List entries = new List(); public IReadOnlyList Entries => entries.AsReadOnly(); public decimal Income => entries.Where(t => t.Delta > 0).Sum(t => (decimal)t.Delta); public decimal Expenses => entries.Where(t => t.Delta < 0).Sum(t => -(decimal)t.Delta); internal void Append(CoinTransaction transaction) { int index = entries.FindIndex(t => t.TimestampUtc <= transaction.TimestampUtc); entries.Insert(index < 0 ? entries.Count : index, transaction); if (entries.Count > Capacity) entries.RemoveAt(entries.Count - 1); } } }