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 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> /// 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; }
/// <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; }
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); }