Exemplo n.º 1
0
        public async Task LotteryAsync(EventContext e)
        {
            ArgObject arg = e.Arguments.FirstOrDefault();

            if (arg == null)
            {
                long totalTickets = await(Global.RedisClient as StackExchangeCacheClient).Client.GetDatabase(0).ListLengthAsync(lotteryKey);
                long yourTickets  = 0;

                string latestWinner = (Global.RedisClient as StackExchangeCacheClient).Client.GetDatabase(0).StringGet("lottery:winner");

                if (await lotteryDict.ContainsAsync(e.Author.Id))
                {
                    yourTickets = long.Parse(await lotteryDict.GetAsync(e.Author.Id));
                }

                string timeLeft = taskScheduler?.GetInstance(0, lotteryId).TimeLeft.ToTimeString(e.Channel.Id, true) ?? "1h?m?s - will be fixed soon!";

                new EmbedBuilder()
                {
                    Title       = "🍀 Lottery",
                    Description = "Make the biggest gamble, and get paid off massively if legit.",
                    Color       = new Color(119, 178, 85)
                }.AddInlineField("Tickets Owned", yourTickets)
                .AddInlineField("Drawing In", timeLeft)
                .AddInlineField("Total Tickets", totalTickets)
                .AddInlineField("Ticket price", $"{100} mekos")
                .AddInlineField("Latest Winner", latestWinner ?? "no name")
                .AddInlineField("How to buy?", ">lottery buy [amount]")
                .ToEmbed().QueueToChannel(e.Channel);
                return;
            }

            switch (arg.Argument.ToLower())
            {
            case "buy":
            {
                arg = arg.Next();
                int amount = arg?.AsInt() ?? 1;

                if (amount < 1)
                {
                    amount = 1;
                }

                using (var context = new MikiContext())
                {
                    User u = await User.GetAsync(context, e.Author);

                    if (amount * 100 > u.Currency)
                    {
                        e.ErrorEmbedResource("miki_mekos_insufficient")
                        .ToEmbed().QueueToChannel(e.Channel);
                        return;
                    }

                    await u.AddCurrencyAsync(-amount * 100, e.Channel);

                    RedisValue[] tickets = new RedisValue[amount];

                    for (int i = 0; i < amount; i++)
                    {
                        tickets[i] = e.Author.Id.ToString();
                    }

                    await(Global.RedisClient as StackExchangeCacheClient).Client.GetDatabase(0).ListRightPushAsync(lotteryKey, tickets);

                    int totalTickets = 0;

                    if (await lotteryDict.ContainsAsync(e.Author.Id.ToString()))
                    {
                        totalTickets = int.Parse(await lotteryDict.GetAsync(e.Author.Id.ToString()));
                    }

                    await lotteryDict.AddAsync(e.Author.Id, amount + totalTickets);

                    await context.SaveChangesAsync();

                    Utils.SuccessEmbed(e.Channel.Id, $"Successfully bought {amount} tickets!")
                    .QueueToChannel(e.Channel);
                }
            }
            break;
            }
        }