コード例 #1
0
        public async Task Bet([Summary("Amount of points to bet")] int bettingPoints, [Summary("Side of coin picked.")] string coinSide)
        {
            var  user   = ((SocketGuildUser)Context.Message.Author);
            var  userId = user.Id;
            bool win    = false;
            var  embed  = new EmbedBuilder();

            embed.WithTitle("Coin Toss");
            UserInfo userInfo = ObjectUtils.GetUserInformation(user.Id);

            if (userInfo != null)
            {
                var currentPoints = userInfo.points;
                if (bettingPoints > currentPoints)
                {
                    await ReplyAsync($"{user} has {currentPoints} points! You cannot bet {bettingPoints}!");
                }
                else if (bettingPoints < 50)
                {
                    await ReplyAsync($"Minimum bet is 50 points");
                }
                else if (!(Util.StringEquals("heads", coinSide) || Util.StringEquals("tails", coinSide)))
                {
                    await ReplyAsync($"Coin sides are ***heads*** or ***tails***.");
                }
                else
                {
                    Random rand   = new Random();
                    var    num    = rand.Next(0, 2);
                    string result = null;
                    if (num == 0)
                    {
                        result = "heads";
                        embed.WithThumbnailUrl("https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/2006_Quarter_Proof.png/244px-2006_Quarter_Proof.png");
                    }
                    else
                    {
                        result = "tails";
                        embed.WithThumbnailUrl("https://mbtskoudsalg.com/images/quarter-transparent-tail-1.png");
                    }

                    if (Util.StringEquals(result, coinSide))
                    {
                        win = true;
                        DatabaseUtils.DecrementDocument(userId, "loseCoinflipStreak", userInfo.loseCoinflipStreak);
                        DatabaseUtils.IncrementDocument(userId, "winCoinflipStreak", 1);
                        DatabaseUtils.IncrementDocument(userId, "points", bettingPoints);
                        embed.WithColor(Color.Green);
                        embed.AddField("Result", "Winner", true);
                        embed.AddField("Coin side", result, true);
                        embed.AddField("Winning streak", userInfo.winCoinflipStreak + 1, true);
                        embed.AddField("Total points", currentPoints + bettingPoints, true);
                    }
                    else
                    {
                        DatabaseUtils.DecrementDocument(userId, "winCoinflipStreak", userInfo.winCoinflipStreak);
                        DatabaseUtils.IncrementDocument(userId, "loseCoinflipStreak", 1);
                        DatabaseUtils.DecrementDocument(userId, "points", bettingPoints);
                        embed.WithColor(Color.Red);
                        embed.AddField("Result", "Loser", true);
                        embed.AddField("Coin side", result, true);
                        embed.AddField("Losing streak", userInfo.loseCoinflipStreak + 1, true);
                        embed.AddField("Total points", currentPoints - bettingPoints, true);
                    }
                    await ReplyAsync("", embed : embed.Build());

                    await RoleUtils.CoinflipRoles(user, bettingPoints, win, Context.Message.Channel.Id);
                }
            }
        }