public void SetInventoryAmount(UserInventoryViewModel inventory, string itemName, int amount)
 {
     if (inventory.Items.ContainsKey(itemName))
     {
         UserInventoryItemViewModel item          = inventory.Items[itemName];
         UserInventoryDataViewModel inventoryData = this.GetInventory(inventory);
         inventoryData.Amounts[itemName] = Math.Min(amount, item.HasMaxAmount ? item.MaxAmount : inventoryData.Inventory.DefaultMaxAmount);
     }
 }
Exemple #2
0
        public async Task PerformShopCommand(UserViewModel user, IEnumerable <string> arguments = null)
        {
            try
            {
                if (ChannelSession.Chat != null && ChannelSession.Settings.Currencies.ContainsKey(this.ShopCurrencyID))
                {
                    UserCurrencyViewModel currency = ChannelSession.Settings.Currencies[this.ShopCurrencyID];

                    if (arguments != null && arguments.Count() > 0)
                    {
                        string arg1 = arguments.ElementAt(0);
                        if (arguments.Count() == 1 && arg1.Equals("list", StringComparison.InvariantCultureIgnoreCase))
                        {
                            List <string> items = new List <string>();
                            foreach (UserInventoryItemViewModel item in this.Items.Values)
                            {
                                if (item.HasBuyAmount || item.HasSellAmount)
                                {
                                    items.Add(item.Name);
                                }
                            }
                            await ChannelSession.Chat.Whisper(user.UserName, "Items Available to Buy/Sell: " + string.Join(", ", items));

                            return;
                        }
                        else if (arguments.Count() >= 2 &&
                                 (arg1.Equals("buy", StringComparison.InvariantCultureIgnoreCase) || arg1.Equals("sell", StringComparison.InvariantCultureIgnoreCase)))
                        {
                            int amount = 1;

                            IEnumerable <string>       itemArgs = arguments.Skip(1);
                            UserInventoryItemViewModel item     = this.GetItem(string.Join(" ", itemArgs));
                            if (item == null && itemArgs.Count() > 1)
                            {
                                itemArgs = itemArgs.Take(itemArgs.Count() - 1);
                                item     = this.GetItem(string.Join(" ", itemArgs));
                                if (item != null)
                                {
                                    if (!int.TryParse(arguments.Last(), out amount) || amount <= 0)
                                    {
                                        await ChannelSession.Chat.Whisper(user.UserName, "A valid amount greater than 0 must be specified");

                                        return;
                                    }
                                }
                            }

                            if (item == null)
                            {
                                await ChannelSession.Chat.Whisper(user.UserName, "The item you specified does not exist");

                                return;
                            }

                            int           totalcost = 0;
                            CustomCommand command   = null;
                            if (arg1.Equals("buy", StringComparison.InvariantCultureIgnoreCase))
                            {
                                if (item.HasBuyAmount)
                                {
                                    int itemMaxAmount = (item.HasMaxAmount) ? item.MaxAmount : this.DefaultMaxAmount;
                                    if ((user.Data.GetInventoryAmount(this, item.Name) + amount) <= itemMaxAmount)
                                    {
                                        totalcost = item.BuyAmount * amount;
                                        if (user.Data.HasCurrencyAmount(currency, totalcost))
                                        {
                                            user.Data.SubtractCurrencyAmount(currency, totalcost);
                                            user.Data.AddInventoryAmount(this, item.Name, amount);
                                            command = this.ItemsBoughtCommand;
                                        }
                                        else
                                        {
                                            await ChannelSession.Chat.Whisper(user.UserName, string.Format("You do not have the required {0} {1} to purchase this item", totalcost, currency.Name));
                                        }
                                    }
                                    else
                                    {
                                        await ChannelSession.Chat.Whisper(user.UserName, string.Format("You can only have {0} {1} in total", itemMaxAmount, item.Name));
                                    }
                                }
                                else
                                {
                                    await ChannelSession.Chat.Whisper(user.UserName, "This item is not available for buying");
                                }
                            }
                            else if (arg1.Equals("sell", StringComparison.InvariantCultureIgnoreCase))
                            {
                                if (item.HasSellAmount)
                                {
                                    totalcost = item.SellAmount * amount;
                                    if (user.Data.HasInventoryAmount(this, item.Name, amount))
                                    {
                                        user.Data.SubtractInventoryAmount(this, item.Name, amount);
                                        user.Data.AddCurrencyAmount(currency, totalcost);
                                        command = this.ItemsSoldCommand;
                                    }
                                    else
                                    {
                                        await ChannelSession.Chat.Whisper(user.UserName, string.Format("You do not have the required {0} {1} to sell", amount, item.Name));
                                    }
                                }
                                else
                                {
                                    await ChannelSession.Chat.Whisper(user.UserName, "This item is not available for selling");
                                }
                            }
                            else
                            {
                                await ChannelSession.Chat.Whisper(user.UserName, "You must specify either \"buy\" & \"sell\"");
                            }

                            if (command != null)
                            {
                                Dictionary <string, string> specialIdentifiers = new Dictionary <string, string>();
                                specialIdentifiers["itemtotal"]    = amount.ToString();
                                specialIdentifiers["itemname"]     = item.Name;
                                specialIdentifiers["itemcost"]     = totalcost.ToString();
                                specialIdentifiers["currencyname"] = currency.Name;
                                await command.Perform(user, arguments : arguments, extraSpecialIdentifiers : specialIdentifiers);
                            }
                            return;
                        }
                        else
                        {
                            UserInventoryItemViewModel item = this.GetItem(string.Join(" ", arguments));
                            if (item != null)
                            {
                                if (item.HasBuyAmount || item.HasSellAmount)
                                {
                                    StringBuilder itemInfo = new StringBuilder();
                                    itemInfo.Append(item.Name + ": ");
                                    if (item.HasBuyAmount)
                                    {
                                        itemInfo.Append(string.Format("Buy = {0} {1}", item.BuyAmount, currency.Name));
                                    }
                                    if (item.HasBuyAmount && item.HasSellAmount)
                                    {
                                        itemInfo.Append(string.Format(", "));
                                    }
                                    if (item.HasSellAmount)
                                    {
                                        itemInfo.Append(string.Format("Sell = {0} {1}", item.SellAmount, currency.Name));
                                    }

                                    await ChannelSession.Chat.Whisper(user.UserName, itemInfo.ToString());
                                }
                                else
                                {
                                    await ChannelSession.Chat.Whisper(user.UserName, "This item is not available to buy/sell");
                                }
                            }
                            else
                            {
                                await ChannelSession.Chat.Whisper(user.UserName, "The item you specified does not exist");
                            }
                            return;
                        }
                    }

                    StringBuilder storeHelp = new StringBuilder();
                    storeHelp.Append(this.ShopCommand + " list = Lists all the items available for buying/selling ** ");
                    storeHelp.Append(this.ShopCommand + " <ITEM NAME> = Lists the buying/selling price for the item ** ");
                    storeHelp.Append(this.ShopCommand + " buy <ITEM NAME> [AMOUNT] = Buys 1 or the amount specified of the item ** ");
                    storeHelp.Append(this.ShopCommand + " sell <ITEM NAME> [AMOUNT] = Sells 1 or the amount specified of the item");
                    await ChannelSession.Chat.Whisper(user.UserName, storeHelp.ToString());
                }
            }
            catch (Exception ex) { Logger.Log(ex); }
        }
 public int GetAmount(UserInventoryItemViewModel item)
 {
     return(this.GetAmount(item.Name));
 }