Ejemplo n.º 1
0
        public override async Task ExecuteAsync(ExcListPacket packet, ClientSession clientSession)
        {
            if ((packet.Gold > clientSession.Character.Gold) || (packet.BankGold > clientSession.Account.BankMoney))
            {
                _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.NOT_ENOUGH_GOLD));
                return;
            }

            var subPacketList = new List <ServerExcListSubPacket?>();

            var target = Broadcaster.Instance.GetCharacter(s =>
                                                           (s.VisualId == _exchangeProvider.GetTargetId(clientSession.Character.VisualId)) &&
                                                           (s.MapInstanceId == clientSession.Character.MapInstanceId)) as Character;

            if ((packet.SubPackets !.Count > 0) && (target != null))
            {
                byte i = 0;
                foreach (var value in packet.SubPackets)
                {
                    var item = clientSession.Character.InventoryService.LoadBySlotAndType(value !.Slot,
                                                                                          (NoscorePocketType)value.PocketType);

                    if ((item == null) || (item.ItemInstance !.Amount < value.Amount))
                    {
                        var closeExchange =
                            _exchangeProvider.CloseExchange(clientSession.Character.VisualId,
                                                            ExchangeResultType.Failure);
                        await clientSession.SendPacketAsync(closeExchange).ConfigureAwait(false);

                        await target.SendPacketAsync(closeExchange).ConfigureAwait(false);

                        _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.INVALID_EXCHANGE_LIST));
                        return;
                    }

                    if (!item.ItemInstance.Item !.IsTradable)
                    {
                        await clientSession.SendPacketAsync(_exchangeProvider.CloseExchange(clientSession.Character.CharacterId,
                                                                                            ExchangeResultType.Failure)).ConfigureAwait(false);

                        await target.SendPacketAsync(_exchangeProvider.CloseExchange(target.VisualId, ExchangeResultType.Failure)).ConfigureAwait(false);

                        _logger.Error(
                            LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.CANNOT_TRADE_NOT_TRADABLE_ITEM));
                        return;
                    }

                    _exchangeProvider.AddItems(clientSession.Character.CharacterId, item, value.Amount);

                    var subPacket = new ServerExcListSubPacket
                    {
                        ExchangeSlot = i,
                        PocketType   = value.PocketType,
                        ItemVnum     = item.ItemInstance.ItemVNum,
                        Upgrade      = item.ItemInstance.Upgrade,
                        AmountOrRare = value.PocketType == PocketType.Equipment ? item.ItemInstance.Rare : value.Amount
                    };


                    subPacketList.Add(subPacket);
                    i++;
                }
            }