Example #1
0
        /// <summary>
        /// Handles the contents of a network message.
        /// </summary>
        /// <param name="message">The message to handle.</param>
        /// <param name="connection">A reference to the connection from where this message is comming from, for context.</param>
        /// <returns>A value tuple with a value indicating whether the handler intends to respond, and a collection of <see cref="IOutgoingPacket"/>s that compose that response.</returns>
        public override (bool IntendsToRespond, IEnumerable <IOutgoingPacket> ResponsePackets) HandleRequest(INetworkMessage message, IConnection connection)
        {
            var lookAtInfo = message.ReadLookAtInfo();

            IThing thing = null;

            var responsePackets = new List <IOutgoingPacket>();

            if (!(this.CreatureFinder.FindCreatureById(connection.PlayerId) is IPlayer player))
            {
                return(false, null);
            }

            this.Logger.Debug($"Player {player.Name} looking at thing with id: {lookAtInfo.ThingId}. {lookAtInfo.Location}");

            if (lookAtInfo.Location.Type != LocationType.Map || player.CanSee(lookAtInfo.Location))
            {
                IContainerItem container;

                // Get thing at location
                switch (lookAtInfo.Location.Type)
                {
                case LocationType.Map:
                    thing = this.TileAccessor.GetTileAt(lookAtInfo.Location, out ITile targetTile) ? targetTile.GetTopThingByOrder(this.CreatureFinder, lookAtInfo.StackPosition) : null;
                    break;

                case LocationType.InsideContainer:
                    container = player.GetContainerById(lookAtInfo.Location.ContainerId);

                    thing = container?[lookAtInfo.Location.ContainerIndex];
                    break;

                case LocationType.InventorySlot:
                    container = player.Inventory[lookAtInfo.Location.Slot] as IContainerItem;

                    thing = container?.Content.FirstOrDefault();
                    break;
                }

                if (thing != null)
                {
                    responsePackets.Add(new TextMessagePacket(MessageType.DescriptionGreen, $"You see {thing.InspectionText}."));
                }
            }

            return(responsePackets.Any(), responsePackets);
        }