Rework shop to support multiple item types and add unified PlayerModel

Generalize the shop from selling only ModifierDefinition to any IShopItem
(modifiers + dice). Add purchase condition checks, hover tooltips, and
fixed prices. Introduce PlayerModel as a facade over CurrencyBank,
InventoryModel, and DiceCollection for centralized state and save/load.

New files:
- IShopItem interface for purchasable items
- ShopCatalog SO (unified catalog for all item types)
- ShopTooltipView (description on hover)
- PlayerModel (aggregates player state)
- DiceCollection (owned dice tracking)
- DiceCatalog SO (dice definition registry)
- DiceCollectionTests

Modified:
- ModifierDefinition/DieDefinitionSO implement IShopItem
- ShopModel/ShopView/ShopItemView/ShopController use IShopItem
- SaveData v3 with OwnedDiceIds
- GameController uses PlayerModel for save/load
- GameLifetimeScope registers new services

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-01 21:04:34 +07:00
parent 85d639aa70
commit fcceb0ce45
18 changed files with 576 additions and 54 deletions
@@ -0,0 +1,133 @@
using System.Collections.Generic;
using NUnit.Framework;
using YachtDice.Dice;
using YachtDice.Player;
namespace YachtDice.Tests
{
public sealed class DiceCollectionTests
{
private DiceCollection collection;
[SetUp]
public void SetUp()
{
collection = new DiceCollection();
}
[Test]
public void Add_IncreasesCount()
{
var die = StandardDieSO.CreateStandardD6ForTest();
collection.Add(die);
Assert.AreEqual(1, collection.OwnedDice.Count);
}
[Test]
public void Add_DuplicateId_Ignored()
{
var die = StandardDieSO.CreateStandardD6ForTest();
collection.Add(die);
collection.Add(die);
Assert.AreEqual(1, collection.OwnedDice.Count);
}
[Test]
public void Add_Null_Ignored()
{
collection.Add(null);
Assert.AreEqual(0, collection.OwnedDice.Count);
}
[Test]
public void OwnsById_ReturnsTrueWhenOwned()
{
var die = StandardDieSO.CreateStandardD6ForTest();
collection.Add(die);
Assert.IsTrue(collection.OwnsById("standard_d6"));
}
[Test]
public void OwnsById_ReturnsFalseWhenNotOwned()
{
Assert.IsFalse(collection.OwnsById("standard_d6"));
}
[Test]
public void Remove_DecreasesCount()
{
var die = StandardDieSO.CreateStandardD6ForTest();
collection.Add(die);
collection.Remove(die);
Assert.AreEqual(0, collection.OwnedDice.Count);
}
[Test]
public void GetSaveData_ReturnsIds()
{
var die = StandardDieSO.CreateStandardD6ForTest();
collection.Add(die);
var ids = collection.GetSaveData();
Assert.AreEqual(1, ids.Count);
Assert.AreEqual("standard_d6", ids[0]);
}
[Test]
public void LoadSaveData_RestoresDice()
{
var die = StandardDieSO.CreateStandardD6ForTest();
var catalog = DiceCatalog.CreateForTest(new List<DieDefinitionSO> { die });
var ids = new List<string> { "standard_d6" };
collection.LoadSaveData(ids, catalog);
Assert.AreEqual(1, collection.OwnedDice.Count);
Assert.AreEqual("standard_d6", collection.OwnedDice[0].Id);
}
[Test]
public void LoadSaveData_SkipsMissingIds()
{
var catalog = DiceCatalog.CreateForTest(new List<DieDefinitionSO>());
var ids = new List<string> { "nonexistent" };
collection.LoadSaveData(ids, catalog);
Assert.AreEqual(0, collection.OwnedDice.Count);
}
[Test]
public void Clear_RemovesAll()
{
var die = StandardDieSO.CreateStandardD6ForTest();
collection.Add(die);
collection.Clear();
Assert.AreEqual(0, collection.OwnedDice.Count);
}
[Test]
public void Add_FiresOnChanged()
{
bool fired = false;
collection.OnChanged += () => fired = true;
var die = StandardDieSO.CreateStandardD6ForTest();
collection.Add(die);
Assert.IsTrue(fired);
}
}
}
@@ -89,5 +89,22 @@ namespace YachtDice.Tests
Assert.IsNotNull(loaded);
Assert.AreEqual(0, loaded.Currency);
}
[Test]
public void SaveAndLoad_RoundTrip_PreservesDiceIds()
{
var data = new SaveData
{
Currency = 100,
OwnedDiceIds = new List<string> { "standard_d6", "chaos_d6" }
};
SaveSystem.Save(data);
var loaded = SaveSystem.Load();
Assert.AreEqual(2, loaded.OwnedDiceIds.Count);
Assert.AreEqual("standard_d6", loaded.OwnedDiceIds[0]);
Assert.AreEqual("chaos_d6", loaded.OwnedDiceIds[1]);
}
}
}
+42 -3
View File
@@ -1,7 +1,9 @@
using NUnit.Framework;
using UnityEngine;
using YachtDice.Dice;
using YachtDice.Economy;
using YachtDice.Inventory;
using YachtDice.Player;
using YachtDice.Shop;
using YachtDice.Modifiers.Definition;
using YachtDice.Modifiers.Runtime;
@@ -13,6 +15,7 @@ namespace YachtDice.Tests
private CurrencyBank bank;
private ModifierRegistry registry;
private InventoryModel inventory;
private DiceCollection diceCollection;
private ShopModel shop;
[SetUp]
@@ -24,7 +27,8 @@ namespace YachtDice.Tests
registry = new ModifierRegistry(5);
inventory = new InventoryModel(registry);
shop = new ShopModel(bank, inventory);
diceCollection = new DiceCollection();
shop = new ShopModel(bank, inventory, diceCollection);
}
[TearDown]
@@ -97,8 +101,8 @@ namespace YachtDice.Tests
[Test]
public void TryPurchase_FiresPurchaseEvent()
{
ModifierDefinition purchased = null;
shop.OnItemPurchased += def => purchased = def;
IShopItem purchased = null;
shop.OnItemPurchased += item => purchased = item;
var mod = CreateDef("test", shopPrice: 100);
@@ -134,5 +138,40 @@ namespace YachtDice.Tests
Assert.AreEqual(ShopItemState.Owned, shop.GetItemState(mod));
}
[Test]
public void TryPurchase_DieItem_AddsToDiceCollection()
{
var die = DieDefinitionSO.CreateForTest<StandardDieSO>("test_die", shopPrice: 100);
bool result = shop.TryPurchase(die);
Assert.IsTrue(result);
Assert.AreEqual(400, bank.Balance);
Assert.AreEqual(1, diceCollection.OwnedDice.Count);
}
[Test]
public void TryPurchase_DieItem_CannotBeBoughtTwice()
{
var die = DieDefinitionSO.CreateForTest<StandardDieSO>("unique_die", shopPrice: 100);
shop.TryPurchase(die);
bool secondResult = shop.TryPurchase(die);
Assert.IsFalse(secondResult);
Assert.AreEqual(400, bank.Balance);
Assert.AreEqual(1, diceCollection.OwnedDice.Count);
}
[Test]
public void GetItemState_Die_Owned_AfterPurchase()
{
var die = DieDefinitionSO.CreateForTest<StandardDieSO>("die1", shopPrice: 50);
shop.TryPurchase(die);
Assert.AreEqual(ShopItemState.Owned, shop.GetItemState(die));
}
}
}