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

229 lines
7.8 KiB
C#

// Filename : CECQShop.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
{
// CECQShop
public class CECQShop : CECShopArrayItems
{
private CECShopItemCategory m_fashionShopCategory = new CECShopItemCategory();
private static CECQShop s_instance;
private GShopLoader m_shopLoader;
private CECQShop() : 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_QSHOP_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.primaryShop != null && m_shopLoader.primaryShop.items != null)
{
m_pItems = m_shopLoader.primaryShop.items;
}
}
}
public static CECQShop Instance()
{
if (s_instance == null)
{
s_instance = new CECQShop();
}
return s_instance;
}
public override bool GetFromServer(int beginIndex, int endIndex)
{
bool result = false;
// TODO: Implement cooldown check (GP_CT_GET_MALL_PRICE)
// For now, always allow if indices are provided
if (beginIndex == 0 && endIndex == 0)
{
// Request all prices
// TODO: Implement UnityGameSession.RequestGetMallItemPrice(0, 0)
result = true;
}
else if (beginIndex != 0 || endIndex != 0)
{
// Request specific range
// TODO: Implement UnityGameSession.RequestGetMallItemPrice(beginIndex, endIndex)
result = true;
}
return result;
}
public override uint GetLocalTimeStamp()
{
if (m_shopLoader != null && m_shopLoader.primaryShop != null)
{
return m_shopLoader.primaryShop.timestamp;
}
return 0;
}
public override uint GetServerTimeStamp()
{
// TODO: Get from game run state
// return g_pGame->GetGameRun()->GetGShopTimeStamp();
return GetLocalTimeStamp(); // Stub for now
}
public bool UpdateFromServer() // TODO: Add S2C::cmd_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_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_mall_item_price::good_item& 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.goods_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.GetCash()
// For now, return 0 as stub
// return CECHostPlayer.Instance().GetCash();
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 mall shopping
UnityGameSession.Instance.RequestMallShopping(1, (int)item.id, itemIndex, buyIndex);
}
else
{
// NPC server mall shopping
// TODO: Implement UnityGameSession.RequestNPCSevMallShopping(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().bEnableQShopFashionShop;
}
public override bool IsFashionShopFlashSaleEnabled()
{
return CECUIConfig.Instance().GetGameUI().bEnableQShopFashionShopFlashSale;
}
public override string GetFashionShopFlashSaleTitle()
{
return CECUIConfig.Instance().GetGameUI().strQShopFashionShopFlashSaleTitle;
}
public override CECShopItemCategory GetFashionShopCategory()
{
return m_fashionShopCategory;
}
}
}