From 6587bb8b7176c733e5fc66407951e14e7103a04c Mon Sep 17 00:00:00 2001 From: SELEKCJONER Date: Wed, 25 Sep 2024 12:31:19 +0200 Subject: [PATCH] try to fix duplicate spare body entries --- EpinelPS/LobbyServer/Msgs/Gacha/ExecGacha.cs | 79 +++++++++++++------- 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/EpinelPS/LobbyServer/Msgs/Gacha/ExecGacha.cs b/EpinelPS/LobbyServer/Msgs/Gacha/ExecGacha.cs index cc9225f..44b7304 100644 --- a/EpinelPS/LobbyServer/Msgs/Gacha/ExecGacha.cs +++ b/EpinelPS/LobbyServer/Msgs/Gacha/ExecGacha.cs @@ -57,33 +57,58 @@ namespace EpinelPS.LobbyServer.Msgs.Gacha var pieceIds = new List>(); // 2D array to store characterId and pieceId as Tuple // Add each character's item to user.Items if the character exists in user.Characters - foreach (var characterData in selectedCharacters) - { - if (user.Characters.Any(c => c.Tid == characterData.id)) - { - user.Items.Add(new ItemData() - { - ItemType = characterData.piece_id, // Assuming item id corresponds to character id - Csn = 0, - Count = 1, // or any relevant count - Level = 0, - Exp = 0, - Position = 0, - Corp = 0, - Isn = user.GenerateUniqueItemId() - }); - response.Items.Add(new NetUserItemData() - { - Tid = characterData.piece_id, // Assuming item id corresponds to character id - Csn = 0, - Count = 1, // or any relevant count - Level = 0, - Exp = 0, - Position = 0, - Isn = user.GenerateUniqueItemId() - }); - } - } + foreach (var characterData in selectedCharacters) + { + // Check if the item for this character already exists in user.Items based on ItemType + var existingItem = user.Items.FirstOrDefault(item => item.ItemType == characterData.piece_id); + + if (existingItem != null) + { + // If the item exists, increment the count + existingItem.Count += 1; + + // Send the updated item in the response + response.Items.Add(new NetUserItemData() + { + Tid = existingItem.ItemType, + Csn = existingItem.Csn, + Count = existingItem.Count, + Level = existingItem.Level, + Exp = existingItem.Exp, + Position = existingItem.Position, + Isn = existingItem.Isn + }); + } + else + { + // If the item does not exist, create a new item entry + var newItem = new ItemData() + { + ItemType = characterData.piece_id, + Csn = 0, + Count = 1, // or any relevant count + Level = 0, + Exp = 0, + Position = 0, + Corp = 0, + Isn = user.GenerateUniqueItemId() + }; + user.Items.Add(newItem); + + // Add the new item to response + response.Items.Add(new NetUserItemData() + { + Tid = newItem.ItemType, + Csn = newItem.Csn, + Count = newItem.Count, + Level = newItem.Level, + Exp = newItem.Exp, + Position = newItem.Position, + Isn = newItem.Isn + }); + } + } + // Populate the 2D array with characterId and pieceId for each selected character foreach (var characterData in selectedCharacters) {