Пример #1
0
            public async Task FinishBet([Summary("Winning Bet Index")] int winning_index)
            {
                using (var db = new NeoContext()) {
                    if (db.NeoBet.Any(x => x.ChannelId == Context.Channel.Id))   //if bet
                    {
                        var           bet     = db.NeoBet.FirstOrDefault(x => x.ChannelId == Context.Channel.Id);
                        var           realbet = bet.Bets.ToArray()[winning_index - 1];
                        StringBuilder sb      = new StringBuilder();
                        StringBuilder sbloser = new StringBuilder();
                        foreach (var userBet in bet.userBets.Where(b => b.BetLoc == winning_index))
                        {
                            var newcash = (int)(userBet.BetAmount * realbet.BetRate);
                            userBet.User.Cash += newcash;
                            db.Users.Update(userBet.User);
                            sb.Append($"{Context.Guild.GetUser(userBet.User.Id).Username} : {newcash}₺\n");
                        } // give everybody their winnings

                        foreach (var loser in bet.userBets.Where(b => b.BetLoc != winning_index))
                        {
                            sbloser.Append($"{Context.Guild.GetUser(loser.User.Id).Username} : {loser.BetAmount}₺\n");
                            if (loser.User.Cash < 500)
                            {
                                loser.User.Cash = 500;
                            }
                            db.Users.Update(loser.User);
                        }
                        //lets remove the bet and update users

                        foreach (var i in bet.userBets)
                        {
                            db.Remove(i);
                        }
                        foreach (var i in bet.Bets)
                        {
                            db.Remove(i);
                        }
                        db.Attach(bet);
                        db.Remove(bet);
                        db.SaveChanges();
                        var winners = sb.ToString();
                        var losers  = sbloser.ToString();
                        var embed   = NeoEmbeds.Minimal($"**Winners**:\n {(!string.IsNullOrEmpty(winners) ? winners : "No one wins")}\n **Losers**:\n {(!string.IsNullOrEmpty(losers) ? losers : "No losers")}");
                        await ReplyAsync("", false, embed.Build());
                    }
                }
            }