Exemple #1
0
 public void Send(ICharacter character, TemporaryBag tempBag)
 {
     this.Send(character, this.TemporaryBagHandle(character, tempBag.Shopper, tempBag.Vendor, tempBag.Identity));
     this.Send(character, this.TemporaryBagHandle(character, tempBag.Vendor, tempBag.Shopper, tempBag.Identity));
 }
Exemple #2
0
        /// <summary>
        /// </summary>
        /// <param name="message">
        /// </param>
        /// <param name="client">
        /// </param>
        protected override void Read(TradeMessage message, IZoneClient client)
        {
            IItemContainer target;

            if ((message.Action != TradeAction.End) && (message.Action != TradeAction.Decline))
            {
                target = Pool.Instance.GetObject <IItemContainer>(
                    client.Controller.Character.Playfield.Identity,
                    message.Target);
            }
            switch (message.Action)
            {
            case TradeAction.None:
            {
                ICharacter isnpc =
                    Pool.Instance.GetObject <ICharacter>(
                        client.Controller.Character.Playfield.Identity,
                        message.Target);
                if (isnpc != null)
                {
                    NPCController controller = isnpc.Controller as NPCController;
                    if (controller != null)
                    {
                        controller.Trade(message.Identity);
                    }
                }
                break;
            }

            case TradeAction.AddItem:
            {
                if (client.Controller.Character.ShoppingBag == null)
                {
                    throw new InvalidOperationException("Characters shopping bag is null?? huh");
                }

                IItemContainer issuer =
                    Pool.Instance.GetObject <IItemContainer>(
                        client.Controller.Character.Playfield.Identity,
                        message.Target);

                if (issuer != null)
                {
                    IItem item;

                    if (issuer is Vendor)
                    {
                        item = issuer.BaseInventory.GetItemInContainer(
                            (int)IdentityType.Inventory,
                            message.Container.Instance);
                    }
                    else
                    {
                        item = issuer.BaseInventory.GetItemInContainer(
                            (int)message.Container.Type,
                            message.Container.Instance);
                    }
                    if (item != null)
                    {
                        TemporaryBag shoppingBag = client.Controller.Character.ShoppingBag;
                        if (issuer is Vendor)
                        {
                            shoppingBag.Add(
                                new Identity()
                                {
                                    Instance = message.Container.Instance
                                },
                                issuer.BaseInventory.RemoveItem(
                                    (int)IdentityType.Inventory,
                                    message.Container.Instance));
                        }
                        else
                        {
                            shoppingBag.Add(
                                message.Target,
                                issuer.BaseInventory.RemoveItem(
                                    (int)message.Container.Type,
                                    message.Container.Instance));
                        }
                        this.AcknowledgeTradeAction(client.Controller.Character, message);
                    }
                }
                break;
            }

            case TradeAction.RemoveItem:
            {
                if (client.Controller.Character.ShoppingBag == null)
                {
                    throw new InvalidOperationException("Characters shopping bag is null?? huh");
                }

                IItemContainer issuer =
                    Pool.Instance.GetObject <IItemContainer>(
                        client.Controller.Character.Playfield.Identity,
                        message.Target);

                if (issuer != null)
                {
                    IItem item;

                    TemporaryBag shoppingBag = client.Controller.Character.ShoppingBag;
                    if (issuer is Vendor)
                    {
                        item = issuer.BaseInventory.GetItemInContainer(
                            (int)IdentityType.Inventory,
                            message.Container.Instance);
                    }
                    else
                    {
                        item = shoppingBag.GetSoldItems()[message.Container.Instance];
                    }
                    if (item != null)
                    {
                        if (issuer is Vendor)
                        {
                            shoppingBag.Remove(
                                new Identity()
                                {
                                    Instance = message.Container.Instance
                                },
                                message.Container.Instance);

                            this.Send(
                                client.Controller.Character,
                                this.AcknowledgeRemove(shoppingBag.Shopper, message));
                            this.Send(
                                client.Controller.Character,
                                this.AcknowledgeRemove(shoppingBag.Vendor, message));
                        }
                        else
                        {
                            IItem returnedItem = shoppingBag.Remove(message.Target, message.Container.Instance);
                            if (returnedItem != null)
                            {
                                InventoryError err =
                                    client.Controller.Character.BaseInventory.AddToPage(
                                        client.Controller.Character.BaseInventory.StandardPage,
                                        client.Controller.Character.BaseInventory.Pages[
                                            client.Controller.Character.BaseInventory.StandardPage].FindFreeSlot(),
                                        returnedItem);

                                if (err == InventoryError.OK)
                                {
                                    ContainerAddItemMessageHandler.Default.Send(
                                        client.Controller.Character,
                                        new Identity()
                                        {
                                            Type     = IdentityType.KnuBotTradeWindow,
                                            Instance = message.Container.Instance
                                        },
                                        0x6f);     // 0x6f = Next free slot in main inventory
                                }
                                else
                                {
                                    // Cant return item code here
                                }

                                InventoryUpdatedMessageHandler.Default.Send(
                                    client.Controller.Character,
                                    shoppingBag.Vendor);
                                InventoryUpdatedMessageHandler.Default.Send(
                                    client.Controller.Character,
                                    client.Controller.Character.Identity);
                            }
                        }
                        this.AcknowledgeTradeAction(client.Controller.Character, message);
                    }
                }
                break;
            }

            case TradeAction.End:
            {
                if (client.Controller.Character.ShoppingBag == null)
                {
                    throw new InvalidOperationException("Characters shopping bag is null?? huh");
                }

                TemporaryBag shoppingBag = client.Controller.Character.ShoppingBag;
                Vendor       vendor      = Pool.Instance.GetObject <Vendor>(
                    client.Controller.Character.Playfield.Identity,
                    shoppingBag.Vendor);
                IItemContainer issuer = client.Controller.Character;

                if ((issuer != null) && (vendor != null))
                {
                    if (shoppingBag != null)
                    {
                        IItem[] items    = shoppingBag.GetBoughtItems();
                        double  CLFactor = vendor.Stats[StatIds.sellmodifier].Value / 100.0f
                                           * (double)
                                           (1
                                            - (client.Controller.Character.Stats[StatIds.computerliteracy].Value
                                               / 40) / 100.0f);
                        int cash = 0;
                        foreach (IItem item in items)
                        {
                            int nextSlot = issuer.BaseInventory[issuer.BaseInventory.StandardPage].FindFreeSlot();
                            if (nextSlot != -1)
                            {
                                issuer.BaseInventory[issuer.BaseInventory.StandardPage].Add(nextSlot, item);
                                AddTemplateMessageHandler.Default.Send(client.Controller.Character, (Item)item);
                                cash += (int)Math.Round(CLFactor * item.GetAttribute(74));
                            }
                        }

                        items    = shoppingBag.GetSoldItems();
                        CLFactor = vendor.Stats[StatIds.buymodifier].Value / 100.0f
                                   + ((double)
                                      ((int)
                                       (client.Controller.Character.Stats[StatIds.computerliteracy].Value / 40))
                                      / 2500.0f);
                        foreach (IItem item in items)
                        {
                            cash -= (int)(CLFactor * item.GetAttribute(74));
                        }

                        client.Controller.Character.Stats[StatIds.cash].Value -= cash;

                        this.Send(
                            client.Controller.Character,
                            TradeAction.Unknown,
                            shoppingBag.Vendor,
                            shoppingBag.Vendor);
                        client.Controller.SendChangedStats();
                        shoppingBag.Dispose();
                    }
                }
                break;
            }

            case TradeAction.Decline:
            {
                IItemContainer issuer      = client.Controller.Character;
                TemporaryBag   shoppingBag = client.Controller.Character.ShoppingBag;

                if (shoppingBag != null)
                {
                    IItem[] items = shoppingBag.GetSoldItems();
                    foreach (IItem item in items)
                    {
                        int nextSlot = issuer.BaseInventory[issuer.BaseInventory.StandardPage].FindFreeSlot();
                        if (nextSlot != -1)
                        {
                            issuer.BaseInventory[issuer.BaseInventory.StandardPage].Add(nextSlot, item);
                        }
                    }

                    this.Send(client.Controller.Character, TradeAction.Decline, Identity.None, Identity.None);
                    shoppingBag.Dispose();
                }

                break;
            }
            }
        }
        /// <summary>
        /// </summary>
        /// <param name="message">
        /// </param>
        /// <param name="client">
        /// </param>
        /// <exception cref="NullReferenceException">
        /// </exception>
        protected override void Read(GenericCmdMessage message, IZoneClient client)
        {
            switch (message.Action)
            {
            case GenericCmdAction.Get:
                break;

            case GenericCmdAction.Drop:
                break;

            case GenericCmdAction.Use:
                if (message.Target[0].Type == IdentityType.Inventory)
                {
                    client.Controller.UseItem(message.Target[0]);

                    // Acknowledge action
                    this.Acknowledge(client.Controller.Character, message);
                }
                else
                {
                    if (Pool.Instance.Contains(message.Target[0]))
                    {
                        // TODO: Call OnUse of the targets controller
                        // Static dynels first
                        IEventHolder temp = null;
                        try
                        {
                            temp =
                                Pool.Instance.GetObject <IEventHolder>(
                                    client.Controller.Character.Playfield.Identity,
                                    message.Target[0]);
                        }
                        catch (Exception)
                        {
                        }
                        if (temp != null)
                        {
                            var entity = temp as IEntity;
                            if (entity != null)
                            {
                                Event ev = temp.Events.FirstOrDefault(x => x.EventType == EventType.OnUse);
                                if (ev != null)
                                {
                                    ev.Perform(client.Controller.Character, entity);
                                    this.Acknowledge(client.Controller.Character, message);
                                }
                                else
                                {
                                    ev = temp.Events.FirstOrDefault(x => x.EventType == EventType.OnTrade);
                                    if (ev != null)
                                    {
                                        ev.Perform(client.Controller.Character, entity);

                                        TemporaryBag tempBag = new TemporaryBag(
                                            client.Controller.Character.Identity,
                                            new Identity()
                                        {
                                            Type     = IdentityType.TempBag,
                                            Instance =
                                                Pool.Instance.GetFreeInstance <TemporaryBag>(
                                                    0,
                                                    IdentityType.TempBag)
                                        },
                                            client.Controller.Character.Identity,
                                            message.Target[0]);
                                        client.Controller.Character.ShoppingBag = tempBag;
                                        TradeMessageHandler.Default.Send(client.Controller.Character, tempBag);
                                        this.Acknowledge(client.Controller.Character, message);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        // Use statel (doors, grid terminals etc)
#if DEBUG
                        string s = string.Format(
                            "Generic Command received:\r\nAction: {0} ({1}){2}Target: {3} {4}",
                            message.Action,
                            (int)message.Action,
                            Environment.NewLine,
                            message.Target[0].Type,
                            message.Target[0].ToString(true));
                        ChatTextMessageHandler.Default.Send(client.Controller.Character, s);
#endif
                        client.Controller.UseStatel(message.Target[0]);
                    }
                }

                break;

            case GenericCmdAction.UseItemOnItem:
                IItem item =
                    Pool.Instance.GetObject <IInventoryPage>(
                        new Identity()
                {
                    Type     = (IdentityType)client.Controller.Character.Identity.Instance,
                    Instance = (int)message.Target[0].Type
                })[message.Target[0].Instance];
                client.Controller.Character.Stats[StatIds.secondaryitemtemplate].Value = item.LowID;
                //client.Controller.Character.Stats[StatIds.secondaryitemtype]
                if (Pool.Instance.Contains(message.Target[1]))
                {
                    StaticDynel temp =
                        Pool.Instance.GetObject <StaticDynel>(
                            client.Controller.Character.Playfield.Identity,
                            message.Target[1]);
                    if (temp != null)
                    {
                        Event ev = temp.Events.FirstOrDefault(x => x.EventType == EventType.OnUseItemOn);
                        if (ev != null)
                        {
                            ev.Perform(client.Controller.Character, temp);
                        }
                    }
                }
                else
                {
                    client.Controller.UseStatel(message.Target[1], EventType.OnUseItemOn);
                }
                break;
            }
        }