using PrizeSystem; using System; using UnityEngine; public static class PaymentSystem { public static int Balance { get; private set; } public static event Action onCoinAdded; public static event Action onCoinRemoved; public static event Action OnBalanceChanged; public static event Action OnRevenueReceived; 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 PriceRevengePve { get; private set; } public static int PriceRevengePvP { 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) { return TryPay(GetModePrice(mode)); } 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); OnRevenueReceived?.Invoke(ValutaPerImpulse); if (SettingsManager.GetString(SettingKey.DIFFICULTY_MODE) == "Заработок") { IncrementPulsesCounter(); } } public static void AddCreditNoCounters(int amount) => ChangeBalance(amount); public static void ReturnCredits(GameMode gameMode) => ReturnCredits(Freeplay ? 0 : GetModePrice(gameMode)); public static void ReturnCredits(int amount) { if (amount <= 0) return; ChangeBalance(amount); } public static void ResetBalance() { int delta = -Balance; ChangeBalance(delta); } public static int GetRevengePrice(GameMode gameMode) => gameMode == GameMode.pve ? PriceRevengePve : PriceRevengePvP; public static int GetModePrice(GameMode gameMode) => gameMode == GameMode.pve ? PricePve : PricePvp; 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); PriceRevengePve = SettingsManager.GetInt(SettingKey.PRICE_REVENGE_PVE); PriceRevengePvP = SettingsManager.GetInt(SettingKey.PRICE_REVENGE_PVP); 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 { return GetPluralizedMoney(GetModePrice(mode)); } } public static string GetPluralizedMoney(int amount) => $"{amount} {PluralizeUtil.GetValutaPluralized(ValutaName, amount)}"; }