From 16094d04cf8e286b7b981cf7c954da5a42878c6e Mon Sep 17 00:00:00 2001 From: HungDK <> Date: Thu, 4 Dec 2025 15:09:20 +0700 Subject: [PATCH] Fixing wrong command to send to server --- .../CSNetwork/C2SCommand/C2SCommand.cs | 20 ++++++++++ .../CSNetwork/C2SCommand/C2SCommandFactory.cs | 40 ++++++++++++++++++- .../Scripts/Network/CSNetwork/GameSession.cs | 10 +++++ .../Scripts/Network/UnityGameSession.cs | 7 ++++ .../Scripts/UI/NPCShopDetailPanel.cs | 20 +++++----- Assets/Prefabs/UI/DialogNPCShop.prefab | 2 +- 6 files changed, 86 insertions(+), 13 deletions(-) diff --git a/Assets/PerfectWorld/Scripts/Network/CSNetwork/C2SCommand/C2SCommand.cs b/Assets/PerfectWorld/Scripts/Network/CSNetwork/C2SCommand/C2SCommand.cs index 8306312b58..6119bce46c 100644 --- a/Assets/PerfectWorld/Scripts/Network/CSNetwork/C2SCommand/C2SCommand.cs +++ b/Assets/PerfectWorld/Scripts/Network/CSNetwork/C2SCommand/C2SCommand.cs @@ -688,6 +688,14 @@ namespace CSNetwork.C2SCommand public bool agree; } + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct npc_trade_item + { + public int tid; + public uint index; + public uint count; + } + [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct npc_sell_item { @@ -697,6 +705,18 @@ namespace CSNetwork.C2SCommand public int price; } + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct NPCSevBuyCONTENT + { + public uint money; // Not use now + public int consume_contrib; + public int cumulate_contrib; + public int force_id; + public int force_repu; + public int force_contrib; + public uint item_count; + } + [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct NPCSevSellCONTENT { diff --git a/Assets/PerfectWorld/Scripts/Network/CSNetwork/C2SCommand/C2SCommandFactory.cs b/Assets/PerfectWorld/Scripts/Network/CSNetwork/C2SCommand/C2SCommandFactory.cs index f33da9ac88..45825038c8 100644 --- a/Assets/PerfectWorld/Scripts/Network/CSNetwork/C2SCommand/C2SCommandFactory.cs +++ b/Assets/PerfectWorld/Scripts/Network/CSNetwork/C2SCommand/C2SCommandFactory.cs @@ -583,6 +583,44 @@ namespace CSNetwork.C2SCommand return SerializeCommand(CommandID.SEVNPC_SERVE, cmd, content); } + public static Octets CreateNPCSevBuyCmd(int itemNum, CSNetwork.C2SCommand.npc_trade_item[] items) + { + if (itemNum <= 0 || items == null || items.Length < itemNum) + throw new ArgumentException("Invalid itemNum or items array"); + + uint contentSize = (uint)Marshal.SizeOf(); + uint itemSize = (uint)Marshal.SizeOf(); + uint totalLen = contentSize + (uint)itemNum * itemSize; + + var cmd = new cmd_sevnpc_serve + { + service_type = NPC_service_type.GP_NPCSEV_SELL, // NPC sells to player = player buys + len = totalLen + }; + + NPCSevBuyCONTENT content = new NPCSevBuyCONTENT() + { + money = 0, // Not use now + consume_contrib = 0, + cumulate_contrib = 0, + force_id = 0, + force_repu = 0, + force_contrib = 0, + item_count = (uint)itemNum + }; + + // Serialize command + content + var octets = SerializeCommand(CommandID.SEVNPC_SERVE, cmd, content); + + // Append items array + for (int i = 0; i < itemNum; i++) + { + WriteStruct(octets, items[i]); + } + + return octets; + } + public static Octets CreateNPCSevSellCmd(int itemNum, npc_sell_item[] items) { if (itemNum <= 0 || items == null || items.Length < itemNum) @@ -594,7 +632,7 @@ namespace CSNetwork.C2SCommand var cmd = new cmd_sevnpc_serve { - service_type = NPC_service_type.GP_NPCSEV_BUY, + service_type = NPC_service_type.GP_NPCSEV_BUY, // NPC buys from player = player sells len = totalLen }; diff --git a/Assets/PerfectWorld/Scripts/Network/CSNetwork/GameSession.cs b/Assets/PerfectWorld/Scripts/Network/CSNetwork/GameSession.cs index 934a6dfa26..f346e72706 100644 --- a/Assets/PerfectWorld/Scripts/Network/CSNetwork/GameSession.cs +++ b/Assets/PerfectWorld/Scripts/Network/CSNetwork/GameSession.cs @@ -1224,6 +1224,16 @@ namespace CSNetwork SendProtocol(gamedatasend); } + public void c2s_SendCmdNPCSevBuy(int itemNum, C2SCommand.npc_trade_item[] items) + { + if (itemNum <= 0 || items == null || items.Length < itemNum) + return; + + gamedatasend gamedatasend = new gamedatasend(); + gamedatasend.Data = C2SCommandFactory.CreateNPCSevBuyCmd(itemNum, items); + SendProtocol(gamedatasend); + } + public void c2s_SendCmdNPCSevSell(int itemNum, C2SCommand.npc_sell_item[] items) { if (itemNum <= 0 || items == null || items.Length < itemNum) diff --git a/Assets/PerfectWorld/Scripts/Network/UnityGameSession.cs b/Assets/PerfectWorld/Scripts/Network/UnityGameSession.cs index 307889626a..5da25ae735 100644 --- a/Assets/PerfectWorld/Scripts/Network/UnityGameSession.cs +++ b/Assets/PerfectWorld/Scripts/Network/UnityGameSession.cs @@ -305,6 +305,13 @@ namespace BrewMonster.Network Instance._gameSession.c2s_SendCmdNPCSevTaskMatter(idTask); } + public static void c2s_CmdNPCSevBuy(int itemNum, npc_trade_item[] items) + { + if (items == null || itemNum <= 0) + return; + Instance._gameSession.c2s_SendCmdNPCSevBuy(itemNum, items); + } + public static void c2s_CmdNPCSevSell(int itemNum, npc_sell_item[] items) { if (items == null || itemNum <= 0) diff --git a/Assets/PerfectWorld/Scripts/UI/NPCShopDetailPanel.cs b/Assets/PerfectWorld/Scripts/UI/NPCShopDetailPanel.cs index f2d0f7e2b8..1a5ed0520a 100644 --- a/Assets/PerfectWorld/Scripts/UI/NPCShopDetailPanel.cs +++ b/Assets/PerfectWorld/Scripts/UI/NPCShopDetailPanel.cs @@ -168,22 +168,20 @@ public class NPCShopDetailPanel : MonoBehaviour } } - // Create npc_sell_item array - // Note: This function is for selling items TO NPC, but called on buy button click - // The tid is the item template ID, index is inventory slot (0 for now), count is 1, price is from shop - npc_sell_item[] items = new npc_sell_item[1]; - items[0] = new npc_sell_item + // 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 + npc_trade_item[] items = new npc_trade_item[1]; + items[0] = new npc_trade_item { tid = (int)currentItem.id, - index = 0, // Inventory slot index - may need to be determined from actual inventory - count = 1, // Quantity to buy/sell - price = (int)price + index = 0, // Shop item index - may need to be determined from shop item position + count = 1 // Quantity to buy }; - // Send the command - UnityGameSession.c2s_CmdNPCSevSell(1, items); + // Send the buy command + UnityGameSession.c2s_CmdNPCSevBuy(1, items); - Debug.Log($"[NPCShopDetailPanel] Sent sell command for item {currentItem.id}, price {price}"); + Debug.Log($"[NPCShopDetailPanel] Sent buy command for item {currentItem.id}, price {price}"); } void OnDestroy() diff --git a/Assets/Prefabs/UI/DialogNPCShop.prefab b/Assets/Prefabs/UI/DialogNPCShop.prefab index 5ef647d585..847811f599 100644 --- a/Assets/Prefabs/UI/DialogNPCShop.prefab +++ b/Assets/Prefabs/UI/DialogNPCShop.prefab @@ -2008,7 +2008,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 009ffcf832a02b545a5cce71d5e32877, type: 3} m_Name: m_EditorClassIdentifier: - npcShopMainPanel: {fileID: 239632690013192579} + npcShopMainPanel: {fileID: 8237288432181259026} npcShopDetailPanel: {fileID: 4704784074983072622} tabButtonContainer: {fileID: 412875348489889375} tabButtonPrefab: {fileID: 532136160345846687, guid: 548ae6ac061bc9648b093c9f9d203615, type: 3}