예제 #1
0
        public void RequestExchange(ExchangeRequestPacket packet)
        {
            var target = Broadcaster.Instance.GetCharacter(s =>
                                                           s.VisualId == packet.VisualId && s.MapInstanceId == Session.Character.MapInstanceId) as Character;
            ExcClosePacket closeExchange;

            if (target != null && (packet.RequestType == RequestExchangeType.Confirmed ||
                                   packet.RequestType == RequestExchangeType.Cancelled))
            {
                _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.CANT_FIND_CHARACTER));
                return;
            }

            if (Session.Character.InShop || (target?.InShop ?? false))
            {
                _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.PLAYER_IN_SHOP));
                return;
            }

            switch (packet.RequestType)
            {
            case RequestExchangeType.Requested:
                if (_exchangeProvider.CheckExchange(Session.Character.CharacterId) ||
                    _exchangeProvider.CheckExchange(target.VisualId))
                {
                    Session.SendPacket(new MsgPacket
                    {
                        Message = Language.Instance.GetMessageFromKey(LanguageKey.ALREADY_EXCHANGE,
                                                                      Session.Account.Language),
                        Type = MessageType.White
                    });
                    return;
                }

                if (target.ExchangeBlocked)
                {
                    Session.SendPacket(Session.Character.GenerateSay(
                                           Language.Instance.GetMessageFromKey(LanguageKey.EXCHANGE_BLOCKED, Session.Account.Language),
                                           SayColorType.Purple));
                    return;
                }

                if (Session.Character.IsRelatedToCharacter(target.VisualId, CharacterRelationType.Blocked))
                {
                    Session.SendPacket(new InfoPacket
                    {
                        Message = Language.Instance.GetMessageFromKey(LanguageKey.BLACKLIST_BLOCKED,
                                                                      Session.Account.Language)
                    });
                    return;
                }

                if (Session.Character.InShop || target.InShop)
                {
                    Session.SendPacket(new MsgPacket
                    {
                        Message =
                            Language.Instance.GetMessageFromKey(LanguageKey.HAS_SHOP_OPENED,
                                                                Session.Account.Language),
                        Type = MessageType.White
                    });
                    return;
                }

                Session.SendPacket(new ModalPacket
                {
                    Message = Language.Instance.GetMessageFromKey(LanguageKey.YOU_ASK_FOR_EXCHANGE,
                                                                  Session.Account.Language),
                    Type = 0
                });

                target.SendPacket(new DlgPacket
                {
                    YesPacket = new ExchangeRequestPacket
                    {
                        RequestType = RequestExchangeType.List, VisualId = Session.Character.VisualId
                    },
                    NoPacket = new ExchangeRequestPacket
                    {
                        RequestType = RequestExchangeType.Declined, VisualId = Session.Character.VisualId
                    },
                    Question = Language.Instance.GetMessageFromKey(LanguageKey.INCOMING_EXCHANGE,
                                                                   Session.Account.Language)
                });
                return;

            case RequestExchangeType.List:
                if (!_exchangeProvider.OpenExchange(Session.Character.VisualId, target.CharacterId))
                {
                    return;
                }

                Session.SendPacket(Session.Character.GenerateServerExcListPacket(null, null, null));
                target.SendPacket(target.GenerateServerExcListPacket(null, null, null));
                return;

            case RequestExchangeType.Declined:
                Session.SendPacket(Session.Character.GenerateSay(
                                       Language.Instance.GetMessageFromKey(LanguageKey.EXCHANGE_REFUSED, Session.Account.Language),
                                       SayColorType.Yellow));
                target?.SendPacket(target.GenerateSay(target.GetMessageFromKey(LanguageKey.EXCHANGE_REFUSED),
                                                      SayColorType.Yellow));
                return;

            case RequestExchangeType.Confirmed:
                var targetId = _exchangeProvider.GetTargetId(Session.Character.CharacterId);

                if (!targetId.HasValue)
                {
                    _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.INVALID_EXCHANGE));
                    return;
                }

                var exchangeTarget = Broadcaster.Instance.GetCharacter(s =>
                                                                       s.VisualId == targetId.Value && s.MapInstance == Session.Character.MapInstance);

                if (exchangeTarget == null)
                {
                    _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.CANT_FIND_CHARACTER));
                    return;
                }

                _exchangeProvider.ConfirmExchange(Session.Character.VisualId);

                if (!_exchangeProvider.IsExchangeConfirmed(Session.Character.VisualId) ||
                    !_exchangeProvider.IsExchangeConfirmed(exchangeTarget.VisualId))
                {
                    Session.SendPacket(new InfoPacket
                    {
                        Message = Language.Instance.GetMessageFromKey(LanguageKey.IN_WAITING_FOR,
                                                                      Session.Account.Language)
                    });
                    return;
                }

                var success = _exchangeProvider.ValidateExchange(Session, exchangeTarget);

                if (success.Item1 == ExchangeResultType.Success)
                {
                    foreach (var infoPacket in success.Item2)
                    {
                        if (infoPacket.Key == Session.Character.CharacterId)
                        {
                            Session.SendPacket(infoPacket.Value);
                        }
                        else if (infoPacket.Key == exchangeTarget.VisualId)
                        {
                            exchangeTarget.SendPacket(infoPacket.Value);
                        }
                        else
                        {
                            _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.INVALID_EXCHANGE));
                        }
                    }
                }
                else
                {
                    var itemList = _exchangeProvider.ProcessExchange(Session.Character.VisualId,
                                                                     exchangeTarget.VisualId, Session.Character.Inventory, exchangeTarget.Inventory);

                    foreach (var item in itemList)
                    {
                        if (item.Key == Session.Character.CharacterId)
                        {
                            Session.SendPacket(item.Value);
                        }
                        else
                        {
                            exchangeTarget.SendPacket(item.Value);
                        }
                    }

                    var getSessionData = _exchangeProvider.GetData(Session.Character.CharacterId);
                    Session.Character.RemoveGold(getSessionData.Gold);
                    Session.Character.RemoveBankGold(getSessionData.BankGold * 1000);

                    exchangeTarget.AddGold(getSessionData.Gold);
                    exchangeTarget.AddBankGold(getSessionData.BankGold * 1000);

                    var getTargetData = _exchangeProvider.GetData(exchangeTarget.VisualId);
                    exchangeTarget.RemoveGold(getTargetData.Gold);
                    exchangeTarget.RemoveBankGold(getTargetData.BankGold * 1000);

                    Session.Character.AddGold(getTargetData.Gold);
                    Session.Character.AddBankGold(getTargetData.BankGold * 1000);
                }

                closeExchange = _exchangeProvider.CloseExchange(Session.Character.VisualId, success.Item1);
                exchangeTarget?.SendPacket(closeExchange);
                Session.SendPacket(closeExchange);
                return;

            case RequestExchangeType.Cancelled:
                var cancelId = _exchangeProvider.GetTargetId(Session.Character.CharacterId);
                if (!cancelId.HasValue)
                {
                    _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.USER_NOT_IN_EXCHANGE));
                    return;
                }

                var cancelTarget = Broadcaster.Instance.GetCharacter(s => s.VisualId == cancelId.Value);

                closeExchange =
                    _exchangeProvider.CloseExchange(Session.Character.VisualId, ExchangeResultType.Failure);
                cancelTarget?.SendPacket(closeExchange);
                Session.SendPacket(closeExchange);
                return;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
