/// <summary> /// Generates the leaderboard and user's rank and points needed. /// </summary> /// <returns>The leaderboard and user's rank/points needed as a partial view.</returns> public ActionResult Leaderboard() { var db = new UsersContext(); var profiles = db.UserProfiles; var activeUser = new UserProfile(); var username = User.Identity.Name; //if there is a logged in user if (username != null && username !="") { var user = from p in profiles where p.UserName == username select p; activeUser = user.FirstOrDefault(); } //select top 10 users, ordered by points descending. List<UserProfile> results = (from p in profiles orderby p.Points descending select p).Take(10).ToList(); List<string> leaders = new List<string>(); List<int> points = new List<int>(); //create the leaderboard list foreach (var result in results) { leaders.Add(result.UserName); points.Add(result.Points); } ViewBag.leaders = leaders; ViewBag.points = points; //update user's view of rank and points. if (activeUser != null) { ViewBag.myrank = activeUser.Rank; ViewBag.mypoints = activeUser.Points; string origrank = activeUser.Rank; string currentRank = origrank; int pointsTilNext = 0; int myPoints = activeUser.Points; //calculate points needed to rank up. while (currentRank == origrank) { pointsTilNext++; currentRank = Ranks.GetRank(myPoints + pointsTilNext); } ViewBag.pointstogo = pointsTilNext; } //return as partial view so it can be on many pages. return PartialView("Leaderboard"); }
public SimpleMembershipInitializer() { Database.SetInitializer<UsersContext>(null); try { using (var context = new UsersContext()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", 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> /// Looks up user by username to find the current points and updates their rank. /// </summary> /// <param name="username">The user's username</param> public static void UpdateRank(string username) { var db = new UsersContext(); var profiles = db.UserProfiles; var user = from p in profiles where p.UserName == username select p; var activeUser = user.FirstOrDefault(); string rank = Ranks.GetRank(activeUser.Points); activeUser.Rank = rank; db.SaveChanges(); }
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 (UsersContext db = new UsersContext()) { UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); // Check if user already exists if (user == null) { // Insert name into the profile table db.UserProfiles.Add(new UserProfile { UserName = model.UserName, Rank = "Loser", Points = 0 }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); 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); }