Fixing buying progress and result in inventory

This commit is contained in:
HungDK
2026-04-06 10:12:21 +07:00
parent fc16108ff0
commit 2060f92f2c
5 changed files with 126 additions and 42 deletions
@@ -159,6 +159,7 @@ namespace BrewMonster.Scripts.Managers
return true;
}
/// <summary>C++ <c>CECInventory::MergeItem</c>: walk slots, call <c>CECIvtrItem::MergeItem</c>, then new slot if needed.</summary>
public bool MergeItem(int tid, int iExpireDate, int iAmount, out int piLastSlot, out int piLastAmount)
{
piLastSlot = -1;
@@ -171,16 +172,8 @@ namespace BrewMonster.Scripts.Managers
var slotItem = m_aItems[i];
if (slotItem != null)
{
if (slotItem.GetTemplateID() != tid)
continue;
int pileLimit = Math.Max(1, EC_IvtrItem.GetPileLimit(tid));
int canAdd = Math.Max(0, pileLimit - Math.Max(0, slotItem.GetCount()));
if (canAdd <= 0) continue;
int add = Math.Min(canAdd, iAmount);
slotItem.AddAmount(add);
iAmount -= add;
int iNumMerge = slotItem.MergeItem(tid, iAmount);
iAmount -= iNumMerge;
if (iAmount == 0)
{
@@ -873,6 +873,23 @@ namespace BrewMonster.Scripts.Managers
}
}
/// <summary>
/// Stack count label under slot buttons: prefabs use <c>text_quatity</c> (typo) or <c>text_quantity</c>; legacy code used <c>text_quality</c>.
/// Only checks immediate children (Unity <see cref="Transform.Find"/>); deeper layouts fall back to <see cref="Component.GetComponentInChildren{T}"/> below.
/// </summary>
private static Transform FindStackCountTextTransform(Transform root)
{
if (root == null) return null;
string[] names = { "text_quality", "text_quatity", "text_quantity" };
for (int n = 0; n < names.Length; n++)
{
var t = root.Find(names[n]);
if (t != null) return t;
}
return null;
}
/// <summary>
/// Update or create text component to show item count on the button
/// </summary>
@@ -885,22 +902,19 @@ namespace BrewMonster.Scripts.Managers
TMPro.TextMeshProUGUI tmpText = null;
Text legacyText = null;
// Find text component
var textTransform = button.transform.Find("text_quality");
var textTransform = FindStackCountTextTransform(button.transform);
if (textTransform != null)
{
tmpText = textTransform.GetComponent<TMPro.TextMeshProUGUI>();
legacyText = textTransform.GetComponent<Text>();
}
else
if (tmpText == null && legacyText == null)
{
// Fallback: find any text in children
tmpText = button.GetComponentInChildren<TMPro.TextMeshProUGUI>();
if (tmpText == null)
{
legacyText = button.GetComponentInChildren<Text>();
}
}
// Update text
@@ -1211,14 +1211,12 @@ namespace BrewMonster.Scripts.Managers
return iNumAdd;
}
/// <summary>Add item amount. Returns new amount of item.</summary>
/// <summary>Add item amount. Returns new count (C++ <c>a_Clamp</c> to <c>m_iPileLimit</c>).</summary>
public int AddAmount(int iAmount)
{
Debug.Log($"[EC_IvtrItem] Old Amount: {m_iCount}");
m_iCount += iAmount;
if (m_iCount < 0) m_iCount = 0;
//if (m_iCount > m_iPileLimit) m_iCount = m_iPileLimit;
Debug.Log($"[EC_IvtrItem] New Amount: {m_iCount}");
if (m_iCount > m_iPileLimit) m_iCount = m_iPileLimit;
return m_iCount;
}
@@ -482,6 +482,47 @@ namespace BrewMonster
}
}
}
/// <summary>C++ CECDealInventory::GetItemIndexByFlag</summary>
public int GetItemIndexByFlag(int iFlag)
{
int n = GetSize();
for (int i = 0; i < n; i++)
{
if (GetItem(i, false) != null && m_aItemInfo[i].iFlag == iFlag)
return i;
}
return -1;
}
/// <summary>C++ CECDealInventory::RemoveItemByFlag</summary>
public void RemoveItemByFlag(int iFlag, int iAmount)
{
int n = GetSize();
for (int i = 0; i < n; i++)
{
var slotItem = GetItem(i, false);
if (slotItem == null)
continue;
var info = m_aItemInfo[i];
if (info.iFlag != iFlag)
continue;
if (iAmount < 0 || iAmount >= info.iAmount)
{
SetItem(i, null);
}
else
{
info.iAmount -= iAmount;
m_aItemInfo[i] = info;
if (info.bDelete)
slotItem.AddAmount(-iAmount);
}
return;
}
}
}
// ̯ƱԶת
+60 -22
View File
@@ -594,7 +594,13 @@ namespace BrewMonster
UpdateEquipSkins();
}
/// <summary>Buy from NPC/booth: server sends PURCHASE_ITEM (cmd_purchase_item). C++ OnMsgHstPurchaseItems.</summary>
/// <summary>
/// Buy from NPC/booth: S2C PURCHASE_ITEM (<c>cmd_purchase_item</c>).
/// Port of <c>CECHostPlayer::OnMsgHstPurchaseItems</c> (EC_HostMsg.cpp): prefer <c>MergeItem</c> like the C++ client.
/// If merge lands on a different slot than <c>inv_index</c> (common when stacking into an earlier pile), we still accept
/// the merged slot—the strict C++ <c>iLastSlot == inv_index</c> check would skip updates and break buy/stack UI here.
/// Fallback: <c>PutItemInSlot(inv_index)</c> then <c>MergeItem</c> when merge alone fails.
/// </summary>
public void OnMsgHstPurchaseItems(ECMSG Msg)
{
var data = Msg.dwParam1 as byte[];
@@ -609,6 +615,7 @@ namespace BrewMonster
return;
var slotsNeedingDetail = new System.Collections.Generic.List<byte>();
bool purchaseSlotMismatch = false;
for (int i = 0; i < header.item_count && index + itemSize <= data.Length; i++)
{
@@ -616,37 +623,58 @@ namespace BrewMonster
int expire_date = BitConverter.ToInt32(data, index); index += 4;
int count = (int)BitConverter.ToUInt32(data, index); index += 4;
ushort inv_index = BitConverter.ToUInt16(data, index); index += 2;
index += 1; // booth_slot
byte booth_slot = data[index];
index += 1;
if (inv_index >= pPack.GetSize())
pPack.Resize(inv_index + 1);
bool placed = pPack.PutItemInSlot(inv_index, item_id, expire_date, count, out int lastSlot, out int slotNum);
int iLastSlot = 0;
int iSlotNum = 0;
bool placed = pPack.MergeItem(item_id, expire_date, count, out iLastSlot, out iSlotNum);
if (!placed)
placed = pPack.PutItemInSlot(inv_index, item_id, expire_date, count, out iLastSlot, out iSlotNum);
if (!placed)
placed = pPack.MergeItem(item_id, expire_date, count, out iLastSlot, out iSlotNum);
if (!placed)
{
placed = pPack.MergeItem(item_id, expire_date, count, out lastSlot, out slotNum);
if (!placed || lastSlot != inv_index)
continue;
#if UNITY_EDITOR
UnityEngine.Debug.LogWarning(
$"[OnMsgHstPurchaseItems] Could not place purchase tid={item_id} count={count} inv_index={inv_index}");
#endif
continue;
}
var pItem = pPack.GetItem(inv_index, false);
if (iLastSlot != inv_index)
purchaseSlotMismatch = true;
#if UNITY_EDITOR
if (iLastSlot != inv_index)
{
UnityEngine.Debug.Log(
$"[OnMsgHstPurchaseItems] Using merge slot {iLastSlot} (packet inv_index={inv_index}) tid={item_id}");
}
#endif
var pItem = pPack.GetItem(iLastSlot, false);
if (pItem != null)
{
pItem.Package = (byte)Inventory_type.IVTRTYPE_PACK;
pItem.Slot = inv_index;
int cid = pItem.GetClassID();
if (pItem.IsEquipment() ||
cid == (int)EC_IvtrItem.InventoryClassId.ICID_TASKNMMATTER ||
cid == (int)EC_IvtrItem.InventoryClassId.ICID_TASKDICE ||
cid == (int)EC_IvtrItem.InventoryClassId.ICID_TASKITEM ||
cid == (int)EC_IvtrItem.InventoryClassId.ICID_GOBLIN_EXPPILL ||
cid == (int)EC_IvtrItem.InventoryClassId.ICID_WEDDINGBOOKCARD ||
cid == (int)EC_IvtrItem.InventoryClassId.ICID_WEDDINGINVITECARD ||
cid == (int)EC_IvtrItem.InventoryClassId.ICID_SKILLTOME ||
cid == (int)EC_IvtrItem.InventoryClassId.ICID_GOBLIN ||
cid == (int)EC_IvtrItem.InventoryClassId.ICID_PETEGG)
pItem.Slot = iLastSlot;
// Keep stack size in sync with MergeItem/PutItemInSlot totals (authoritative for this slot).
if (iSlotNum > 0)
pItem.SetCount(iSlotNum);
if (pItem.IsEquipment())
slotsNeedingDetail.Add((byte)iLastSlot);
}
if (header.flag != 0 && GetBoothState() == 2)
{
var boothBPack = GetBoothBuyPack();
if (boothBPack != null)
{
slotsNeedingDetail.Add((byte)inv_index);
// C++: CDlgInfo FIXMSG_BOOTHBUY — no matching in-game UI hook here; update booth pack only.
boothBPack.RemoveItemByFlag(booth_slot, count);
}
}
}
@@ -656,8 +684,18 @@ namespace BrewMonster
foreach (byte slot in slotsNeedingDetail)
UnityGameSession.c2s_CmdGetItemInfo((byte)Inventory_type.IVTRTYPE_PACK, slot);
var ui = GameObject.FindFirstObjectByType<EC_InventoryUI>();
ui?.RefreshAll();
{
var ui = GameObject.FindFirstObjectByType<EC_InventoryUI>();
ui?.RefreshAll();
if (purchaseSlotMismatch)
{
UnityGameSession.RequestInventoryAsync(0, () =>
{
var ui2 = GameObject.FindFirstObjectByType<EC_InventoryUI>();
ui2?.RefreshAll();
});
}
}
UpdateEquipSkins();
}