113 lines
3.7 KiB
C#
113 lines
3.7 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using YachtDice.Dice;
|
|
using YachtDice.Economy;
|
|
using YachtDice.Modifiers.Definition;
|
|
using YachtDice.Modifiers.Runtime;
|
|
using YachtDice.Persistence;
|
|
using YachtDice.Player;
|
|
using YachtDice.Shop;
|
|
|
|
namespace YachtDice.UI.Presentation
|
|
{
|
|
public sealed class GameSaveService : IGameSaveService
|
|
{
|
|
private readonly CurrencyBank _currencyBank;
|
|
private readonly ModifierRegistry _modifierRegistry;
|
|
private readonly ModifierCatalog _modifierCatalog;
|
|
private readonly DiceCatalog _diceCatalog;
|
|
private readonly ShopModel _shopModel;
|
|
private readonly PlayerModel _playerModel;
|
|
|
|
public GameSaveService(
|
|
CurrencyBank currencyBank,
|
|
ModifierRegistry modifierRegistry,
|
|
ModifierCatalog modifierCatalog,
|
|
DiceCatalog diceCatalog,
|
|
ShopModel shopModel,
|
|
PlayerModel playerModel)
|
|
{
|
|
_currencyBank = currencyBank;
|
|
_modifierRegistry = modifierRegistry;
|
|
_modifierCatalog = modifierCatalog;
|
|
_diceCatalog = diceCatalog;
|
|
_shopModel = shopModel;
|
|
_playerModel = playerModel;
|
|
}
|
|
|
|
public void Load()
|
|
{
|
|
var save = SaveSystem.Load();
|
|
|
|
if (save.currency > 0)
|
|
_currencyBank.SetBalance(save.currency);
|
|
|
|
if (_modifierCatalog != null && save.ownedModifiers != null && save.ownedModifiers.Count > 0)
|
|
LoadModifiers(save.ownedModifiers);
|
|
|
|
if (_diceCatalog != null && save.ownedDiceIds != null && save.ownedDiceIds.Count > 0)
|
|
LoadDice(save.ownedDiceIds);
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
var save = new SaveData
|
|
{
|
|
currency = _currencyBank.Balance,
|
|
ownedDiceIds = _playerModel.Dice.GetSaveData(),
|
|
};
|
|
|
|
var entries = _modifierRegistry.GetSaveData();
|
|
for (var i = 0; i < entries.Count; i++)
|
|
save.ownedModifiers.Add(entries[i]);
|
|
|
|
SaveSystem.Save(save);
|
|
}
|
|
|
|
private void LoadModifiers(List<ModifierSaveEntry> savedModifiers)
|
|
{
|
|
var validEntries = new List<ModifierSaveEntry>();
|
|
var permanentIds = new HashSet<string>();
|
|
|
|
for (var i = 0; i < savedModifiers.Count; i++)
|
|
{
|
|
var entry = savedModifiers[i];
|
|
var definition = _modifierCatalog.FindById(entry.modifierId);
|
|
|
|
if (definition == null)
|
|
{
|
|
Debug.LogWarning($"Modifier '{entry.modifierId}' not found in catalog, skipping.");
|
|
continue;
|
|
}
|
|
|
|
validEntries.Add(new ModifierSaveEntry
|
|
{
|
|
modifierId = entry.modifierId,
|
|
isActive = entry.isActive,
|
|
remainingUses = entry.remainingUses,
|
|
stacks = entry.stacks,
|
|
customState = entry.customState,
|
|
});
|
|
|
|
if (!definition.HasLimitedUses)
|
|
permanentIds.Add(definition.Id);
|
|
}
|
|
|
|
_modifierRegistry.LoadSaveData(validEntries, _modifierCatalog);
|
|
_shopModel.LoadPurchasedPermanentIds(permanentIds);
|
|
}
|
|
|
|
private void LoadDice(List<string> ownedDiceIds)
|
|
{
|
|
_playerModel.Dice.LoadSaveData(ownedDiceIds, _diceCatalog);
|
|
|
|
var dicePermanentIds = new HashSet<string>(ownedDiceIds);
|
|
var existingPermanentIds = _shopModel.GetPurchasedPermanentIds();
|
|
foreach (var id in dicePermanentIds)
|
|
existingPermanentIds.Add(id);
|
|
|
|
_shopModel.LoadPurchasedPermanentIds(existingPermanentIds);
|
|
}
|
|
}
|
|
}
|