Exemple #1
0
        private static void OpenContainer(User.User player, List <string> commands)
        {
            //this is a quick work around for knowing which container to open without implementing the dot operator
            //I need to come back and make it work like with NPCS once I've tested everything works correctly
            string location;

            if (string.Equals(commands[commands.Count - 1], "inventory", StringComparison.InvariantCultureIgnoreCase))
            {
                location = null;
                commands.RemoveAt(commands.Count - 1); //get rid of "inventory" se we can parse an index specifier if there is one
            }
            else
            {
                location = player.Player.Location;
            }

            string itemNameToGet = Items.Items.ParseItemName(commands);
            bool   itemFound     = false;

            int itemPosition = 1;

            string[] position = commands[commands.Count - 1].Split('.'); //we are separating based on using the decimal operator after the name of the npc/item
            if (position.Count() > 1)
            {
                int.TryParse(position[position.Count() - 1], out itemPosition);
            }

            int  index = 1;
            Room room  = Room.GetRoom(location);

            if (!string.IsNullOrEmpty(location))  //player didn't specify it was in his inventory check room first
            {
                foreach (string itemID in room.GetObjectsInRoom(Room.RoomObjects.Items))
                {
                    Items.Iitem inventoryItem = Items.Items.GetByID(itemID);
                    inventoryItem = KeepOpening(itemNameToGet, inventoryItem, itemPosition);

                    if (string.Equals(inventoryItem.Name, itemNameToGet, StringComparison.InvariantCultureIgnoreCase))
                    {
                        if (index == itemPosition)
                        {
                            Items.Icontainer container = inventoryItem as Items.Icontainer;
                            player.MessageHandler(container.Open());
                            itemFound = true;
                            break;
                        }
                        else
                        {
                            index++;
                        }
                    }
                }
            }


            if (!itemFound)   //so we didn't find one in the room that matches
            {
                var playerInventory = player.Player.Inventory.GetInventoryAsItemList();
                foreach (Items.Iitem inventoryItem in playerInventory)
                {
                    if (string.Equals(inventoryItem.Name, itemNameToGet, StringComparison.InvariantCultureIgnoreCase))
                    {
                        //if player didn't specify an index number loop through all items until we find the want we want otherwise we will
                        // keep going through each item that matches until we hit the index number
                        if (index == itemPosition)
                        {
                            Items.Icontainer container = inventoryItem as Items.Icontainer;
                            player.MessageHandler(container.Open());
                            room.InformPlayersInRoom(player.Player.FirstName + " opens " + inventoryItem.Name.ToLower(), new List <string>(new string[] { player.UserID }));
                            itemFound = true;
                            break;
                        }
                        else
                        {
                            index++;
                        }
                    }
                }
            }

            if (!itemFound)
            {
                player.MessageHandler("Open what?");
            }
        }