Exemplo n.º 1
0
            public async Task Buy(int index)
            {
                index -= 1;
                if (index < 0)
                {
                    return;
                }
                ShopEntry entry;

                using (var uow = _db.GetDbContext())
                {
                    var config = uow.GuildConfigs.ForId(ctx.Guild.Id, set => set
                                                        .Include(x => x.ShopEntries)
                                                        .ThenInclude(x => x.Items));
                    var entries = new IndexedCollection <ShopEntry>(config.ShopEntries);
                    entry = entries.ElementAtOrDefault(index);
                    uow.SaveChanges();
                }

                if (entry == null)
                {
                    await ReplyErrorLocalizedAsync("shop_item_not_found").ConfigureAwait(false);

                    return;
                }

                if (entry.Type == ShopEntryType.Role)
                {
                    var guser = (IGuildUser)ctx.User;
                    var role  = ctx.Guild.GetRole(entry.RoleId);

                    if (role == null)
                    {
                        await ReplyErrorLocalizedAsync("shop_role_not_found").ConfigureAwait(false);

                        return;
                    }

                    if (await _cs.RemoveAsync(ctx.User.Id, $"Shop purchase - {entry.Type}", entry.Price).ConfigureAwait(false))
                    {
                        try
                        {
                            await guser.AddRoleAsync(role).ConfigureAwait(false);
                        }
                        catch (Exception ex)
                        {
                            _log.Warn(ex);
                            await _cs.AddAsync(ctx.User.Id, $"Shop error refund", entry.Price).ConfigureAwait(false);
                            await ReplyErrorLocalizedAsync("shop_role_purchase_error").ConfigureAwait(false);

                            return;
                        }
                        await _ri.AddAsync(ctx.User.Id, role.Id);

                        var profit = GetProfitAmount(entry.Price);
                        await _cs.AddAsync(entry.AuthorId, $"Shop sell item - {entry.Type}", profit).ConfigureAwait(false);

                        await _cs.AddAsync(ctx.Client.CurrentUser.Id, $"Shop sell item - cut", entry.Price - profit).ConfigureAwait(false);

                        await _lb.AddAsync(ctx.User.Id, LeaderboardType.GamblingSpent, LeaderboardTimeType.AllTime, entry.Price);
                        await ReplyConfirmLocalizedAsync("shop_role_purchase", Format.Bold(role.Name)).ConfigureAwait(false);

                        return;
                    }
                    else
                    {
                        await ReplyErrorLocalizedAsync("not_enough", Bc.BotConfig.CurrencySign).ConfigureAwait(false);

                        return;
                    }
                }
                else if (entry.Type == ShopEntryType.List)
                {
                    if (entry.Items.Count == 0)
                    {
                        await ReplyErrorLocalizedAsync("out_of_stock").ConfigureAwait(false);

                        return;
                    }

                    var item = entry.Items.ToArray()[new NadekoRandom().Next(0, entry.Items.Count)];

                    if (await _cs.RemoveAsync(ctx.User.Id, $"Shop purchase - {entry.Type}", entry.Price).ConfigureAwait(false))
                    {
                        using (var uow = _db.GetDbContext())
                        {
                            var x = uow._context.Set <ShopEntryItem>().Remove(item);
                            uow.SaveChanges();
                        }
                        try
                        {
                            await(await ctx.User.GetOrCreateDMChannelAsync().ConfigureAwait(false))
                            .EmbedAsync(new EmbedBuilder().WithOkColor()
                                        .WithTitle(GetText("shop_purchase", ctx.Guild.Name))
                                        .AddField(efb => efb.WithName(GetText("item")).WithValue(item.Text).WithIsInline(false))
                                        .AddField(efb => efb.WithName(GetText("price")).WithValue(entry.Price.ToString()).WithIsInline(true))
                                        .AddField(efb => efb.WithName(GetText("name")).WithValue(entry.Name).WithIsInline(true)))
                            .ConfigureAwait(false);

                            await _cs.AddAsync(entry.AuthorId,
                                               $"Shop sell item - {entry.Name}",
                                               GetProfitAmount(entry.Price)).ConfigureAwait(false);
                        }
                        catch
                        {
                            await _cs.AddAsync(ctx.User.Id,
                                               $"Shop error refund - {entry.Name}",
                                               entry.Price).ConfigureAwait(false);

                            using (var uow = _db.GetDbContext())
                            {
                                var entries = new IndexedCollection <ShopEntry>(uow.GuildConfigs.ForId(ctx.Guild.Id,
                                                                                                       set => set.Include(x => x.ShopEntries)
                                                                                                       .ThenInclude(x => x.Items)).ShopEntries);
                                entry = entries.ElementAtOrDefault(index);
                                if (entry != null)
                                {
                                    if (entry.Items.Add(item))
                                    {
                                        uow.SaveChanges();
                                    }
                                }
                            }
                            await ReplyErrorLocalizedAsync("shop_buy_error").ConfigureAwait(false);

                            return;
                        }
                        await ReplyConfirmLocalizedAsync("shop_item_purchase").ConfigureAwait(false);
                    }
                    else
                    {
                        await ReplyErrorLocalizedAsync("not_enough", Bc.BotConfig.CurrencySign).ConfigureAwait(false);

                        return;
                    }
                }
            }