| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- using System;
- namespace DevilLink2.Gameplay
- {
- public enum ItemType { Hint, Shuffle, Time }
- public sealed class ItemInventory
- {
- private readonly int[] counts;
- public ItemInventory(int hint = 1, int shuffle = 1, int time = 0)
- {
- if (hint < 0 || shuffle < 0 || time < 0) throw new ArgumentOutOfRangeException(nameof(hint));
- counts = new[] { hint, shuffle, time };
- }
- public int GetCount(ItemType type) { Validate(type); return counts[(int)type]; }
- internal bool CanAdd(ItemType type) => GetCount(type) < int.MaxValue;
- internal void Add(ItemType type) { Validate(type); counts[(int)type] = checked(counts[(int)type] + 1); }
- internal bool Consume(ItemType type)
- { if (GetCount(type) == 0) return false; counts[(int)type]--; return true; }
- internal static void Validate(ItemType type)
- { if (!Enum.IsDefined(typeof(ItemType), type)) throw new ArgumentOutOfRangeException(nameof(type)); }
- }
- public enum PurchaseStatus { Success, InsufficientCoins, DuplicateTransaction, InvalidItem, InvalidTransactionId, InventoryFull }
- public sealed class ShopService
- {
- private readonly PlayerEconomy economy;
- public ShopService(PlayerEconomy economy) { this.economy = economy ?? throw new ArgumentNullException(nameof(economy)); }
- public static int Price(ItemType type)
- {
- switch (type)
- {
- case ItemType.Hint: return 2000;
- case ItemType.Shuffle: return 5000;
- case ItemType.Time: return 3000;
- default: throw new ArgumentOutOfRangeException(nameof(type));
- }
- }
- // The caller reuses the same ID when delivering/retrying the same logical purchase.
- public PurchaseStatus TryBuy(ItemType type, Guid transactionId) => economy.Purchase(type, transactionId);
- }
- }
|