Exemple #1
0
 /// <summary>
 /// Get the list of Elligible sellers to display in Market.
 /// </summary>
 /// <param name="db"></param>
 /// <returns></returns>
 public static List <User> GetElligiblesSellers(SmartTreeEntities db)
 {
     return(db.Users
            .Where(u => u.MultiCoins > 0 && u.Childs.Count < Constants.MAX_CHILDS && u.FatherId != null)
            .OrderBy(u => u.Rank)
            .ToList());
 }
 public MarketViewModel()
 {
     using (SmartTreeEntities db = new SmartTreeEntities())
     {
         List<User> sellers = User.GetElligiblesSellers(db);
         Sellers = sellers;
     }
 }
 //
 // GET: /Profile/
 public ActionResult Index(int userId = -1)
 {
     using (SmartTreeEntities db = new SmartTreeEntities())
     {
         var user = User.GetUserFromDb(db);
         userId = userId == -1 ? user.UserId : userId;
         ProfileViewModel model =  new ProfileViewModel(userId);
         return View(model);
     }
 }
        public ActionResult Deposit()
        {
            using (SmartTreeEntities db = new SmartTreeEntities())
            {
                var user = User.GetUserFromDb(db);
                user.Balance += Constants.MULTI_COIN_PRICE;
                try { db.SaveChanges(); }
                catch (DbUpdateConcurrencyException e){ Deposit(); }

            }
            return RedirectToAction("Index", "Profile");
        }
 /// <summary>
 /// finds db user from controller user
 /// </summary>
 /// <param name="user"></param>
 /// <param name="db"></param>
 /// <returns></returns>
 public static User GetUserFromDb(this System.Security.Principal.IPrincipal user, SmartTreeEntities db)
 {
     try
     {
         return user == null || String.IsNullOrEmpty(user.Identity.Name) ?
             null
             : db.Users.SingleOrDefault(u => u.Email == user.Identity.Name);
     }
     catch
     {
         return null;
     }
 }
 public ActionResult Call()
 {
     using (SmartTreeEntities db = new SmartTreeEntities())
     {
         var user = User.GetUserFromDb(db);
         if (user.FatherId != null)
         {
             if (!call(user))
             {
                 //TODO
             }
         }
     }
     return RedirectToAction("Index", "Profile");
 }
        public ProfileViewModel(int userId)
        {
            using (SmartTreeEntities db = new SmartTreeEntities())
            {
                var user = db.Users.Find(userId);

                Email = user.Email;
                Id = user.UserId;
                Balance = user.Balance;
                ReservedPart = user.ReservedPartOfBalance;
                MultiCoins = user.MultiCoins;
                Seller = user.Father;
                Buyers = user.Childs;
                EmitedCalls = user.EmitedCalls;
                ReceivedCalls = user.ReceivedCalls;
            }
        }
 public ActionResult Buy(int userId)
 {
     using (SmartTreeEntities db = new SmartTreeEntities())
     {
         var seller = db.Users.Find(userId);
         var user = User.GetUserFromDb(db);
         if ((seller != null && user.Father == null)
             || (user.FatherId == seller.UserId))
         {
             if (!buy(user, seller))
             {
                 return RedirectToAction("Index", "Market");
             }
         }
     }
     return RedirectToAction("Index","Profile");
 }
        public bool AddMultiCoins(int number)
        {
            using (SmartTreeEntities db = new SmartTreeEntities())
            {
                var user = User.GetUserFromDb(db);
                user.MultiCoins += number;
                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException e)
                {
                    return AddMultiCoins(number);
                }
                MarketController.re_sell(user);
            }

            return true;
        }
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<SmartTreeEntities>(null);

                try
                {
                    using (var context = new SmartTreeEntities())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }

                    WebSecurity.InitializeDatabaseConnection("SmartTreeEntities", "Users", "UserId", "Email", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
        /// <summary>
        /// Automaticly re-sell to the maximum number of Multicoins
        /// to childs who made reservations.
        /// </summary>
        public static void re_sell(User _me)
        {
            if (_me.ReceivedCalls > 0)
            {
                using (SmartTreeEntities db = new SmartTreeEntities())
                {
                    _me = db.Users.Find(_me.UserId);

                    while (_me.MultiCoins != 0 && _me.ReceivedCalls != 0)
                    {
                        foreach (User child in _me.Childs.Where(u => u.EmitedCalls > 0).ToList())
                        {
                            var amounth = Math.Min(child.EmitedCalls, _me.MultiCoins);
                            if (!buy(child, child.Father, amounth))
                                break;
                            _me.MultiCoins -= amounth;
                        }
                        _me = db.Users.Find(_me.UserId);
                    }
                }
            }
        }
Exemple #12
0
 /// <summary>
 /// Get the list of Elligible sellers to display in Market.
 /// </summary>
 /// <param name="db"></param>
 /// <returns></returns>
 public static List<User> GetElligiblesSellers(SmartTreeEntities db)
 {
     return db.Users
         .Where(u => u.MultiCoins > 0 && u.Childs.Count < Constants.MAX_CHILDS && u.FatherId != null)
         .OrderBy(u => u.Rank)
         .ToList();
 }
 //
 // GET: /Market/
 public ActionResult Index()
 {
     using (SmartTreeEntities db = new SmartTreeEntities())
     {
         var user = User.GetUserFromDb(db);
         if (user == null || user.Father != null)
             return RedirectToAction("Index", "Profile");
     }
     MarketViewModel model = new MarketViewModel();
     return View(model);
 }
        /// <summary>
        /// Call the father
        /// </summary>
        /// <param name="amounth"></param>
        /// <returns></returns>
        private static Boolean call(User _me, int amounth = 1)
        {
            //Check if the user has enough money to call
            if (_me.Balance - _me.ReservedPartOfBalance >= Constants.MULTI_COIN_PRICE * amounth)
            {
                // if the amounth is 0 or less, do nothing and return true
                if (amounth > 0)
                {
                    //check if the father has some Multicoins to sell before calling him
                    if (_me.Father.MultiCoins != 0)
                    {
                        // buy the available multicoins.
                        var amounthBuy = Math.Min(_me.Father.MultiCoins, amounth);
                        if (buy(_me, _me.Father, amounthBuy))
                        {
                            // Call for the difference
                            return call(_me, amounth - amounthBuy);
                        }
                        // If buy failed recall call function
                        return call(_me, amounth);
                    }

                    // If there is no available multicoins, proceed call
                    using (SmartTreeEntities db = new SmartTreeEntities())
                    {
                        var user = db.Users.Find(_me.UserId);
                        var father = db.Users.Find(_me.FatherId);

                        user.EmitedCalls += amounth;
                        user.ReservedPartOfBalance += Constants.MULTI_COIN_PRICE * amounth;
                        father.ReceivedCalls += amounth;
                        try{ db.SaveChanges(); }
                        catch (DbUpdateConcurrencyException e) { return call(_me, amounth); }
                    }
                }
                return true;
            }
            return false;
        }
        /// <summary>
        /// Buy to an other user
        /// </summary>
        /// <param name="_other"> The other User we want to buy to</param>
        /// <param name="amounth"> The amounth of multicoins we want to buy</param>
        /// <returns></returns>
        private static Boolean buy(User _me, User _other, int amounth = 1)
        {
            // First check if _other is the father of the user or
            // if user has no father, an elligible seller
            if (_me.Balance >= Constants.MULTI_COIN_PRICE * amounth
                && _other.MultiCoins >= amounth
                && (_me.Father == _other
                || (_me.Father == null && _other.isElligibleSeller())))
            {
                // Proceed the sell
                using (SmartTreeEntities db = new SmartTreeEntities())
                {
                    var other = db.Users.Find(_other.UserId);
                    var me = db.Users.Find(_me.UserId);

                    //Update seller infos
                    other.MultiCoins -= amounth;
                    sell(other, amounth);

                    //Update user infos
                    me.Balance -= Constants.MULTI_COIN_PRICE * amounth;
                    me.MultiCoins += amounth * 5;
                    me.untaxed += amounth;

                    // update infos about calls
                    var nbCallToCancel = Math.Min(_me.EmitedCalls, amounth);
                    me.EmitedCalls -= nbCallToCancel;
                    me.ReservedPartOfBalance -= Constants.MULTI_COIN_PRICE * nbCallToCancel;
                    other.ReceivedCalls -= nbCallToCancel;

                    // bind seller & buyer if it is not the case yet
                    if (me.Father == null)
                    {
                        me.Father = other;
                        other.Childs.Add(me);
                        me.Rank = other.Rank + 1;
                    }
                    try { db.SaveChanges(); }
                    catch (DbUpdateConcurrencyException e) { return buy(_me, _other, amounth); }
                }
                re_sell(_me);
                return true;
            }
            return false;
        }
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (SmartTreeEntities db = new SmartTreeEntities())
                {
                    User user = db.Users.FirstOrDefault(u => u.Email.ToLower() == model.Email.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.Users.Add(new User { Email = model.Email });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.Email);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }