public UserPointsModel Get() { var userId = GetUser(); var userPoints = Context.UsersPoints.Where(a => a.User.Id == userId).FirstOrDefault(); if (userId == null || userPoints == null) { return(new UserPointsModel { MaxListeningPoints = PointsLevel.GetMaxLevelPoints(PointsLevel.GetLevel(0)), MaxSpeakingPoints = PointsLevel.GetMaxLevelPoints(PointsLevel.GetLevel(0)), MaxWritingPoints = PointsLevel.GetMaxLevelPoints(PointsLevel.GetLevel(0)), ProgressLevelPercentage = PointsLevel.GetProgress(0), ListeningProgressPercentage = 0, SpeakingProgressPercentage = 0, WritingProgressPercentage = 0 }); } var avgPoints = (int)((userPoints.WritingPoints + userPoints.SpeakingPoints + userPoints.ListeningPoints) / 3); var userLevel = PointsLevel.GetLevel(avgPoints); return(new UserPointsModel { MaxListeningPoints = PointsLevel.GetMaxLevelPoints(PointsLevel.GetLevel(userPoints.ListeningPoints ?? 0)), MaxSpeakingPoints = PointsLevel.GetMaxLevelPoints(PointsLevel.GetLevel(userPoints.SpeakingPoints ?? 0)), MaxWritingPoints = PointsLevel.GetMaxLevelPoints(PointsLevel.GetLevel(userPoints.WritingPoints ?? 0)), UserLevel = (int)userLevel, ProgressLevelPercentage = PointsLevel.GetProgress(avgPoints), ListeningProgressPercentage = PointsLevel.GetProgress(userPoints.ListeningPoints ?? 0), SpeakingProgressPercentage = PointsLevel.GetProgress(userPoints.SpeakingPoints ?? 0), WritingProgressPercentage = PointsLevel.GetProgress(userPoints.WritingPoints ?? 0) }); //userPoints.ProgressLevelPercentage = (int)((avgPoints / (float)PointsLevel.GetMaxLevelPoints(userLevel)) * 100); //userPoints.MaxListeningPoints = PointsLevel.GetMaxLevelPoints(PointsLevel.GetLevel(userPoints.ListeningPoints)); //userPoints.MaxSpeakingPoints = PointsLevel.GetMaxLevelPoints(PointsLevel.GetLevel(userPoints.SpeakingPoints)); //userPoints.MaxWritingPoints = PointsLevel.GetMaxLevelPoints(PointsLevel.GetLevel(userPoints.WritingPoints)); //userPoints.UserLevel = (int)userLevel; //return userPoints; }
public string ChangeUserLevel(int level) { try { string userId = GetUser(); if (userId == null) { return(""); } var userPoints = Context.UsersPoints.Where(a => a.User.Id == userId).FirstOrDefault(); var user = Context.Users.Where(a => a.Id == userId).FirstOrDefault(); if (userPoints == null) { var points = level > 1 ? PointsLevel.GetMaxLevelPoints((Enums.Level)(level - 1)) + 1 : 1; Context.UsersPoints.Add(new UserPoints() { User = user, ListeningPoints = points, WritingPoints = points, SpeakingPoints = points }); Context.SaveChanges(); return("Save the level Successful."); } else { var startPoints = level > 1 ? PointsLevel.GetMaxLevelPoints((Enums.Level)(level - 1)) + 1 : 1; var maxLevelPoints = PointsLevel.GetMaxLevelPoints((Enums.Level)level); userPoints.User = user; userPoints.ListeningPoints = userPoints.ListeningPoints <= maxLevelPoints && userPoints.ListeningPoints >= startPoints ? userPoints.ListeningPoints : startPoints; userPoints.WritingPoints = userPoints.WritingPoints <= maxLevelPoints && userPoints.WritingPoints >= startPoints ? userPoints.WritingPoints : startPoints;; userPoints.SpeakingPoints = userPoints.SpeakingPoints <= maxLevelPoints && userPoints.SpeakingPoints >= startPoints ? userPoints.SpeakingPoints : startPoints;; Context.SaveChanges(); return("Save the level Successful."); } } catch (Exception ex) { return(ex.Message); } }