Example #1
0
        public async Task <IActionResult> Attack(Guid boardId, AttackRequest attackRequest)
        {
            var boardTask = _boardService.GetBoard(boardId);
            var shipsTask = _shipService.GetShipsPlaced(boardId);

            await Task.WhenAll(boardTask, shipsTask);

            var board = await boardTask;

            if (board == null)
            {
                return(NotFound());
            }

            var  ships        = await shipsTask;
            var  attackStatus = AttackStatus.Miss;
            Guid?shipId       = null;

            foreach (var ship in ships)
            {
                var shipPosition = ship.Positions.FirstOrDefault(x => x.RowPosition == attackRequest.RowPosition &&
                                                                 x.ColumnPosition == attackRequest.ColumnPosition);

                if (shipPosition != null)
                {
                    attackStatus = AttackStatus.Hit;
                    shipId       = ship.Id;

                    var currentAttackPositions = await _attackService.GetAttackPositionsForShip(boardId, ship.Id);

                    if (ship.Size == (currentAttackPositions.Count + 1))
                    {
                        await _shipService.UpdateShipStatus(ship.Id, ShipStatus.Sunk);
                    }
                    break;
                }
            }

            await _attackService.UpdateAttackStatus(boardId, shipId, attackRequest, attackStatus);

            return(Ok(new AttackResponse {
                AttackStatus = attackStatus
            }));
        }