Files
2025-12-03 16:43:11 +07:00

231 lines
8.1 KiB
C#

// Filename : CECBackShop.cs
// Creator : Xu Wenbin
// Date : 2013/12/11
// Converted to C#: 2024
using System;
using System.Collections.Generic;
using UnityEngine;
using BrewMonster.Network;
namespace PerfectWorld.Scripts.Shop
{
// CECBackShop
public class CECBackShop : CECShopArrayItems
{
private CECShopItemCategory m_fashionShopCategory = new CECShopItemCategory();
private static CECBackShop s_instance;
private GShopLoader m_shopLoader;
private CECBackShop() : base(null)
{
// Initialize with empty list first
m_pItems = new List<GShopItem>();
// Try to get shop loader data (may not be available immediately)
RefreshShopData();
if (IsFashionShopEnabled())
{
m_fashionShopCategory.InitFromQShopConfig(CECQShopConfig.CID_BACKSHOP_FASHION);
}
}
public void RefreshShopData()
{
// Initialize with shop loader data
GameObject shopLoaderObj = GameObject.FindFirstObjectByType<GShopLoader>()?.gameObject;
if (shopLoaderObj != null)
{
m_shopLoader = shopLoaderObj.GetComponent<GShopLoader>();
if (m_shopLoader != null && m_shopLoader.secondaryShop != null && m_shopLoader.secondaryShop.items != null)
{
m_pItems = m_shopLoader.secondaryShop.items;
}
}
}
public static CECBackShop Instance()
{
if (s_instance == null)
{
s_instance = new CECBackShop();
}
return s_instance;
}
public override bool GetFromServer(int beginIndex, int endIndex)
{
bool result = false;
// TODO: Implement cooldown check (GP_CT_GET_DIVIDEND_MALL_PRICE)
// For now, always allow if indices are provided
if (beginIndex == 0 && endIndex == 0)
{
// Request all prices
// TODO: Implement UnityGameSession.RequestGetDividendMallItemPrice(0, 0)
result = true;
}
else if (beginIndex != 0 || endIndex != 0)
{
// Request specific range
// TODO: Implement UnityGameSession.RequestGetDividendMallItemPrice(beginIndex, endIndex)
result = true;
}
return result;
}
public override uint GetLocalTimeStamp()
{
if (m_shopLoader != null && m_shopLoader.secondaryShop != null)
{
return m_shopLoader.secondaryShop.timestamp;
}
return 0;
}
public override uint GetServerTimeStamp()
{
// TODO: Get from game run state
// return g_pGame->GetGameRun()->GetGShopTimeStamp2();
return GetLocalTimeStamp(); // Stub for now
}
public bool UpdateFromServer() // TODO: Add S2C::cmd_dividend_mall_item_price parameter when available
{
// TODO: Implement server update logic when S2C command structure is available
// For now, this is a stub
/*
List<GShopItem> items = new List<GShopItem>(m_pItems);
if (!ApplyChangesFromServer(items, pCmd))
{
return false;
}
if (IsSame(items, m_pItems))
{
return true;
}
m_pItems = items;
OnItemChange();
*/
return true;
}
private bool ApplyChangesFromServer(List<GShopItem> pItems) // TODO: Add S2C::cmd_dividend_mall_item_price parameter when available
{
// TODO: Implement when S2C command structure is available
/*
int i = 0;
for (i = pCmd->start_index; i < pCmd->end_index; i++)
{
if (i < pItems.Count)
{
GShopItem data = pItems[i];
for (int j = 0; j < 4; j++)
{
if (data.buy != null && j < data.buy.Length && data.buy[j].type != -1)
{
GShopBuyOption buyOption = data.buy[j];
buyOption.type = -1;
buyOption.price = 0;
data.buy[j] = buyOption;
}
}
pItems[i] = data;
}
}
for (i = 0; i < pCmd->count; i++)
{
const S2C::cmd_dividend_mall_item_price::good_info& tempList = pCmd->list[i];
if (tempList.good_index < pItems.Count)
{
GShopItem data = pItems[tempList.good_index];
if (data.id == (uint)tempList.good_id)
{
if (data.buy != null && tempList.good_slot < data.buy.Length)
{
GShopBuyOption buyOption = data.buy[tempList.good_slot];
buyOption.price = tempList.good_price;
buyOption.status = tempList.good_status;
buyOption.time = tempList.expire_time;
buyOption.type = 3;
data.buy[tempList.good_slot] = buyOption;
}
pItems[tempList.good_index] = data;
}
else
{
Debug.Assert(data.id == (uint)tempList.good_id);
return false;
}
}
}
*/
return true;
}
public override int GetCash()
{
// TODO: Integrate with CECHostPlayer.GetDividend()
// For now, return 0 as stub
// return CECHostPlayer.Instance().GetDividend();
return 0;
}
public override bool Buy(int itemIndex, int buyIndex)
{
bool bOK = false;
if (ReadyToBuy(itemIndex, buyIndex))
{
GShopItem? pItem = GetItem(itemIndex);
if (pItem.HasValue)
{
GShopItem item = pItem.Value;
if (!CECShopItemOwnerNPC.HasOwnerNPC(item))
{
// Regular dividend mall shopping
// TODO: Implement UnityGameSession.RequestDividendMallShopping(1, itemIndex, item.id, buyIndex)
// For now, use regular mall shopping as fallback
UnityGameSession.Instance.RequestMallShopping(1, (int)item.id, itemIndex, buyIndex);
}
else
{
// NPC server dividend mall shopping
// TODO: Implement UnityGameSession.RequestNPCSevDividendMallShopping(1, itemIndex, item.id, buyIndex)
UnityGameSession.Instance.RequestMallShopping(1, (int)item.id, itemIndex, buyIndex);
}
CECQShopConfig.Instance().OnItemBuyed(item.id);
// TODO: CECShoppingItemsMover::Instance().OnItemBuyed(this, itemIndex, buyIndex);
bOK = true;
}
}
else
{
Debug.Assert(false, "Item not ready to buy");
}
return bOK;
}
public override bool IsFashionShopEnabled()
{
return CECUIConfig.Instance().GetGameUI().bEnableBackShopFashionShop;
}
public override bool IsFashionShopFlashSaleEnabled()
{
return CECUIConfig.Instance().GetGameUI().bEnableBackShopFashionShopFlashSale;
}
public override string GetFashionShopFlashSaleTitle()
{
return CECUIConfig.Instance().GetGameUI().strBackShopFashionShopFlashSaleTitle;
}
public override CECShopItemCategory GetFashionShopCategory()
{
return m_fashionShopCategory;
}
}
}