77 lines
1.7 KiB
C#
77 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using YachtDice.Dice;
|
|
|
|
namespace YachtDice.Player
|
|
{
|
|
public class DiceCollection
|
|
{
|
|
private readonly List<DiceDefinition> _ownedDice = new();
|
|
|
|
public event Action OnChanged;
|
|
|
|
public IReadOnlyList<DiceDefinition> OwnedDice => _ownedDice;
|
|
|
|
public void Add(DiceDefinition definition)
|
|
{
|
|
if (definition == null) return;
|
|
if (OwnsById(definition.Id)) return;
|
|
|
|
_ownedDice.Add(definition);
|
|
OnChanged?.Invoke();
|
|
}
|
|
|
|
public void Remove(DiceDefinition definition)
|
|
{
|
|
if (_ownedDice.Remove(definition))
|
|
OnChanged?.Invoke();
|
|
}
|
|
|
|
public bool OwnsById(string id)
|
|
{
|
|
foreach (var t in _ownedDice)
|
|
{
|
|
if (t != null && t.Id == id)
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public List<string> GetSaveData()
|
|
{
|
|
var ids = new List<string>();
|
|
foreach (var t in _ownedDice)
|
|
ids.Add(t.Id);
|
|
|
|
return ids;
|
|
}
|
|
|
|
public void LoadSaveData(List<string> diceIds, DiceCatalog catalog)
|
|
{
|
|
_ownedDice.Clear();
|
|
|
|
if (diceIds == null)
|
|
{
|
|
OnChanged?.Invoke();
|
|
return;
|
|
}
|
|
|
|
foreach (var t in diceIds)
|
|
{
|
|
var def = catalog.FindById(t);
|
|
if (def != null)
|
|
_ownedDice.Add(def);
|
|
}
|
|
|
|
OnChanged?.Invoke();
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
_ownedDice.Clear();
|
|
OnChanged?.Invoke();
|
|
}
|
|
}
|
|
}
|