/// <summary> /// that function is register the new client to the DB. /// </summary> public bool Register(string userName, string hashedPassword, string emojiIcon) { try { using (var ctx = new FourInRowDB_Context()) { var user = (from u in ctx.Users where u.UserName == userName select u).FirstOrDefault(); if (user == null) { ctx.Users.Add(new User { UserName = userName, HashedPassword = hashedPassword, EmojiIcon = emojiIcon }); ctx.SaveChanges(); } else { var userExists = new UserExistsFault { Details = "User name: " + userName + " already Exits On DB, Try something else!" }; throw new FaultException <UserExistsFault>(userExists); } return(true); } } catch (FaultException <UserExistsFault> fault) { throw; } catch (DbUpdateException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (DbException ex) { var dFault = new DbFault() { Details = ex.Message }; throw new FaultException <DbFault>(dFault); } catch (Exception ex) { var exceptionFault = new ExceptionFault() { Details = ex.Message + "\n" + "Type: " + ex.GetType() }; throw new FaultException <ExceptionFault>(exceptionFault); } }
/// <summary> /// that function is connect the registered client to the Server. /// </summary> public void Connect(string userName, string hashedPassword) { lock (syncObj) { if (_clients.ContainsKey(userName)) { var userExists = new UserExistsFault { Details = "User name: " + userName + " already Connected, Try something else!" }; throw new FaultException <UserExistsFault>(userExists); } using (var ctx = new FourInRowDB_Context()) { var user = (from u in ctx.Users where u.UserName == userName select u).FirstOrDefault(); if (user == null) { var userNotExists = new UserNotExistsFault { Details = "User name: " + userName + " Not Exits On DB, Please Register First!" }; throw new FaultException <UserNotExistsFault>(userNotExists); } else if (user.HashedPassword != hashedPassword) { var fault = new WrongPasswordFault { Details = "Wrong Password, Please Try Again!" }; throw new FaultException <WrongPasswordFault>(fault); } else { _clients.Add(userName, OperationContext.Current.GetCallbackChannel <IFourRowCallback>()); _liveClients.Add(userName, true); } } } }