예제 #1
0
        public void Handle(WorldClient client, NpcBuyItemPacket packet)
        {
            var cellId = _gameWorld.Players[_gameSession.CharId].CellId;

            var npc = _mapProvider.Map.GetNPC(cellId, packet.NpcId);

            if (npc is null || !npc.ContainsProduct(packet.ItemIndex))
            {
                _logger.LogWarning("NPC with id {npcId} doesn't contain item at index: {itemIndex}.", packet.NpcId, packet.ItemIndex);
                return;
            }

            var discount = 0f;

            if (_mapProvider.Map is GuildHouseMap)
            {
                if (!_guildManager.HasGuild)
                {
                    _packetFactory.SendGuildHouseActionError(client, GuildHouseActionError.LowRank, 30);
                    return;
                }

                var allowed = _guildManager.CanUseNpc(npc.Type, npc.TypeId, out var requiredRank);
                if (!allowed)
                {
                    _packetFactory.SendGuildHouseActionError(client, GuildHouseActionError.LowRank, requiredRank);
                    return;
                }

                allowed = _guildManager.HasNpcLevel(npc.Type, npc.TypeId);
                if (!allowed)
                {
                    _packetFactory.SendGuildHouseActionError(client, GuildHouseActionError.LowLevel, 0);
                    return;
                }

                discount = _guildManager.GetDiscount(npc.Type, npc.TypeId);
            }

            var buyItem    = npc.Products[packet.ItemIndex];
            var boughtItem = _inventoryManager.BuyItem(buyItem, packet.Count, discount, out var result);

            _packetFactory.SendBoughtItem(client, result, boughtItem, _inventoryManager.Gold);
        }