Exemplo n.º 1
0
        public GamblingModule()
        {
            if (!Global.Config.IsMainBot)
            {
                lotteryId = taskScheduler.AddTask(0, (s) =>
                {
                    long size = (Global.RedisClient as StackExchangeCacheClient).Client.GetDatabase(0).ListLength(lotteryKey);

                    if (size == 0)
                    {
                        return;
                    }

                    string value = (Global.RedisClient as StackExchangeCacheClient).Client.GetDatabase(0).ListGetByIndex(lotteryKey, MikiRandom.Next(size));

                    ulong winnerId = ulong.Parse(value);
                    int wonAmount  = (int)Math.Round(size * 100 * 0.75);

                    IDiscordUser user = null;                     //Bot.Instance.ChatClient.GetUser(winnerId);

                    using (var context = new MikiContext())
                    {
                        long id          = winnerId.ToDbLong();
                        User profileUser = context.Users.Find(id);

                        if (user != null)
                        {
                            IDiscordChannel channel = user.GetDMChannel().Result;

                            EmbedBuilder embed = new EmbedBuilder()
                            {
                                Author = new EmbedAuthor()
                                {
                                    Name    = "Winner winner chicken dinner",
                                    IconUrl = user.GetAvatarUrl()
                                },
                                Description = $"Wow! You won the lottery and gained {wonAmount} mekos!"
                            };

                            profileUser.AddCurrencyAsync(wonAmount, channel);

                            embed.ToEmbed().QueueToChannel(channel);

                            context.SaveChanges();

                            Global.RedisClient.RemoveAsync(lotteryKey);
                            Global.RedisClient.UpsertAsync("lottery:winner", profileUser.Name ?? "unknown");
                            lotteryDict.ClearAsync();

                            var lotteryAchievement = AchievementManager.Instance.GetContainerById("lottery");

                            if (wonAmount > 100000)
                            {
                                lotteryAchievement.Achievements[0].UnlockAsync(channel, user, 0);
                            }

                            if (wonAmount > 10000000)
                            {
                                lotteryAchievement.Achievements[1].UnlockAsync(channel, user, 1);
                            }

                            if (wonAmount > 250000000)
                            {
                                lotteryAchievement.Achievements[2].UnlockAsync(channel, user, 1);
                            }
                        }
                    }
                }, "", new TimeSpan(0, 1, 0, 0), true);
            }
        }