public async Task <IActionResult> Register(LoginModel model) { if (ModelState.IsValid) { if (model.Password != model.PasswordConfirmation) { ModelState.AddModelError("", "Password and password confirmation field are not the same."); } else { User user = await db.Users.FirstOrDefaultAsync(u => u.Email == model.Email || u.Nickname == model.Nickname); if (user == null) { db.Users.Add(new User { Nickname = model.Nickname, Email = model.Email, Password = HashPassword(model.Password), RegistrationDate = DateTime.Today.Date, UserType = "Player" }); await db.SaveChangesAsync(); db.Profiles.Add(new Profile { AttackAttempts = 0, DefenseAttempts = 0, SeasonId = 1, FractionId = new Random().Next(1, 4), SuccessfulAttacks = 0, SuccessfulDefences = 0, UserId = db.Users.First(x => x.Nickname == model.Nickname).Id }); await db.SaveChangesAsync(); await Authenticate(model.Email); return(RedirectToAction("Index", "Home")); } ModelState.AddModelError("", "User with this email or nickname exists."); } } return(View("Login", model)); }
public async void UpdateMessages() { var scope = Program.host.Services.CreateScope(); db = scope.ServiceProvider.GetService <HexaceContext>(); //db.Database.OpenConnection(); for (int i = 1; i < 4; i++) { foreach (var message in Chats[i]) { if (message.Id == 0) { db.ChatMessages.Add(message); await db.SaveChangesAsync(); } } } }
public async void UpdateStats() { var scope = Program.host.Services.CreateScope(); db = scope.ServiceProvider.GetService <HexaceContext>(); foreach (var item in FractionInfo.Values) { db.Database.OpenConnection(); item.ScoreDatetime = DateTime.Now; db.FractionScores.Add(new FractionScore { AttackAttempts = item.AttackAttempts, DefenseAttempts = item.DefenseAttempts, FractionId = item.FractionId, HexesUnderControl = item.HexesUnderControl, Score = item.Score, SeasonId = item.SeasonId, SuccessfulAttacks = item.SuccessfulAttacks, SuccessfulDefences = item.SuccessfulDefences, ScoreDatetime = DateTime.Now }); } await db.SaveChangesAsync(); }
public static void CheckForAchievements(int userId, HexaceContext context) { var userAchievements = context.UsersAchievements.Where(ua => ua.UserId == userId); var lockedAchievements = new List <Achievement>(); foreach (var item in context.Achievements.ToList()) { var isLocked = true; foreach (var userAchievement in userAchievements) { if (item.Id == userAchievement.Id) { isLocked = false; } } if (isLocked) { lockedAchievements.Add(item); } } foreach (var item in lockedAchievements) { switch (item.AchievementName.TrimEnd()) { case "Attacker I": if (context.Profiles.First(p => p.UserId == userId).SuccessfulAttacks >= 5) { context.UsersAchievements.Add(new UserAchievement { AchievementGotDatetime = DateTime.Now, AchievementId = item.Id, UserId = userId }); } break; case "Attacker II": if (context.Profiles.First(p => p.UserId == userId).SuccessfulAttacks >= 50) { context.UsersAchievements.Add(new UserAchievement { AchievementGotDatetime = DateTime.Now, AchievementId = item.Id, UserId = userId }); } break; case "Attacker III": if (context.Profiles.First(p => p.UserId == userId).SuccessfulAttacks >= 100) { context.UsersAchievements.Add(new UserAchievement { AchievementGotDatetime = DateTime.Now, AchievementId = item.Id, UserId = userId }); } break; case "Defender I": if (context.Profiles.First(p => p.UserId == userId).SuccessfulDefences >= 10) { context.UsersAchievements.Add(new UserAchievement { AchievementGotDatetime = DateTime.Now, AchievementId = item.Id, UserId = userId }); } break; case "Defender II": if (context.Profiles.First(p => p.UserId == userId).SuccessfulDefences >= 100) { context.UsersAchievements.Add(new UserAchievement { AchievementGotDatetime = DateTime.Now, AchievementId = item.Id, UserId = userId }); } break; case "Defender III": if (context.Profiles.First(p => p.UserId == userId).SuccessfulDefences >= 1000) { context.UsersAchievements.Add(new UserAchievement { AchievementGotDatetime = DateTime.Now, AchievementId = item.Id, UserId = userId }); } break; case "Long-liver": if (DateTime.Now.Subtract(context.Users.First(p => p.Id == userId).RegistrationDate).TotalDays > 365) { context.UsersAchievements.Add(new UserAchievement { AchievementGotDatetime = DateTime.Now, AchievementId = item.Id, UserId = userId }); } break; } } context.SaveChangesAsync(); }