public async Task Redeem()
        {
            RedemptionStoreProductModel product = this.Product;
            UserViewModel user = this.User;

            if (product != null && user != null)
            {
                CommandModelBase command = product.Command;
                if (command == null)
                {
                    command = ChannelSession.Settings.GetCommand(ChannelSession.Settings.RedemptionStoreDefaultRedemptionCommandID);
                }

                if (command != null)
                {
                    Dictionary <string, string> specialIdentifiers = new Dictionary <string, string>();
                    specialIdentifiers[RedemptionStoreProductModel.ProductNameSpecialIdentifier] = product.Name;

                    await ChannelSession.Services.Command.Queue(command, new CommandParametersModel(user, specialIdentifiers : specialIdentifiers));
                }

                if (this.State == RedemptionStorePurchaseRedemptionState.ManualRedeemNeeded)
                {
                    this.State = RedemptionStorePurchaseRedemptionState.ManualRedeemPerformed;
                }
                else
                {
                    this.State = RedemptionStorePurchaseRedemptionState.AutoRedeemed;
                }
            }
            GlobalEvents.RedemptionStorePurchasesUpdated();
        }
        public async Task Redeem()
        {
            RedemptionStoreProductModel product = this.Product;
            UserViewModel user = this.User;

            if (product != null && user != null)
            {
                CustomCommand command = product.Command;
                if (command == null)
                {
                    command = ChannelSession.Settings.GetCustomCommand(ChannelSession.Settings.RedemptionStoreDefaultRedemptionCommandID);
                }

                if (command != null)
                {
                    Dictionary <string, string> extraSpecialIdentifiers = new Dictionary <string, string>();
                    extraSpecialIdentifiers[RedemptionStoreProductModel.ProductNameSpecialIdentifier] = product.Name;

                    await command.Perform(user, extraSpecialIdentifiers : extraSpecialIdentifiers);
                }

                if (this.State == RedemptionStorePurchaseRedemptionState.ManualRedeemNeeded)
                {
                    this.State = RedemptionStorePurchaseRedemptionState.ManualRedeemPerformed;
                }
                else
                {
                    this.State = RedemptionStorePurchaseRedemptionState.AutoRedeemed;
                }
            }
            GlobalEvents.RedemptionStorePurchasesUpdated();
        }
 public RedemptionStorePurchaseModel(RedemptionStoreProductModel product, UserViewModel user)
 {
     this.ID           = Guid.NewGuid();
     this.ProductID    = product.ID;
     this.UserID       = user.ID;
     this.PurchaseDate = DateTimeOffset.Now;
 }
        public static async Task Redeem(UserViewModel user, IEnumerable <string> arguments)
        {
            if (!user.HasPermissionsTo(UserRoleEnum.Mod))
            {
                if (ChannelSession.Services.Chat != null)
                {
                    await ChannelSession.Services.Chat.SendMessage(MixItUp.Base.Resources.YouDoNotHavePermissions);
                }
                return;
            }

            string name = string.Join(" ", arguments);
            RedemptionStorePurchaseModel purchase = null;

            RedemptionStoreProductModel product = ChannelSession.Settings.RedemptionStoreProducts.Values.ToList().FirstOrDefault(p => p.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase));

            if (product != null)
            {
                IEnumerable <RedemptionStorePurchaseModel> purchases = ChannelSession.Settings.RedemptionStorePurchases.ToList().Where(p => p.ProductID == product.ID);
                if (purchases != null && purchases.Count() > 0)
                {
                    purchase = purchases.OrderBy(p => p.PurchaseDate).FirstOrDefault();
                }
            }
            else
            {
                name = name.Replace("@", "");
                UserViewModel purchaseUser = ChannelSession.Services.User.GetActiveUserByUsername(name, user.Platform);
                if (purchaseUser != null)
                {
                    IEnumerable <RedemptionStorePurchaseModel> purchases = ChannelSession.Settings.RedemptionStorePurchases.ToList().Where(p => p.UserID == user.ID);
                    if (purchases != null && purchases.Count() > 0)
                    {
                        purchase = purchases.OrderBy(p => p.PurchaseDate).FirstOrDefault();
                    }
                }
            }

            if (purchase != null)
            {
                await purchase.Redeem();
            }
            else
            {
                if (ChannelSession.Services.Chat != null)
                {
                    await ChannelSession.Services.Chat.SendMessage(MixItUp.Base.Resources.NoRedemptionStorePurchasesWithThatName);
                }
            }
        }
        public async Task Refund()
        {
            RedemptionStoreProductModel product = this.Product;
            UserViewModel user = this.User;

            if (product != null && user != null)
            {
                await product.Requirements.Refund(new CommandParametersModel(user));

                if (!product.IsInfinite)
                {
                    product.CurrentAmount++;
                }
            }
            this.Remove();
        }
        public static async Task Purchase(UserViewModel user, IEnumerable <string> arguments)
        {
            if (arguments.Count() == 0)
            {
                List <string> items = new List <string>();
                foreach (RedemptionStoreProductModel product in ChannelSession.Settings.RedemptionStoreProducts.Values.ToList())
                {
                    if (product.IsInfinite || product.CurrentAmount > 0)
                    {
                        items.Add(product.Name);
                    }
                }
                await ChannelSession.Services.Chat.SendMessage("Products Available to Purchase: " + string.Join(", ", items), platform : user.Platform);
            }
            else
            {
                string name = string.Join(" ", arguments);
                RedemptionStoreProductModel product = ChannelSession.Settings.RedemptionStoreProducts.Values.ToList().FirstOrDefault(p => p.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase));
                if (product == null)
                {
                    if (ChannelSession.Services.Chat != null)
                    {
                        await ChannelSession.Services.Chat.SendMessage(MixItUp.Base.Resources.NoRedemptionStoreProductWithThatName);
                    }
                    return;
                }

                if (!product.IsInfinite)
                {
                    if (product.CurrentAmount <= 0)
                    {
                        if (ChannelSession.Services.Chat != null)
                        {
                            await ChannelSession.Services.Chat.SendMessage(MixItUp.Base.Resources.NoMoreRedemptionStoreProducts);
                        }
                        return;
                    }

                    ThresholdRequirementModel threshold = product.Requirements.Threshold;
                    if (threshold != null && threshold.IsEnabled && threshold.Amount > product.CurrentAmount)
                    {
                        if (ChannelSession.Services.Chat != null)
                        {
                            await ChannelSession.Services.Chat.SendMessage(MixItUp.Base.Resources.NotEnoughRedemptionStoreProducts);
                        }
                        return;
                    }
                }

                Result result = await product.Requirements.Validate(new CommandParametersModel(user, arguments));

                if (result.Success)
                {
                    await product.Requirements.Perform(new CommandParametersModel(user, arguments));

                    foreach (CommandParametersModel u in product.Requirements.GetPerformingUsers(new CommandParametersModel(user, arguments)))
                    {
                        if (!product.IsInfinite)
                        {
                            product.CurrentAmount--;
                        }

                        RedemptionStorePurchaseModel purchase = new RedemptionStorePurchaseModel(product, u.User);
                        ChannelSession.Settings.RedemptionStorePurchases.Add(purchase);

                        if (product.AutoRedeem)
                        {
                            await purchase.Redeem();
                        }
                        else
                        {
                            purchase.State = RedemptionStorePurchaseRedemptionState.ManualRedeemNeeded;
                            u.SpecialIdentifiers[RedemptionStoreProductModel.ProductNameSpecialIdentifier] = product.Name;

                            CommandModelBase command = ChannelSession.Settings.GetCommand(ChannelSession.Settings.RedemptionStoreManualRedeemNeededCommandID);
                            if (command != null)
                            {
                                await ChannelSession.Services.Command.Queue(command, u);
                            }

                            GlobalEvents.RedemptionStorePurchasesUpdated();
                        }
                    }
                }
            }
        }