Ejemplo n.º 1
0
        public static Task MoveAsync(this INonPlayableEntity nonPlayableEntity, IDistanceCalculator distanceCalculator)
        {
            if (!nonPlayableEntity.IsAlive)
            {
                return(Task.CompletedTask);
            }

            if (!nonPlayableEntity.IsMoving || (nonPlayableEntity.Speed <= 0))
            {
                return(Task.CompletedTask);
            }

            var time = (SystemTime.Now() - nonPlayableEntity.LastMove).TotalMilliseconds;

            if (!(time > RandomHelper.Instance.RandomNumber(400, 3200)))
            {
                return(Task.CompletedTask);
            }

            var mapX = nonPlayableEntity.MapX;
            var mapY = nonPlayableEntity.MapY;

            if (!nonPlayableEntity.MapInstance.Map.GetFreePosition(ref mapX, ref mapY,
                                                                   (byte)RandomHelper.Instance.RandomNumber(0, 3),
                                                                   (byte)RandomHelper.Instance.RandomNumber(0, 3)))
            {
                return(Task.CompletedTask);
            }

            var distance = (int)distanceCalculator.GetDistance(new MapCell {
                X = nonPlayableEntity.PositionX, Y = nonPlayableEntity.PositionY
            }, new MapCell {
                X = mapX, Y = mapY
            });
            var value = 1000d * distance / (2 * nonPlayableEntity.Speed);

            Observable.Timer(TimeSpan.FromMilliseconds(value))
            .Subscribe(
                _ =>
            {
                nonPlayableEntity.PositionX = mapX;
                nonPlayableEntity.PositionY = mapY;
            });

            nonPlayableEntity.LastMove = SystemTime.Now().AddMilliseconds(value);
            return(nonPlayableEntity.MapInstance.SendPacketAsync(
                       nonPlayableEntity.GenerateMove(mapX, mapY)));
        }
