140 lines
4.2 KiB
C#
140 lines
4.2 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 event Action OnPaymentDataChanged;
|
||
|
||
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;
|
||
|
||
ApplySettings();
|
||
SettingsManager.OnSettingsChanged += ApplySettings;
|
||
}
|
||
|
||
public static bool TryPay(int amount)
|
||
{
|
||
if (Freeplay) return true;
|
||
|
||
if (Balance >= amount)
|
||
{
|
||
ChangeBalance(-amount);
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
OnNotEnoughMoney?.Invoke();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public static bool TryPayForMode(GameMode mode)
|
||
{
|
||
int price = mode == GameMode.pve ? PricePve : PricePvp;
|
||
return TryPay(price);
|
||
}
|
||
|
||
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(SettingKey.DIFFICULTY_MODE) == "Заработок")
|
||
{
|
||
IncrementPulsesCounter();
|
||
}
|
||
}
|
||
|
||
public static void AddCreditNoCounters(int amount) => ChangeBalance(amount);
|
||
|
||
public static void ReturnCredits(GameMode gameMode)
|
||
{
|
||
int gamemodePrice = gameMode == GameMode.pvp ? PricePvp : PricePve;
|
||
ChangeBalance(gamemodePrice);
|
||
}
|
||
|
||
public static void ResetBalance()
|
||
{
|
||
int delta = -Balance;
|
||
ChangeBalance(delta);
|
||
}
|
||
|
||
private static void ApplySettings()
|
||
{
|
||
Freeplay = SettingsManager.GetBool(SettingKey.FREEPLAY);
|
||
ValutaPerImpulse = PlayerPrefs.GetInt("ValutaPerImpulse", 10);
|
||
ValutaName = PlayerPrefs.GetString("ValutaName", "руб.");
|
||
ValutaName = SettingsManager.GetString(SettingKey.VALUTA_NAME);
|
||
|
||
PricePve = SettingsManager.GetInt(SettingKey.PRICE_PVE);
|
||
PricePvp = SettingsManager.GetInt(SettingKey.PRICE_PVP);
|
||
PriceRevenge = SettingsManager.GetInt(SettingKey.PRICE_REVENGE);
|
||
|
||
PulsesCounter = PlayerPrefs.GetInt("PulsesCounter", 0);
|
||
|
||
OnPaymentDataChanged?.Invoke();
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
public static string GetPriceString(GameMode mode)
|
||
{
|
||
if (Freeplay)
|
||
{
|
||
return "бесплатно";
|
||
}
|
||
else
|
||
{
|
||
int price = mode == GameMode.pve ? PricePve : PricePvp;
|
||
return GetPluralizedMoney(price);
|
||
}
|
||
}
|
||
|
||
public static string GetPluralizedMoney(int amount) => $"{amount} {PluralizeUtil.GetValutaPluralized(ValutaName, amount)}";
|
||
} |