Add missing purchase item

This commit is contained in:
HungDK
2026-02-28 18:14:10 +07:00
parent 8199d01929
commit 2b644ba5eb
7 changed files with 2154 additions and 2058 deletions
@@ -129,6 +129,42 @@ namespace BrewMonster.Scripts.Managers
m_aItems[iSlot2] = tmp;
}
/// <summary>
/// Place or stack item in a specific slot (server-specified slot). Matches C++ expectation that client uses same slot as server.
/// </summary>
public bool PutItemInSlot(int iSlot, int tid, int iExpireDate, int iAmount, out int piLastSlot, out int piLastAmount)
{
piLastSlot = -1;
piLastAmount = 0;
if (iSlot < 0 || iSlot >= m_aItems.Length || iAmount <= 0)
return false;
var slotItem = m_aItems[iSlot];
if (slotItem == null)
{
var newItem = EC_IvtrItem.CreateItem(tid, iExpireDate, iAmount);
if (newItem == null)
return false;
newItem.Slot = iSlot;
newItem.SetCount(iAmount);
m_aItems[iSlot] = newItem;
piLastSlot = iSlot;
piLastAmount = iAmount;
return true;
}
if (slotItem.GetTemplateID() != tid)
return false;
int pileLimit = Math.Max(1, EC_IvtrItem.GetPileLimit(tid));
int canAdd = Math.Max(0, pileLimit - Math.Max(0, slotItem.GetCount()));
if (canAdd <= 0)
return false;
int add = Math.Min(canAdd, iAmount);
slotItem.AddAmount(add);
piLastSlot = iSlot;
piLastAmount = slotItem.GetCount();
return true;
}
public bool MergeItem(int tid, int iExpireDate, int iAmount, out int piLastSlot, out int piLastAmount)
{
piLastSlot = -1;
@@ -170,6 +206,9 @@ namespace BrewMonster.Scripts.Managers
return false;
}
var newItem = EC_IvtrItem.CreateItem(tid, iExpireDate, iAmount);
if (newItem == null)
return false;
newItem.Slot = firstEmpty;
newItem.SetCount(iAmount);
m_aItems[firstEmpty] = newItem;
@@ -1315,6 +1315,26 @@ namespace CSNetwork.GPDataType
public byte bySlot;
}
/// <summary>One item in cmd_purchase_item (buy from NPC/booth). Wire: item_id, expire_date, count, inv_index, booth_slot = 15 bytes.</summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct cmd_purchase_item_ITEM
{
public int item_id;
public int expire_date;
public uint count;
public ushort inv_index;
public byte booth_slot;
}
/// <summary>Fixed header of cmd_purchase_item. Rest of packet is item_count x cmd_purchase_item_ITEM.</summary>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct cmd_purchase_item_header
{
public uint cost;
public uint yinpiao;
public byte flag;
public ushort item_count;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct cmd_get_own_money
@@ -1394,7 +1414,7 @@ namespace CSNetwork.GPDataType
public byte index;
};
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct cmd_pickup_item
public struct cmd_pickup_item
{
public int tid;
public int expire_date;
File diff suppressed because it is too large Load Diff
@@ -24,6 +24,7 @@ public class NPCShopDetailPanel : MonoBehaviour
private GShopItem currentItem;
private NPCShopUIManager shopManager;
private int shopItemIndex;
void Start()
{
@@ -39,10 +40,11 @@ public class NPCShopDetailPanel : MonoBehaviour
buyButton.onClick.AddListener(OnBuyButtonClicked);
}
public void SetupDetailPanel(GShopItem item, NPCShopUIManager manager)
public void SetupDetailPanel(GShopItem item, NPCShopUIManager manager, int index)
{
currentItem = item;
shopManager = manager;
shopItemIndex = index;
UpdateDisplay();
}
@@ -189,17 +191,19 @@ public class NPCShopDetailPanel : MonoBehaviour
}
}
// Create npc_trade_item array for buying from NPC
// The tid is the item template ID, index is shop item index (0 for now), count is quantity to buy
// Server requires SEVNPC_HELLO with NPC id before buy, and the correct shop slot index
if (shopManager != null && shopManager.CurrentNPCID != 0)
UnityGameSession.c2s_CmdNPCSevHello((int)shopManager.CurrentNPCID);
// Create npc_trade_item: tid = template ID, index = shop slot (server validates this), count = quantity
npc_trade_item[] items = new npc_trade_item[1];
items[0] = new npc_trade_item
{
tid = (int)currentItem.id,
index = 0, // Shop item index - may need to be determined from shop item position
count = 1 // Quantity to buy
index = (uint)shopItemIndex,
count = 1
};
// Send the buy command
UnityGameSession.c2s_CmdNPCSevBuy(1, items);
Debug.Log($"[NPCShopDetailPanel] Sent buy command for item {currentItem.id}, price {price}");
@@ -19,6 +19,7 @@ public class NPCShopItemPanel : MonoBehaviour
private GShopItem itemData;
private Coroutine iconLoadCoroutine;
private NPCShopUIManager shopManager;
private int shopItemIndex;
void Start()
{
@@ -54,10 +55,11 @@ public class NPCShopItemPanel : MonoBehaviour
}
}
public void SetupItem(GShopItem item, NPCShopUIManager manager)
public void SetupItem(GShopItem item, NPCShopUIManager manager, int index)
{
itemData = item;
shopManager = manager;
shopItemIndex = index;
UpdateDisplay();
}
@@ -65,7 +67,7 @@ public class NPCShopItemPanel : MonoBehaviour
{
if (shopManager != null && itemData.id != 0)
{
shopManager.ShowItemDetail(itemData);
shopManager.ShowItemDetail(itemData, shopItemIndex);
}
}
@@ -35,6 +35,9 @@ public class NPCShopUIManager : MonoBehaviour
private int currentTabIndex = 0;
private uint currentNPCID = 0;
private NPC_SELL_SERVICE? cachedSellService = null;
/// <summary>Current NPC id for this shop session. Send SEVNPC_HELLO with this before buy.</summary>
public uint CurrentNPCID => currentNPCID;
private NPCShopDetailPanel detailPanelScript;
void Start()
@@ -278,8 +281,9 @@ public class NPCShopUIManager : MonoBehaviour
if (elementDataMan == null)
return;
foreach (var good in page.goods)
for (int i = 0; i < page.goods.Length; i++)
{
var good = page.goods[i];
if (good.id == 0)
continue;
@@ -293,8 +297,8 @@ public class NPCShopUIManager : MonoBehaviour
// Create GShopItem
GShopItem shopItem = CreateShopItemFromGood(good, itemData, itemDataType);
// Create panel
CreateItemPanel(shopItem);
// Create panel with shop slot index (server expects this in npc_trade_item.index)
CreateItemPanel(shopItem, i);
}
}
}
@@ -372,7 +376,7 @@ public class NPCShopUIManager : MonoBehaviour
return shopItem;
}
void CreateItemPanel(GShopItem item)
void CreateItemPanel(GShopItem item, int shopItemIndex)
{
if (itemPanelPrefab == null || itemContainer == null)
return;
@@ -386,7 +390,7 @@ public class NPCShopUIManager : MonoBehaviour
if (itemPanelScript != null)
{
itemPanelScript.SetupItem(item, this);
itemPanelScript.SetupItem(item, this, shopItemIndex);
}
else
{
@@ -406,7 +410,7 @@ public class NPCShopUIManager : MonoBehaviour
currentItemPanels.Clear();
}
public void ShowItemDetail(GShopItem item)
public void ShowItemDetail(GShopItem item, int shopItemIndex)
{
if (item.id == 0)
return;
@@ -428,7 +432,7 @@ public class NPCShopUIManager : MonoBehaviour
if (detailPanelScript != null)
{
npcShopDetailPanel.SetActive(true);
detailPanelScript.SetupDetailPanel(item, this);
detailPanelScript.SetupDetailPanel(item, this, shopItemIndex);
}
else
{
+3
View File
@@ -565,6 +565,9 @@ namespace BrewMonster
case EC_MsgDef.MSG_HST_PICKUPITEM:
OnMsgHstPickupItem(Msg);
break;
case EC_MsgDef.MSG_HST_PURCHASEITEMS:
OnMsgHstPurchaseItems(Msg);
break;
case EC_MsgDef.MSG_HST_PRODUCEITEM:
OnMsgHstProduceItem(Msg);
break;