コード例 #1
0
        public PlaceBattleshipResponse AddBattleShip([FromBody] PlaceBattleshipRequest 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 = BoardSerializer.DeSerializeObject(deserial);
                    bool result = _game.AddBattleship(localB, new BoardCell()
                    {
                        RowCoordinate    = request.RowCoordinate,
                        ColumnCoordinate = request.ColumCoordinate
                    }, new Battleship()
                    {
                        Orientation = orientation, Width = request.Width
                    });

                    HttpContext.Session.Set("LocalB", BoardSerializer.SerializeObject(localB));
                    return(new PlaceBattleshipResponse()
                    {
                        Result = result, Board = localB
                    });
                }

                //Force the player to create new game before placing battleship
                throw new HttpResponseException
                      {
                          HttpStatusCode      = HttpStatusCode.NotFound,
                          HttpResponseMessage = "The game board has not been created. Please create a game board before attacking."
                      };
            }
            catch (Exception ex)
            {
                throw new HttpResponseException
                      {
                          HttpStatusCode      = HttpStatusCode.InternalServerError,
                          HttpResponseMessage = ex.Message
                      };
            }
        }
コード例 #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    = BoardSerializer.DeSerializeObject(deserial);
                    var HitOrMiss = _game.Attack(localB, new BoardCell()
                    {
                        RowCoordinate    = request.RowCoordinate,
                        ColumnCoordinate = request.ColumnCoordinate
                    });
                    HttpContext.Session.Set("LocalB", BoardSerializer.SerializeObject(localB));
                    return(new AttackResponse()
                    {
                        Board = localB, Result = Enum.GetName(typeof(AttackResult), HitOrMiss)
                    });
                }

                //Force the player to create new game before placing battleship
                throw new HttpResponseException
                      {
                          HttpStatusCode      = HttpStatusCode.NotFound,
                          HttpResponseMessage = "The game board has not been created. Please create a game board before attacking."
                      };
            }
            catch (Exception ex)
            {
                throw new HttpResponseException
                      {
                          HttpStatusCode      = HttpStatusCode.InternalServerError,
                          HttpResponseMessage = ex.Message
                      };
            }
        }