public async Task Topup()
    {
        var      amount   = 20;
        var      time     = 1;
        var      user_id  = this.Context.Message.Author.AvatarId;
        long     guild_id = (long)this.Context.Guild.Id;
        DateTime current  = DateTime.Now;

        using (var db = new DatabaseContext())
        {
            db.Database.Initialize(true);
        }
        using (var db = new WalletDbContext())
        {
            var row = db.Wallets.SingleOrDefault(u => u.User_id == user_id && u.Guild_id == guild_id);
            using (var wdb = new WalletInfoDbContext())
            {
                var        info       = wdb.WalletInfos.SingleOrDefaultAsync(w => w.Guid == row.Guid);
                var        user       = Context.User.Mention;
                WalletInfo walletinfo = (from x in wdb.WalletInfos where x.Guid == row.Guid select x).First();
                if (info.Result.Modified_on <= current.AddHours(-time))
                {
                    walletinfo.Points      = walletinfo.Points + amount;
                    walletinfo.TopupAmount = walletinfo.TopupAmount + amount;
                    walletinfo.TopupCount  = walletinfo.TopupCount + 1;
                    walletinfo.Modified_on = current;
                    this.Context.Channel.SendMessageAsync(user + " Topup successfull. Your new wallet balance is : " + walletinfo.Points);
                }
                else
                {
                    walletinfo.TopupFail = walletinfo.TopupFail + 1;
                    TimeSpan span = current.Subtract(info.Result.Modified_on);
                    if (time == 1)
                    {
                        this.Context.Channel.SendMessageAsync(user + "You may top up every hour. It has been " + (int)span.TotalMinutes + " minutes since your last topup.");
                    }
                    else
                    {
                        this.Context.Channel.SendMessageAsync(user + "You may top up every " + time + " hours. It has been " + (int)span.TotalMinutes + " minutes since your last topup.");
                    }
                }
                wdb.SaveChanges();
            }
        }
    }
Esempio n. 2
0
    public async Task Flip()
    {
        int    amount   = 2;
        Random rand     = new Random();
        bool   modified = false;
        bool   win      = false;
        var    user_id  = this.Context.Message.Author.AvatarId;
        long   guild_id = (long)this.Context.Guild.Id;

        using (var db = new DatabaseContext())
        {
            db.Database.Initialize(true);
        }

        if (this.ValidateUser(user_id, guild_id))
        {
            using (var db = new WalletDbContext())
            {
                var row = db.Wallets.SingleOrDefault(u => u.User_id == user_id && u.Guild_id == guild_id);
                using (var wdb = new WalletInfoDbContext())
                {
                    var info = wdb.WalletInfos.SingleOrDefault(w => w.Guid == row.Guid);
                    var user = Context.User.Mention;


                    WalletInfo walletinfo = (from x in wdb.WalletInfos where x.Guid == row.Guid select x).First();
                    if (rand.Next(0, 2) == 0)
                    {
                        await this.Context.Channel.SendMessageAsync("Heads!");

                        walletinfo.Points = walletinfo.Points + amount;
                        walletinfo.Won    = walletinfo.Won + amount;
                        modified          = true;
                        win = true;
                    }
                    else
                    {
                        if (walletinfo.Points - amount > 0)
                        {
                            await this.Context.Channel.SendMessageAsync("Tails!");

                            walletinfo.Points = walletinfo.Points - amount;
                            walletinfo.Lost   = walletinfo.Lost + amount;
                            modified          = true;
                            win = false;
                        }
                        else
                        {
                            this.Context.Channel.SendMessageAsync(user + "You may not bet more than is avaiable in your wallet. Amount to bet: " + amount + " Amount in Wallet: " + walletinfo.Points);
                        }
                    }
                    if (modified)
                    {
                        wdb.SaveChanges();
                        if (win)
                        {
                            this.Context.Channel.SendMessageAsync(user + "You Win! Your new wallet balance is : " + walletinfo.Points);
                        }
                        else
                        {
                            this.Context.Channel.SendMessageAsync(user + "You Lose. Your new wallet balance is : " + walletinfo.Points);
                        }
                    }
                }
            }
        }
        else
        {
            this.Context.Channel.SendMessageAsync("You must !register for an account before using this command. (Type !help for help)");
        }
    }
    public async Task Register()
    {
        var  user_id  = this.Context.Message.Author.AvatarId;
        long guild_id = (long)this.Context.Guild.Id;

        using (var db = new DatabaseContext())
        {
            db.Database.Initialize(true);
        }
        using (var db = new UserDbContext())
        {
            var user = new User
            {
                User_id    = user_id,
                Guild_id   = guild_id,
                Status     = 1,
                Created_on = DateTime.Now
            };
            db.Database.CreateIfNotExists();

            var row = db.Users.Count(u => u.User_id == user_id && u.Guild_id == guild_id);
            if (row > 0)
            {
                this.Context.Channel.SendMessageAsync("User Already Exists On This Server");
            }
            else
            {
                db.Users.Add(user);
                db.SaveChanges();
                this.Context.Channel.SendMessageAsync("User successfully saved");
            }
        }
        using (var db = new WalletDbContext())
        {
            var wallet = new Wallet
            {
                Guid     = Guid.NewGuid(),
                User_id  = user_id,
                Guild_id = guild_id
            };

            var row = db.Wallets.Count(w => w.User_id == user_id && w.Guild_id == guild_id);
            if (row > 0)
            {
                this.Context.Channel.SendMessageAsync("Wallet Already Exists For This User On This Server");
            }
            else
            {
                db.Wallets.Add(wallet);
                db.SaveChanges();
                this.Context.Channel.SendMessageAsync("User Wallet successfully saved");
                using (var wdb = new WalletInfoDbContext())
                {
                    var walletInfo = new WalletInfo()
                    {
                        Guid        = wallet.Guid,
                        Created_on  = DateTime.Now,
                        Modified_on = DateTime.Now
                    };
                    wdb.Database.CreateIfNotExists();
                    wdb.WalletInfos.Add(walletInfo);
                    wdb.SaveChanges();
                    this.Context.Channel.SendMessageAsync("User Wallet Info successfully saved");
                }
            }
        }
    }