Beispiel #1
0
        static public LoginException Create(LoginExceptions exception)
        {
            LoginException loginException = new LoginException("Unknown error.");

            switch (exception)
            {
            case LoginExceptions.IncorrectCredentials:
                loginException = new LoginException(GetMessageFor(LoginExceptions.IncorrectCredentials));
                break;
            }

            return(loginException);
        }
Beispiel #2
0
        public LoginMessages.Response Handle(LoginMessages.Request request)
        {
            // Validate request
            try
            {
                Validator.Validate(request);
            } catch (LoginValidationException ex)
            {
                var errorResponse = new LoginMessages.Response()
                {
                    Success   = false,
                    Exception = ex
                };

                return(errorResponse);
            }

            // Authenticate Account
            Account account;

            try
            {
                account = AccountGateway.GetAccount(request.Username);
                if (account == null)
                {
                    throw LoginException.Create(LoginExceptions.IncorrectCredentials);
                }

                if (account.Password != request.Password)
                {
                    throw LoginException.Create(LoginExceptions.IncorrectCredentials);
                }
            } catch (LoginException ex)
            {
                var errorResponse = new LoginMessages.Response()
                {
                    Success   = false,
                    Exception = ex
                };

                return(errorResponse);
            }

            var sessionString = SessionCreator.CreateSession();
            var session       = new Session
            {
                Id       = sessionString,
                PlayerId = account.PlayerId,
                GameId   = null
            };

            try
            {
                SessionGateway.CreateSession(session);
            } catch (Exception e)
            {
                var message       = e.Message;
                var errorResponse = new LoginMessages.Response()
                {
                    Success   = false,
                    Exception = new Exception("Could not create a session! Error: " + message)
                };

                return(errorResponse);
            }

            var response = new LoginMessages.Response()
            {
                Success   = true,
                Session   = sessionString,
                Exception = null
            };

            return(response);
        }