示例#1
0
        public AddBattleshipResponse AddBattleShip([FromBody] AddBattleshipRequest request)
        {
            if (request == null)
            {
                throw new ArgumentException("The request object is null.");
            }
            try
            {
                var orientation = (OrientationType)Enum.Parse(typeof(OrientationType), request.Orientation);

                byte[] deserial;
                bool   canRead = HttpContext.Session.TryGetValue("LocalB", out deserial);
                if (canRead)
                {
                    var  localB = BinaryBoardSerialiazer.DeSerializeObject(deserial);
                    bool result = _boardCoordinator.AddBattleship(localB, new BoardCell()
                    {
                        XCoordinate = request.XCoordinate,
                        YCoordinate = request.YCoordinate
                    }, new Battleship()
                    {
                        Orientation = orientation, Width = request.Width
                    });

                    HttpContext.Session.Set("LocalB", BinaryBoardSerialiazer.SerializeObject(localB));
                    return(new AddBattleshipResponse()
                    {
                        Result = result, Board = localB
                    });
                }
                else
                {
                    var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                    {
                        Content      = new StringContent("The game board has not been created. Please create a game board before attacking."),
                        ReasonPhrase = "Board Not Found."
                    };
                    throw new System.Web.Http.HttpResponseException(resp);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                throw new System.Web.Http.HttpResponseException(new HttpResponseMessage()
                {
                    Content    = new StringContent(ex.Message),
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }
示例#2
0
        public AttackResponse Attack([FromBody] AttackRequest request)
        {
            if (request == null)
            {
                throw new ArgumentException("The request object is null.");
            }

            try
            {
                byte[] deserial;
                bool   canRead = HttpContext.Session.TryGetValue("LocalB", out deserial);
                if (canRead)
                {
                    var localB    = BinaryBoardSerialiazer.DeSerializeObject(deserial);
                    var HitOrMiss = _boardCoordinator.Attack(localB, new BoardCell()
                    {
                        XCoordinate = request.XCoordinate,
                        YCoordinate = request.YCoordinate
                    });
                    HttpContext.Session.Set("LocalB", BinaryBoardSerialiazer.SerializeObject(localB));
                    return(new AttackResponse()
                    {
                        Board = localB, Result = Enum.GetName(typeof(AttackResult), HitOrMiss)
                    });
                }
                else
                {
                    var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
                    {
                        Content      = new StringContent("The game board has not been created. Please create a game board before attacking."),
                        ReasonPhrase = "Board Not Found"
                    };
                    throw new System.Web.Http.HttpResponseException(resp);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                throw new System.Web.Http.HttpResponseException(new HttpResponseMessage()
                {
                    Content    = new StringContent(ex.Message),
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }
示例#3
0
        public IBoard Get()
        {
            try
            {
                var localBoard = _boardCoordinator.CreateBoard();

                HttpContext.Session.Set("LocalB", BinaryBoardSerialiazer.SerializeObject(localBoard));
                return(localBoard);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
                throw new System.Web.Http.HttpResponseException(new HttpResponseMessage()
                {
                    Content    = new StringContent(ex.Message),
                    StatusCode = HttpStatusCode.InternalServerError
                });
            }
        }