mirror of
https://github.com/Bragin-Stepan/project-entity.git
synced 2026-03-05 07:41:10 +00:00
init: add project
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
namespace _Project.Develop.Runtime.Logic.Meta.Features.Wallet
|
||||
{
|
||||
public enum CurrencyTypes
|
||||
{
|
||||
Gold,
|
||||
Diamond
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b1822fc8809b486dbb88227c36bec03f
|
||||
timeCreated: 1769721690
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using _Project.Develop.Runtime.Logic.Meta.Features.Wallet;
|
||||
using _Project.Develop.Runtime.Utils.ReactiveManagement;
|
||||
using Assets._Project.Develop.Runtime.Utilities.DataManagement;
|
||||
using Assets._Project.Develop.Runtime.Utilities.DataManagement.DataProviders;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Meta.Features.Wallet
|
||||
{
|
||||
public class WalletService : IDataReader<PlayerData>, IDataWriter<PlayerData>
|
||||
{
|
||||
private readonly Dictionary<CurrencyTypes, ReactiveVariable<int>> _currencies;
|
||||
|
||||
public WalletService(
|
||||
Dictionary<CurrencyTypes, ReactiveVariable<int>> currencies,
|
||||
PlayerDataProvider playerDataProvider)
|
||||
{
|
||||
_currencies = new Dictionary<CurrencyTypes, ReactiveVariable<int>>(currencies);
|
||||
playerDataProvider.RegisterWriter(this);
|
||||
playerDataProvider.RegisterReader(this);
|
||||
}
|
||||
|
||||
public List<CurrencyTypes> AvailableCurrencies => _currencies.Keys.ToList();
|
||||
|
||||
public IReadOnlyVariable<int> GetCurrency(CurrencyTypes type) => _currencies[type];
|
||||
|
||||
public bool Enough(CurrencyTypes type, int amount)
|
||||
{
|
||||
if (amount < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(amount));
|
||||
|
||||
return _currencies[type].Value >= amount;
|
||||
}
|
||||
|
||||
public void Add(CurrencyTypes type, int amount)
|
||||
{
|
||||
if (amount < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(amount));
|
||||
|
||||
_currencies[type].Value += amount;
|
||||
}
|
||||
|
||||
public void Spend(CurrencyTypes type, int amount)
|
||||
{
|
||||
if (Enough(type, amount) == false)
|
||||
throw new InvalidOperationException("Not enough: " + type.ToString());
|
||||
|
||||
if (amount < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(amount));
|
||||
|
||||
_currencies[type].Value -= amount;
|
||||
}
|
||||
|
||||
public void ReadFrom(PlayerData data)
|
||||
{
|
||||
foreach (KeyValuePair<CurrencyTypes, int> currency in data.WalletData)
|
||||
{
|
||||
if (_currencies.ContainsKey(currency.Key))
|
||||
_currencies[currency.Key].Value = currency.Value;
|
||||
else
|
||||
_currencies.Add(currency.Key, new ReactiveVariable<int>(currency.Value));
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteTo(PlayerData data)
|
||||
{
|
||||
foreach (KeyValuePair<CurrencyTypes, ReactiveVariable<int>> currency in _currencies)
|
||||
{
|
||||
if (data.WalletData.ContainsKey(currency.Key))
|
||||
data.WalletData[currency.Key] = currency.Value.Value;
|
||||
else
|
||||
data.WalletData.Add(currency.Key, currency.Value.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bac6967622d94d37886219039cead5ed
|
||||
timeCreated: 1769721675
|
||||
Reference in New Issue
Block a user