117 lines
3.5 KiB
C#
117 lines
3.5 KiB
C#
using PrizeSystem;
|
||
using System;
|
||
using UnityEngine;
|
||
|
||
public static class PaymentSystem
|
||
{
|
||
public static int Balance { get; private set; }
|
||
|
||
public static event Action<int> onCoinAdded;
|
||
public static event Action<int> onCoinRemoved;
|
||
public static event Action<int> OnBalanceChanged;
|
||
public static event Action OnNotEnoughMoney;
|
||
|
||
public static bool Freeplay { get; private set; }
|
||
public static int ValutaPerImpulse { get; private set; }
|
||
public static string ValutaName { get; private set; }
|
||
public static int PricePve { get; private set; }
|
||
public static int PricePvp { get; private set; }
|
||
public static int PriceRevenge { get; private set; }
|
||
public static int PulsesCounter { get; private set; }
|
||
|
||
public static void Initialize()
|
||
{
|
||
Balance = LoadBalance();
|
||
ComWrapper.PaymentReceived += AddCredit;
|
||
|
||
PullSettings();
|
||
SettingsManager.OnSettingsChanged.AddListener(PullSettings);
|
||
}
|
||
|
||
public static bool TryPay(int amount)
|
||
{
|
||
if (Freeplay) return true;
|
||
|
||
if (Balance >= amount)
|
||
{
|
||
ChangeBalance(-amount);
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
OnNotEnoughMoney?.Invoke();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public static void AddCredit()
|
||
{
|
||
ChangeBalance(ValutaPerImpulse);
|
||
|
||
//вот это вот некрасивое надо куда то девать.
|
||
PlayerPrefs.SetInt(PrefsKeyVault.CASH_COUNTER, PlayerPrefs.GetInt(PrefsKeyVault.CASH_COUNTER) + ValutaPerImpulse);
|
||
PlayerPrefs.SetInt(PrefsKeyVault.CASH_COUNTER_NUL, PlayerPrefs.GetInt(PrefsKeyVault.CASH_COUNTER_NUL) + ValutaPerImpulse);
|
||
|
||
if (SettingsManager.GetString("BaseBotDifficulty", "Фиксированная") == "Повышенная")
|
||
{
|
||
IncrementPulsesCounter();
|
||
}
|
||
}
|
||
|
||
public static void AddCreditNoCounters(int amount) => ChangeBalance(amount);
|
||
|
||
public static void ReturnCredits(GameMode gameMode)
|
||
{
|
||
string gamemodePriceKey = gameMode == GameMode.pvp ? "PricePVP" : "PricePVE";
|
||
int gamemodePrice = PlayerPrefs.GetInt(gamemodePriceKey, 10);
|
||
|
||
ChangeBalance(gamemodePrice);
|
||
}
|
||
|
||
public static void ResetBalance()
|
||
{
|
||
int delta = -Balance;
|
||
ChangeBalance(delta);
|
||
}
|
||
|
||
private static void PullSettings()
|
||
{
|
||
Freeplay = SettingsManager.GetBool("Freeplay", false);
|
||
ValutaPerImpulse = PlayerPrefs.GetInt("ValutaPerImpulse", 10);
|
||
ValutaName = PlayerPrefs.GetString("ValutaName", "руб.");
|
||
|
||
PricePve = PlayerPrefs.GetInt("PricePVE", 10);
|
||
PricePvp = PlayerPrefs.GetInt("PricePVP", 10);
|
||
PriceRevenge = PlayerPrefs.GetInt("PriceRevenge", 10);
|
||
|
||
PulsesCounter = PlayerPrefs.GetInt("PulsesCounter", 0);
|
||
}
|
||
|
||
private static void ChangeBalance(int delta)
|
||
{
|
||
if (delta == 0) return;
|
||
|
||
Balance += delta;
|
||
Balance = Mathf.Max(Balance, 0);
|
||
SaveBalance();
|
||
|
||
if (delta > 0) onCoinAdded?.Invoke(delta);
|
||
else onCoinRemoved?.Invoke(delta);
|
||
OnBalanceChanged?.Invoke(Balance);
|
||
}
|
||
|
||
private static void SaveBalance() => PlayerPrefs.SetInt("CoinsCount", Balance);
|
||
private static int LoadBalance() => PlayerPrefs.GetInt("CoinsCount", 0);
|
||
|
||
public static void IncrementPulsesCounter()
|
||
{
|
||
PulsesCounter += 1;
|
||
PlayerPrefs.SetInt("PulsesCounter", PulsesCounter);
|
||
}
|
||
|
||
public static void ResetPulsesCounter()
|
||
{
|
||
PulsesCounter = 0;
|
||
PlayerPrefs.SetInt("PulsesCounter", PulsesCounter);
|
||
}
|
||
} |