コード例 #1
0
ファイル: NpcShopSystem.cs プロジェクト: zero16832/Rhisis
        /// <inheritdoc />
        public void BuyItem(IPlayerEntity player, NpcShopItemInfo shopItemInfo, int quantity)
        {
            var npc = player.FindEntity <INpcEntity>(x => x.Object.Name == player.PlayerData.CurrentShopName);

            if (npc == null)
            {
                _logger.LogError($"ShopSystem: Cannot find NPC: {player.PlayerData.CurrentShopName}");
                return;
            }

            if (!npc.NpcData.HasShop)
            {
                _logger.LogError($"ShopSystem: NPC {npc.Object.Name} doesn't have a shop.");
                return;
            }

            if (shopItemInfo.Tab < 0 || shopItemInfo.Tab >= ShopData.DefaultTabCount)
            {
                _logger.LogError($"Attempt to buy an item from {npc.Object.Name} shop tab that is out of range.");
                return;
            }

            ItemContainerComponent shopTab = npc.Shop.ElementAt(shopItemInfo.Tab);

            if (shopItemInfo.Slot < 0 || shopItemInfo.Slot > shopTab.MaxCapacity - 1)
            {
                _logger.LogError($"ShopSystem: Item slot index was out of tab bounds. Slot: {shopItemInfo.Slot}");
                return;
            }

            Item shopItem = shopTab.GetItemAtSlot(shopItemInfo.Slot);

            if (shopItem.Id != shopItemInfo.ItemId)
            {
                _logger.LogError($"ShopSystem: Shop item id doens't match the item id that {player.Object.Name} is trying to buy.");
                return;
            }

            if (player.PlayerData.Gold < shopItem.Data.Cost)
            {
                _logger.LogTrace($"ShopSystem: {player.Object.Name} doens't have enough gold to buy item {shopItem.Data.Name} at {shopItem.Data.Cost}.");
                _textPacketFactory.SendDefinedText(player, DefineText.TID_GAME_LACKMONEY);
                return;
            }

            int itemsCreatedCount = _inventorySystem.CreateItem(player, shopItem, quantity);

            if (itemsCreatedCount > 0)
            {
                _playerDataSystem.DecreaseGold(player, shopItem.Data.Cost * itemsCreatedCount);
            }
        }