-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
TODO: Encryption
- Loading branch information
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using UnityEngine; | ||
|
||
namespace CurrencySystem | ||
{ | ||
public class FileSaver : IWalletSaver | ||
{ | ||
public const string FILE_NAME = "wallet.dat"; | ||
|
||
public bool Load(out Dictionary<string, CurrencyAccount> wallet) | ||
{ | ||
wallet = new Dictionary<string, CurrencyAccount>(); | ||
if (!File.Exists(Path.Combine(Application.persistentDataPath, FILE_NAME))) return false; | ||
var data = File.ReadAllText(Path.Combine(Application.persistentDataPath, FILE_NAME)); | ||
if (string.IsNullOrEmpty(data)) return false; | ||
wallet = Serializer.Deserialize<Dictionary<string, CurrencyAccount>>(Convert.FromBase64String(data)); | ||
return true; | ||
} | ||
|
||
public bool Save(Dictionary<string, CurrencyAccount> wallet) | ||
{ | ||
File.WriteAllText(Path.Combine(Application.persistentDataPath, FILE_NAME), Convert.ToBase64String(Serializer.Serialize(wallet))); | ||
return true; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace CurrencySystem | ||
{ | ||
public interface IWalletSaver | ||
{ | ||
bool Save(Dictionary<string, CurrencyAccount> wallet); | ||
bool Load(out Dictionary<string, CurrencyAccount> wallet); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.IO; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
|
||
namespace CurrencySystem | ||
{ | ||
public class Serializer | ||
{ | ||
public static T Deserialize<T>(byte[] data) where T : class | ||
{ | ||
using (var memory = new MemoryStream(data)) | ||
{ | ||
BinaryFormatter formatter = new BinaryFormatter(); | ||
return formatter.Deserialize(memory) as T; | ||
} | ||
} | ||
|
||
public static byte[] Serialize<T>(T data) where T : class | ||
{ | ||
using (var memory = new MemoryStream()) | ||
{ | ||
BinaryFormatter formatter = new BinaryFormatter(); | ||
formatter.Serialize(memory, data); | ||
return memory.ToArray(); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
namespace CurrencySystem | ||
{ | ||
public class PlayerPrefsSaver : IWalletSaver | ||
{ | ||
public const string PLAYER_PREFS_WALLET_KEY = "wallet"; | ||
|
||
public bool Load(out Dictionary<string, CurrencyAccount> wallet) | ||
{ | ||
wallet = new Dictionary<string, CurrencyAccount>(); | ||
var data = PlayerPrefs.GetString(PLAYER_PREFS_WALLET_KEY, ""); | ||
if (string.IsNullOrEmpty(data)) return false; | ||
wallet = Serializer.Deserialize<Dictionary<string, CurrencyAccount>>(Convert.FromBase64String(data)); | ||
return true; | ||
} | ||
|
||
public bool Save(Dictionary<string, CurrencyAccount> wallet) | ||
{ | ||
PlayerPrefs.SetString(PLAYER_PREFS_WALLET_KEY, Convert.ToBase64String(Serializer.Serialize(wallet))); | ||
return true; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using System; | ||
using System.Security.Cryptography; | ||
|
||
namespace CurrencySystem | ||
{ | ||
[Serializable] | ||
public class CurrencyAccount : ICurrency | ||
{ | ||
private readonly string _currencyCode; | ||
private float _multiplier = 1; | ||
private double _amount; | ||
|
||
[field: NonSerialized] | ||
public event ICurrency.CurrencyEvent OnCurrencyChange; | ||
|
||
private double Amount | ||
{ | ||
get { return _amount; } // TODO decrypt | ||
set { _amount = value; } // TODO encrypt | ||
} | ||
|
||
public string CurrencyCode => _currencyCode; | ||
|
||
/// <param name="currencyCode">Currency name used for accesing currency in the wallet and en/decryption currency</param> | ||
/// <param name="amount">Initial money amount</param> | ||
/// <param name="multiplier">Booster for income. By default it's 1.0f</param> | ||
public CurrencyAccount(string currencyCode, double amount, float multiplier = 1) | ||
{ | ||
_currencyCode = currencyCode ?? throw new ArgumentNullException(nameof(currencyCode)); | ||
_amount = amount; | ||
_multiplier = multiplier; | ||
} | ||
|
||
private void NotifyEvent() | ||
{ | ||
Aes.Create(); | ||
OnCurrencyChange?.Invoke(Amount); | ||
} | ||
|
||
public void Add(double amount) | ||
{ | ||
Amount += amount * _multiplier; | ||
NotifyEvent(); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
Amount = 0; | ||
NotifyEvent(); | ||
} | ||
|
||
public double Get() | ||
{ | ||
return Amount; | ||
} | ||
|
||
public void Subtract(double amount) | ||
{ | ||
Amount -= amount; | ||
NotifyEvent(); | ||
} | ||
|
||
public bool TrySubtract(double amount) | ||
{ | ||
if (Amount - amount >= 0) | ||
{ | ||
Amount -= amount; | ||
NotifyEvent(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static CurrencyAccount operator +(CurrencyAccount vault, double income) | ||
{ | ||
vault.Add(income); | ||
return vault; | ||
} | ||
|
||
public static CurrencyAccount operator +(CurrencyAccount vault, CurrencyAccount vault2) | ||
{ | ||
vault.Add(vault2.Get()); | ||
return vault; | ||
} | ||
|
||
public static CurrencyAccount operator ++(CurrencyAccount vault) | ||
{ | ||
vault.Add(1); | ||
return vault; | ||
} | ||
|
||
public static CurrencyAccount operator -(CurrencyAccount vault, double toll) | ||
{ | ||
vault.Subtract(toll); | ||
return vault; | ||
} | ||
|
||
public static CurrencyAccount operator -(CurrencyAccount vault, CurrencyAccount vault2) | ||
{ | ||
vault.Subtract(vault2.Get()); | ||
return vault; | ||
} | ||
|
||
public static CurrencyAccount operator --(CurrencyAccount vault) | ||
{ | ||
vault.Subtract(1); | ||
return vault; | ||
} | ||
|
||
public static implicit operator double(CurrencyAccount vault) => vault.Amount; | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
return Amount.Equals(obj); | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return Amount.ToString(); | ||
} | ||
} | ||
} |