2025-10-18 03:26:16 +05:00

155 lines
4.4 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System;
using System.Collections;
using System.Collections.Generic;
using TPSBR;
public class GameShop : MonoBehaviour
{
[Serializable]
public class ShopItem
{
public Button buyButton;
public TMP_Text buyLabel;
public TMP_Text nameLabel;
public Image iconImage;
public string id;
public int price;
}
public AgentSettings agentSettings;
public string shopKey = "Characters";
public List<ShopItem> items = new();
public event Action<string> OnPurchaseSuccess;
public event Action<string> OnPurchaseFail;
string WalletAddress => PlayerPrefs.GetString("WALLET_ADDRESS", "");
void Start()
{
ApplyCatalogIfPresent();
EnsureFirstUnlocked();
RefreshUI();
}
void ApplyCatalogIfPresent()
{
if (agentSettings == null || agentSettings.Agents == null || agentSettings.Agents.Length == 0)
{
for (int i = 0; i < items.Count; i++)
if (string.IsNullOrWhiteSpace(items[i].id)) items[i].id = $"Item{i + 1}";
return;
}
int count = Mathf.Min(items.Count, agentSettings.Agents.Length);
for (int i = 0; i < count; i++)
{
var a = agentSettings.Agents[i];
var slot = items[i];
slot.id = a.ID;
slot.price = a.Price;
if (slot.nameLabel) slot.nameLabel.text = string.IsNullOrEmpty(a.DisplayName) ? a.ID : a.DisplayName;
if (slot.iconImage) slot.iconImage.sprite = a.Icon;
}
for (int i = count; i < items.Count; i++)
if (string.IsNullOrWhiteSpace(items[i].id)) items[i].id = $"Item{i + 1}";
}
void EnsureFirstUnlocked()
{
if (items.Count == 0) return;
var id = string.IsNullOrWhiteSpace(items[0].id) ? "Item1" : items[0].id;
ShopUnlocks.EnsureUnlocked(id, shopKey);
}
public void TryBuy(string id)
{
int idx = items.FindIndex(s => s.id == id);
if (idx < 0) return;
if (ShopUnlocks.IsUnlocked(id, shopKey)) return;
//if (string.IsNullOrEmpty(WalletAddress))
//{
// OnPurchaseFail?.Invoke(id);
// return;
//}
StartCoroutine(SimPurchase(idx));
}
long currentPrice;
public async void GetCurrency()
{
try
{
var rec = await GameDb.GetCurrencyAsync(PlayerPrefs.GetString("WALLET_ADDRESS"));
currentPrice = rec;
Debug.Log(rec.ToString());
}
catch (System.Exception e)
{
Debug.LogError($"[Runtime] ❌ Ensure failed: {e.Message}");
}
}
IEnumerator SimPurchase(int idx)
{
yield return new WaitForSeconds(0.6f);
var id = items[idx].id;
bool ok = true;
Debug.Log("CurrentPrice: " + currentPrice);
if (items[idx].price > currentPrice)
{
ok = false;
}
if (ok)
{
ShopUnlocks.Unlock(id, shopKey);
RefreshUI();
OnPurchaseSuccess?.Invoke(id);
}
else
{
OnPurchaseFail?.Invoke(id);
}
}
void RefreshUI()
{
for (int i = 0; i < items.Count; i++)
{
var slot = items[i];
bool owned = !string.IsNullOrWhiteSpace(slot.id) && ShopUnlocks.IsUnlocked(slot.id, shopKey);
if (slot.buyButton)
{
if (i == 0)
{
slot.buyButton.gameObject.SetActive(false);
}
else
{
slot.buyButton.onClick.RemoveAllListeners();
slot.buyButton.gameObject.SetActive(!owned);
if (!owned)
{
var label = slot.buyLabel ? slot.buyLabel : slot.buyButton.GetComponentInChildren<TMP_Text>(true);
if (label) label.text = $"BUY {slot.price}";
string captured = slot.id;
slot.buyButton.onClick.AddListener(() => TryBuy(captured));
}
}
}
//if (slot.lockIcon) slot.lockIcon.SetActive(!owned);
}
}
[ContextMenu("Reset Unlocks")]
void ResetUnlocks()
{
ShopUnlocks.Reset(shopKey);
EnsureFirstUnlocked();
RefreshUI();
}
}