Ejemplo n.º 2
0
        public override async Task ExecuteAsync(MShopPacket mShopPacket, ClientSession clientSession)
        {
            var portal = clientSession.Character.MapInstance.Portals.Find(port =>
                                                                          _distanceCalculator.GetDistance(new MapCell {
                X = clientSession.Character.PositionX, Y = clientSession.Character.PositionY
            }, new MapCell {
                X = port.SourceX, Y = port.SourceY
            }) <= 6);

            if (portal != null)
            {
                await clientSession.SendPacketAsync(new MsgPacket
                {
                    Message = GameLanguage.Instance.GetMessageFromKey(LanguageKey.SHOP_NEAR_PORTAL,
                                                                      clientSession.Account.Language),
                    Type = 0
                }).ConfigureAwait(false);

                return;
            }

            if ((clientSession.Character.Group != null) && (clientSession.Character.Group?.Type != GroupType.Group))
            {
                await clientSession.SendPacketAsync(new MsgPacket
                {
                    Message = GameLanguage.Instance.GetMessageFromKey(LanguageKey.SHOP_NOT_ALLOWED_IN_RAID,
                                                                      clientSession.Account.Language),
                    Type = MessageType.White
                }).ConfigureAwait(false);

                return;
            }

            if (!clientSession.Character.MapInstance.ShopAllowed)
            {
                await clientSession.SendPacketAsync(new MsgPacket
                {
                    Message = GameLanguage.Instance.GetMessageFromKey(LanguageKey.SHOP_NOT_ALLOWED,
                                                                      clientSession.Account.Language),
                    Type = MessageType.White
                }).ConfigureAwait(false);

                return;
            }

            switch (mShopPacket.Type)
            {
            case CreateShopPacketType.Open:
                clientSession.Character.Shop = new Shop();
                sbyte shopSlot = -1;
                foreach (var item in mShopPacket.ItemList !)
                {
                    shopSlot++;
                    if (item !.Amount == 0)
                    {
                        continue;
                    }

                    var inv = clientSession.Character.InventoryService.LoadBySlotAndType(item.Slot,
                                                                                         (NoscorePocketType)item.Type);
                    if (inv == null)
                    {
                        //log
                        continue;
                    }

                    if (inv.ItemInstance !.Amount < item.Amount)
                    {
                        //todo log
                        return;
                    }

                    if (!inv.ItemInstance.Item !.IsTradable || (inv.ItemInstance.BoundCharacterId != null))
                    {
                        await clientSession.SendPacketAsync(new ShopEndPacket { Type = ShopEndPacketType.PersonalShop }).ConfigureAwait(false);

                        await clientSession.SendPacketAsync(clientSession.Character.GenerateSay(
                                                                GameLanguage.Instance.GetMessageFromKey(LanguageKey.SHOP_ONLY_TRADABLE_ITEMS,
                                                                                                        clientSession.Account.Language),
                                                                SayColorType.Yellow)).ConfigureAwait(false);

                        clientSession.Character.Shop = null;
                        return;
                    }

                    clientSession.Character.Shop.ShopItems.TryAdd(shopSlot,
                                                                  new ShopItem
                    {
                        Amount       = item.Amount,
                        Price        = item.Price,
                        Slot         = (byte)shopSlot,
                        Type         = 0,
                        ItemInstance = inv.ItemInstance
                    });
                }

                if (clientSession.Character.Shop.ShopItems.Count == 0)
                {
                    await clientSession.SendPacketAsync(new ShopEndPacket { Type = ShopEndPacketType.PersonalShop }).ConfigureAwait(false);

                    await clientSession.SendPacketAsync(clientSession.Character.GenerateSay(
                                                            GameLanguage.Instance.GetMessageFromKey(LanguageKey.SHOP_EMPTY, clientSession.Account.Language),
                                                            SayColorType.Yellow)).ConfigureAwait(false);

                    clientSession.Character.Shop = null;
                    return;
                }

                clientSession.Character.Shop.Session  = clientSession;
                clientSession.Character.Shop.MenuType = 3;
                clientSession.Character.Shop.ShopId   = 501;
                clientSession.Character.Shop.Size     = 60;

                for (var i = 0; i < clientSession.Character.Shop.Name.Keys.Count; i++)
                {
                    var key = clientSession.Character.Shop.Name.Keys.ElementAt(i);
                    clientSession.Character.Shop.Name[key] = string.IsNullOrWhiteSpace(mShopPacket.Name) ?
                                                             GameLanguage.Instance.GetMessageFromKey(LanguageKey.SHOP_PRIVATE_SHOP, key) :
                                                             mShopPacket.Name.Substring(0, Math.Min(mShopPacket.Name.Length, 20));
                }

                await clientSession.Character.MapInstance.SendPacketAsync(clientSession.Character.GenerateShop(clientSession.Character.AccountLanguage)).ConfigureAwait(false);

                await clientSession.SendPacketAsync(new InfoPacket
                {
                    Message = GameLanguage.Instance.GetMessageFromKey(LanguageKey.SHOP_OPEN,
                                                                      clientSession.Account.Language)
                }).ConfigureAwait(false);

                clientSession.Character.Requests.Subscribe(data =>
                                                           data.ClientSession.SendPacketAsync(
                                                               clientSession.Character.GenerateNpcReq(clientSession.Character.Shop.ShopId)));
                await clientSession.Character.MapInstance.SendPacketAsync(clientSession.Character.GeneratePFlag(),
                                                                          new EveryoneBut(clientSession.Channel !.Id)).ConfigureAwait(false);

                clientSession.Character.IsSitting = true;
                clientSession.Character.LoadSpeed();
                await clientSession.SendPacketAsync(clientSession.Character.GenerateCond()).ConfigureAwait(false);

                await clientSession.Character.MapInstance.SendPacketAsync(clientSession.Character.GenerateRest()).ConfigureAwait(false);

                break;

            case CreateShopPacketType.Close:
                await clientSession.Character.CloseShopAsync().ConfigureAwait(false);

                break;

            case CreateShopPacketType.Create:
                await clientSession.SendPacketAsync(new IshopPacket()).ConfigureAwait(false);

                break;

            default:
                //todo log
                return;
            }
        }