| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace DevilLink2.Gameplay
- {
- public enum RewardOperationKind { DailyCheckIn, WheelSpin }
- public enum RewardStatus { Success, AlreadyClaimed, ClockRolledBack, InsufficientTickets, CoinOverflow, InventoryOverflow,
- InvalidOperationId, DuplicateOperation, OperationFailed }
- public sealed class RewardOperationRecord
- {
- public Guid Id { get; }
- public RewardOperationKind Kind { get; }
- public int Number { get; } // Check-in day or one-based wheel sector.
- public RewardDefinition Reward { get; }
- public DateTimeOffset TimestampUtc { get; }
- public bool UsedTicket { get; }
- public bool UsedFreeSpin => Kind == RewardOperationKind.WheelSpin && !UsedTicket;
- public long RemainingTicketsAfter { get; }
- public RewardOperationRecord(Guid id, RewardOperationKind kind, int number, DateTimeOffset timestampUtc, bool usedTicket, long remainingTicketsAfter)
- {
- if (id == Guid.Empty || !Enum.IsDefined(typeof(RewardOperationKind), kind) || timestampUtc == DateTimeOffset.MinValue ||
- remainingTicketsAfter < 0 || kind == RewardOperationKind.DailyCheckIn && usedTicket) throw new ArgumentException("Invalid reward receipt.");
- Reward = kind == RewardOperationKind.DailyCheckIn ? RewardCatalog.Daily(number) : RewardCatalog.Wheel(number);
- Id = id; Kind = kind; Number = number; TimestampUtc = timestampUtc.ToUniversalTime();
- UsedTicket = usedTicket; RemainingTicketsAfter = remainingTicketsAfter;
- }
- }
- public sealed class RewardState
- {
- private readonly List<RewardOperationRecord> operations;
- public int NextCheckInDay { get; private set; }
- public DateTime? LastCheckInDateUtc { get; private set; }
- public DateTime? LastFreeSpinDateUtc { get; private set; }
- public IReadOnlyList<RewardOperationRecord> Operations => operations.AsReadOnly();
- public RewardState(int nextCheckInDay = 1, DateTime? lastCheckInDateUtc = null, DateTime? lastFreeSpinDateUtc = null,
- IEnumerable<RewardOperationRecord> operations = null)
- {
- if (nextCheckInDay < 1 || nextCheckInDay > RewardCatalog.CheckInDays) throw new ArgumentOutOfRangeException(nameof(nextCheckInDay));
- NextCheckInDay = nextCheckInDay;
- LastCheckInDateUtc = Date(lastCheckInDateUtc); LastFreeSpinDateUtc = Date(lastFreeSpinDateUtc);
- this.operations = operations == null ? new List<RewardOperationRecord>() : new List<RewardOperationRecord>(operations);
- if (this.operations.Any(o => o == null) || this.operations.Select(o => o.Id).Distinct().Count() != this.operations.Count)
- throw new ArgumentException("Invalid or duplicated reward receipts.", nameof(operations));
- }
- private static DateTime? Date(DateTime? value) => value.HasValue ? DateTime.SpecifyKind(value.Value.Date, DateTimeKind.Utc) : (DateTime?)null;
- internal RewardOperationRecord Find(Guid id) => operations.Find(r => r.Id == id);
- internal void Commit(RewardOperationRecord record)
- {
- operations.Add(record);
- if (record.Kind == RewardOperationKind.DailyCheckIn)
- { LastCheckInDateUtc = record.TimestampUtc.UtcDateTime.Date; NextCheckInDay = record.Number % RewardCatalog.CheckInDays + 1; }
- else if (!record.UsedTicket) LastFreeSpinDateUtc = record.TimestampUtc.UtcDateTime.Date;
- }
- }
- public sealed class RewardResult
- {
- public RewardStatus Status { get; }
- public bool Succeeded => Status == RewardStatus.Success;
- public RewardOperationRecord Record { get; }
- public int Number { get; }
- public RewardDefinition Reward { get; }
- public long RemainingTickets { get; }
- public string Error { get; }
- public RewardResult(RewardStatus status, long tickets, RewardOperationRecord record = null,
- int number = 0, RewardDefinition reward = null, string error = null)
- { Status = status; RemainingTickets = tickets; Record = record; Number = record?.Number ?? number; Reward = record?.Reward ?? reward; Error = error; }
- }
- }
|