예제 #2
0
        public override async Task ExecuteAsync(ExchangeRequestPacket packet, ClientSession clientSession)
        {
            var target = Broadcaster.Instance.GetCharacter(s =>
                                                           (s.VisualId == packet.VisualId) &&
                                                           (s.MapInstanceId == clientSession.Character.MapInstanceId)) as Character;
            ExcClosePacket closeExchange;

            if ((target != null) && ((packet.RequestType == RequestExchangeType.Confirmed) ||
                                     (packet.RequestType == RequestExchangeType.Cancelled)))
            {
                _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.CANT_FIND_CHARACTER));
                return;
            }

            if (clientSession.Character.InShop || (target?.InShop ?? false))
            {
                _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.PLAYER_IN_SHOP));
                return;
            }

            switch (packet.RequestType)
            {
            case RequestExchangeType.Requested:
                if (_exchangeProvider.CheckExchange(clientSession.Character.CharacterId) ||
                    _exchangeProvider.CheckExchange(target?.VisualId ?? 0))
                {
                    await clientSession.SendPacketAsync(new MsgPacket
                    {
                        Message = GameLanguage.Instance.GetMessageFromKey(LanguageKey.ALREADY_EXCHANGE,
                                                                          clientSession.Account.Language),
                        Type = MessageType.White
                    }).ConfigureAwait(false);

                    return;
                }

                if (target?.ExchangeBlocked ?? true)
                {
                    await clientSession.SendPacketAsync(clientSession.Character.GenerateSay(
                                                            GameLanguage.Instance.GetMessageFromKey(LanguageKey.EXCHANGE_BLOCKED,
                                                                                                    clientSession.Account.Language),
                                                            SayColorType.Purple)).ConfigureAwait(false);

                    return;
                }

                var blacklisteds = await _blacklistHttpClient.GetBlackListsAsync(clientSession.Character.VisualId).ConfigureAwait(false);

                if (blacklisteds.Any(s => s.CharacterId == target.VisualId))
                {
                    await clientSession.SendPacketAsync(new InfoPacket
                    {
                        Message = GameLanguage.Instance.GetMessageFromKey(LanguageKey.BLACKLIST_BLOCKED,
                                                                          clientSession.Account.Language)
                    }).ConfigureAwait(false);

                    return;
                }

                if (clientSession.Character.InShop || target.InShop)
                {
                    await clientSession.SendPacketAsync(new MsgPacket
                    {
                        Message =
                            GameLanguage.Instance.GetMessageFromKey(LanguageKey.HAS_SHOP_OPENED,
                                                                    clientSession.Account.Language),
                        Type = MessageType.White
                    }).ConfigureAwait(false);

                    return;
                }

                await clientSession.SendPacketAsync(new ModalPacket
                {
                    Message = string.Format(GameLanguage.Instance.GetMessageFromKey(LanguageKey.YOU_ASK_FOR_EXCHANGE,
                                                                                    clientSession.Account.Language), target.Name),
                    Type = 0
                }).ConfigureAwait(false);

                await target.SendPacketAsync(new DlgPacket
                {
                    YesPacket = new ExchangeRequestPacket
                    {
                        RequestType = RequestExchangeType.List, VisualId = clientSession.Character.VisualId
                    },
                    NoPacket = new ExchangeRequestPacket
                    {
                        RequestType = RequestExchangeType.Declined, VisualId = clientSession.Character.VisualId
                    },
                    Question = string.Format(GameLanguage.Instance.GetMessageFromKey(LanguageKey.INCOMING_EXCHANGE,
                                                                                     clientSession.Account.Language), clientSession.Character.Name)
                }).ConfigureAwait(false);

                return;

            case RequestExchangeType.List:
                if (!_exchangeProvider.OpenExchange(clientSession.Character.VisualId, target?.CharacterId ?? 0))
                {
                    return;
                }

                await clientSession.SendPacketAsync(clientSession.Character.GenerateServerExcListPacket(null, null, null)).ConfigureAwait(false);

                await(target == null ? Task.CompletedTask : target.SendPacketAsync(target.GenerateServerExcListPacket(null, null, null))).ConfigureAwait(false);
                return;

            case RequestExchangeType.Declined:
                await clientSession.SendPacketAsync(clientSession.Character.GenerateSay(
                                                        GameLanguage.Instance.GetMessageFromKey(LanguageKey.EXCHANGE_REFUSED,
                                                                                                clientSession.Account.Language),
                                                        SayColorType.Yellow)).ConfigureAwait(false);

                await(target == null ? Task.CompletedTask : target.SendPacketAsync(target.GenerateSay(target.GetMessageFromKey(LanguageKey.EXCHANGE_REFUSED),
                                                                                                      SayColorType.Yellow))).ConfigureAwait(false);
                return;

            case RequestExchangeType.Confirmed:
                var targetId = _exchangeProvider.GetTargetId(clientSession.Character.CharacterId);

                if (!targetId.HasValue)
                {
                    _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.INVALID_EXCHANGE));
                    return;
                }

                var exchangeTarget = Broadcaster.Instance.GetCharacter(s =>
                                                                       (s.VisualId == targetId.Value) && (s.MapInstance == clientSession.Character.MapInstance));

                if (exchangeTarget == null)
                {
                    _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.CANT_FIND_CHARACTER));
                    return;
                }

                _exchangeProvider.ConfirmExchange(clientSession.Character.VisualId);

                if (!_exchangeProvider.IsExchangeConfirmed(clientSession.Character.VisualId) ||
                    !_exchangeProvider.IsExchangeConfirmed(exchangeTarget.VisualId))
                {
                    await clientSession.SendPacketAsync(new InfoPacket
                    {
                        Message = string.Format(GameLanguage.Instance.GetMessageFromKey(LanguageKey.IN_WAITING_FOR,
                                                                                        clientSession.Account.Language), exchangeTarget.Name)
                    }).ConfigureAwait(false);

                    return;
                }

                var success = _exchangeProvider.ValidateExchange(clientSession, exchangeTarget);

                if (success.Item1 == ExchangeResultType.Success)
                {
                    foreach (var infoPacket in success.Item2 !)
                    {
                        if (infoPacket.Key == clientSession.Character.CharacterId)
                        {
                            await clientSession.SendPacketAsync(infoPacket.Value).ConfigureAwait(false);
                        }
                        else if (infoPacket.Key == exchangeTarget.VisualId)
                        {
                            await exchangeTarget.SendPacketAsync(infoPacket.Value).ConfigureAwait(false);
                        }
                        else
                        {
                            _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.INVALID_EXCHANGE));
                        }
                    }
                }
                else
                {
                    var itemList = _exchangeProvider.ProcessExchange(clientSession.Character.VisualId,
                                                                     exchangeTarget.VisualId, clientSession.Character.InventoryService, exchangeTarget.InventoryService);

                    foreach (var item in itemList)
                    {
                        if (item.Key == clientSession.Character.CharacterId)
                        {
                            await clientSession.SendPacketAsync(item.Value).ConfigureAwait(false);
                        }
                        else
                        {
                            await exchangeTarget.SendPacketAsync(item.Value).ConfigureAwait(false);
                        }
                    }

                    var getSessionData = _exchangeProvider.GetData(clientSession.Character.CharacterId);
                    await clientSession.Character.RemoveGoldAsync(getSessionData.Gold).ConfigureAwait(false);

                    clientSession.Character.RemoveBankGold(getSessionData.BankGold * 1000);

                    await exchangeTarget.AddGoldAsync(getSessionData.Gold).ConfigureAwait(false);

                    exchangeTarget.AddBankGold(getSessionData.BankGold * 1000);

                    var getTargetData = _exchangeProvider.GetData(exchangeTarget.VisualId);
                    await exchangeTarget.RemoveGoldAsync(getTargetData.Gold).ConfigureAwait(false);

                    exchangeTarget.RemoveBankGold(getTargetData.BankGold * 1000);

                    await clientSession.Character.AddGoldAsync(getTargetData.Gold).ConfigureAwait(false);

                    clientSession.Character.AddBankGold(getTargetData.BankGold * 1000);
                }

                closeExchange = _exchangeProvider.CloseExchange(clientSession.Character.VisualId, success.Item1) !;
                await exchangeTarget.SendPacketAsync(closeExchange).ConfigureAwait(false);

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

                return;

            case RequestExchangeType.Cancelled:
                var cancelId = _exchangeProvider.GetTargetId(clientSession.Character.CharacterId);
                if (!cancelId.HasValue)
                {
                    _logger.Error(LogLanguage.Instance.GetMessageFromKey(LogLanguageKey.USER_NOT_IN_EXCHANGE));
                    return;
                }

                var cancelTarget = Broadcaster.Instance.GetCharacter(s => s.VisualId == cancelId.Value);

                closeExchange =
                    _exchangeProvider.CloseExchange(clientSession.Character.VisualId, ExchangeResultType.Failure) !;
                cancelTarget?.SendPacketAsync(closeExchange);
                await clientSession.SendPacketAsync(closeExchange).ConfigureAwait(false);

                return;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }