public IActionResult RoomCreate(CreateModel Created) { // Creation of ScrumPoker room if (ModelState.IsValid) { int _nullablepassword; using (var _context = new PokerPlanningContext()) { _nullablepassword = PasswordEncrypt.GetPassword(Created.Password); var NewRoom = new PokerRoom() { Title = Created.RoomTitle, Description = "", Password = _nullablepassword, CreateDate = DateTime.Now, CloseDate = null, TypeCards = Created.CardsType }; _context.PokerRooms.Add(NewRoom); _context.SaveChanges(); var NewPlayer = new Player() { Name = Created.Name, Role = 2, PokerRoomId = NewRoom.Id, IsOnline = false }; _context.Players.Add(NewPlayer); _context.SaveChanges(); return(RedirectToAction("RoomEntrance", "ScrumRoom", new { PokerRoomId = NewRoom.Id, PlayerId = NewPlayer.Id, password = _nullablepassword })); //переход в комнату } } else { return(View(Created)); } }
public IActionResult RoomJoin(JoinModel Joined) { // Joining to ScrumPoker room if (ModelState.IsValid) { int _nullablepassword; try { using (var _context = new PokerPlanningContext()) { _nullablepassword = PasswordEncrypt.GetPassword(Joined.Password); var Room = _context.PokerRooms.Where(m => m.Id == Joined.RoomId).SingleOrDefault(); if (Equals(Room.Password, _nullablepassword))//Проверка пароля, регистр важен { var NewPlayer = new Player() { Name = Joined.Name, Role = 1, PokerRoomId = Joined.RoomId.Value, IsOnline = false }; var Checker = _context.Players .Where(m => m.PokerRoomId == NewPlayer.PokerRoomId && string.Compare(NewPlayer.Name, m.Name, true) == 0).ToList().Count; if (Checker == 0)//Создать нового пользователя { _context.Players.Add(NewPlayer); _context.SaveChanges(); return(RedirectToAction("RoomDiscussion", "ScrumRoom", new { PokerRoomId = Room.Id, PlayerId = NewPlayer.Id, password = _nullablepassword }));//переход в комнату } else //Такой пользователь уже есть, зайти под ним { var player = _context.Players.Where(p => p.PokerRoomId == Joined.RoomId && p.Name == NewPlayer.Name).SingleOrDefault(); if (player.Role == 2) { return(RedirectToAction("RoomEntrance", "ScrumRoom", new { PokerRoomId = Room.Id, PlayerId = player.Id, password = _nullablepassword }));//переход в комнату } else { return(RedirectToAction("RoomDiscussion", "ScrumRoom", new { PokerRoomId = Room.Id, PlayerId = player.Id, password = _nullablepassword })); } } } else { ModelState.AddModelError("Password", "Неверный пароль"); return(View(Joined)); } } } catch (NullReferenceException) { ModelState.AddModelError("RoomId", "Неверный ID Комнаты"); return(View(Joined)); } } else { return(View(Joined)); } }