Пример #1
0
        public void Validate(CreateGameMessages.Request request)
        {
            var exceptionMessage = "";
            var session          = request.Session;

            if (session == null || String.IsNullOrWhiteSpace(session.Id))
            {
                exceptionMessage = "Session is empty. ";
            }

            var gameName = request.GameName;

            if (String.IsNullOrWhiteSpace(gameName))
            {
                exceptionMessage += "Game name cannot be empty. ";
            }

            if (request.AccessLevel == null)
            {
                exceptionMessage += "Game must have an access level.";
            }

            if (String.IsNullOrWhiteSpace(exceptionMessage) == false)
            {
                throw new Exception(exceptionMessage);
            }
        }
Пример #2
0
        public CreateGameMessages.Response Handle(CreateGameMessages.Request request)
        {
            try
            {
                Validator.Validate(request);
            } catch (Exception e)
            {
                return(new Response()
                {
                    Success = false,
                    Exception = e
                });
            }

            // Validate session
            try
            {
                var sessionExists = SessionGateway.GetSession(request.Session.Id);
                if (sessionExists == null)
                {
                    throw new Exception("Session does not valid!");
                }
            } catch (Exception e)
            {
                return(new Response()
                {
                    Success = false,
                    Exception = e
                });
            }

            // Validate game name is unique
            try
            {
                var gameExists = GameGateway.GetGameBy(request.GameName); // does not throw
                if (gameExists != null)
                {
                    throw new Exception("Game with name already exists!");
                }
            }
            catch (Exception e)
            {
                return(new Response()
                {
                    Success = false,
                    Exception = e
                });
            }

            // Save game
            int gameId;

            try
            {
                var game = new Game()
                {
                    GameId      = 0, // assigned by gateway
                    Name        = request.GameName,
                    AccessLevel = request.AccessLevel
                };

                gameId = GameGateway.CreateGame(game);
            } catch (Exception e)
            {
                return(new Response()
                {
                    Success = false,
                    Exception = e
                });
            }

            return(new Response()
            {
                Success = true,
                GameId = gameId
            });
        }